From f9bf102504799d78feae5122f293040fb75f3cdf Mon Sep 17 00:00:00 2001 From: BuildTools Date: Thu, 1 Feb 2024 15:38:22 +0500 Subject: [PATCH] bugfix --- .../su/nightexpress/nightcore/NightCore.java | 18 +++++++++++++++++- .../nightcore/util/EntityUtil.java | 13 +++++++++---- .../nightexpress/nightcore/util/ItemUtil.java | 3 ++- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/main/java/su/nightexpress/nightcore/NightCore.java b/src/main/java/su/nightexpress/nightcore/NightCore.java index 5d4c0a0..3122f69 100644 --- a/src/main/java/su/nightexpress/nightcore/NightCore.java +++ b/src/main/java/su/nightexpress/nightcore/NightCore.java @@ -1,5 +1,7 @@ package su.nightexpress.nightcore; +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; import su.nightexpress.nightcore.command.base.ReloadSubCommand; import su.nightexpress.nightcore.config.PluginDetails; @@ -11,7 +13,8 @@ import su.nightexpress.nightcore.core.command.CheckPermCommand; import su.nightexpress.nightcore.dialog.Dialog; import su.nightexpress.nightcore.integration.VaultHook; import su.nightexpress.nightcore.language.LangAssets; -import su.nightexpress.nightcore.menu.impl.EditorMenu; +import su.nightexpress.nightcore.util.EntityUtil; +import su.nightexpress.nightcore.util.ItemUtil; import su.nightexpress.nightcore.util.Plugins; import su.nightexpress.nightcore.util.blocktracker.PlayerBlockTracker; @@ -38,6 +41,7 @@ public class NightCore extends NightPlugin { } this.getBaseCommand().addChildren(new ReloadSubCommand(this, CorePerms.COMMAND_RELOAD)); + this.testMethods(); this.coreManager.setup(); } @@ -70,4 +74,16 @@ public class NightCore extends NightPlugin { public Set getChildrens() { return new HashSet<>(this.childrens); } + + private void testMethods() { + if (EntityUtil.ENTITY_COUNTER == null) { + this.warn("Entity Id Counter: NULL!"); + } + else this.info("Entity Id Counter: OK."); + + if (ItemUtil.compress(new ItemStack(Material.DIAMOND_SWORD)) == null) { + this.warn("Item NBT Compress: FAILED!"); + } + else this.info("Item NBT Compress: OK."); + } } diff --git a/src/main/java/su/nightexpress/nightcore/util/EntityUtil.java b/src/main/java/su/nightexpress/nightcore/util/EntityUtil.java index a1cb8b9..6c9b1f0 100644 --- a/src/main/java/su/nightexpress/nightcore/util/EntityUtil.java +++ b/src/main/java/su/nightexpress/nightcore/util/EntityUtil.java @@ -10,17 +10,22 @@ import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import su.nightexpress.nightcore.util.random.Rnd; import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; public class EntityUtil { - /*@Deprecated - public static boolean isNPC(@NotNull Entity entity) { - return entity.hasMetadata("NPC"); - }*/ + private static final Class NMS_ENTITY = Reflex.getClass("net.minecraft.world.entity", "Entity"); + private static final String ENTITY_COUNTER_NAME = Version.isAtLeast(Version.V1_19_R3) ? "d" : "c"; + public static final AtomicInteger ENTITY_COUNTER = (AtomicInteger) Reflex.getFieldValue(NMS_ENTITY, ENTITY_COUNTER_NAME); + + public static int nextEntityId() { + return ENTITY_COUNTER == null ? Rnd.nextInt(9999) : ENTITY_COUNTER.incrementAndGet(); + } public static double getAttribute(@NotNull LivingEntity entity, @NotNull Attribute attribute) { AttributeInstance instance = entity.getAttribute(attribute); diff --git a/src/main/java/su/nightexpress/nightcore/util/ItemUtil.java b/src/main/java/su/nightexpress/nightcore/util/ItemUtil.java index 1570996..405785c 100644 --- a/src/main/java/su/nightexpress/nightcore/util/ItemUtil.java +++ b/src/main/java/su/nightexpress/nightcore/util/ItemUtil.java @@ -277,7 +277,8 @@ public class ItemUtil { @Nullable public static String compress(@NotNull ItemStack item) { if (NBT_TAG_COMPOUND_NEW == null || AS_NMS_COPY == null || NMS_SAVE == null || NBT_IO_WRITE == null) { - throw new UnsupportedOperationException("Unsupported server version!"); + //throw new UnsupportedOperationException("Unsupported server version!"); + return null; } ByteArrayOutputStream outputStream = new ByteArrayOutputStream();