mirror of
https://github.com/HibiscusMC/HibiscusCommons.git
synced 2025-12-19 15:09:26 +00:00
feat: add ItemBuilder util
This commit is contained in:
@@ -5,7 +5,6 @@ import io.papermc.paper.registry.RegistryKey;
|
|||||||
import me.lojosho.hibiscuscommons.HibiscusCommonsPlugin;
|
import me.lojosho.hibiscuscommons.HibiscusCommonsPlugin;
|
||||||
import me.lojosho.hibiscuscommons.hooks.Hooks;
|
import me.lojosho.hibiscuscommons.hooks.Hooks;
|
||||||
import me.lojosho.hibiscuscommons.nms.MinecraftVersion;
|
import me.lojosho.hibiscuscommons.nms.MinecraftVersion;
|
||||||
import me.lojosho.hibiscuscommons.nms.NMSHandler;
|
|
||||||
import me.lojosho.hibiscuscommons.nms.NMSHandlers;
|
import me.lojosho.hibiscuscommons.nms.NMSHandlers;
|
||||||
import me.lojosho.hibiscuscommons.util.*;
|
import me.lojosho.hibiscuscommons.util.*;
|
||||||
import org.apache.commons.lang3.EnumUtils;
|
import org.apache.commons.lang3.EnumUtils;
|
||||||
@@ -28,7 +27,6 @@ import java.net.URL;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
public class ItemSerializer implements TypeSerializer<ItemStack> {
|
public class ItemSerializer implements TypeSerializer<ItemStack> {
|
||||||
|
|
||||||
@@ -87,43 +85,25 @@ public class ItemSerializer implements TypeSerializer<ItemStack> {
|
|||||||
ItemMeta itemMeta = item.getItemMeta();
|
ItemMeta itemMeta = item.getItemMeta();
|
||||||
if (itemMeta == null) return item;
|
if (itemMeta == null) return item;
|
||||||
|
|
||||||
|
ItemBuilder itemBuilder = new ItemBuilder(item, itemMeta);
|
||||||
|
|
||||||
if (!nameNode.virtual()) {
|
if (!nameNode.virtual()) {
|
||||||
if (HibiscusCommonsPlugin.isOnPaper()) {
|
itemBuilder.setDisplayName(nameNode.getString(""));
|
||||||
itemMeta.displayName(AdventureUtils.MINI_MESSAGE.deserialize(nameNode.getString("")));
|
}
|
||||||
} else {
|
if (!unbreakableNode.virtual()) {
|
||||||
itemMeta.setDisplayName(StringUtils.parseStringToString(nameNode.getString("")));
|
itemBuilder.setUnbreakable(unbreakableNode.getBoolean(false));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (!unbreakableNode.virtual()) itemMeta.setUnbreakable(unbreakableNode.getBoolean());
|
|
||||||
if (!glowingNode.virtual()) {
|
if (!glowingNode.virtual()) {
|
||||||
itemMeta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
|
itemBuilder.setGlowing(true);
|
||||||
itemMeta.addEnchant(Enchantment.UNBREAKING, 1, true);
|
|
||||||
}
|
}
|
||||||
if (!loreNode.virtual()) {
|
if (!loreNode.virtual()) {
|
||||||
if (HibiscusCommonsPlugin.isOnPaper()) {
|
itemBuilder.setLore(loreNode.getList(String.class, new ArrayList<>()));
|
||||||
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()));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
if (!modelDataNode.virtual()) itemMeta.setCustomModelData(modelDataNode.getInt());
|
if (!modelDataNode.virtual()) {
|
||||||
if (NMSHandlers.getVersion().isHigherOrEqual(MinecraftVersion.v1_21_4) && !modelIdNode.virtual()) {
|
itemBuilder.setCustomModelId(modelDataNode.getInt());
|
||||||
String itemModelId = modelIdNode.getString("");
|
}
|
||||||
String stringKey = HibiscusCommonsPlugin.getInstance().getName();
|
if (!modelIdNode.virtual()) {
|
||||||
if (itemModelId.contains(":")) {
|
itemBuilder.setModelItemId(modelIdNode.getString(""));
|
||||||
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 (!nbtNode.virtual()) {
|
if (!nbtNode.virtual()) {
|
||||||
@@ -135,19 +115,14 @@ public class ItemSerializer implements TypeSerializer<ItemStack> {
|
|||||||
if (!enchantsNode.virtual()) {
|
if (!enchantsNode.virtual()) {
|
||||||
for (ConfigurationNode enchantNode : enchantsNode.childrenMap().values()) {
|
for (ConfigurationNode enchantNode : enchantsNode.childrenMap().values()) {
|
||||||
String enchantName = enchantNode.key().toString().toLowerCase();
|
String enchantName = enchantNode.key().toString().toLowerCase();
|
||||||
NamespacedKey key = NamespacedKey.minecraft(enchantName);
|
int level = enchantNode.getInt(1);
|
||||||
Enchantment enchant = null;
|
itemBuilder.addEnchantment(enchantName, level);
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
item = itemBuilder.build();
|
||||||
|
itemMeta = item.getItemMeta();
|
||||||
|
// A few more specific misc things
|
||||||
|
|
||||||
if (!itemFlagsNode.virtual()) {
|
if (!itemFlagsNode.virtual()) {
|
||||||
if (HibiscusCommonsPlugin.isOnPaper() && NMSHandlers.getVersion().isHigherOrEqual(MinecraftVersion.v1_20_6)) {
|
if (HibiscusCommonsPlugin.isOnPaper() && NMSHandlers.getVersion().isHigherOrEqual(MinecraftVersion.v1_20_6)) {
|
||||||
|
|||||||
@@ -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<String> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user