diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java index 2a12c2d..d04832a 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java @@ -335,7 +335,8 @@ public class ZNpcsPlus { .addSubcommand("set", new HoloSetCommand(npcRegistry)) .addSubcommand("setitem", new HoloSetItemCommand(npcRegistry)) .addSubcommand("offset", new HoloOffsetCommand(npcRegistry)) - .addSubcommand("refreshdelay", new HoloRefreshDelayCommand(npcRegistry))) + .addSubcommand("refreshdelay", new HoloRefreshDelayCommand(npcRegistry)) + .addSubcommand("removeduplicate", new HoloRemoveDuplicateCommand(npcRegistry))) .addSubcommand("action", new MultiCommand(bootstrap.loadHelpMessage("action")) .addSubcommand("add", new ActionAddCommand(npcRegistry, actionRegistry)) .addSubcommand("clear", new ActionClearCommand(npcRegistry)) diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/hologram/HoloRemoveDuplicateCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/hologram/HoloRemoveDuplicateCommand.java new file mode 100644 index 0000000..96c0ab2 --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/hologram/HoloRemoveDuplicateCommand.java @@ -0,0 +1,58 @@ +package lol.pyr.znpcsplus.commands.hologram; + +import lol.pyr.director.adventure.command.CommandContext; +import lol.pyr.director.adventure.command.CommandHandler; +import lol.pyr.director.common.command.CommandExecutionException; +import lol.pyr.znpcsplus.hologram.HologramImpl; +import lol.pyr.znpcsplus.hologram.HologramLine; +import lol.pyr.znpcsplus.hologram.HologramText; +import lol.pyr.znpcsplus.npc.NpcEntryImpl; +import lol.pyr.znpcsplus.npc.NpcRegistryImpl; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + +public class HoloRemoveDuplicateCommand implements CommandHandler { + private final NpcRegistryImpl registry; + + public HoloRemoveDuplicateCommand(NpcRegistryImpl registry) { + this.registry = registry; + } + + @Override + public void run(CommandContext context) throws CommandExecutionException { + context.setUsage(context.getLabel() + " holo removeduplicates "); + HologramImpl hologram = context.parse(NpcEntryImpl.class).getNpc().getHologram(); + List> lines = new ArrayList<>(hologram.getLines()); + List textLines = new ArrayList<>(); + + Iterator> iterator = lines.iterator(); + while (iterator.hasNext()) { + HologramLine line = iterator.next(); + if (line instanceof HologramText textLine + && !textLine.getValue().equals(Component.empty()) + && textLines.contains(textLine)) { + iterator.remove(); + continue; + } + + if (line instanceof HologramText textLine) { + textLines.add(textLine); + } + } + + hologram.clearLines(); + hologram.addLines(lines); + context.send(Component.text("NPC lines fixed!", NamedTextColor.GREEN)); + } + + @Override + public List suggest(CommandContext context) throws CommandExecutionException { + if (context.argSize() == 1) return context.suggestCollection(registry.getModifiableIds()); + return Collections.emptyList(); + } +} \ No newline at end of file diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/hologram/HologramImpl.java b/plugin/src/main/java/lol/pyr/znpcsplus/hologram/HologramImpl.java index 6605515..5d55d06 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/hologram/HologramImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/hologram/HologramImpl.java @@ -101,6 +101,12 @@ public class HologramImpl extends Viewable implements Hologram { lines.clear(); } + public void addLines(List> lines) { + this.lines.addAll(lines); + relocateLines(); + for (Player viewer : getViewers()) for (HologramLine line : lines) line.show(viewer); + } + public void insertTextLineComponent(int index, Component line) { HologramText newLine = new HologramText(this, propertyRegistry, packetFactory, null, line); lines.add(index, newLine); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/hologram/HologramText.java b/plugin/src/main/java/lol/pyr/znpcsplus/hologram/HologramText.java index 23f3428..c552194 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/hologram/HologramText.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/hologram/HologramText.java @@ -39,4 +39,12 @@ public class HologramText extends HologramLine { public boolean hasProperty(EntityProperty key) { return key.getName().equalsIgnoreCase("name") || key.getName().equalsIgnoreCase("invisible"); } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null || getClass() != obj.getClass()) return false; + HologramText that = (HologramText) obj; + return getValue().equals(that.getValue()); + } }