diff --git a/common/src/main/java/me/lojosho/hibiscuscommons/config/serializer/ItemSerializer.java b/common/src/main/java/me/lojosho/hibiscuscommons/config/serializer/ItemSerializer.java index 7d037ba..ba65492 100644 --- a/common/src/main/java/me/lojosho/hibiscuscommons/config/serializer/ItemSerializer.java +++ b/common/src/main/java/me/lojosho/hibiscuscommons/config/serializer/ItemSerializer.java @@ -5,7 +5,6 @@ import io.papermc.paper.registry.RegistryKey; import me.lojosho.hibiscuscommons.HibiscusCommonsPlugin; import me.lojosho.hibiscuscommons.hooks.Hooks; import me.lojosho.hibiscuscommons.nms.MinecraftVersion; -import me.lojosho.hibiscuscommons.nms.NMSHandler; import me.lojosho.hibiscuscommons.nms.NMSHandlers; import me.lojosho.hibiscuscommons.util.*; import org.apache.commons.lang3.EnumUtils; @@ -28,7 +27,6 @@ import java.net.URL; import java.util.ArrayList; import java.util.Base64; import java.util.UUID; -import java.util.stream.Collectors; public class ItemSerializer implements TypeSerializer { @@ -87,43 +85,25 @@ public class ItemSerializer implements TypeSerializer { ItemMeta itemMeta = item.getItemMeta(); if (itemMeta == null) return item; + ItemBuilder itemBuilder = new ItemBuilder(item, itemMeta); + if (!nameNode.virtual()) { - if (HibiscusCommonsPlugin.isOnPaper()) { - itemMeta.displayName(AdventureUtils.MINI_MESSAGE.deserialize(nameNode.getString(""))); - } else { - itemMeta.setDisplayName(StringUtils.parseStringToString(nameNode.getString(""))); - } + itemBuilder.setDisplayName(nameNode.getString("")); + } + if (!unbreakableNode.virtual()) { + itemBuilder.setUnbreakable(unbreakableNode.getBoolean(false)); } - if (!unbreakableNode.virtual()) itemMeta.setUnbreakable(unbreakableNode.getBoolean()); if (!glowingNode.virtual()) { - itemMeta.addItemFlags(ItemFlag.HIDE_ENCHANTS); - itemMeta.addEnchant(Enchantment.UNBREAKING, 1, true); + itemBuilder.setGlowing(true); } if (!loreNode.virtual()) { - if (HibiscusCommonsPlugin.isOnPaper()) { - itemMeta.lore(loreNode.getList(String.class, new ArrayList<>()). - stream().map(AdventureUtils.MINI_MESSAGE::deserialize).collect(Collectors.toList())); - } else { - itemMeta.setLore(loreNode.getList(String.class, new ArrayList<>()). - stream().map(StringUtils::parseStringToString).collect(Collectors.toList())); - } - + itemBuilder.setLore(loreNode.getList(String.class, new ArrayList<>())); } - if (!modelDataNode.virtual()) itemMeta.setCustomModelData(modelDataNode.getInt()); - if (NMSHandlers.getVersion().isHigherOrEqual(MinecraftVersion.v1_21_4) && !modelIdNode.virtual()) { - String itemModelId = modelIdNode.getString(""); - String stringKey = HibiscusCommonsPlugin.getInstance().getName(); - if (itemModelId.contains(":")) { - String[] split = itemModelId.split(":"); - itemModelId = split[1]; - stringKey = split[0]; - } - if (!itemModelId.isEmpty()) { - NamespacedKey key = new NamespacedKey(stringKey, itemModelId); - itemMeta.setItemModel(key); - } else { - MessagesUtil.sendDebugMessages("Could not find item model id for " + stringKey + " in " + itemModelId); - } + if (!modelDataNode.virtual()) { + itemBuilder.setCustomModelId(modelDataNode.getInt()); + } + if (!modelIdNode.virtual()) { + itemBuilder.setModelItemId(modelIdNode.getString("")); } if (!nbtNode.virtual()) { @@ -135,19 +115,14 @@ public class ItemSerializer implements TypeSerializer { if (!enchantsNode.virtual()) { for (ConfigurationNode enchantNode : enchantsNode.childrenMap().values()) { String enchantName = enchantNode.key().toString().toLowerCase(); - NamespacedKey key = NamespacedKey.minecraft(enchantName); - Enchantment enchant = null; - - if (HibiscusCommonsPlugin.isOnPaper() && NMSHandlers.getVersion().isHigherOrEqual(MinecraftVersion.v1_21_4)) { - enchant = RegistryAccess.registryAccess().getRegistry(RegistryKey.ENCHANTMENT).get(key); - } else { - enchant = Registry.ENCHANTMENT.get(key); - } - if (enchant == null) continue; - itemMeta.addEnchant(enchant, enchantNode.getInt(1), true); + int level = enchantNode.getInt(1); + itemBuilder.addEnchantment(enchantName, level); } } + item = itemBuilder.build(); + itemMeta = item.getItemMeta(); + // A few more specific misc things if (!itemFlagsNode.virtual()) { if (HibiscusCommonsPlugin.isOnPaper() && NMSHandlers.getVersion().isHigherOrEqual(MinecraftVersion.v1_20_6)) { diff --git a/common/src/main/java/me/lojosho/hibiscuscommons/util/ItemBuilder.java b/common/src/main/java/me/lojosho/hibiscuscommons/util/ItemBuilder.java new file mode 100644 index 0000000..2ed0010 --- /dev/null +++ b/common/src/main/java/me/lojosho/hibiscuscommons/util/ItemBuilder.java @@ -0,0 +1,152 @@ +package me.lojosho.hibiscuscommons.util; + +import io.papermc.paper.registry.RegistryAccess; +import io.papermc.paper.registry.RegistryKey; +import lombok.Getter; +import me.lojosho.hibiscuscommons.HibiscusCommonsPlugin; +import me.lojosho.hibiscuscommons.nms.MinecraftVersion; +import me.lojosho.hibiscuscommons.nms.NMSHandlers; +import net.kyori.adventure.text.Component; +import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.bukkit.Registry; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemFlag; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.inventory.meta.components.CustomModelDataComponent; +import org.jetbrains.annotations.NotNull; + +import java.util.List; +import java.util.stream.Collectors; + +public class ItemBuilder { + + private final ItemStack itemStack; + private final ItemMeta itemMeta; + private final boolean onPaper; + @Getter + private final Material material; + + public ItemBuilder(@NotNull Material material) { + this.material = material; + this.itemStack = new ItemStack(material); + this.itemMeta = itemStack.getItemMeta(); + this.onPaper = HibiscusCommonsPlugin.isOnPaper(); + } + + public ItemBuilder(@NotNull ItemStack itemStack, @NotNull ItemMeta itemMeta) { + this.itemStack = itemStack; + this.itemMeta = itemMeta; + this.material = itemStack.getType(); + this.onPaper = HibiscusCommonsPlugin.isOnPaper(); + } + + public ItemBuilder setDisplayName(@NotNull String displayName) { + if (onPaper) itemMeta.displayName(AdventureUtils.MINI_MESSAGE.deserialize(displayName)); + else itemMeta.setDisplayName(StringUtils.parseStringToString(displayName)); + return this; + } + + public ItemBuilder setDisplayName(@NotNull Component displayName) { + if (onPaper) itemMeta.displayName(displayName); + return this; + } + + public ItemBuilder setDisplayNameRaw(@NotNull String displayNameRaw) { + if (onPaper) itemMeta.displayName(Component.text(displayNameRaw)); + else itemMeta.setDisplayName(displayNameRaw); + return this; + } + + public ItemBuilder setUnbreakable(boolean unbreakable) { + itemMeta.setUnbreakable(unbreakable); + return this; + } + + public ItemBuilder setCustomModelId(int number) { + if (onPaper) { + CustomModelDataComponent modelDataComponent = itemMeta.getCustomModelDataComponent(); + modelDataComponent.setFloats(List.of((float) number)); + itemMeta.setCustomModelDataComponent(modelDataComponent); + } + else itemMeta.setCustomModelData(number); + return this; + } + + public ItemBuilder setGlowing(boolean glowing) { + if (glowing) { + itemMeta.addEnchant(Enchantment.LUCK_OF_THE_SEA, 1, true); + itemMeta.addItemFlags(ItemFlag.HIDE_ENCHANTS); + } + return this; + } + + public ItemBuilder setLore(@NotNull List lore) { + if (HibiscusCommonsPlugin.isOnPaper()) { + itemMeta.lore(lore. + stream().map(AdventureUtils.MINI_MESSAGE::deserialize).collect(Collectors.toList())); + } else { + itemMeta.setLore(lore. + stream().map(StringUtils::parseStringToString).collect(Collectors.toList())); + } + return this; + } + + public ItemBuilder setQuantity(int number) { + itemStack.setAmount(number); + return this; + } + + public ItemBuilder setModelItemId(@NotNull String itemModelId) { + // If lower than 1.21.4, ignore + if (NMSHandlers.getVersion().isLower(MinecraftVersion.v1_21_4)) return this; + String stringKey = HibiscusCommonsPlugin.getInstance().getName(); + if (itemModelId.contains(":")) { + String[] split = itemModelId.split(":"); + itemModelId = split[1]; + stringKey = split[0]; + } + if (!itemModelId.isEmpty()) { + NamespacedKey key = new NamespacedKey(stringKey, itemModelId); + itemMeta.setItemModel(key); + } else { + MessagesUtil.sendDebugMessages("Could not find item model id for " + stringKey + " in " + itemModelId); + } + return this; + } + + public ItemBuilder addEnchantment(@NotNull Enchantment enchantment, int level) { + itemMeta.addEnchant(enchantment, level, true); + return this; + } + + public ItemBuilder setItemFlag(ItemFlag itemFlag) { + //if (HibiscusCommonsPlugin.isOnPaper() && NMSHandlers.getVersion().isHigherOrEqual(MinecraftVersion.v1_20_6)) { + // itemMeta.setAttributeModifiers(item.getType().getDefaultAttributeModifiers()); + //} + + itemMeta.addItemFlags(itemFlag); + return this; + } + + public ItemBuilder addEnchantment(@NotNull String enchantName, int level) { + enchantName = enchantName.toLowerCase(); + NamespacedKey key = NamespacedKey.minecraft(enchantName); + Enchantment enchant = null; + + if (HibiscusCommonsPlugin.isOnPaper() && NMSHandlers.getVersion().isHigherOrEqual(MinecraftVersion.v1_21_4)) { + enchant = RegistryAccess.registryAccess().getRegistry(RegistryKey.ENCHANTMENT).get(key); + } else { + enchant = Registry.ENCHANTMENT.get(key); + } + if (enchant == null) return this; + addEnchantment(enchant, level); + return this; + } + + public ItemStack build() { + itemStack.setItemMeta(itemMeta); + return itemStack; + } +}