From c019276fe2a12bc12e0a34f4f5fe80f7775de7ad Mon Sep 17 00:00:00 2001 From: Szczurowsky Date: Mon, 19 Aug 2024 20:15:24 +0200 Subject: [PATCH] fix: set proper setting of enchant function in set_components --- README.md | 4 +-- build.gradle.kts | 2 +- .../util/paper/EnchantmentUtil.java | 26 +++++++++++++++++++ .../util/paper/HandleFunctionUtil.java | 20 +++++++++++--- 4 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 src/main/java/pl/szczurowsky/loottableparser/util/paper/EnchantmentUtil.java diff --git a/README.md b/README.md index b1c65bd..b9ed025 100644 --- a/README.md +++ b/README.md @@ -33,11 +33,11 @@ Framework Core pl.szczurowsky loot-table-parse - 1.0.3 + 1.0.4 ``` ```kotlin -implementation("pl.szczurowsky:loot-table-parse:1.0.3") +implementation("pl.szczurowsky:loot-table-parse:1.0.4") ``` ### Important note! diff --git a/build.gradle.kts b/build.gradle.kts index 6d3a571..bb81c4c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -4,7 +4,7 @@ plugins { } group = "pl.szczurowsky" -version = "1.0.3" +version = "1.0.4" repositories { mavenCentral() diff --git a/src/main/java/pl/szczurowsky/loottableparser/util/paper/EnchantmentUtil.java b/src/main/java/pl/szczurowsky/loottableparser/util/paper/EnchantmentUtil.java new file mode 100644 index 0000000..1bb214d --- /dev/null +++ b/src/main/java/pl/szczurowsky/loottableparser/util/paper/EnchantmentUtil.java @@ -0,0 +1,26 @@ +package pl.szczurowsky.loottableparser.util.paper; + +import org.bukkit.enchantments.Enchantment; + +import java.lang.reflect.Method; +import java.util.Optional; + +public class EnchantmentUtil { + + private EnchantmentUtil() { + throw new IllegalStateException("Utility class"); + } + + + public static Optional getEnchantment(String enchantment) { + try { + if (enchantment.contains("minecraft:")) enchantment = enchantment.replace("minecraft:", ""); + Method method = Enchantment.class.getDeclaredMethod("getEnchantment", String.class); + method.setAccessible(true); + Enchantment foundEnchantment = (Enchantment) method.invoke(null, enchantment); + return Optional.ofNullable(foundEnchantment); + } catch (Exception e) { + return Optional.empty(); + } + } +} diff --git a/src/main/java/pl/szczurowsky/loottableparser/util/paper/HandleFunctionUtil.java b/src/main/java/pl/szczurowsky/loottableparser/util/paper/HandleFunctionUtil.java index 58e6357..d900ada 100644 --- a/src/main/java/pl/szczurowsky/loottableparser/util/paper/HandleFunctionUtil.java +++ b/src/main/java/pl/szczurowsky/loottableparser/util/paper/HandleFunctionUtil.java @@ -5,6 +5,7 @@ import de.tr7zw.changeme.nbtapi.NBT; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.serializer.json.JSONComponentSerializer; +import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import pl.szczurowsky.loottableparser.pojo.LootEntry; @@ -12,6 +13,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.Optional; /** * Utility class for handling loot functions @@ -62,9 +64,21 @@ public static void handleFunction(ItemStack itemStack, LootEntry lootEntry) { } case "minecraft:set_components" -> { JsonObject components = jsonObject.getAsJsonObject("components"); - NBT.modify(itemStack, nbt -> { - nbt.mergeCompound(NBT.parseNBT(components.toString())); - }); + if (components.has("minecraft:enchantments")) { + JsonObject enchantments = components.getAsJsonObject("minecraft:enchantments"); + if (enchantments.has("levels")) { + JsonObject levels = enchantments.getAsJsonObject("levels"); + levels.asMap().forEach((enchantment, level) -> { + Optional enchantmentOptional = EnchantmentUtil.getEnchantment(enchantment); + enchantmentOptional.ifPresent(value -> itemStack.addUnsafeEnchantment(value, level.getAsInt())); + }); + } else { + enchantments.asMap().forEach((enchantment, level) -> { + Optional enchantmentOptional = EnchantmentUtil.getEnchantment(enchantment); + enchantmentOptional.ifPresent(value -> itemStack.addUnsafeEnchantment(value, level.getAsInt())); + }); + } + } } case "minecraft:set_nbt" -> {