From 6eb47d5aab485f3d5a3a146fe30cc97c42abc57b Mon Sep 17 00:00:00 2001 From: lokka30 Date: Fri, 23 Dec 2022 15:55:30 +0800 Subject: [PATCH 1/5] Full code cleanup in the 'nms' (now 'nametag') package. --- .../me/lokka30/levelledmobs/Companion.java | 4 +- .../me/lokka30/levelledmobs/LevelledMobs.java | 2 +- .../commands/subcommands/DebugSubcommand.java | 6 +- .../listeners/paper/PlayerDeathListener.java | 2 +- .../levelledmobs/managers/LevelManager.java | 4 +- .../managers/NametagQueueManager.java | 20 +-- .../{nms => nametag}/ComponentUtils.java | 73 +++++---- .../{nms => nametag}/Definitions.java | 142 +++++++++-------- .../{nms => nametag}/KyoriNametags.java | 46 ++++-- .../{nms => nametag}/MiscUtils.java | 32 +++- .../levelledmobs/nametag/NametagSender.java | 22 +++ .../NametagSenderHandler.java} | 39 ++--- .../NmsNametagSender.java} | 150 +++++++++--------- .../ProtocolLibNametagSender.java} | 44 +++-- .../{nms => nametag}/ServerVersionInfo.java | 56 ++++--- .../me/lokka30/levelledmobs/nms/NMSUtil.java | 18 --- 16 files changed, 378 insertions(+), 282 deletions(-) rename src/main/java/me/lokka30/levelledmobs/{nms => nametag}/ComponentUtils.java (56%) rename src/main/java/me/lokka30/levelledmobs/{nms => nametag}/Definitions.java (79%) rename src/main/java/me/lokka30/levelledmobs/{nms => nametag}/KyoriNametags.java (50%) rename src/main/java/me/lokka30/levelledmobs/{nms => nametag}/MiscUtils.java (79%) create mode 100644 src/main/java/me/lokka30/levelledmobs/nametag/NametagSender.java rename src/main/java/me/lokka30/levelledmobs/{nms/NMSHandler.java => nametag/NametagSenderHandler.java} (63%) rename src/main/java/me/lokka30/levelledmobs/{nms/NametagSender.java => nametag/NmsNametagSender.java} (64%) rename src/main/java/me/lokka30/levelledmobs/{nms/ProtocolLibHandler.java => nametag/ProtocolLibNametagSender.java} (72%) rename src/main/java/me/lokka30/levelledmobs/{nms => nametag}/ServerVersionInfo.java (59%) delete mode 100644 src/main/java/me/lokka30/levelledmobs/nms/NMSUtil.java diff --git a/src/main/java/me/lokka30/levelledmobs/Companion.java b/src/main/java/me/lokka30/levelledmobs/Companion.java index 6e9ffa8aa..f16a8741b 100644 --- a/src/main/java/me/lokka30/levelledmobs/Companion.java +++ b/src/main/java/me/lokka30/levelledmobs/Companion.java @@ -52,7 +52,7 @@ import me.lokka30.levelledmobs.misc.FileLoader; import me.lokka30.levelledmobs.misc.FileMigrator; import me.lokka30.levelledmobs.misc.VersionInfo; -import me.lokka30.levelledmobs.nms.ServerVersionInfo; +import me.lokka30.levelledmobs.nametag.ServerVersionInfo; import me.lokka30.levelledmobs.rules.MetricsInfo; import me.lokka30.levelledmobs.util.Utils; import me.lokka30.microlib.exceptions.OutdatedServerVersionException; @@ -572,7 +572,7 @@ void shutDownAsyncTasks() { } private void buildUniversalGroups() { - final ServerVersionInfo versionInfo = main.nametagQueueManager.nmsHandler.versionInfo; + final ServerVersionInfo versionInfo = main.nametagQueueManager.nametagSenderHandler.versionInfo; // include interfaces: Monster, Boss hostileMobsGroup = Stream.of( diff --git a/src/main/java/me/lokka30/levelledmobs/LevelledMobs.java b/src/main/java/me/lokka30/levelledmobs/LevelledMobs.java index 1aae085e5..152b4ac8d 100644 --- a/src/main/java/me/lokka30/levelledmobs/LevelledMobs.java +++ b/src/main/java/me/lokka30/levelledmobs/LevelledMobs.java @@ -31,7 +31,7 @@ import me.lokka30.levelledmobs.misc.NamespacedKeys; import me.lokka30.levelledmobs.misc.NametagTimerChecker; import me.lokka30.levelledmobs.misc.YmlParsingHelper; -import me.lokka30.levelledmobs.nms.Definitions; +import me.lokka30.levelledmobs.nametag.Definitions; import me.lokka30.levelledmobs.rules.RulesManager; import me.lokka30.levelledmobs.rules.RulesParsingManager; import me.lokka30.levelledmobs.util.ConfigUtils; diff --git a/src/main/java/me/lokka30/levelledmobs/commands/subcommands/DebugSubcommand.java b/src/main/java/me/lokka30/levelledmobs/commands/subcommands/DebugSubcommand.java index 706731d0f..2ba87432d 100644 --- a/src/main/java/me/lokka30/levelledmobs/commands/subcommands/DebugSubcommand.java +++ b/src/main/java/me/lokka30/levelledmobs/commands/subcommands/DebugSubcommand.java @@ -7,7 +7,7 @@ import me.lokka30.levelledmobs.managers.ExternalCompatibilityManager; import me.lokka30.levelledmobs.misc.DebugCreator; import me.lokka30.levelledmobs.misc.LivingEntityWrapper; -import me.lokka30.levelledmobs.nms.MiscUtils; +import me.lokka30.levelledmobs.nametag.MiscUtils; import me.lokka30.levelledmobs.util.Utils; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -54,7 +54,7 @@ public void parseSubcommand(final LevelledMobs main, final @NotNull CommandSende } else if ("chunk_kill_count".equalsIgnoreCase(args[1])) { chunkKillCount(sender, args); } else if ("nbt_dump".equalsIgnoreCase(args[1])) { - if (!main.nametagQueueManager.nmsHandler.versionInfo.isNMSVersionValid()){ + if (!main.nametagQueueManager.nametagSenderHandler.versionInfo.isNMSVersionValid()){ sender.sendMessage("Unable to dump, an unknown NMS version was detected"); return; } @@ -127,7 +127,7 @@ private void doNbtDump(final @NotNull CommandSender sender, final String @NotNul lmEntity.getNameIfBaby(), lmEntity.getWorldName(), locationStr, - MiscUtils.getNBTDump(lmEntity.getLivingEntity(), main) + MiscUtils.getNBTDump(lmEntity.getLivingEntity()) ); lmEntity.free(); diff --git a/src/main/java/me/lokka30/levelledmobs/listeners/paper/PlayerDeathListener.java b/src/main/java/me/lokka30/levelledmobs/listeners/paper/PlayerDeathListener.java index bcd382ff6..67a1ece60 100644 --- a/src/main/java/me/lokka30/levelledmobs/listeners/paper/PlayerDeathListener.java +++ b/src/main/java/me/lokka30/levelledmobs/listeners/paper/PlayerDeathListener.java @@ -168,7 +168,7 @@ private void updateDeathMessage(final @NotNull PlayerDeathEvent event, final @No } private @NotNull Component buildPlayerComponent(final @NotNull Player player){ - final Component playerName = main.nametagQueueManager.nmsHandler.versionInfo.getMinecraftVersion() >= 1.18 ? + final Component playerName = main.nametagQueueManager.nametagSenderHandler.versionInfo.getMinecraftVersion() >= 1.18 ? player.name() : Component.text(player.getName()); final HoverEvent hoverEvent = HoverEvent.showEntity( Key.key("minecraft"), player.getUniqueId(), playerName); diff --git a/src/main/java/me/lokka30/levelledmobs/managers/LevelManager.java b/src/main/java/me/lokka30/levelledmobs/managers/LevelManager.java index b7b835cd4..2521fa79c 100644 --- a/src/main/java/me/lokka30/levelledmobs/managers/LevelManager.java +++ b/src/main/java/me/lokka30/levelledmobs/managers/LevelManager.java @@ -878,7 +878,7 @@ public void run() { } public void updateNametag(final LivingEntityWrapper lmEntity) { - final boolean preserveMobName = !main.nametagQueueManager.nmsHandler.isUsingProtocolLib; + final boolean preserveMobName = !main.nametagQueueManager.nametagSenderHandler.isUsingProtocolLib; final NametagResult nametag = getNametag(lmEntity, false, preserveMobName); final QueueItem queueItem = new QueueItem( @@ -1142,7 +1142,7 @@ private void checkLevelledEntity(@NotNull final LivingEntityWrapper lmEntity, location.getWorld().equals(lmEntity.getWorld()) && lmEntity.getLocation().distanceSquared(location) <= maxDistance) { //if within distance, update nametag. - final boolean preserveMobName = !main.nametagQueueManager.nmsHandler.isUsingProtocolLib; + final boolean preserveMobName = !main.nametagQueueManager.nametagSenderHandler.isUsingProtocolLib; final NametagResult nametag = main.levelManager.getNametag(lmEntity, false, preserveMobName); main.nametagQueueManager.addToQueue( new QueueItem(lmEntity, nametag, List.of(player))); diff --git a/src/main/java/me/lokka30/levelledmobs/managers/NametagQueueManager.java b/src/main/java/me/lokka30/levelledmobs/managers/NametagQueueManager.java index 09ccaf54f..0390022bc 100644 --- a/src/main/java/me/lokka30/levelledmobs/managers/NametagQueueManager.java +++ b/src/main/java/me/lokka30/levelledmobs/managers/NametagQueueManager.java @@ -15,8 +15,8 @@ import me.lokka30.levelledmobs.misc.LivingEntityWrapper; import me.lokka30.levelledmobs.misc.NametagTimerChecker; import me.lokka30.levelledmobs.misc.QueueItem; -import me.lokka30.levelledmobs.nms.NMSHandler; -import me.lokka30.levelledmobs.nms.NMSUtil; +import me.lokka30.levelledmobs.nametag.NametagSenderHandler; +import me.lokka30.levelledmobs.nametag.NametagSender; import me.lokka30.levelledmobs.result.NametagResult; import me.lokka30.levelledmobs.rules.NametagVisibilityEnum; import me.lokka30.levelledmobs.util.Utils; @@ -38,7 +38,7 @@ public class NametagQueueManager { public NametagQueueManager(final LevelledMobs main) { this.main = main; - this.nmsHandler = new NMSHandler(main); + this.nametagSenderHandler = new NametagSenderHandler(main); this.queue = new LinkedBlockingQueue<>(); getNMSUtil(); } @@ -46,17 +46,17 @@ public NametagQueueManager(final LevelledMobs main) { private final LevelledMobs main; private boolean isRunning; private boolean doThread; - private NMSUtil nmsUtil; + private NametagSender nametagSender; private final LinkedBlockingQueue queue; - public final NMSHandler nmsHandler; + public final NametagSenderHandler nametagSenderHandler; private void getNMSUtil() { - this.nmsUtil = nmsHandler.getCurrentUtil(); + this.nametagSender = nametagSenderHandler.getCurrentUtil(); } public boolean hasNametagSupport() { - return this.nmsUtil != null; + return this.nametagSender != null; } public void start() { @@ -129,7 +129,7 @@ private void main() throws InterruptedException { } private void processItem(final @NotNull QueueItem item) { - if (this.nmsUtil == null) { + if (this.nametagSender == null) { // this would happen if the Minecraft version isn't supported directly by NMS // and ProtocolLib is not installed return; @@ -236,13 +236,13 @@ private void updateNametag(final @NotNull LivingEntityWrapper lmEntity, final @N continue; } - nmsUtil.sendNametag(lmEntity.getLivingEntity(), nametag, player, + nametagSender.sendNametag(lmEntity.getLivingEntity(), nametag, player, doAlwaysVisible); } } else { // these players are getting always on nametags for (final Player player : lmEntity.playersNeedingNametagCooldownUpdate) { - nmsUtil.sendNametag(lmEntity.getLivingEntity(), nametag, player, true); + nametagSender.sendNametag(lmEntity.getLivingEntity(), nametag, player, true); } } } diff --git a/src/main/java/me/lokka30/levelledmobs/nms/ComponentUtils.java b/src/main/java/me/lokka30/levelledmobs/nametag/ComponentUtils.java similarity index 56% rename from src/main/java/me/lokka30/levelledmobs/nms/ComponentUtils.java rename to src/main/java/me/lokka30/levelledmobs/nametag/ComponentUtils.java index c1cc520ec..e27a8c904 100644 --- a/src/main/java/me/lokka30/levelledmobs/nms/ComponentUtils.java +++ b/src/main/java/me/lokka30/levelledmobs/nametag/ComponentUtils.java @@ -1,40 +1,52 @@ -package me.lokka30.levelledmobs.nms; +package me.lokka30.levelledmobs.nametag; +import java.util.Objects; import me.lokka30.levelledmobs.LevelledMobs; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +/** + * @author PenalBuffalo (aka stumper66) + */ public class ComponentUtils { - public static void append( - final @NotNull Object component, - final @Nullable Object appendingComponent + + public static void appendComponents( + final @NotNull Object component, + final @Nullable Object appendingComponent ) { - if (appendingComponent == null) return; + if (appendingComponent == null) { + return; + } + final Definitions def = LevelledMobs.getInstance().getDefinitions(); try { if (component.getClass() != def.clazz_IChatMutableComponent) { - throw new Exception("Invalid type: " + component.getClass().getName()); + throw new IllegalArgumentException + ("Invalid type: " + component.getClass().getName()); } - if (appendingComponent.getClass() != def.clazz_IChatMutableComponent){ - throw new Exception("Invalid type: " + appendingComponent.getClass().getName()); + if (appendingComponent.getClass() != def.clazz_IChatMutableComponent) { + throw new IllegalArgumentException + ("Invalid type: " + appendingComponent.getClass().getName()); } def.method_ComponentAppend.invoke(component, appendingComponent); - } catch (Exception e) { - e.printStackTrace(); + + } catch (final Exception ex) { + ex.printStackTrace(); } } public static @NotNull Object getEmptyComponent() { - final Object result = getTextComponent(null); - assert result != null; - return result; + return Objects.requireNonNull( + getTextComponent(null), + "text component cannot be null" + ); } public static @Nullable Object getTextComponent( - final @Nullable String text + final @Nullable String text ) { final Definitions def = LevelledMobs.getInstance().getDefinitions(); try { @@ -45,43 +57,44 @@ public static void append( // #nullToEmpty(text) return def.method_TextComponent.invoke(null, text); } - } catch (Exception e){ + } catch (Exception e) { e.printStackTrace(); return null; } } public static @Nullable Object getTranslatableComponent( - final @NotNull String key + final @NotNull String key ) { return getTranslatableComponent(key, (Object) null); } public static @Nullable Object getTranslatableComponent( - final @NotNull String key, - final @Nullable Object... args + final @NotNull String key, + final @Nullable Object... args ) { final Definitions def = LevelledMobs.getInstance().getDefinitions(); + try { - if (def.getServerVersionInfo().getMinecraftVersion() >= 1.19){ + if (def.getServerVersionInfo().getMinecraftVersion() >= 1.19d) { if (args == null || args.length == 0) { return def.method_Translatable.invoke(null, key); - } - else { + } else { return def.method_TranslatableWithArgs.invoke(null, key, args); } - } - else{ + } else { if (args == null || args.length == 0) { - return def.clazz_TranslatableComponent.getConstructor(String.class).newInstance(key); - } - else { - return def.clazz_TranslatableComponent.getConstructor( - String.class, Object[].class).newInstance(key, args); + return def.clazz_TranslatableComponent + .getConstructor(String.class) + .newInstance(key); + } else { + return def.clazz_TranslatableComponent + .getConstructor(String.class, Object[].class) + .newInstance(key, args); } } - } catch (Exception e) { - e.printStackTrace(); + } catch (final Exception ex) { + ex.printStackTrace(); return null; } } diff --git a/src/main/java/me/lokka30/levelledmobs/nms/Definitions.java b/src/main/java/me/lokka30/levelledmobs/nametag/Definitions.java similarity index 79% rename from src/main/java/me/lokka30/levelledmobs/nms/Definitions.java rename to src/main/java/me/lokka30/levelledmobs/nametag/Definitions.java index c09518167..58719e5f0 100644 --- a/src/main/java/me/lokka30/levelledmobs/nms/Definitions.java +++ b/src/main/java/me/lokka30/levelledmobs/nametag/Definitions.java @@ -1,8 +1,4 @@ -package me.lokka30.levelledmobs.nms; - -import net.kyori.adventure.text.Component; -import org.bukkit.entity.LivingEntity; -import org.jetbrains.annotations.NotNull; +package me.lokka30.levelledmobs.nametag; import java.lang.reflect.Constructor; import java.lang.reflect.Field; @@ -10,9 +6,16 @@ import java.lang.reflect.Method; import java.util.List; import java.util.Optional; +import net.kyori.adventure.text.Component; +import org.bukkit.entity.LivingEntity; +import org.jetbrains.annotations.NotNull; +/** + * @author PenalBuffalo (aka stumper66) + */ public class Definitions { - public Definitions(){ + + public Definitions() { this.ver = new ServerVersionInfo(); build(); } @@ -78,9 +81,10 @@ public Definitions(){ Constructor ctor_SynchedEntityData; Constructor ctor_Packet; - private void build(){ - this.isOneNinteenThreeOrNewer = ver.getMinecraftVersion() == 1.19 && ver.getRevision()>= 3 || - ver.getMinecraftVersion() >= 1.20; + private void build() { + this.isOneNinteenThreeOrNewer = + ver.getMinecraftVersion() == 1.19d && ver.getRevision() >= 3d || + ver.getMinecraftVersion() >= 1.20d; try { buildClasses(); @@ -99,91 +103,92 @@ private void build(){ private void buildClasses() throws ClassNotFoundException { this.clazz_IChatMutableComponent = Class.forName( - "net.minecraft.network.chat.IChatMutableComponent"); + "net.minecraft.network.chat.IChatMutableComponent"); this.clazz_IChatBaseComponent = Class.forName( - "net.minecraft.network.chat.IChatBaseComponent"); + "net.minecraft.network.chat.IChatBaseComponent"); this.clazz_CraftEntity = Class.forName( - "org.bukkit.craftbukkit." + ver.getNMSVersion() + ".entity.CraftEntity"); + "org.bukkit.craftbukkit." + ver.getNMSVersion() + ".entity.CraftEntity"); this.clazz_CraftLivingEntity = Class.forName( - "org.bukkit.craftbukkit." + ver.getNMSVersion() + ".entity.CraftLivingEntity"); + "org.bukkit.craftbukkit." + ver.getNMSVersion() + ".entity.CraftLivingEntity"); // net.minecraft.network.syncher.SynchedEntityData this.clazz_DataWatcher = Class.forName( - "net.minecraft.network.syncher.DataWatcher"); + "net.minecraft.network.syncher.DataWatcher"); this.clazz_DataWatcher_Item = Class.forName( - "net.minecraft.network.syncher.DataWatcher$Item"); + "net.minecraft.network.syncher.DataWatcher$Item"); this.clazz_DataWatcherRegistry = Class.forName( - "net.minecraft.network.syncher.DataWatcherRegistry"); + "net.minecraft.network.syncher.DataWatcherRegistry"); this.clazz_Entity = Class.forName( - "net.minecraft.world.entity.Entity"); + "net.minecraft.world.entity.Entity"); this.clazz_DataWatcherObject = Class.forName( - "net.minecraft.network.syncher.DataWatcherObject"); + "net.minecraft.network.syncher.DataWatcherObject"); this.clazz_DataWatcherSerializer = Class.forName( - "net.minecraft.network.syncher.DataWatcherSerializer"); + "net.minecraft.network.syncher.DataWatcherSerializer"); this.clazz_ClientboundSetEntityDataPacket = Class.forName( - "net.minecraft.network.protocol.game.PacketPlayOutEntityMetadata"); + "net.minecraft.network.protocol.game.PacketPlayOutEntityMetadata"); this.clazz_CraftPlayer = Class.forName( - "org.bukkit.craftbukkit." + ver.getNMSVersion() + ".entity.CraftPlayer"); + "org.bukkit.craftbukkit." + ver.getNMSVersion() + ".entity.CraftPlayer"); this.clazz_Packet = Class.forName( - "net.minecraft.network.protocol.Packet"); + "net.minecraft.network.protocol.Packet"); this.clazz_NetworkManager = Class.forName( - "net.minecraft.network.NetworkManager"); + "net.minecraft.network.NetworkManager"); //net.minecraft.server.network.ServerPlayerConnection -> // void send(net.minecraft.network.protocol.Packet) -> this.clazz_PlayerConnection = Class.forName( - "net.minecraft.server.network.PlayerConnection"); + "net.minecraft.server.network.PlayerConnection"); this.clazz_ServerPlayerConnection = Class.forName( - "net.minecraft.server.network.ServerPlayerConnection"); + "net.minecraft.server.network.ServerPlayerConnection"); // net.minecraft.server.level.ServerPlayer -> this.clazz_EntityPlayer = Class.forName( - "net.minecraft.server.level.EntityPlayer"); + "net.minecraft.server.level.EntityPlayer"); if (ver.getMinecraftVersion() < 1.19) { // this is basically TranslatableComponent this.clazz_TranslatableComponent = Class.forName( - "net.minecraft.network.chat.ChatMessage"); + "net.minecraft.network.chat.ChatMessage"); } try { this.clazz_PaperAdventure = Class.forName( - "io.papermc.paper.adventure.PaperAdventure"); + "io.papermc.paper.adventure.PaperAdventure"); this.hasKiori = true; + } catch (ClassNotFoundException ignored) { } - catch (ClassNotFoundException ignored){ } this.clazz_EntityTypes = Class.forName( - "net.minecraft.world.entity.EntityTypes"); + "net.minecraft.world.entity.EntityTypes"); } private void getMethodComponentAppend() throws NoSuchMethodException { // net.minecraft.network.chat.MutableComponent append(net.minecraft.network.chat.Component) -> // 1.18 = b, 1.19.0 = a, 1.19.1 = b String methodName = ver.getRevision() == 0 || ver.getMinecraftVersion() == 1.18 - ? "a" : "b"; + ? "a" : "b"; - if (ver.getMinecraftVersion() <= 1.17) + if (ver.getMinecraftVersion() <= 1.17) { methodName = "addSibling"; + } this.method_ComponentAppend = clazz_IChatMutableComponent.getDeclaredMethod( - methodName, this.clazz_IChatBaseComponent); + methodName, this.clazz_IChatBaseComponent); } - private void getMethodTextComponents() throws NoSuchMethodException{ + private void getMethodTextComponents() throws NoSuchMethodException { // net.minecraft.network.chat.Component -> // net.minecraft.network.chat.MutableComponent empty() @@ -214,11 +219,12 @@ private void getMethodTranslatable() throws NoSuchMethodException { // net.minecraft.network.chat.MutableComponent translatable(java.lang.String,java.lang.Object[]) this.method_Translatable = clazz_IChatBaseComponent.getDeclaredMethod("a"); - this.method_TranslatableWithArgs = clazz_IChatBaseComponent.getDeclaredMethod("a", String.class, Object[].class); + this.method_TranslatableWithArgs = clazz_IChatBaseComponent.getDeclaredMethod("a", + String.class, Object[].class); } @SuppressWarnings("deprecation") - public @NotNull String getTranslationKey(final @NotNull LivingEntity livingEntity){ + public @NotNull String getTranslationKey(final @NotNull LivingEntity livingEntity) { // only needed for spigot. paper has a built-in method // net.minecraft.world.entity.EntityType -> @@ -227,7 +233,8 @@ private void getMethodTranslatable() throws NoSuchMethodException { Optional optionalResult; try { - optionalResult = (Optional)this.method_EntityTypeByString.invoke(null, livingEntity.getType().getName()); + optionalResult = (Optional) this.method_EntityTypeByString.invoke(null, + livingEntity.getType().getName()); if (optionalResult.isEmpty()) { return ""; @@ -248,24 +255,27 @@ private void buildSimpleMethods() throws NoSuchMethodException { // net.minecraft.network.syncher.SynchedEntityData getEntityData() -> String methodName = this.isOneNinteenThreeOrNewer ? - "al" : "ai"; - if (ver.getMinecraftVersion() <= 1.17) + "al" : "ai"; + if (ver.getMinecraftVersion() <= 1.17) { methodName = "getDataWatcher"; + } // net.minecraft.network.syncher.SynchedEntityData getEntityData() -> this.method_getEntityData = clazz_Entity.getMethod(methodName); methodName = ver.getMinecraftVersion() >= 1.18 ? - "b" : "set"; + "b" : "set"; // set(net.minecraft.network.syncher.EntityDataAccessor,java.lang.Object) -> - this.method_set = clazz_DataWatcher.getMethod(methodName, clazz_DataWatcherObject, Object.class); + this.method_set = clazz_DataWatcher.getMethod(methodName, clazz_DataWatcherObject, + Object.class); // int getId() -> methodName = this.isOneNinteenThreeOrNewer ? - "ah" : "ae"; - if (ver.getMinecraftVersion() <= 1.17) + "ah" : "ae"; + if (ver.getMinecraftVersion() <= 1.17) { methodName = "getId"; + } this.method_getId = clazz_Entity.getDeclaredMethod(methodName); @@ -275,20 +285,21 @@ private void buildSimpleMethods() throws NoSuchMethodException { // void send(net.minecraft.network.protocol.Packet) -> methodName = ver.getMinecraftVersion() >= 1.18 ? - "a" : "sendPacket"; + "a" : "sendPacket"; this.method_Send = clazz_ServerPlayerConnection.getDeclaredMethod(methodName, clazz_Packet); methodName = ver.getMinecraftVersion() >= 1.18 ? - "c" : "getAll"; + "c" : "getAll"; // java.util.List getAll() -> this.method_getAll = clazz_DataWatcher.getDeclaredMethod(methodName); methodName = ver.getMinecraftVersion() >= 1.18 ? - "a" : "register"; + "a" : "register"; // net.minecraft.network.syncher.SynchedEntityData -> // define(net.minecraft.network.syncher.EntityDataAccessor,java.lang.Object) -> - this.method_define = clazz_DataWatcher.getDeclaredMethod(methodName, clazz_DataWatcherObject, Object.class); + this.method_define = clazz_DataWatcher.getDeclaredMethod(methodName, + clazz_DataWatcherObject, Object.class); // net.minecraft.network.syncher.EntityDataAccessor getAccessor() -> this.method_getAccessor = clazz_DataWatcher_Item.getDeclaredMethod("a"); @@ -298,8 +309,10 @@ private void buildSimpleMethods() throws NoSuchMethodException { // net.minecraft.network.Connection getConnection() -> //this.method_getConnection = clazz_CraftPlayer.getDeclaredMethod("networkManager"); - if (this.hasKiori) - this.method_AsVanilla = clazz_PaperAdventure.getDeclaredMethod("asVanilla", Component.class); + if (this.hasKiori) { + this.method_AsVanilla = clazz_PaperAdventure.getDeclaredMethod("asVanilla", + Component.class); + } // java.util.Optional byString(java.lang.String) -> a this.method_EntityTypeByString = clazz_EntityTypes.getDeclaredMethod("a", String.class); @@ -307,18 +320,20 @@ private void buildSimpleMethods() throws NoSuchMethodException { // java.lang.String getDescriptionId() -> g this.method_GetDescriptionId = clazz_EntityTypes.getDeclaredMethod("g"); - if (this.getIsOneNinteenThreeOrNewer()){ + if (this.isOneNinteenThreeOrNewer()) { // new methods here were added in 1.19.3 // java.util.List getNonDefaultValues() -> c this.method_getNonDefaultValues = clazz_DataWatcher.getDeclaredMethod("c"); // define(net.minecraft.network.syncher.EntityDataAccessor,java.lang.Object) -> a - this.method_SynchedEntityData_Define = clazz_DataWatcher.getMethod("a", clazz_DataWatcherObject, Object.class); + this.method_SynchedEntityData_Define = clazz_DataWatcher.getMethod("a", + clazz_DataWatcherObject, Object.class); // private DataWatcher.Item getItem(DataWatcherObject datawatcherobject) // net.minecraft.network.syncher.SynchedEntityData$DataItem getItem(net.minecraft.network.syncher.EntityDataAccessor) -> b - this.method_DataWatcher_GetItem = clazz_DataWatcher.getDeclaredMethod("b", clazz_DataWatcherObject); + this.method_DataWatcher_GetItem = clazz_DataWatcher.getDeclaredMethod("b", + clazz_DataWatcherObject); this.method_DataWatcher_GetItem.setAccessible(true); // net.minecraft.network.syncher.SynchedEntityData$DataItem -> abq$a: @@ -338,7 +353,7 @@ private void buildFields() throws NoSuchFieldException { // net.minecraft.server.network.ServerGamePacketListenerImpl connection -> this.field_Connection = clazz_EntityPlayer.getDeclaredField("b"); - if (this.isOneNinteenThreeOrNewer){ + if (this.isOneNinteenThreeOrNewer) { // private final Int2ObjectMap> itemsById this.field_Int2ObjectMap = clazz_DataWatcher.getDeclaredField("e"); this.field_Int2ObjectMap.setAccessible(true); @@ -347,7 +362,7 @@ private void buildFields() throws NoSuchFieldException { private void buildConstructors() throws NoSuchMethodException { this.ctor_EntityDataAccessor = clazz_DataWatcherObject.getConstructor( - int.class, clazz_DataWatcherSerializer); + int.class, clazz_DataWatcherSerializer); this.ctor_SynchedEntityData = clazz_DataWatcher.getConstructor(clazz_Entity); @@ -356,24 +371,23 @@ private void buildConstructors() throws NoSuchMethodException { // public net.minecraft.network.protocol.game.PacketPlayOutEntityMetadata(int,java.util.List>) this.ctor_Packet = clazz_ClientboundSetEntityDataPacket.getConstructor( - int.class, List.class); - } - else{ + int.class, List.class); + } else { // up to 1.19.2 use this one: this.ctor_Packet = clazz_ClientboundSetEntityDataPacket.getConstructor( - int.class, clazz_DataWatcher, boolean.class); + int.class, clazz_DataWatcher, boolean.class); } } - public boolean getHasKiori(){ - return this.hasKiori; + public boolean hasKiori() { + return hasKiori; } - public @NotNull ServerVersionInfo getServerVersionInfo(){ - return this.ver; + public @NotNull ServerVersionInfo getServerVersionInfo() { + return ver; } - public boolean getIsOneNinteenThreeOrNewer(){ - return this.isOneNinteenThreeOrNewer; + public boolean isOneNinteenThreeOrNewer() { + return isOneNinteenThreeOrNewer; } } \ No newline at end of file diff --git a/src/main/java/me/lokka30/levelledmobs/nms/KyoriNametags.java b/src/main/java/me/lokka30/levelledmobs/nametag/KyoriNametags.java similarity index 50% rename from src/main/java/me/lokka30/levelledmobs/nms/KyoriNametags.java rename to src/main/java/me/lokka30/levelledmobs/nametag/KyoriNametags.java index fe2ad66fb..fb467ef4b 100644 --- a/src/main/java/me/lokka30/levelledmobs/nms/KyoriNametags.java +++ b/src/main/java/me/lokka30/levelledmobs/nametag/KyoriNametags.java @@ -1,5 +1,6 @@ -package me.lokka30.levelledmobs.nms; +package me.lokka30.levelledmobs.nametag; +import java.lang.reflect.Method; import me.lokka30.levelledmobs.result.NametagResult; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextReplacementConfig; @@ -7,33 +8,48 @@ import org.bukkit.entity.LivingEntity; import org.jetbrains.annotations.NotNull; -import java.lang.reflect.Method; - +/** + * @author PenalBuffalo (aka stumper66) + */ public class KyoriNametags { + public static @NotNull Object generateComponent( - final @NotNull LivingEntity livingEntity, final @NotNull NametagResult nametagResult){ + final @NotNull LivingEntity livingEntity, + final @NotNull NametagResult nametagResult + ) { final String nametag = nametagResult.getNametagNonNull(); final String mobKey = livingEntity.getType().translationKey(); // this component holds the component of the mob name and will show the translated name on clients - final net.kyori.adventure.text.Component mobNameComponent = nametagResult.overriddenName == null ? + final net.kyori.adventure.text.Component mobNameComponent = + nametagResult.overriddenName == null ? net.kyori.adventure.text.Component.translatable(mobKey) : - LegacyComponentSerializer.legacyAmpersand().deserialize(nametagResult.overriddenName); + LegacyComponentSerializer + .legacyAmpersand() + .deserialize(nametagResult.overriddenName); // replace placeholders and set the new death message - final Component result = LegacyComponentSerializer.legacyAmpersand().deserialize(nametag) - .replaceText(TextReplacementConfig.builder() - .matchLiteral("{DisplayName}").replacement(mobNameComponent).build()); + final Component result = LegacyComponentSerializer + .legacyAmpersand() + .deserialize(nametag) + .replaceText( + TextReplacementConfig.builder() + .matchLiteral("{DisplayName}") + .replacement(mobNameComponent).build() + ); // PaperAdventure.asVanilla(kyoriComponent) - try{ - final Class clazz = Class.forName("io.papermc.paper.adventure.PaperAdventure"); - final Method asVanilla = clazz.getDeclaredMethod("asVanilla", Component.class); + try { + final Class clazz = Class + .forName("io.papermc.paper.adventure.PaperAdventure"); + + final Method asVanilla = clazz + .getDeclaredMethod("asVanilla", Component.class); + return asVanilla.invoke(clazz, result); - } - catch (Exception e){ - e.printStackTrace(); + } catch (final Exception ex) { + ex.printStackTrace(); } return ComponentUtils.getEmptyComponent(); diff --git a/src/main/java/me/lokka30/levelledmobs/nms/MiscUtils.java b/src/main/java/me/lokka30/levelledmobs/nametag/MiscUtils.java similarity index 79% rename from src/main/java/me/lokka30/levelledmobs/nms/MiscUtils.java rename to src/main/java/me/lokka30/levelledmobs/nametag/MiscUtils.java index 8273e370c..6eb7b17eb 100644 --- a/src/main/java/me/lokka30/levelledmobs/nms/MiscUtils.java +++ b/src/main/java/me/lokka30/levelledmobs/nametag/MiscUtils.java @@ -1,29 +1,44 @@ -package me.lokka30.levelledmobs.nms; +package me.lokka30.levelledmobs.nametag; +import java.lang.reflect.Method; import me.lokka30.levelledmobs.LevelledMobs; import org.bukkit.entity.LivingEntity; import org.jetbrains.annotations.NotNull; -import java.lang.reflect.Method; - +/** + * @author PenalBuffalo (aka stumper66) + */ public class MiscUtils { - public static @NotNull String getNBTDump(final @NotNull LivingEntity livingEntity, final @NotNull LevelledMobs main){ + + public static @NotNull String getNBTDump( + final @NotNull LivingEntity livingEntity + ) { // final ServerVersionInfo versionInfo = main.nametagQueueManager.nmsHandler.versionInfo; // if (versionInfo.getMinecraftVersion() <= 1.16){ // return getNBTDump_1_16(livingEntity, versionInfo.getNMSVersion()); // } +// TODO - Handle commented out code final Definitions def = LevelledMobs.getInstance().getDefinitions(); + try { //final Method method_getHandle = def.clazz_CraftLivingEntity.getDeclaredMethod("getHandle"); + // TODO - Handle commented out code final Object internalLivingEntity = def.method_getHandle.invoke(livingEntity); - final Class compoundTagClazz = Class.forName("net.minecraft.nbt.NBTTagCompound"); + + final Class compoundTagClazz = + Class.forName("net.minecraft.nbt.NBTTagCompound"); + final Object compoundTag = compoundTagClazz.getConstructor().newInstance(); - final Method saveWithoutId = def.clazz_Entity.getDeclaredMethod("e", compoundTagClazz); + + final Method saveWithoutId = + def.clazz_Entity.getDeclaredMethod("e", compoundTagClazz); + saveWithoutId.invoke(internalLivingEntity, compoundTag); + return compoundTag.toString(); - } catch (Exception e) { - e.printStackTrace(); + } catch (final Exception ex) { + ex.printStackTrace(); } return ""; @@ -58,4 +73,5 @@ public class MiscUtils { // // return ""; // } +// TODO - Handle commented out code } diff --git a/src/main/java/me/lokka30/levelledmobs/nametag/NametagSender.java b/src/main/java/me/lokka30/levelledmobs/nametag/NametagSender.java new file mode 100644 index 000000000..e54e5c86b --- /dev/null +++ b/src/main/java/me/lokka30/levelledmobs/nametag/NametagSender.java @@ -0,0 +1,22 @@ +package me.lokka30.levelledmobs.nametag; + +import me.lokka30.levelledmobs.result.NametagResult; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +/** + * A common interface for sending nametag packets + * + * @author PenalBuffalo (aka stumper66) + * @since 3.6.0 + */ +public interface NametagSender { + + void sendNametag( + final @NotNull LivingEntity livingEntity, + final @NotNull NametagResult nametag, + final @NotNull Player player, + final boolean alwaysVisible + ); +} diff --git a/src/main/java/me/lokka30/levelledmobs/nms/NMSHandler.java b/src/main/java/me/lokka30/levelledmobs/nametag/NametagSenderHandler.java similarity index 63% rename from src/main/java/me/lokka30/levelledmobs/nms/NMSHandler.java rename to src/main/java/me/lokka30/levelledmobs/nametag/NametagSenderHandler.java index 87ebe1f34..000954bd9 100644 --- a/src/main/java/me/lokka30/levelledmobs/nms/NMSHandler.java +++ b/src/main/java/me/lokka30/levelledmobs/nametag/NametagSenderHandler.java @@ -1,4 +1,4 @@ -package me.lokka30.levelledmobs.nms; +package me.lokka30.levelledmobs.nametag; import me.lokka30.levelledmobs.LevelledMobs; import me.lokka30.levelledmobs.managers.ExternalCompatibilityManager; @@ -8,52 +8,55 @@ import org.jetbrains.annotations.Nullable; /** - * Gets the correct NMS version for sending nametag packets + * Manages the ideal nametag sender implementation for the server's version * - * @author stumper66 + * @author PenalBuffalo (aka stumper66) * @since 3.6.0 */ -public class NMSHandler { +public class NametagSenderHandler { - public NMSHandler(final @NotNull LevelledMobs main) { + public NametagSenderHandler( + final @NotNull LevelledMobs main + ) { this.main = main; this.hasPaper = VersionUtils.isRunningPaper(); this.versionInfo = new ServerVersionInfo(); } private final LevelledMobs main; - private NMSUtil currentUtil; + private NametagSender currentUtil; public boolean isUsingProtocolLib; public final ServerVersionInfo versionInfo; public final boolean hasPaper; - @Nullable public NMSUtil getCurrentUtil() { + @Nullable + public NametagSender getCurrentUtil() { if (this.currentUtil != null) { return this.currentUtil; } - // supported is paper >= 1.18 or spigot >= 1.19 + // supported is spigot >= 1.17 // otherwise protocollib is used - //if (hasPaper && versionInfo.getMinecraftVersion() >= 1.18 || - // !hasPaper && versionInfo.getMinecraftVersion() >= 1.19) { - if (versionInfo.getMinecraftVersion() >= 1.17) { // 1.18 and newer we support with direct nms (Paper) // or 1.19 spigot and newer - this.currentUtil = new NametagSender(); + this.currentUtil = new NmsNametagSender(); + Utils.logger.info( - String.format("Using NMS version %s for nametag support", versionInfo.getNMSVersion())); + String.format("Using NMS version %s for nametag support", + versionInfo.getNMSVersion()) + ); + } else if (ExternalCompatibilityManager.hasProtocolLibInstalled()) { // we don't directly support this version, use ProtocolLib Utils.logger.info( "We don't have NMS support for this version of Minecraft, using ProtocolLib"); - this.currentUtil = new ProtocolLibHandler(main); + + this.currentUtil = new ProtocolLibNametagSender(main); this.isUsingProtocolLib = true; - } - else{ - Utils.logger.warning( - "ProtocolLib is not installed. No nametags will be visible"); + } else { + Utils.logger.warning("ProtocolLib is not installed. No nametags will be visible"); } return this.currentUtil; diff --git a/src/main/java/me/lokka30/levelledmobs/nms/NametagSender.java b/src/main/java/me/lokka30/levelledmobs/nametag/NmsNametagSender.java similarity index 64% rename from src/main/java/me/lokka30/levelledmobs/nms/NametagSender.java rename to src/main/java/me/lokka30/levelledmobs/nametag/NmsNametagSender.java index abc34a71c..dff2b1f2f 100644 --- a/src/main/java/me/lokka30/levelledmobs/nms/NametagSender.java +++ b/src/main/java/me/lokka30/levelledmobs/nametag/NmsNametagSender.java @@ -1,11 +1,10 @@ -package me.lokka30.levelledmobs.nms; +package me.lokka30.levelledmobs.nametag; import java.lang.reflect.InvocationTargetException; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Optional; - import me.lokka30.levelledmobs.LevelledMobs; import me.lokka30.levelledmobs.result.NametagResult; import me.lokka30.microlib.messaging.MessageUtils; @@ -18,31 +17,40 @@ /** * Sends NMS verison specific nametag packets to players * - * @author stumper66 + * @author PenalBuffalo (aka stumper66) * @since 3.6.0 */ @SuppressWarnings("unchecked") -public class NametagSender implements NMSUtil { +public class NmsNametagSender implements NametagSender { - public NametagSender() { + public NmsNametagSender() { this.def = LevelledMobs.getInstance().getDefinitions(); } private final Definitions def; - public void sendNametag(final @NotNull LivingEntity livingEntity, final @NotNull NametagResult nametag, - final @NotNull Player player, final boolean doAlwaysVisible) { - - if (!player.isOnline() || !player.isValid()) return; - - final Runnable runnable = () -> sendNametagNonAsync(livingEntity, nametag, player, doAlwaysVisible); + public void sendNametag( + final @NotNull LivingEntity livingEntity, + final @NotNull NametagResult nametag, + final @NotNull Player player, + final boolean alwaysVisible + ) { + if (!player.isOnline() || !player.isValid()) { + return; + } - Bukkit.getScheduler().runTask(LevelledMobs.getInstance(), runnable); + Bukkit.getScheduler().runTask( + LevelledMobs.getInstance(), + () -> sendNametagNonAsync(livingEntity, nametag, player, alwaysVisible) + ); } - private void sendNametagNonAsync(final @NotNull LivingEntity livingEntity, final @NotNull NametagResult nametag, - final @NotNull Player player, final boolean doAlwaysVisible) { - + private void sendNametagNonAsync( + final @NotNull LivingEntity livingEntity, + final @NotNull NametagResult nametag, + final @NotNull Player player, + final boolean doAlwaysVisible + ) { try { // livingEntity.getHandle() final Object internalLivingEntity = def.method_getHandle.invoke(livingEntity); @@ -50,40 +58,43 @@ private void sendNametagNonAsync(final @NotNull LivingEntity livingEntity, final final Object entityDataPreClone = def.method_getEntityData.invoke(internalLivingEntity); final Object entityData = cloneEntityData(entityDataPreClone, internalLivingEntity); - if (entityData == null){ + if (entityData == null) { return; } //final Object entityData = entityDataPreClone; - final Object optionalComponent = def.field_OPTIONAL_COMPONENT.get(def.clazz_DataWatcherRegistry); + final Object optionalComponent = + def.field_OPTIONAL_COMPONENT.get(def.clazz_DataWatcherRegistry); // final EntityDataAccessor> customNameAccessor = // //new EntityDataAccessor<>(2, EntityDataSerializers.OPTIONAL_COMPONENT); - final Object customNameAccessor = def.ctor_EntityDataAccessor.newInstance(2, optionalComponent); + final Object customNameAccessor = + + def.ctor_EntityDataAccessor.newInstance(2, optionalComponent); final Optional customName = buildNametagComponent(livingEntity, nametag); //final Optional customName = entityData.set(customNameAccessor, customName); def.method_set.invoke(entityData, customNameAccessor, customName); final Object BOOLEAN = def.field_BOOLEAN.get(def.clazz_DataWatcherRegistry); - final Object customNameVisibleAccessor = def.ctor_EntityDataAccessor.newInstance(3, BOOLEAN); + final Object customNameVisibleAccessor = + def.ctor_EntityDataAccessor.newInstance(3, BOOLEAN); // entityData.set(customNameVisibleAccessor, !nametag.isNullOrEmpty() && doAlwaysVisible); def.method_set.invoke(entityData, customNameVisibleAccessor, doAlwaysVisible); - final int livingEntityId = (int)def.method_getId.invoke(internalLivingEntity); + final int livingEntityId = (int) def.method_getId.invoke(internalLivingEntity); Object packet; - if (def.getIsOneNinteenThreeOrNewer()){ + if (def.isOneNinteenThreeOrNewer()) { // List> // java.util.List getAllNonDefaultValues() -> c final List getAllNonDefaultValues = getNametagFields(entityData); packet = def.ctor_Packet - .newInstance(livingEntityId, getAllNonDefaultValues); - } - else{ + .newInstance(livingEntityId, getAllNonDefaultValues); + } else { packet = def.ctor_Packet - .newInstance(livingEntityId, entityData, true); + .newInstance(livingEntityId, entityData, true); } final Object serverPlayer = def.method_PlayergetHandle.invoke(player); @@ -99,11 +110,11 @@ private void sendNametagNonAsync(final @NotNull LivingEntity livingEntity, final // returns SynchedEntityData (DataWatcher) // args: SynchedEntityData, LivingEntity (nms) private @Nullable Object cloneEntityData( - final @NotNull Object entityDataPreClone, - final @NotNull Object internalLivingEntity + final @NotNull Object entityDataPreClone, + final @NotNull Object internalLivingEntity ) throws InvocationTargetException, InstantiationException, IllegalAccessException { - if (!def.getIsOneNinteenThreeOrNewer()) { + if (!def.isOneNinteenThreeOrNewer()) { return cloneEntityDataLegacy(entityDataPreClone, internalLivingEntity); } @@ -111,21 +122,20 @@ private void sendNametagNonAsync(final @NotNull LivingEntity livingEntity, final // public net.minecraft.network.syncher.DataWatcher(net.minecraft.world.entity.Entity) final Object entityData = def.ctor_SynchedEntityData.newInstance(internalLivingEntity); - try{ + try { final Map itemsById = (Map) - def.field_Int2ObjectMap.get(entityDataPreClone); + def.field_Int2ObjectMap.get(entityDataPreClone); if (itemsById.isEmpty()) { return null; } - for (final Object objDataItem : itemsById.values()){ + for (final Object objDataItem : itemsById.values()) { final Object accessor = def.method_getAccessor.invoke(objDataItem); final Object value = def.method_getValue.invoke(objDataItem); def.method_define.invoke(entityData, accessor, value); } return entityData; - } - catch (Exception e){ + } catch (Exception e) { e.printStackTrace(); } @@ -133,18 +143,18 @@ private void sendNametagNonAsync(final @NotNull LivingEntity livingEntity, final } private @NotNull Object cloneEntityDataLegacy( - final @NotNull Object entityDataPreClone, - final @NotNull Object internalLivingEntity + final @NotNull Object entityDataPreClone, + final @NotNull Object internalLivingEntity ) throws InvocationTargetException, InstantiationException, IllegalAccessException { final Object entityData = def.ctor_SynchedEntityData.newInstance(internalLivingEntity); - if (def.method_getAll.invoke(entityDataPreClone) == null){ + if (def.method_getAll.invoke(entityDataPreClone) == null) { return entityData; } // SynchedEntityData.DataItem // List> getAll() - for (final Object dataItem : (List)def.method_getAll.invoke(entityDataPreClone)){ + for (final Object dataItem : (List) def.method_getAll.invoke(entityDataPreClone)) { // entityData.define(dataItem.getAccessor(), dataItem.getValue()); final Object accessor = def.method_getAccessor.invoke(dataItem); final Object value = def.method_getValue.invoke(dataItem); @@ -154,42 +164,46 @@ private void sendNametagNonAsync(final @NotNull LivingEntity livingEntity, final return entityData; } - private @NotNull List getNametagFields(final @NotNull Object entityData){ + private @NotNull List getNametagFields( + final @NotNull Object entityData + ) { final List results = new LinkedList<>(); - try{ - final Map itemsById = (Map) - def.field_Int2ObjectMap.get(entityData); - if (itemsById.isEmpty()) { - return results; - } - for (final int objDataId : itemsById.keySet()){ + try { + final Map itemsById = + (Map) def.field_Int2ObjectMap.get(entityData); + + if (itemsById.isEmpty()) return results; + + for (final int objDataId : itemsById.keySet()) { if (objDataId < 2 || objDataId > 3) continue; + final Object objDataItem = itemsById.get(objDataId); final Object accessor = def.method_getAccessor.invoke(objDataItem); // DataWatcher.Item - final Object dataWatcherItem = def.method_DataWatcher_GetItem.invoke(entityData, accessor); + final Object dataWatcherItem = def.method_DataWatcher_GetItem + .invoke(entityData, accessor); + results.add(def.method_DataWatcherItem_Value.invoke(dataWatcherItem)); //results.add(objDataItem); } - } - catch (Exception e){ - e.printStackTrace(); + } catch (final Exception ex) { + ex.printStackTrace(); } return results; } private Optional buildNametagComponent( - final @NotNull LivingEntity livingEntity, - final @NotNull NametagResult nametag + final @NotNull LivingEntity livingEntity, + final @NotNull NametagResult nametag ) { if (nametag.isNullOrEmpty()) { return Optional.of(ComponentUtils.getEmptyComponent()); } - if (def.getHasKiori()){ + if (def.hasKiori()) { // paper servers go here: return Optional.of(KyoriNametags.generateComponent(livingEntity, nametag)); } @@ -203,49 +217,43 @@ private Optional buildNametagComponent( if (displayNameIndex < 0) { final Object comp = ComponentUtils.getTextComponent(nametag.getNametagNonNull()); return comp == null ? - Optional.empty() : Optional.of(comp); + Optional.empty() : Optional.of(comp); } final String leftText = displayNameIndex > 0 ? - resolveText(mobName.substring(0, displayNameIndex)) : - null; - final String rightText = mobName.length() > displayNameIndex + displayName.length() ? - resolveText(mobName.substring(displayNameIndex + displayName.length())) : - null; + resolveText(mobName.substring(0, displayNameIndex)) : + null; + final String rightText = mobName.length() > displayNameIndex + displayName.length() ? + resolveText(mobName.substring(displayNameIndex + displayName.length())) : + null; final Object mobNameComponent = nametag.overriddenName == null ? - ComponentUtils.getTranslatableComponent(def.getTranslationKey(livingEntity)) : - ComponentUtils.getTextComponent(nametag.overriddenName); + ComponentUtils.getTranslatableComponent(def.getTranslationKey(livingEntity)) : + ComponentUtils.getTextComponent(nametag.overriddenName); final Object comp = ComponentUtils.getEmptyComponent(); // MutableComponent comp = Component.empty(); if (leftText != null) { // comp.append(Component); - ComponentUtils.append(comp, ComponentUtils.getTextComponent(leftText)); + ComponentUtils.appendComponents(comp, ComponentUtils.getTextComponent(leftText)); } - ComponentUtils.append(comp, mobNameComponent); + ComponentUtils.appendComponents(comp, mobNameComponent); if (rightText != null) { // comp.append(Component); - ComponentUtils.append(comp, ComponentUtils.getTextComponent(rightText)); + ComponentUtils.appendComponents(comp, ComponentUtils.getTextComponent(rightText)); } return Optional.of(comp); } - private @Nullable String resolveText(final @Nullable String text){ + private @Nullable String resolveText(@Nullable String text) { if (text == null || text.isEmpty()) return null; - String result = text; - if (text.contains("&#")) - result = MessageUtils.colorizeHexCodes(text); - if (text.contains("&")) - result = MessageUtils.colorizeAll(result); - - return result; + return MessageUtils.colorizeAll(text); } public String toString() { diff --git a/src/main/java/me/lokka30/levelledmobs/nms/ProtocolLibHandler.java b/src/main/java/me/lokka30/levelledmobs/nametag/ProtocolLibNametagSender.java similarity index 72% rename from src/main/java/me/lokka30/levelledmobs/nms/ProtocolLibHandler.java rename to src/main/java/me/lokka30/levelledmobs/nametag/ProtocolLibNametagSender.java index 4568efe2e..b06577f2c 100644 --- a/src/main/java/me/lokka30/levelledmobs/nms/ProtocolLibHandler.java +++ b/src/main/java/me/lokka30/levelledmobs/nametag/ProtocolLibNametagSender.java @@ -1,4 +1,4 @@ -package me.lokka30.levelledmobs.nms; +package me.lokka30.levelledmobs.nametag; import com.comphenix.protocol.PacketType; import com.comphenix.protocol.ProtocolLibrary; @@ -19,21 +19,28 @@ /** * Handles sending nametag packets to player via ProtocolLib * - * @author stumper66 + * @author PenalBuffalo (aka stumper66) * @since 3.6.0 */ -public class ProtocolLibHandler implements NMSUtil { +public class ProtocolLibNametagSender implements NametagSender { - public ProtocolLibHandler(final @NotNull LevelledMobs main) { + public ProtocolLibNametagSender( + final @NotNull LevelledMobs main + ) { this.main = main; } private final LevelledMobs main; - public void sendNametag(final @NotNull LivingEntity livingEntity, final @NotNull NametagResult nametagInfo, - final @NotNull Player player, final boolean doAlwaysVisible) { - - if (!player.isOnline() || !player.isValid()) return; + public void sendNametag( + final @NotNull LivingEntity livingEntity, + final @NotNull NametagResult nametagInfo, + final @NotNull Player player, + final boolean alwaysVisible + ) { + if (!player.isOnline() || !player.isValid()) { + return; + } final WrappedDataWatcher dataWatcher; final WrappedDataWatcher.Serializer chatSerializer; @@ -50,31 +57,33 @@ public void sendNametag(final @NotNull LivingEntity livingEntity, final @NotNull chatSerializer = WrappedDataWatcher.Registry.getChatComponentSerializer(true); } catch (final ConcurrentModificationException ex) { Utils.debugLog(main, DebugType.UPDATE_NAMETAG_FAIL, - "&bConcurrentModificationException &7caught, skipping nametag update of &b" + "&bConcurrentModificationException &7caught, " + + "skipping nametag update of &b" + livingEntity.getName() + "&7."); return; } catch (final IllegalArgumentException ex) { Utils.debugLog(main, DebugType.UPDATE_NAMETAG_FAIL, - "Registry is empty (&bIllegalArgumentException&7 caught), skipping nametag update of &b" + "Registry is empty (&bIllegalArgumentException&7 caught), " + + "skipping nametag update of &b" + livingEntity.getName() + "&7."); return; } - final WrappedDataWatcher.WrappedDataWatcherObject watcherObject = new WrappedDataWatcher.WrappedDataWatcherObject( - 2, chatSerializer); + final WrappedDataWatcher.WrappedDataWatcherObject watcherObject = + new WrappedDataWatcher.WrappedDataWatcherObject(2, chatSerializer); final int objectIndex = 3; final int fieldIndex = 0; final Optional optional = nametagInfo.isNullOrEmpty() ? Optional.empty() : Optional.of(WrappedChatComponent.fromChatMessage( - MessageUtils.colorizeAll(nametagInfo.getNametag()))[0].getHandle()); + MessageUtils.colorizeAll(nametagInfo.getNametag()))[0].getHandle()); dataWatcher.setObject(watcherObject, optional); if (nametagInfo.isNullOrEmpty()) { dataWatcher.setObject(objectIndex, false); } else { - dataWatcher.setObject(objectIndex, doAlwaysVisible); + dataWatcher.setObject(objectIndex, alwaysVisible); } final PacketContainer packet = ProtocolLibrary.getProtocolManager() @@ -86,8 +95,11 @@ public void sendNametag(final @NotNull LivingEntity livingEntity, final @NotNull try { ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet); } catch (final Exception ex) { - Utils.logger.error("Unable to update nametag packet for player &b" + player.getName() - + "&7; stack trace:"); + Utils.logger.error( + "Unable to update nametag packet for player &b" + + player.getName() + + "&7; stack trace:" + ); ex.printStackTrace(); } } diff --git a/src/main/java/me/lokka30/levelledmobs/nms/ServerVersionInfo.java b/src/main/java/me/lokka30/levelledmobs/nametag/ServerVersionInfo.java similarity index 59% rename from src/main/java/me/lokka30/levelledmobs/nms/ServerVersionInfo.java rename to src/main/java/me/lokka30/levelledmobs/nametag/ServerVersionInfo.java index 99758818d..f8c03b38a 100644 --- a/src/main/java/me/lokka30/levelledmobs/nms/ServerVersionInfo.java +++ b/src/main/java/me/lokka30/levelledmobs/nametag/ServerVersionInfo.java @@ -1,15 +1,18 @@ -package me.lokka30.levelledmobs.nms; +package me.lokka30.levelledmobs.nametag; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import me.lokka30.levelledmobs.util.Utils; import org.bukkit.Bukkit; import org.jetbrains.annotations.NotNull; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - +/** + * @author PenalBuffalo (aka stumper66) + */ @SuppressWarnings("unused") public class ServerVersionInfo { - public ServerVersionInfo(){ + + public ServerVersionInfo() { parseBukkitVersion(); parseNMSVersion(); } @@ -19,15 +22,17 @@ public ServerVersionInfo(){ private int revision; private double minecraftVersion; private @NotNull String nmsVersion = "unknown"; - private static final Pattern versionPattern = Pattern.compile(".*\\.(v\\d+_\\d+_R\\d+)(?:.+)?"); - private static final Pattern versionShortPattern = Pattern.compile(".*\\.(v\\d+_\\d+)(?:.+)?"); + private static final Pattern versionPattern = + Pattern.compile(".*\\.(v\\d+_\\d+_R\\d+)(?:.+)?"); + private static final Pattern versionShortPattern = + Pattern.compile(".*\\.(v\\d+_\\d+)(?:.+)?"); - private void parseBukkitVersion(){ + private void parseBukkitVersion() { final String bukkitVersion = Bukkit.getBukkitVersion(); // 1.19.2-R0.1-SNAPSHOT --> 1.19.2 final int firstDash = bukkitVersion.indexOf("-"); final String[] versions = bukkitVersion.substring(0, firstDash).split("\\."); - for (int i = 0; i < versions.length; i++){ + for (int i = 0; i < versions.length; i++) { switch (i) { case 0 -> this.majorVersion = Integer.parseInt(versions[i]); case 1 -> this.minorVersion = Integer.parseInt(versions[i]); @@ -39,19 +44,23 @@ private void parseBukkitVersion(){ private void parseNMSVersion() { // example: org.bukkit.craftbukkit.v1_18_R2.CraftServer final Matcher nmsRegex = versionPattern.matcher( - Bukkit.getServer().getClass().getCanonicalName()); + Bukkit.getServer().getClass().getCanonicalName()); final Matcher nmsShortRegex = versionShortPattern.matcher( - Bukkit.getServer().getClass().getCanonicalName()); + Bukkit.getServer().getClass().getCanonicalName()); if (nmsShortRegex.find()) { // example: 1.18 - final String versionStr = nmsShortRegex.group(1).replace("_", ".").replace("v", ""); + final String versionStr = nmsShortRegex + .group(1) + .replace("_", ".") + .replace("v", ""); + try { this.minecraftVersion = Double.parseDouble(versionStr); } catch (Exception e) { Utils.logger.warning( - String.format("Could not extract the minecraft version from '%s'. %s", - Bukkit.getServer().getClass().getCanonicalName(), e.getMessage())); + String.format("Could not extract the minecraft version from '%s'. %s", + Bukkit.getServer().getClass().getCanonicalName(), e.getMessage())); } } @@ -60,20 +69,21 @@ private void parseNMSVersion() { this.nmsVersion = nmsRegex.group(1); } else { Utils.logger.warning( - "NMSHandler: Could not match regex for bukkit version: " + Bukkit.getServer() - .getClass().getCanonicalName()); + "NMSHandler: Could not match regex for bukkit version: " + Bukkit.getServer() + .getClass().getCanonicalName() + ); } } - public int getMajorVersion(){ + public int getMajorVersion() { return this.majorVersion; } - public int getMinorVersion(){ + public int getMinorVersion() { return this.minorVersion; } - public int getRevision(){ + public int getRevision() { return this.revision; } @@ -81,16 +91,16 @@ public double getMinecraftVersion() { return minecraftVersion; } - public @NotNull String getNMSVersion(){ + public @NotNull String getNMSVersion() { return this.nmsVersion; } - public boolean isNMSVersionValid(){ + public boolean isNMSVersionValid() { return !"unknown".equals(this.nmsVersion); } - public String toString(){ + public String toString() { return String.format("%s.%s.%s - %s", - this.majorVersion, this.minorVersion, this.revision, this.nmsVersion); + this.majorVersion, this.minorVersion, this.revision, this.nmsVersion); } } diff --git a/src/main/java/me/lokka30/levelledmobs/nms/NMSUtil.java b/src/main/java/me/lokka30/levelledmobs/nms/NMSUtil.java deleted file mode 100644 index fb2e178a2..000000000 --- a/src/main/java/me/lokka30/levelledmobs/nms/NMSUtil.java +++ /dev/null @@ -1,18 +0,0 @@ -package me.lokka30.levelledmobs.nms; - -import me.lokka30.levelledmobs.result.NametagResult; -import org.bukkit.entity.LivingEntity; -import org.bukkit.entity.Player; -import org.jetbrains.annotations.NotNull; - -/** - * A common interface for sending nametag packets - * - * @author stumper66 - * @since 3.6.0 - */ -public interface NMSUtil { - - void sendNametag(final @NotNull LivingEntity livingEntity, final @NotNull NametagResult nametag, - final @NotNull Player player, final boolean doAlwaysVisible); -} From 2374d9b8da54c053de76e9443081e7a5fdd45d45 Mon Sep 17 00:00:00 2001 From: lokka30 Date: Fri, 23 Dec 2022 15:59:51 +0800 Subject: [PATCH 2/5] update version; update build configuration --- pom.xml | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/pom.xml b/pom.xml index 93e198497..38091ce62 100644 --- a/pom.xml +++ b/pom.xml @@ -6,10 +6,12 @@ me.lokka30 LevelledMobs - 3.9.1 b730 - jar + 3.9.2 b731 + LevelledMobs - The Ultimate RPG Mob Levelling Plugin + The Ultimate RPG Mob Levelling Solution + + jar 17 @@ -25,28 +27,28 @@ maven-compiler-plugin 3.10.1 - 16 - 16 + 17 + 17 net.md-5 specialsource-maven-plugin - 1.2.2 + 1.2.4 org.apache.maven.plugins maven-shade-plugin - 3.3.0 + 3.4.1 org.bstats - me.lokka30.levelledmobs.bstats + me.lokka30.levelledmobs.lib.bstats me.lokka30.microlib - ${project.groupId}.microlib + me.lokka30.levelledmobs.lib.microlib @@ -125,6 +127,7 @@ https://maven.enginehub.org/repo/ + net.essentialsx @@ -150,6 +153,7 @@ 3.3.39 provided + org.spigotmc spigot-api From cb7ca5cc5e05d96c67a6b9e704cbc864b7c76e4d Mon Sep 17 00:00:00 2001 From: Penal Buffalo Date: Tue, 27 Dec 2022 09:17:30 -0600 Subject: [PATCH 3/5] v3.9.2 b732 * fixed player levelling not cloning properly when used multiple times in rules --- pom.xml | 2 +- src/main/java/me/lokka30/levelledmobs/rules/RulesManager.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 38091ce62..7bcba8c4f 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ me.lokka30 LevelledMobs - 3.9.2 b731 + 3.9.2 b732 LevelledMobs The Ultimate RPG Mob Levelling Solution diff --git a/src/main/java/me/lokka30/levelledmobs/rules/RulesManager.java b/src/main/java/me/lokka30/levelledmobs/rules/RulesManager.java index 08e79883c..380f148d1 100644 --- a/src/main/java/me/lokka30/levelledmobs/rules/RulesManager.java +++ b/src/main/java/me/lokka30/levelledmobs/rules/RulesManager.java @@ -394,7 +394,7 @@ public int getRuleMobMaxLevel(@NotNull final LivingEntityInterface lmInterface) for (final RuleInfo ruleInfo : lmEntity.getApplicableRules()) { if (ruleInfo.playerLevellingOptions != null) { if (levellingOptions == null) - levellingOptions = ruleInfo.playerLevellingOptions; + levellingOptions = ruleInfo.playerLevellingOptions.cloneItem(); else levellingOptions.mergeRule(ruleInfo.playerLevellingOptions); } From 19ee538416a1e91ab6540a299c5e09446d7fd259 Mon Sep 17 00:00:00 2001 From: Penal Buffalo Date: Thu, 5 Jan 2023 15:00:44 -0600 Subject: [PATCH 4/5] v3.9.2 b733 * if `creature-death-nametag` in rules.yml doesn't contain `%displayname%` it will no longer display strange death nametags --- pom.xml | 2 +- .../subcommands/SpawnerEggCommand.java | 28 ++----- .../subcommands/SpawnerSubCommand.java | 82 +++++++------------ .../subcommands/SummonSubcommand.java | 16 ++-- .../listeners/paper/PlayerDeathListener.java | 9 +- 5 files changed, 57 insertions(+), 80 deletions(-) diff --git a/pom.xml b/pom.xml index 7bcba8c4f..8506351ae 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ me.lokka30 LevelledMobs - 3.9.2 b732 + 3.9.2 b733 LevelledMobs The Ultimate RPG Mob Levelling Solution diff --git a/src/main/java/me/lokka30/levelledmobs/commands/subcommands/SpawnerEggCommand.java b/src/main/java/me/lokka30/levelledmobs/commands/subcommands/SpawnerEggCommand.java index d18fb920a..7e61f8a91 100644 --- a/src/main/java/me/lokka30/levelledmobs/commands/subcommands/SpawnerEggCommand.java +++ b/src/main/java/me/lokka30/levelledmobs/commands/subcommands/SpawnerEggCommand.java @@ -97,30 +97,20 @@ private void parseEggCommand(final String[] args) { } switch (command) { - case "/name": - info.customName = foundValue; - break; - case "/customdropid": - info.customDropId = foundValue; - break; - case "/lore": - info.customLore = foundValue; - break; - case "/entity": + case "/name" -> info.customName = foundValue; + case "/customdropid" -> info.customDropId = foundValue; + case "/lore" -> info.customLore = foundValue; + case "/entity" -> { try { info.spawnType = EntityType.valueOf(foundValue.toUpperCase()); } catch (final Exception ignored) { commandSender.sendMessage("Invalid spawn type: " + foundValue); return; } - break; - case "/minlevel": - info.minLevel = Integer.parseInt(foundValue); - break; - case "/maxlevel": - info.maxLevel = Integer.parseInt(foundValue); - break; - case "/giveplayer": + } + case "/minlevel" -> info.minLevel = Integer.parseInt(foundValue); + case "/maxlevel" -> info.maxLevel = Integer.parseInt(foundValue); + case "/giveplayer" -> { if (Utils.isNullOrEmpty(foundValue)) { showMessage("command.levelledmobs.spawn_egg.no-player-specified"); return; @@ -135,7 +125,7 @@ private void parseEggCommand(final String[] args) { showMessage("common.player-offline", "%player%", foundValue); return; } - break; + } } } diff --git a/src/main/java/me/lokka30/levelledmobs/commands/subcommands/SpawnerSubCommand.java b/src/main/java/me/lokka30/levelledmobs/commands/subcommands/SpawnerSubCommand.java index f7f676a34..d28984b4f 100644 --- a/src/main/java/me/lokka30/levelledmobs/commands/subcommands/SpawnerSubCommand.java +++ b/src/main/java/me/lokka30/levelledmobs/commands/subcommands/SpawnerSubCommand.java @@ -181,51 +181,27 @@ private void parseCreateCommand(final String[] args) { } switch (command) { - case "/name": - info.customName = foundValue; - break; - case "/customdropid": - info.customDropId = foundValue; - break; - case "/lore": - info.customLore = foundValue; - break; - case "/spawntype": + case "/name" -> info.customName = foundValue; + case "/customdropid" -> info.customDropId = foundValue; + case "/lore" -> info.customLore = foundValue; + case "/spawntype" -> { try { info.spawnType = EntityType.valueOf(foundValue.toUpperCase()); } catch (final Exception ignored) { commandSender.sendMessage("Invalid spawn type: " + foundValue); return; } - break; - case "/minlevel": - info.minLevel = Integer.parseInt(foundValue); - break; - case "/maxlevel": - info.maxLevel = Integer.parseInt(foundValue); - break; - case "/delay": - info.delay = Integer.parseInt(foundValue); - break; - case "/maxnearbyentities": - info.maxNearbyEntities = Integer.parseInt(foundValue); - break; - case "/minspawndelay": - info.minSpawnDelay = Integer.parseInt(foundValue); - break; - case "/maxspawndelay": - info.maxSpawnDelay = Integer.parseInt(foundValue); - break; - case "/requiredplayerrange": - info.requiredPlayerRange = Integer.parseInt(foundValue); - break; - case "/spawncount": - info.spawnCount = Integer.parseInt(foundValue); - break; - case "/spawnrange": - info.spawnRange = Integer.parseInt(foundValue); - break; - case "/giveplayer": + } + case "/minlevel" -> info.minLevel = Integer.parseInt(foundValue); + case "/maxlevel" -> info.maxLevel = Integer.parseInt(foundValue); + case "/delay" -> info.delay = Integer.parseInt(foundValue); + case "/maxnearbyentities" -> info.maxNearbyEntities = Integer.parseInt(foundValue); + case "/minspawndelay" -> info.minSpawnDelay = Integer.parseInt(foundValue); + case "/maxspawndelay" -> info.maxSpawnDelay = Integer.parseInt(foundValue); + case "/requiredplayerrange" -> info.requiredPlayerRange = Integer.parseInt(foundValue); + case "/spawncount" -> info.spawnCount = Integer.parseInt(foundValue); + case "/spawnrange" -> info.spawnRange = Integer.parseInt(foundValue); + case "/giveplayer" -> { if (Utils.isNullOrEmpty(foundValue)) { showMessage("command.levelledmobs.spawner.no-player-specified"); return; @@ -240,7 +216,7 @@ private void parseCreateCommand(final String[] args) { showMessage("common.player-offline", "%player%", foundValue); return; } - break; + } } } @@ -380,7 +356,7 @@ public List parseTabCompletions(final LevelledMobs main, if ("create".equalsIgnoreCase(args[1])) { return tabCompletions_Create(args); } else if (("info".equalsIgnoreCase(args[1]) || "copy".equalsIgnoreCase(args[1])) - && args.length <= 3) { + && args.length == 3) { return Arrays.asList("on", "off"); } @@ -390,32 +366,36 @@ public List parseTabCompletions(final LevelledMobs main, @NotNull private List tabCompletions_Create(@NotNull final String @NotNull [] args) { if (!Utils.isNullOrEmpty(args[args.length - 2])) { switch (args[args.length - 2].toLowerCase()) { - case "/spawntype": + case "/spawntype" -> { final List entityNames = new LinkedList<>(); for (final EntityType entityType : EntityType.values()) { entityNames.add(entityType.toString().toLowerCase()); } - return entityNames; - case "/delay": + } + case "/delay" -> { return Collections.singletonList("0"); - case "/minspawndelay": + } + case "/minspawndelay" -> { return Collections.singletonList("200"); - case "/maxspawndelay": + } + case "/maxspawndelay" -> { return Collections.singletonList("800"); - case "/maxnearbyentities": - case "/requiredplayerrange": + } + case "/maxnearbyentities", "/requiredplayerrange" -> { return Collections.singletonList("16"); - case "/spawncount": - case "/spawnrange": + } + case "/spawncount", "/spawnrange" -> { return Collections.singletonList("4"); - case "/giveplayer": + } + case "/giveplayer" -> { final List players = new LinkedList<>(); for (final Player player : Bukkit.getOnlinePlayers()) { players.add(player.getName()); } players.sort(String.CASE_INSENSITIVE_ORDER); return players; + } } } diff --git a/src/main/java/me/lokka30/levelledmobs/commands/subcommands/SummonSubcommand.java b/src/main/java/me/lokka30/levelledmobs/commands/subcommands/SummonSubcommand.java index b420e608b..50fa8f85e 100644 --- a/src/main/java/me/lokka30/levelledmobs/commands/subcommands/SummonSubcommand.java +++ b/src/main/java/me/lokka30/levelledmobs/commands/subcommands/SummonSubcommand.java @@ -339,7 +339,7 @@ public List parseTabCompletions(final LevelledMobs main, // these are for atPlayer and atLocation if (args.length > 5) { switch (args[4].toLowerCase()) { - case "atplayer": + case "atplayer" -> { if (args.length == 6) { final List suggestions = new LinkedList<>(); for (final Player onlinePlayer : Bukkit.getOnlinePlayers()) { @@ -359,9 +359,8 @@ public List parseTabCompletions(final LevelledMobs main, return List.of(); } } - break; - - case "atlocation": + } + case "atlocation" -> { if (args.length < 9) { // args 6, 7 and 8 = x, y and z return List.of("~"); } else if (args.length == 9) { @@ -375,16 +374,17 @@ public List parseTabCompletions(final LevelledMobs main, return List.of(); } } - - break; - case "here": + } + case "here" -> { if (!skipOverride) { return List.of("/override"); } else { return List.of(); } - default: + } + default -> { return List.of(); + } } } diff --git a/src/main/java/me/lokka30/levelledmobs/listeners/paper/PlayerDeathListener.java b/src/main/java/me/lokka30/levelledmobs/listeners/paper/PlayerDeathListener.java index 67a1ece60..51f927f74 100644 --- a/src/main/java/me/lokka30/levelledmobs/listeners/paper/PlayerDeathListener.java +++ b/src/main/java/me/lokka30/levelledmobs/listeners/paper/PlayerDeathListener.java @@ -124,6 +124,7 @@ private void updateDeathMessage(final @NotNull PlayerDeathEvent event, final @No return; } final String mobName = nametagResult.getNametagNonNull(); + final int displayNameIndex = mobName.indexOf("{DisplayName}"); Component newCom; if (nametagResult.hadCustomDeathMessage){ @@ -132,8 +133,14 @@ private void updateDeathMessage(final @NotNull PlayerDeathEvent event, final @No newCom = LegacyComponentSerializer.legacyAmpersand().deserialize(mobName) .replaceText(replacementConfig); } + else if (displayNameIndex < 0){ + // creature-death-nametag in rules.yml doesn't contain %displayname% + // so we'll just send the whole thing as text + newCom = Component.translatable(tc.key(), + buildPlayerComponent(event.getEntity()), + LegacyComponentSerializer.legacyAmpersand().deserialize(mobName)); + } else { - final int displayNameIndex = mobName.indexOf("{DisplayName}"); final Component leftComp = displayNameIndex > 0 ? LegacyComponentSerializer.legacyAmpersand().deserialize(mobName.substring(0, displayNameIndex)) : Component.empty(); From 04ac4a85f4c1b0e8ba3c75a12b34016079fa62cb Mon Sep 17 00:00:00 2001 From: Penal Buffalo Date: Fri, 6 Jan 2023 08:51:28 -0600 Subject: [PATCH 5/5] v3.9.2 b734 * fix nametags not working on minecraft 1.16 --- pom.xml | 2 +- src/main/java/me/lokka30/levelledmobs/nametag/Definitions.java | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 8506351ae..cb1369177 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ me.lokka30 LevelledMobs - 3.9.2 b733 + 3.9.2 b734 LevelledMobs The Ultimate RPG Mob Levelling Solution diff --git a/src/main/java/me/lokka30/levelledmobs/nametag/Definitions.java b/src/main/java/me/lokka30/levelledmobs/nametag/Definitions.java index 58719e5f0..d7709d0e2 100644 --- a/src/main/java/me/lokka30/levelledmobs/nametag/Definitions.java +++ b/src/main/java/me/lokka30/levelledmobs/nametag/Definitions.java @@ -86,6 +86,9 @@ private void build() { ver.getMinecraftVersion() == 1.19d && ver.getRevision() >= 3d || ver.getMinecraftVersion() >= 1.20d; + // protocollib is used on 1.16 + if (ver.getMinecraftVersion() < 1.17) return; + try { buildClasses();