9
0
mirror of https://github.com/HibiscusMC/HibiscusCommons.git synced 2025-12-31 12:56:28 +00:00

feat: move serializers around, prefer builder serializer from itemstacks

This commit is contained in:
LoJoSho
2024-02-23 16:14:55 -06:00
parent b716801f1e
commit 1e7a8ce720
3 changed files with 166 additions and 160 deletions

View File

@@ -0,0 +1,131 @@
package me.lojosho.hibiscuscommons.config.serializer;
import me.lojosho.hibiscuscommons.items.ItemBuilder;
import me.lojosho.hibiscuscommons.items.LoreAppendMode;
import me.lojosho.hibiscuscommons.util.InventoryUtils;
import me.lojosho.hibiscuscommons.util.ServerUtils;
import org.apache.commons.lang3.EnumUtils;
import org.bukkit.Color;
import org.bukkit.NamespacedKey;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFlag;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.serialize.TypeSerializer;
import java.lang.reflect.Type;
import java.util.ArrayList;
public class ItemBuilderSerializer implements TypeSerializer<ItemBuilder> {
public static final ItemBuilderSerializer INSTANCE = new ItemBuilderSerializer();
private static final String MATERIAL = "material";
private static final String AMOUNT = "amount";
private static final String NAME = "name";
private static final String UNBREAKABLE = "unbreakable";
private static final String GLOWING = "glowing";
private static final String LORE = "lore";
private static final String APPEND_LORE = "lore-append-mode";
private static final String MODEL_DATA = "model-data";
private static final String NBT_TAGS = "nbt-tag";
private static final String ENCHANTS = "enchants";
private static final String ITEM_FLAGS = "item-flags";
private static final String TEXTURE = "texture";
private static final String OWNER = "owner";
private static final String COLOR = "color";
private static final String RED = "red";
private static final String GREEN = "green";
private static final String BLUE = "blue";
private ItemBuilderSerializer() {}
@Override
public ItemBuilder deserialize(final Type type, final ConfigurationNode source)
throws SerializationException {
final ConfigurationNode materialNode = source.node(MATERIAL);
final ConfigurationNode amountNode = source.node(AMOUNT);
final ConfigurationNode nameNode = source.node(NAME);
final ConfigurationNode unbreakableNode = source.node(UNBREAKABLE);
final ConfigurationNode glowingNode = source.node(GLOWING);
final ConfigurationNode loreNode = source.node(LORE);
final ConfigurationNode appendLoreNode = source.node(APPEND_LORE);
final ConfigurationNode modelDataNode = source.node(MODEL_DATA);
final ConfigurationNode nbtNode = source.node(NBT_TAGS);
final ConfigurationNode enchantsNode = source.node(ENCHANTS);
final ConfigurationNode itemFlagsNode = source.node(ITEM_FLAGS);
final ConfigurationNode textureNode = source.node(TEXTURE);
final ConfigurationNode ownerNode = source.node(OWNER);
final ConfigurationNode colorNode = source.node(COLOR);
final ConfigurationNode redNode = colorNode.node(RED);
final ConfigurationNode greenNode = colorNode.node(GREEN);
final ConfigurationNode blueNode = colorNode.node(BLUE);
if (materialNode.virtual()) return null;
ItemBuilder builder = new ItemBuilder(materialNode.getString());
if (!amountNode.virtual()) builder.setAmount(amountNode.getInt(1));
if (!nameNode.virtual()) builder.setDisplayName(nameNode.getString(""));
if (!unbreakableNode.virtual()) builder.setUnbreakable(unbreakableNode.getBoolean());
if (!glowingNode.virtual()) builder.setGlowing(glowingNode.getBoolean());
if (!loreNode.virtual()) builder.setLore(new ArrayList<>(loreNode.getList(String.class, new ArrayList<>())));
if (!appendLoreNode.virtual()) {
String loreAppendMode = appendLoreNode.getString("").toUpperCase();
if (EnumUtils.isValidEnum(LoreAppendMode.class, loreAppendMode)) builder.setLoreAppendMode(LoreAppendMode.valueOf(loreAppendMode));
}
if (!modelDataNode.virtual()) builder.setModelData(modelDataNode.getInt());
if (!nbtNode.virtual()) {
for (ConfigurationNode nbtNodes : nbtNode.childrenMap().values()) {
builder.addNBTData(NamespacedKey.minecraft(nbtNodes.key().toString()), nbtNodes.getString());
}
}
if (!enchantsNode.virtual()) {
for (ConfigurationNode enchantNode : enchantsNode.childrenMap().values()) {
if (Enchantment.getByKey(NamespacedKey.minecraft(enchantNode.key().toString())) == null) continue;
builder.addEnchantment(enchantNode.key().toString(), enchantNode.getInt(1));
}
}
try {
if (!itemFlagsNode.virtual()) {
for (String itemFlag : itemFlagsNode.getList(String.class)) {
builder.addItemFlag(ItemFlag.valueOf(itemFlag));
}
}
} catch (Exception e) {
e.printStackTrace();
}
if (!ownerNode.virtual()) {
String ownerString = ownerNode.getString();
if (ownerString.contains("%")) {
// This means it has PAPI placeholders in it
builder.addNBTData(InventoryUtils.getSkullOwner(), ownerString);
}
builder.setSkullOwner(ownerString);
}
if (!textureNode.virtual()) {
String textureString = textureNode.getString();
if (textureString.contains("%")) {
// This means it has PAPI placeholders in it
builder.addNBTData(InventoryUtils.getSkullTexture(), textureString);
}
builder.setTexture(textureString);
}
if (!colorNode.virtual()) {
if (!redNode.virtual()) {
builder.setColor(Color.fromRGB(redNode.getInt(0), greenNode.getInt(0), blueNode.getInt(0)));
} else {
builder.setColor(ServerUtils.hex2Rgb(colorNode.getString("#FFFFFF")));
}
}
return builder;
}
@Override
public void serialize(final Type type, @Nullable final ItemBuilder obj, final ConfigurationNode node) throws SerializationException {
// Empty
}
}

View File

@@ -1,179 +1,24 @@
package me.lojosho.hibiscuscommons.config.serializer;
import me.lojosho.hibiscuscommons.hooks.Hooks;
import me.lojosho.hibiscuscommons.util.ColorBuilder;
import me.lojosho.hibiscuscommons.util.InventoryUtils;
import me.lojosho.hibiscuscommons.util.ServerUtils;
import me.lojosho.hibiscuscommons.util.StringUtils;
import org.apache.commons.lang3.EnumUtils;
import org.bukkit.*;
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.SkullMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.profile.PlayerProfile;
import org.bukkit.profile.PlayerTextures;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.serialize.TypeSerializer;
import java.lang.reflect.Type;
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<ItemStack> {
public static final ItemSerializer INSTANCE = new ItemSerializer();
private static final String MATERIAL = "material";
private static final String AMOUNT = "amount";
private static final String NAME = "name";
private static final String UNBREAKABLE = "unbreakable";
private static final String GLOWING = "glowing";
private static final String LORE = "lore";
private static final String MODEL_DATA = "model-data";
private static final String NBT_TAGS = "nbt-tag";
private static final String ENCHANTS = "enchants";
private static final String ITEM_FLAGS = "item-flags";
private static final String TEXTURE = "texture";
private static final String OWNER = "owner";
private static final String COLOR = "color";
private static final String RED = "red";
private static final String GREEN = "green";
private static final String BLUE = "blue";
private ItemSerializer() {
}
private ItemSerializer() {}
@Override
public ItemStack deserialize(final Type type, final ConfigurationNode source)
throws SerializationException {
final ConfigurationNode materialNode = source.node(MATERIAL);
final ConfigurationNode amountNode = source.node(AMOUNT);
final ConfigurationNode nameNode = source.node(NAME);
final ConfigurationNode unbreakableNode = source.node(UNBREAKABLE);
final ConfigurationNode glowingNode = source.node(GLOWING);
final ConfigurationNode loreNode = source.node(LORE);
final ConfigurationNode modelDataNode = source.node(MODEL_DATA);
final ConfigurationNode nbtNode = source.node(NBT_TAGS);
final ConfigurationNode enchantsNode = source.node(ENCHANTS);
final ConfigurationNode itemFlagsNode = source.node(ITEM_FLAGS);
final ConfigurationNode textureNode = source.node(TEXTURE);
final ConfigurationNode ownerNode = source.node(OWNER);
final ConfigurationNode colorNode = source.node(COLOR);
final ConfigurationNode redNode = colorNode.node(RED);
final ConfigurationNode greenNode = colorNode.node(GREEN);
final ConfigurationNode blueNode = colorNode.node(BLUE);
if (materialNode.virtual()) return null;
String material = materialNode.getString();
ItemStack item = Hooks.getItem(material);
if (item == null) {
//HMCCosmeticsPlugin.getInstance().getLogger().severe("Invalid Material -> " + material);
return new ItemStack(Material.AIR);
}
item.setAmount(amountNode.getInt(1));
ItemMeta itemMeta = item.getItemMeta();
if (itemMeta == null) return item;
if (!nameNode.virtual())
itemMeta.setDisplayName(StringUtils.parseStringToString((nameNode.getString(""))));
if (!unbreakableNode.virtual()) itemMeta.setUnbreakable(unbreakableNode.getBoolean());
if (!glowingNode.virtual()) {
itemMeta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
itemMeta.addEnchant(Enchantment.LUCK, 1, true);
}
if (!loreNode.virtual()) itemMeta.setLore(loreNode.getList(String.class, new ArrayList<String>()).
stream().map(StringUtils::parseStringToString).collect(Collectors.toList()));
if (!modelDataNode.virtual()) itemMeta.setCustomModelData(modelDataNode.getInt());
if (!nbtNode.virtual()) {
for (ConfigurationNode nbtNodes : nbtNode.childrenMap().values()) {
itemMeta.getPersistentDataContainer().set(NamespacedKey.minecraft(nbtNodes.key().toString()), PersistentDataType.STRING, nbtNodes.getString());
}
}
if (!enchantsNode.virtual()) {
for (ConfigurationNode enchantNode : enchantsNode.childrenMap().values()) {
if (Enchantment.getByKey(NamespacedKey.minecraft(enchantNode.key().toString())) == null) continue;
itemMeta.addEnchant(Enchantment.getByKey(NamespacedKey.minecraft(enchantNode.key().toString())), enchantNode.getInt(1), true);
}
}
try {
if (!itemFlagsNode.virtual()) {
for (String itemFlag : itemFlagsNode.getList(String.class)) {
if (!EnumUtils.isValidEnum(ItemFlag.class, itemFlag)) continue;
//MessagesUtil.sendDebugMessages("Added " + itemFlag + " to the item!");
itemMeta.addItemFlags(ItemFlag.valueOf(itemFlag));
}
}
} catch (Exception e) {
e.printStackTrace();
}
if (item.getType() == Material.PLAYER_HEAD) {
SkullMeta skullMeta = (SkullMeta) itemMeta;
if (!ownerNode.virtual()) {
String ownerString = ownerNode.getString();
if (ownerString.contains("%")) {
// This means it has PAPI placeholders in it
skullMeta.getPersistentDataContainer().set(InventoryUtils.getSkullOwner(), PersistentDataType.STRING, ownerString);
}
OfflinePlayer player = Bukkit.getOfflinePlayer(ownerString);
skullMeta.setOwningPlayer(player);
}
if (!textureNode.virtual()) {
String textureString = textureNode.getString();
if (textureString.contains("%")) {
// This means it has PAPI placeholders in it
skullMeta.getPersistentDataContainer().set(InventoryUtils.getSkullTexture(), PersistentDataType.STRING, textureString);
}
// Decodes the texture string and sets the texture url to the skull
PlayerProfile profile = Bukkit.createPlayerProfile(UUID.randomUUID());
PlayerTextures textures = profile.getTextures();
String decoded = new String(Base64.getDecoder().decode(textureString));
URL url = null;
try {
url = new URL(decoded.substring("{\"textures\":{\"SKIN\":{\"url\":\"".length(), decoded.length() - "\"}}}".length()));
} catch (Exception exception) {
exception.printStackTrace();
}
if (url != null) {
textures.setSkin(url);
profile.setTextures(textures);
skullMeta.setOwnerProfile(profile);
}
}
itemMeta = skullMeta;
}
if (!colorNode.virtual()) {
if (ColorBuilder.canBeColored(item.getType())) {
if (!redNode.virtual()) {
itemMeta = ColorBuilder.color(itemMeta, Color.fromRGB(redNode.getInt(0), greenNode.getInt(0), blueNode.getInt(0)));
} else {
itemMeta = ColorBuilder.color(itemMeta, ServerUtils.hex2Rgb(colorNode.getString("#FFFFFF")));
}
}
}
item.setItemMeta(itemMeta);
return item;
@Override @Deprecated
public ItemStack deserialize(final Type type, final ConfigurationNode source) throws SerializationException {
return SerializerManager.serializeItemBuilder(source).build();
}
@Override
public void serialize(final Type type, @Nullable final ItemStack obj, final ConfigurationNode node) throws SerializationException {
}
public void serialize(final Type type, @Nullable final ItemStack obj, final ConfigurationNode node) throws SerializationException {}
}

View File

@@ -0,0 +1,30 @@
package me.lojosho.hibiscuscommons.config.serializer;
import me.lojosho.hibiscuscommons.items.ItemBuilder;
import org.bukkit.Location;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.serialize.SerializationException;
public class SerializerManager {
/**
* @deprecated Use {@link #getItemBuilderSerializer()} instead
*/
@Deprecated (since = "0.2.7", forRemoval = true)
public static ItemSerializer getItemSerializer() {
return ItemSerializer.INSTANCE;
}
public static ItemBuilderSerializer getItemBuilderSerializer() {
return ItemBuilderSerializer.INSTANCE;
}
public static ItemBuilder serializeItemBuilder(ConfigurationNode node) throws SerializationException {
return ItemBuilderSerializer.INSTANCE.deserialize(ItemBuilder.class, node);
}
public static Location serializeLocation(ConfigurationNode node) throws SerializationException {
return LocationSerializer.INSTANCE.deserialize(Location.class, node);
}
}