From bbb9099452b9fe70c7ec3035d3bcf59c004d0101 Mon Sep 17 00:00:00 2001 From: bridge Date: Thu, 28 Nov 2024 18:37:52 +0100 Subject: [PATCH] feat: HoloRemoveDuplicateCommand --- plugin/build.gradle | 12 ++-- .../java/lol/pyr/znpcsplus/ZNpcsPlus.java | 3 +- .../hologram/HoloRemoveDuplicateCommand.java | 58 +++++++++++++++++++ .../pyr/znpcsplus/hologram/HologramImpl.java | 6 ++ .../pyr/znpcsplus/hologram/HologramText.java | 8 +++ 5 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/commands/hologram/HoloRemoveDuplicateCommand.java diff --git a/plugin/build.gradle b/plugin/build.gradle index 02b1601..6a975f6 100644 --- a/plugin/build.gradle +++ b/plugin/build.gradle @@ -8,7 +8,7 @@ runServer { javaLauncher = javaToolchains.launcherFor { languageVersion = JavaLanguageVersion.of(21) } - minecraftVersion "1.20.6" + minecraftVersion "1.21.3" } processResources { @@ -17,9 +17,9 @@ processResources { dependencies { compileOnly "me.clip:placeholderapi:2.11.6" // Placeholder support - compileOnly "com.github.retrooper:packetevents-spigot:2.6.1-SNAPSHOT" // Packets - implementation "com.google.code.gson:gson:2.11.0" // JSON parsing + implementation "com.google.code.gson:gson:2.10.1" // JSON parsing implementation "org.bstats:bstats-bukkit:3.0.2" // Plugin stats + implementation "com.github.retrooper:packetevents-spigot:2.6.0" // Packets implementation "space.arim.dazzleconf:dazzleconf-ext-snakeyaml:1.2.1" // Configs implementation "lol.pyr:director-adventure:2.1.2" // Commands @@ -37,10 +37,12 @@ shadowJar { relocate "org.objectweb.asm", "lol.pyr.znpcsplus.libraries.asm" relocate "me.lucko.jarrelocator", "lol.pyr.znpcsplus.libraries.jarrelocator" - // When changing anything here remember to also update the bootstrap relocate "org.bstats", "lol.pyr.znpcsplus.libraries.bstats" + relocate "net.kyori", "lol.pyr.znpcsplus.libraries.kyori" relocate "org.checkerframework", "lol.pyr.znpcsplus.libraries.checkerframework" relocate "com.google.gson", "lol.pyr.znpcsplus.libraries.gson" + relocate "com.github.retrooper.packetevents", "lol.pyr.znpcsplus.libraries.packetevents.api" + relocate "io.github.retrooper.packetevents", "lol.pyr.znpcsplus.libraries.packetevents.impl" relocate "org.yaml.snakeyaml", "lol.pyr.znpcsplus.libraries.snakeyaml" relocate "space.arim.dazzleconf", "lol.pyr.znpcsplus.libraries.dazzleconf" relocate "lol.pyr.director", "lol.pyr.znpcsplus.libraries.command" @@ -48,4 +50,4 @@ shadowJar { minimize() } -tasks.assemble.dependsOn shadowJar +tasks.assemble.dependsOn shadowJar \ No newline at end of file diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java index c4d1f9a..011ddad 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java @@ -332,7 +332,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 4092e92..568a85d 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/hologram/HologramImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/hologram/HologramImpl.java @@ -98,6 +98,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(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 47e5e91..904a413 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/hologram/HologramText.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/hologram/HologramText.java @@ -37,4 +37,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()); + } }