Skip to content

Instantly share code, notes, and snippets.

@DevSrSouza
Created December 3, 2017 01:48
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save DevSrSouza/aa2f39fb2299dfe72b49b52fa46d9a73 to your computer and use it in GitHub Desktop.
Save DevSrSouza/aa2f39fb2299dfe72b49b52fa46d9a73 to your computer and use it in GitHub Desktop.
Parse Bukkit ItemStack to JSON
import com.google.gson.*;
import org.bukkit.Color;
import org.bukkit.DyeColor;
import org.bukkit.FireworkEffect;
import org.bukkit.Material;
import org.bukkit.block.banner.Pattern;
import org.bukkit.block.banner.PatternType;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.*;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import java.util.*;
/**
* Parse {@link ItemStack} to JSON
*
* @author DevSrSouza
* @version 1.0
*
* https://github.com/DevSrSouza/
* You can find updates here https://gist.github.com/DevSrSouza
*/
public class JsonItemStack {
private static final String[] BYPASS_CLASS = {"CraftMetaBlockState", "CraftMetaItem"
/*Glowstone Support*/ ,"GlowMetaItem"};
/**
* Parse the {@link ItemStack} to JSON
*
* @param itemStack The {@link ItemStack} instance
* @return The JSON string
*/
public static String toJson(ItemStack itemStack) {
Gson gson = new Gson();
JsonObject itemJson = new JsonObject();
itemJson.addProperty("type", itemStack.getType().name());
if (itemStack.getDurability() > 0) itemJson.addProperty("data", itemStack.getDurability());
if (itemStack.getAmount() != 1) itemJson.addProperty("amount", itemStack.getAmount());
if (itemStack.hasItemMeta()) {
JsonObject metaJson = new JsonObject();
ItemMeta meta = itemStack.getItemMeta();
if (meta.hasDisplayName()) {
metaJson.addProperty("displayname", meta.getDisplayName());
}
if (meta.hasLore()) {
JsonArray lore = new JsonArray();
meta.getLore().forEach(str -> lore.add(new JsonPrimitive(str)));
metaJson.add("lore", lore);
}
if (meta.hasEnchants()) {
JsonArray enchants = new JsonArray();
meta.getEnchants().forEach((enchantment, integer) -> {
enchants.add(new JsonPrimitive(enchantment.getName() + ":" + integer));
});
metaJson.add("enchants", enchants);
}
if (!meta.getItemFlags().isEmpty()) {
JsonArray flags = new JsonArray();
meta.getItemFlags().stream().map(ItemFlag::name).forEach(str -> flags.add(new JsonPrimitive(str)));
metaJson.add("flags", flags);
}
for (String clazz : BYPASS_CLASS) {
if (meta.getClass().getSimpleName().equals(clazz)) {
itemJson.add("item-meta", metaJson);
return gson.toJson(itemJson);
}
}
if (meta instanceof SkullMeta) {
SkullMeta skullMeta = (SkullMeta) meta;
if (skullMeta.hasOwner()) {
JsonObject extraMeta = new JsonObject();
extraMeta.addProperty("owner", skullMeta.getOwner());
metaJson.add("extra-meta", extraMeta);
}
} else if (meta instanceof BannerMeta) {
BannerMeta bannerMeta = (BannerMeta) meta;
JsonObject extraMeta = new JsonObject();
extraMeta.addProperty("base-color", bannerMeta.getBaseColor().name());
if (bannerMeta.numberOfPatterns() > 0) {
JsonArray patterns = new JsonArray();
bannerMeta.getPatterns()
.stream()
.map(pattern ->
pattern.getColor().name() + ":" + pattern.getPattern().getIdentifier())
.forEach(str -> patterns.add(new JsonPrimitive(str)));
extraMeta.add("patterns", patterns);
}
metaJson.add("extra-meta", extraMeta);
} else if (meta instanceof EnchantmentStorageMeta) {
EnchantmentStorageMeta esmeta = (EnchantmentStorageMeta) meta;
if (esmeta.hasStoredEnchants()) {
JsonObject extraMeta = new JsonObject();
JsonArray storedEnchants = new JsonArray();
esmeta.getStoredEnchants().forEach((enchantment, integer) -> {
storedEnchants.add(new JsonPrimitive(enchantment.getName() + ":" + integer));
});
extraMeta.add("stored-enchants", storedEnchants);
metaJson.add("extra-meta", extraMeta);
}
} else if (meta instanceof LeatherArmorMeta) {
LeatherArmorMeta lameta = (LeatherArmorMeta) meta;
JsonObject extraMeta = new JsonObject();
extraMeta.addProperty("color", Integer.toHexString(lameta.getColor().asRGB()));
metaJson.add("extra-meta", extraMeta);
} else if (meta instanceof BookMeta) {
BookMeta bmeta = (BookMeta) meta;
if (bmeta.hasAuthor() || bmeta.hasPages() || bmeta.hasTitle()) {
JsonObject extraMeta = new JsonObject();
if (bmeta.hasTitle()) {
extraMeta.addProperty("title", bmeta.getTitle());
}
if (bmeta.hasAuthor()) {
extraMeta.addProperty("author", bmeta.getAuthor());
}
if (bmeta.hasPages()) {
JsonArray pages = new JsonArray();
bmeta.getPages().forEach(str -> pages.add(new JsonPrimitive(str)));
extraMeta.add("pages", pages);
}
metaJson.add("extra-meta", extraMeta);
}
} else if (meta instanceof PotionMeta) {
PotionMeta pmeta = (PotionMeta) meta;
if (pmeta.hasCustomEffects()) {
JsonObject extraMeta = new JsonObject();
JsonArray customEffects = new JsonArray();
pmeta.getCustomEffects().forEach(potionEffect -> {
customEffects.add(new JsonPrimitive(potionEffect.getType().getName()
+ ":" + potionEffect.getAmplifier()
+ ":" + potionEffect.getDuration() / 20));
});
extraMeta.add("custom-effects", customEffects);
metaJson.add("extra-meta", extraMeta);
}
} else if (meta instanceof FireworkEffectMeta) {
FireworkEffectMeta femeta = (FireworkEffectMeta) meta;
if (femeta.hasEffect()) {
FireworkEffect effect = femeta.getEffect();
JsonObject extraMeta = new JsonObject();
extraMeta.addProperty("type", effect.getType().name());
if (effect.hasFlicker()) extraMeta.addProperty("flicker", true);
if (effect.hasTrail()) extraMeta.addProperty("trail", true);
if (!effect.getColors().isEmpty()) {
JsonArray colors = new JsonArray();
effect.getColors().forEach(color ->
colors.add(new JsonPrimitive(Integer.toHexString(color.asRGB()))));
extraMeta.add("colors", colors);
}
if (!effect.getFadeColors().isEmpty()) {
JsonArray fadeColors = new JsonArray();
effect.getFadeColors().forEach(color ->
fadeColors.add(new JsonPrimitive(Integer.toHexString(color.asRGB()))));
extraMeta.add("fade-colors", fadeColors);
}
metaJson.add("extra-meta", extraMeta);
}
} else if (meta instanceof FireworkMeta) {
FireworkMeta fmeta = (FireworkMeta) meta;
JsonObject extraMeta = new JsonObject();
extraMeta.addProperty("power", fmeta.getPower());
if (fmeta.hasEffects()) {
JsonArray effects = new JsonArray();
fmeta.getEffects().forEach(effect -> {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("type", effect.getType().name());
if (effect.hasFlicker()) jsonObject.addProperty("flicker", true);
if (effect.hasTrail()) jsonObject.addProperty("trail", true);
if (!effect.getColors().isEmpty()) {
JsonArray colors = new JsonArray();
effect.getColors().forEach(color ->
colors.add(new JsonPrimitive(Integer.toHexString(color.asRGB()))));
jsonObject.add("colors", colors);
}
if (!effect.getFadeColors().isEmpty()) {
JsonArray fadeColors = new JsonArray();
effect.getFadeColors().forEach(color ->
fadeColors.add(new JsonPrimitive(Integer.toHexString(color.asRGB()))));
jsonObject.add("fade-colors", fadeColors);
}
effects.add(jsonObject);
});
extraMeta.add("effects", effects);
}
metaJson.add("extra-meta", extraMeta);
} else if (meta instanceof MapMeta) {
MapMeta mmeta = (MapMeta) meta;
JsonObject extraMeta = new JsonObject();
/* 1.11
if(mmeta.hasLocationName()) {
extraMeta.addProperty("location-name", mmeta.getLocationName());
}
if(mmeta.hasColor()) {
extraMeta.addProperty("color", Integer.toHexString(mmeta.getColor().asRGB()));
}*/
extraMeta.addProperty("scaling", mmeta.isScaling());
metaJson.add("extra-meta", extraMeta);
}
itemJson.add("item-meta", metaJson);
}
return gson.toJson(itemJson);
}
/**
* Parse a JSON to {@link ItemStack}
*
* @param string The JSON string
* @return The {@link ItemStack} or null if not succeed
*/
public static ItemStack fromJson(String string) {
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(string);
if (element.isJsonObject()) {
JsonObject itemJson = element.getAsJsonObject();
JsonElement typeElement = itemJson.get("type");
JsonElement dataElement = itemJson.get("data");
JsonElement amountElement = itemJson.get("amount");
if (typeElement.isJsonPrimitive()) {
String type = typeElement.getAsString();
short data = dataElement != null ? dataElement.getAsShort() : 0;
int amount = amountElement != null ? amountElement.getAsInt() : 1;
ItemStack itemStack = new ItemStack(Material.getMaterial(type));
itemStack.setDurability(data);
itemStack.setAmount(amount);
JsonElement itemMetaElement = itemJson.get("item-meta");
if (itemMetaElement != null && itemMetaElement.isJsonObject()) {
ItemMeta meta = itemStack.getItemMeta();
JsonObject metaJson = itemMetaElement.getAsJsonObject();
JsonElement displaynameElement = metaJson.get("displayname");
JsonElement loreElement = metaJson.get("lore");
JsonElement enchants = metaJson.get("enchants");
JsonElement flagsElement = metaJson.get("flags");
if (displaynameElement != null && displaynameElement.isJsonPrimitive()) {
meta.setDisplayName(displaynameElement.getAsString());
}
if (loreElement != null && loreElement.isJsonArray()) {
JsonArray jarray = loreElement.getAsJsonArray();
List<String> lore = new ArrayList<>(jarray.size());
jarray.forEach(jsonElement -> {
if (jsonElement.isJsonPrimitive()) lore.add(jsonElement.getAsString());
});
meta.setLore(lore);
}
if (enchants != null && enchants.isJsonArray()) {
JsonArray jarray = enchants.getAsJsonArray();
jarray.forEach(jsonElement -> {
if (jsonElement.isJsonPrimitive()) {
String enchantString = jsonElement.getAsString();
if (enchantString.contains(":")) {
try {
String[] splitEnchant = enchantString.split(":");
Enchantment enchantment = Enchantment.getByName(splitEnchant[0]);
int level = Integer.parseInt(splitEnchant[1]);
if (enchantment != null && level > 0) {
meta.addEnchant(enchantment, level, true);
}
} catch (NumberFormatException ex) {
}
}
}
});
}
if (flagsElement != null && flagsElement.isJsonArray()) {
JsonArray jarray = flagsElement.getAsJsonArray();
jarray.forEach(jsonElement -> {
if (jsonElement.isJsonPrimitive()) {
for (ItemFlag flag : ItemFlag.values()) {
if (flag.name().equalsIgnoreCase(jsonElement.getAsString())) {
meta.addItemFlags(flag);
break;
}
}
}
});
}
for (String clazz : BYPASS_CLASS) {
if (meta.getClass().getSimpleName().equals(clazz)) {
return itemStack;
}
}
JsonElement extrametaElement = metaJson.get("extra-meta");
if (extrametaElement != null
&& extrametaElement.isJsonObject()) {
try {
JsonObject extraJson = extrametaElement.getAsJsonObject();
if (meta instanceof SkullMeta) {
JsonElement ownerElement = extraJson.get("owner");
if (ownerElement != null && ownerElement.isJsonPrimitive()) {
SkullMeta smeta = (SkullMeta) meta;
smeta.setOwner(ownerElement.getAsString());
}
} else if (meta instanceof BannerMeta) {
JsonElement baseColorElement = extraJson.get("base-color");
JsonElement patternsElement = extraJson.get("patterns");
BannerMeta bmeta = (BannerMeta) meta;
if (baseColorElement != null && baseColorElement.isJsonPrimitive()) {
try {
Optional<DyeColor> color = Arrays.stream(DyeColor.values())
.filter(dyeColor -> dyeColor.name().equalsIgnoreCase(baseColorElement.getAsString()))
.findFirst();
if (color.isPresent()) {
bmeta.setBaseColor(color.get());
}
} catch (NumberFormatException ex) {
}
}
if (patternsElement != null && patternsElement.isJsonArray()) {
JsonArray jarray = patternsElement.getAsJsonArray();
List<Pattern> patterns = new ArrayList<>(jarray.size());
jarray.forEach(jsonElement -> {
String patternString = jsonElement.getAsString();
if (patternString.contains(":")) {
String[] splitPattern = patternString.split(":");
Optional<DyeColor> color = Arrays.stream(DyeColor.values())
.filter(dyeColor -> dyeColor.name().equalsIgnoreCase(splitPattern[0]))
.findFirst();
PatternType patternType = PatternType.getByIdentifier(splitPattern[1]);
if (color.isPresent() && patternType != null) {
patterns.add(new Pattern(color.get(), patternType));
}
}
});
if (!patterns.isEmpty()) bmeta.setPatterns(patterns);
}
} else if (meta instanceof EnchantmentStorageMeta) {
JsonElement storedEnchantsElement = extraJson.get("stored-enchants");
if (storedEnchantsElement != null && storedEnchantsElement.isJsonArray()) {
EnchantmentStorageMeta esmeta = (EnchantmentStorageMeta) meta;
JsonArray jarray = storedEnchantsElement.getAsJsonArray();
jarray.forEach(jsonElement -> {
if (jsonElement.isJsonPrimitive()) {
String enchantString = jsonElement.getAsString();
if (enchantString.contains(":")) {
try {
String[] splitEnchant = enchantString.split(":");
Enchantment enchantment = Enchantment.getByName(splitEnchant[0]);
int level = Integer.parseInt(splitEnchant[1]);
if (enchantment != null && level > 0) {
esmeta.addStoredEnchant(enchantment, level, true);
}
} catch (NumberFormatException ex) {
}
}
}
});
}
} else if (meta instanceof LeatherArmorMeta) {
JsonElement colorElement = extraJson.get("color");
if (colorElement != null && colorElement.isJsonPrimitive()) {
LeatherArmorMeta lameta = (LeatherArmorMeta) meta;
try {
lameta.setColor(Color.fromRGB(Integer.parseInt(colorElement.getAsString(), 16)));
} catch (NumberFormatException ex) {
}
}
} else if (meta instanceof BookMeta) {
JsonElement titleElement = extraJson.get("title");
JsonElement authorElement = extraJson.get("author");
JsonElement pagesElement = extraJson.get("pages");
BookMeta bmeta = (BookMeta) meta;
if (titleElement != null && titleElement.isJsonPrimitive()) {
bmeta.setTitle(titleElement.getAsString());
}
if (authorElement != null && authorElement.isJsonPrimitive()) {
bmeta.setAuthor(authorElement.getAsString());
}
if (pagesElement != null && pagesElement.isJsonArray()) {
JsonArray jarray = pagesElement.getAsJsonArray();
List<String> pages = new ArrayList<>(jarray.size());
jarray.forEach(jsonElement -> {
if (jsonElement.isJsonPrimitive()) pages.add(jsonElement.getAsString());
});
bmeta.setPages(pages);
}
} else if (meta instanceof PotionMeta) {
JsonElement customEffectsElement = extraJson.get("custom-effects");
if (customEffectsElement != null && customEffectsElement.isJsonArray()) {
PotionMeta pmeta = (PotionMeta) meta;
JsonArray jarray = customEffectsElement.getAsJsonArray();
jarray.forEach(jsonElement -> {
if (jsonElement.isJsonPrimitive()) {
String enchantString = jsonElement.getAsString();
if (enchantString.contains(":")) {
try {
String[] splitPotions = enchantString.split(":");
PotionEffectType potionType = PotionEffectType.getByName(splitPotions[0]);
int amplifier = Integer.parseInt(splitPotions[1]);
int duration = Integer.parseInt(splitPotions[2]) * 20;
if (potionType != null) {
pmeta.addCustomEffect(new PotionEffect(potionType, amplifier, duration), true);
}
} catch (NumberFormatException ex) {
}
}
}
});
}
} else if (meta instanceof FireworkEffectMeta) {
JsonElement effectTypeElement = extraJson.get("type");
JsonElement flickerElement = extraJson.get("flicker");
JsonElement trailElement = extraJson.get("trail");
JsonElement colorsElement = extraJson.get("colors");
JsonElement fadeColorsElement = extraJson.get("fade-colors");
if (effectTypeElement != null && effectTypeElement.isJsonPrimitive()) {
FireworkEffectMeta femeta = (FireworkEffectMeta) meta;
FireworkEffect.Type effectType = FireworkEffect.Type.valueOf(effectTypeElement.getAsString());
if (effectType != null) {
List<Color> colors = new ArrayList<>();
if (colorsElement != null && colorsElement.isJsonArray())
colorsElement.getAsJsonArray().forEach(colorElement -> {
if (colorElement.isJsonPrimitive())
colors.add(Color.fromRGB(Integer.parseInt(colorElement.getAsString(), 16)));
});
List<Color> fadeColors = new ArrayList<>();
if (fadeColorsElement != null && fadeColorsElement.isJsonArray())
fadeColorsElement.getAsJsonArray().forEach(colorElement -> {
if (colorElement.isJsonPrimitive())
fadeColors.add(Color.fromRGB(Integer.parseInt(colorElement.getAsString(), 16)));
});
FireworkEffect.Builder builder = FireworkEffect.builder().with(effectType);
if (flickerElement != null && flickerElement.isJsonPrimitive())
builder.flicker(flickerElement.getAsBoolean());
if (trailElement != null && trailElement.isJsonPrimitive())
builder.trail(trailElement.getAsBoolean());
if (!colors.isEmpty()) builder.withColor(colors);
if (!fadeColors.isEmpty()) builder.withFade(fadeColors);
femeta.setEffect(builder.build());
}
}
} else if (meta instanceof FireworkMeta) {
FireworkMeta fmeta = (FireworkMeta) meta;
JsonElement effectArrayElement = extraJson.get("effects");
JsonElement powerElement = extraJson.get("power");
if (powerElement != null && powerElement.isJsonPrimitive()) {
fmeta.setPower(powerElement.getAsInt());
}
if (effectArrayElement != null && effectArrayElement.isJsonArray()) {
effectArrayElement.getAsJsonArray().forEach(jsonElement -> {
if (jsonElement.isJsonObject()) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
JsonElement effectTypeElement = jsonObject.get("type");
JsonElement flickerElement = jsonObject.get("flicker");
JsonElement trailElement = jsonObject.get("trail");
JsonElement colorsElement = jsonObject.get("colors");
JsonElement fadeColorsElement = jsonObject.get("fade-colors");
if (effectTypeElement != null && effectTypeElement.isJsonPrimitive()) {
FireworkEffect.Type effectType = FireworkEffect.Type.valueOf(effectTypeElement.getAsString());
if (effectType != null) {
List<Color> colors = new ArrayList<>();
if (colorsElement != null && colorsElement.isJsonArray())
colorsElement.getAsJsonArray().forEach(colorElement -> {
if (colorElement.isJsonPrimitive())
colors.add(Color.fromRGB(Integer.parseInt(colorElement.getAsString(), 16)));
});
List<Color> fadeColors = new ArrayList<>();
if (fadeColorsElement != null && fadeColorsElement.isJsonArray())
fadeColorsElement.getAsJsonArray().forEach(colorElement -> {
if (colorElement.isJsonPrimitive())
fadeColors.add(Color.fromRGB(Integer.parseInt(colorElement.getAsString(), 16)));
});
FireworkEffect.Builder builder = FireworkEffect.builder().with(effectType);
if (flickerElement != null && flickerElement.isJsonPrimitive())
builder.flicker(flickerElement.getAsBoolean());
if (trailElement != null && trailElement.isJsonPrimitive())
builder.trail(trailElement.getAsBoolean());
if (!colors.isEmpty()) builder.withColor(colors);
if (!fadeColors.isEmpty()) builder.withFade(fadeColors);
fmeta.addEffect(builder.build());
}
}
}
});
}
} else if (meta instanceof MapMeta) {
MapMeta mmeta = (MapMeta) meta;
JsonElement scalingElement = extraJson.get("scaling");
if (scalingElement != null && scalingElement.isJsonPrimitive()) {
mmeta.setScaling(scalingElement.getAsBoolean());
}
/* 1.11
JsonElement locationNameElement = extraJson.get("location-name");
if(locationNameElement != null && locationNameElement.isJsonPrimitive()) {
mmeta.setLocationName(locationNameElement.getAsString());
}
JsonElement colorElement = extraJson.get("color");
if(colorElement != null && colorElement.isJsonPrimitive()) {
mmeta.setColor(Color.fromRGB(Integer.parseInt(colorElement.getAsString(), 16)));
}*/
}
} catch (Exception e) {return null;}
}
itemStack.setItemMeta(meta);
}
return itemStack;
} else return null;
} else return null;
}
}
@gepron1x
Copy link

holy sh*t

@zkingboos
Copy link

wtf

@PolyRocketMatt
Copy link

Why not just serialize it and use Gson?

@DevSrSouza
Copy link
Author

@PolyRocketMatt

Why not just serialize it and use Gson?

Because this is Java and I like to torture my self

Copy link

ghost commented Aug 9, 2021

Why not just serialize it and use Gson?

Cause Gson and ItemMeta doesn't work

@bouchbi
Copy link

bouchbi commented Dec 6, 2021

Hi, I wanted to use your class since gson and itemStacks works together pretty bad.
However, do you have an idea on why does the item-metas are not deserializing ?

@titivermeesch
Copy link

titivermeesch commented Dec 22, 2021

@bouchbi Try this version:

Anyone having issues serialising potions could use this as well

import com.google.gson.*;
import org.bukkit.Color;
import org.bukkit.DyeColor;
import org.bukkit.FireworkEffect;
import org.bukkit.Material;
import org.bukkit.block.banner.Pattern;
import org.bukkit.block.banner.PatternType;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.*;
import org.bukkit.potion.*;

import java.util.*;

/**
 * Parse {@link ItemStack} to JSON
 *
 * @author DevSrSouza
 * @version 1.0
 * <p>
 * https://github.com/DevSrSouza/
 * You can find updates here https://gist.github.com/DevSrSouza
 */
public class JsonItemStack {
    private static final String[] BYPASS_CLASS = { "CraftMetaBlockState", "CraftMetaItem"
            /*Glowstone Support*/, "GlowMetaItem" };

    /**
     * Parse the {@link ItemStack} to JSON
     *
     * @param itemStack The {@link ItemStack} instance
     *
     * @return The JSON string
     */
    public static String toJson(ItemStack itemStack) {

        Gson gson = new Gson();
        JsonObject itemJson = new JsonObject();

        itemJson.addProperty("type", itemStack.getType().name());
        if (itemStack.getDurability() > 0) {
            itemJson.addProperty("data", itemStack.getDurability());
        }
        if (itemStack.getAmount() != 1) {
            itemJson.addProperty("amount", itemStack.getAmount());
        }


        if (itemStack.hasItemMeta()) {
            JsonObject metaJson = new JsonObject();

            ItemMeta meta = itemStack.getItemMeta();

            if (meta.hasDisplayName()) {
                metaJson.addProperty("displayname", meta.getDisplayName());
            }
            if (meta.hasLore()) {
                JsonArray lore = new JsonArray();
                meta.getLore().forEach(str -> lore.add(new JsonPrimitive(str)));
                metaJson.add("lore", lore);
            }
            if (meta.hasEnchants()) {
                JsonArray enchants = new JsonArray();
                meta.getEnchants().forEach((enchantment, integer) -> {
                    enchants.add(new JsonPrimitive(enchantment.getName() + ":" + integer));
                });
                metaJson.add("enchants", enchants);
            }
            if (!meta.getItemFlags().isEmpty()) {
                JsonArray flags = new JsonArray();
                meta.getItemFlags().stream().map(ItemFlag::name).forEach(str -> flags.add(new JsonPrimitive(str)));
                metaJson.add("flags", flags);
            }

            for (String clazz : BYPASS_CLASS) {
                if (meta.getClass().getSimpleName().equals(clazz)) {
                    itemJson.add("item-meta", metaJson);
                    return gson.toJson(itemJson);
                }
            }

            if (meta instanceof SkullMeta skullMeta) {
                if (skullMeta.hasOwner()) {
                    JsonObject extraMeta = new JsonObject();
                    extraMeta.addProperty("owner", skullMeta.getOwner());
                    metaJson.add("extra-meta", extraMeta);
                }
            } else if (meta instanceof BannerMeta bannerMeta) {
                JsonObject extraMeta = new JsonObject();
                extraMeta.addProperty("base-color", bannerMeta.getBaseColor().name());

                if (bannerMeta.numberOfPatterns() > 0) {
                    JsonArray patterns = new JsonArray();
                    bannerMeta.getPatterns()
                            .stream()
                            .map(pattern ->
                                         pattern.getColor().name() + ":" + pattern.getPattern().getIdentifier())
                            .forEach(str -> patterns.add(new JsonPrimitive(str)));
                    extraMeta.add("patterns", patterns);
                }

                metaJson.add("extra-meta", extraMeta);
            } else if (meta instanceof EnchantmentStorageMeta esmeta) {
                if (esmeta.hasStoredEnchants()) {
                    JsonObject extraMeta = new JsonObject();
                    JsonArray storedEnchants = new JsonArray();
                    esmeta.getStoredEnchants().forEach((enchantment, integer) -> {
                        storedEnchants.add(new JsonPrimitive(enchantment.getName() + ":" + integer));
                    });
                    extraMeta.add("stored-enchants", storedEnchants);
                    metaJson.add("extra-meta", extraMeta);
                }
            } else if (meta instanceof LeatherArmorMeta lameta) {
                JsonObject extraMeta = new JsonObject();
                extraMeta.addProperty("color", Integer.toHexString(lameta.getColor().asRGB()));
                metaJson.add("extra-meta", extraMeta);
            } else if (meta instanceof BookMeta bmeta) {
                if (bmeta.hasAuthor() || bmeta.hasPages() || bmeta.hasTitle()) {
                    JsonObject extraMeta = new JsonObject();
                    if (bmeta.hasTitle()) {
                        extraMeta.addProperty("title", bmeta.getTitle());
                    }
                    if (bmeta.hasAuthor()) {
                        extraMeta.addProperty("author", bmeta.getAuthor());
                    }
                    if (bmeta.hasPages()) {
                        JsonArray pages = new JsonArray();
                        bmeta.getPages().forEach(str -> pages.add(new JsonPrimitive(str)));
                        extraMeta.add("pages", pages);
                    }
                    metaJson.add("extra-meta", extraMeta);
                }
            } else if (meta instanceof PotionMeta pmeta) {
                JsonObject extraMeta = new JsonObject();
                if (pmeta.hasCustomEffects()) {
                    JsonArray customEffects = new JsonArray();
                    pmeta.getCustomEffects().forEach(potionEffect -> {
                        customEffects.add(new JsonPrimitive(potionEffect.getType().getName()
                                                                    + ":" + potionEffect.getAmplifier()
                                                                    + ":" + potionEffect.getDuration() / 20));
                    });
                    extraMeta.add("custom-effects", customEffects);
                } else {
                    PotionType type = pmeta.getBasePotionData().getType();
                    boolean isExtended = pmeta.getBasePotionData().isExtended();
                    boolean isUpgraded = pmeta.getBasePotionData().isUpgraded();
                    JsonObject baseEffect = new JsonObject();
                    baseEffect.addProperty("type", type.getEffectType().getName());
                    baseEffect.addProperty("isExtended", isExtended);
                    baseEffect.addProperty("isUpgraded", isUpgraded);
                    extraMeta.add("base-effect", baseEffect);
                }
                metaJson.add("extra-meta", extraMeta);
            } else if (meta instanceof FireworkEffectMeta femeta) {
                if (femeta.hasEffect()) {
                    FireworkEffect effect = femeta.getEffect();
                    JsonObject extraMeta = new JsonObject();

                    extraMeta.addProperty("type", effect.getType().name());
                    if (effect.hasFlicker()) {
                        extraMeta.addProperty("flicker", true);
                    }
                    if (effect.hasTrail()) {
                        extraMeta.addProperty("trail", true);
                    }

                    if (!effect.getColors().isEmpty()) {
                        JsonArray colors = new JsonArray();
                        effect.getColors().forEach(color ->
                                                           colors.add(new JsonPrimitive(Integer.toHexString(color.asRGB()))));
                        extraMeta.add("colors", colors);
                    }

                    if (!effect.getFadeColors().isEmpty()) {
                        JsonArray fadeColors = new JsonArray();
                        effect.getFadeColors().forEach(color ->
                                                               fadeColors.add(new JsonPrimitive(Integer.toHexString(color.asRGB()))));
                        extraMeta.add("fade-colors", fadeColors);
                    }

                    metaJson.add("extra-meta", extraMeta);
                }
            } else if (meta instanceof FireworkMeta fmeta) {
                JsonObject extraMeta = new JsonObject();

                extraMeta.addProperty("power", fmeta.getPower());

                if (fmeta.hasEffects()) {
                    JsonArray effects = new JsonArray();
                    fmeta.getEffects().forEach(effect -> {
                        JsonObject jsonObject = new JsonObject();

                        jsonObject.addProperty("type", effect.getType().name());
                        if (effect.hasFlicker()) {
                            jsonObject.addProperty("flicker", true);
                        }
                        if (effect.hasTrail()) {
                            jsonObject.addProperty("trail", true);
                        }

                        if (!effect.getColors().isEmpty()) {
                            JsonArray colors = new JsonArray();
                            effect.getColors().forEach(color ->
                                                               colors.add(new JsonPrimitive(Integer.toHexString(color.asRGB()))));
                            jsonObject.add("colors", colors);
                        }

                        if (!effect.getFadeColors().isEmpty()) {
                            JsonArray fadeColors = new JsonArray();
                            effect.getFadeColors().forEach(color ->
                                                                   fadeColors.add(new JsonPrimitive(Integer.toHexString(color.asRGB()))));
                            jsonObject.add("fade-colors", fadeColors);
                        }

                        effects.add(jsonObject);
                    });
                    extraMeta.add("effects", effects);
                }
                metaJson.add("extra-meta", extraMeta);
            } else if (meta instanceof MapMeta mmeta) {
                JsonObject extraMeta = new JsonObject();

                if (mmeta.hasLocationName()) {
                    extraMeta.addProperty("location-name", mmeta.getLocationName());
                }
                if (mmeta.hasColor()) {
                    extraMeta.addProperty("color", Integer.toHexString(mmeta.getColor().asRGB()));
                }
                extraMeta.addProperty("scaling", mmeta.isScaling());

                metaJson.add("extra-meta", extraMeta);
            }

            itemJson.add("item-meta", metaJson);
        }
        return gson.toJson(itemJson);
    }

    /**
     * Parse a JSON to {@link ItemStack}
     *
     * @param string The JSON string
     *
     * @return The {@link ItemStack} or null if not succeed
     */
    public static ItemStack fromJson(String string) {
        JsonParser parser = new JsonParser();
        JsonElement element = parser.parse(string);
        if (element.isJsonObject()) {
            JsonObject itemJson = element.getAsJsonObject();

            JsonElement typeElement = itemJson.get("type");
            JsonElement dataElement = itemJson.get("data");
            JsonElement amountElement = itemJson.get("amount");

            if (typeElement.isJsonPrimitive()) {

                String type = typeElement.getAsString();
                short data = dataElement != null ? dataElement.getAsShort() : 0;
                int amount = amountElement != null ? amountElement.getAsInt() : 1;

                ItemStack itemStack = new ItemStack(Material.getMaterial(type));
                itemStack.setDurability(data);
                itemStack.setAmount(amount);

                JsonElement itemMetaElement = itemJson.get("item-meta");
                if (itemMetaElement != null && itemMetaElement.isJsonObject()) {

                    ItemMeta meta = itemStack.getItemMeta();
                    JsonObject metaJson = itemMetaElement.getAsJsonObject();

                    JsonElement displaynameElement = metaJson.get("displayname");
                    JsonElement loreElement = metaJson.get("lore");
                    JsonElement enchants = metaJson.get("enchants");
                    JsonElement flagsElement = metaJson.get("flags");
                    if (displaynameElement != null && displaynameElement.isJsonPrimitive()) {
                        meta.setDisplayName(displaynameElement.getAsString());
                    }
                    if (loreElement != null && loreElement.isJsonArray()) {
                        JsonArray jarray = loreElement.getAsJsonArray();
                        List<String> lore = new ArrayList<>(jarray.size());
                        jarray.forEach(jsonElement -> {
                            if (jsonElement.isJsonPrimitive()) {
                                lore.add(jsonElement.getAsString());
                            }
                        });
                        meta.setLore(lore);
                    }
                    if (enchants != null && enchants.isJsonArray()) {
                        JsonArray jarray = enchants.getAsJsonArray();
                        jarray.forEach(jsonElement -> {
                            if (jsonElement.isJsonPrimitive()) {
                                String enchantString = jsonElement.getAsString();
                                if (enchantString.contains(":")) {
                                    try {
                                        String[] splitEnchant = enchantString.split(":");
                                        Enchantment enchantment = Enchantment.getByName(splitEnchant[0]);
                                        int level = Integer.parseInt(splitEnchant[1]);
                                        if (enchantment != null && level > 0) {
                                            meta.addEnchant(enchantment, level, true);
                                        }
                                    } catch (NumberFormatException ignored) {
                                    }
                                }
                            }
                        });
                    }
                    if (flagsElement != null && flagsElement.isJsonArray()) {
                        JsonArray jarray = flagsElement.getAsJsonArray();
                        jarray.forEach(jsonElement -> {
                            if (jsonElement.isJsonPrimitive()) {
                                for (ItemFlag flag : ItemFlag.values()) {
                                    if (flag.name().equalsIgnoreCase(jsonElement.getAsString())) {
                                        meta.addItemFlags(flag);
                                        break;
                                    }
                                }
                            }
                        });
                    }
                    for (String clazz : BYPASS_CLASS) {
                        if (meta.getClass().getSimpleName().equals(clazz)) {
                            return itemStack;
                        }
                    }

                    JsonElement extrametaElement = metaJson.get("extra-meta");

                    if (extrametaElement != null
                            && extrametaElement.isJsonObject()) {
                        try {
                            JsonObject extraJson = extrametaElement.getAsJsonObject();
                            if (meta instanceof SkullMeta) {
                                JsonElement ownerElement = extraJson.get("owner");
                                if (ownerElement != null && ownerElement.isJsonPrimitive()) {
                                    SkullMeta smeta = (SkullMeta) meta;
                                    smeta.setOwner(ownerElement.getAsString());
                                }
                            } else if (meta instanceof BannerMeta bmeta) {
                                JsonElement baseColorElement = extraJson.get("base-color");
                                JsonElement patternsElement = extraJson.get("patterns");
                                if (baseColorElement != null && baseColorElement.isJsonPrimitive()) {
                                    try {
                                        Optional<DyeColor> color = Arrays.stream(DyeColor.values())
                                                .filter(dyeColor -> dyeColor.name().equalsIgnoreCase(baseColorElement.getAsString()))
                                                .findFirst();
                                        if (color.isPresent()) {
                                            bmeta.setBaseColor(color.get());
                                        }
                                    } catch (NumberFormatException ignored) {
                                    }
                                }
                                if (patternsElement != null && patternsElement.isJsonArray()) {
                                    JsonArray jarray = patternsElement.getAsJsonArray();
                                    List<Pattern> patterns = new ArrayList<>(jarray.size());
                                    jarray.forEach(jsonElement -> {
                                        String patternString = jsonElement.getAsString();
                                        if (patternString.contains(":")) {
                                            String[] splitPattern = patternString.split(":");
                                            Optional<DyeColor> color = Arrays.stream(DyeColor.values())
                                                    .filter(dyeColor -> dyeColor.name().equalsIgnoreCase(splitPattern[0]))
                                                    .findFirst();
                                            PatternType patternType = PatternType.getByIdentifier(splitPattern[1]);
                                            if (color.isPresent() && patternType != null) {
                                                patterns.add(new Pattern(color.get(), patternType));
                                            }
                                        }
                                    });
                                    if (!patterns.isEmpty()) {
                                        bmeta.setPatterns(patterns);
                                    }
                                }
                            } else if (meta instanceof EnchantmentStorageMeta) {
                                JsonElement storedEnchantsElement = extraJson.get("stored-enchants");
                                if (storedEnchantsElement != null && storedEnchantsElement.isJsonArray()) {
                                    EnchantmentStorageMeta esmeta = (EnchantmentStorageMeta) meta;
                                    JsonArray jarray = storedEnchantsElement.getAsJsonArray();
                                    jarray.forEach(jsonElement -> {
                                        if (jsonElement.isJsonPrimitive()) {
                                            String enchantString = jsonElement.getAsString();
                                            if (enchantString.contains(":")) {
                                                try {
                                                    String[] splitEnchant = enchantString.split(":");
                                                    Enchantment enchantment = Enchantment.getByName(splitEnchant[0]);
                                                    int level = Integer.parseInt(splitEnchant[1]);
                                                    if (enchantment != null && level > 0) {
                                                        esmeta.addStoredEnchant(enchantment, level, true);
                                                    }
                                                } catch (NumberFormatException ignored) {
                                                }
                                            }
                                        }
                                    });
                                }
                            } else if (meta instanceof LeatherArmorMeta) {
                                JsonElement colorElement = extraJson.get("color");
                                if (colorElement != null && colorElement.isJsonPrimitive()) {
                                    LeatherArmorMeta lameta = (LeatherArmorMeta) meta;
                                    try {
                                        lameta.setColor(Color.fromRGB(Integer.parseInt(colorElement.getAsString(),
                                                                                       16)));
                                    } catch (NumberFormatException ignored) {
                                    }
                                }
                            } else if (meta instanceof BookMeta bmeta) {
                                JsonElement titleElement = extraJson.get("title");
                                JsonElement authorElement = extraJson.get("author");
                                JsonElement pagesElement = extraJson.get("pages");

                                if (titleElement != null && titleElement.isJsonPrimitive()) {
                                    bmeta.setTitle(titleElement.getAsString());
                                }
                                if (authorElement != null && authorElement.isJsonPrimitive()) {
                                    bmeta.setAuthor(authorElement.getAsString());
                                }
                                if (pagesElement != null && pagesElement.isJsonArray()) {
                                    JsonArray jarray = pagesElement.getAsJsonArray();
                                    List<String> pages = new ArrayList<>(jarray.size());
                                    jarray.forEach(jsonElement -> {
                                        if (jsonElement.isJsonPrimitive()) {
                                            pages.add(jsonElement.getAsString());
                                        }
                                    });
                                    bmeta.setPages(pages);
                                }

                            } else if (meta instanceof PotionMeta) {
                                JsonElement customEffectsElement = extraJson.get("custom-effects");
                                PotionMeta pmeta = (PotionMeta) meta;
                                if (customEffectsElement != null && customEffectsElement.isJsonArray()) {
                                    JsonArray jarray = customEffectsElement.getAsJsonArray();
                                    jarray.forEach(jsonElement -> {
                                        if (jsonElement.isJsonPrimitive()) {
                                            String enchantString = jsonElement.getAsString();
                                            if (enchantString.contains(":")) {
                                                try {
                                                    String[] splitPotions = enchantString.split(":");
                                                    PotionEffectType potionType =
                                                            PotionEffectType.getByName(splitPotions[0]);
                                                    int amplifier = Integer.parseInt(splitPotions[1]);
                                                    int duration = Integer.parseInt(splitPotions[2]) * 20;
                                                    if (potionType != null) {
                                                        pmeta.addCustomEffect(new PotionEffect(potionType, amplifier,
                                                                                               duration), true);
                                                    }
                                                } catch (NumberFormatException ignored) {
                                                }
                                            }
                                        }
                                    });
                                } else {
                                    JsonObject basePotion = extraJson.getAsJsonObject("base-effect");
                                    PotionType potionType = PotionType.valueOf(basePotion.get("type").getAsString());
                                    boolean isExtended = basePotion.get("isExtended").getAsBoolean();
                                    boolean isUpgraded = basePotion.get("isUpgraded").getAsBoolean();
                                    PotionData potionData = new PotionData(potionType, isExtended, isUpgraded);
                                    pmeta.setBasePotionData(potionData);

                                }
                            } else if (meta instanceof FireworkEffectMeta) {
                                JsonElement effectTypeElement = extraJson.get("type");
                                JsonElement flickerElement = extraJson.get("flicker");
                                JsonElement trailElement = extraJson.get("trail");
                                JsonElement colorsElement = extraJson.get("colors");
                                JsonElement fadeColorsElement = extraJson.get("fade-colors");

                                if (effectTypeElement != null && effectTypeElement.isJsonPrimitive()) {
                                    FireworkEffectMeta femeta = (FireworkEffectMeta) meta;

                                    FireworkEffect.Type effectType =
                                            FireworkEffect.Type.valueOf(effectTypeElement.getAsString());

                                    if (effectType != null) {
                                        List<Color> colors = new ArrayList<>();
                                        if (colorsElement != null && colorsElement.isJsonArray()) {
                                            colorsElement.getAsJsonArray().forEach(colorElement -> {
                                                if (colorElement.isJsonPrimitive()) {
                                                    colors.add(Color.fromRGB(Integer.parseInt(colorElement.getAsString(), 16)));
                                                }
                                            });
                                        }

                                        List<Color> fadeColors = new ArrayList<>();
                                        if (fadeColorsElement != null && fadeColorsElement.isJsonArray()) {
                                            fadeColorsElement.getAsJsonArray().forEach(colorElement -> {
                                                if (colorElement.isJsonPrimitive()) {
                                                    fadeColors.add(Color.fromRGB(Integer.parseInt(colorElement.getAsString(), 16)));
                                                }
                                            });
                                        }

                                        FireworkEffect.Builder builder = FireworkEffect.builder().with(effectType);

                                        if (flickerElement != null && flickerElement.isJsonPrimitive()) {
                                            builder.flicker(flickerElement.getAsBoolean());
                                        }
                                        if (trailElement != null && trailElement.isJsonPrimitive()) {
                                            builder.trail(trailElement.getAsBoolean());
                                        }

                                        if (!colors.isEmpty()) {
                                            builder.withColor(colors);
                                        }
                                        if (!fadeColors.isEmpty()) {
                                            builder.withFade(fadeColors);
                                        }

                                        femeta.setEffect(builder.build());
                                    }
                                }
                            } else if (meta instanceof FireworkMeta) {
                                FireworkMeta fmeta = (FireworkMeta) meta;

                                JsonElement effectArrayElement = extraJson.get("effects");
                                JsonElement powerElement = extraJson.get("power");

                                if (powerElement != null && powerElement.isJsonPrimitive()) {
                                    fmeta.setPower(powerElement.getAsInt());
                                }

                                if (effectArrayElement != null && effectArrayElement.isJsonArray()) {

                                    effectArrayElement.getAsJsonArray().forEach(jsonElement -> {
                                        if (jsonElement.isJsonObject()) {

                                            JsonObject jsonObject = jsonElement.getAsJsonObject();

                                            JsonElement effectTypeElement = jsonObject.get("type");
                                            JsonElement flickerElement = jsonObject.get("flicker");
                                            JsonElement trailElement = jsonObject.get("trail");
                                            JsonElement colorsElement = jsonObject.get("colors");
                                            JsonElement fadeColorsElement = jsonObject.get("fade-colors");

                                            if (effectTypeElement != null && effectTypeElement.isJsonPrimitive()) {

                                                FireworkEffect.Type effectType =
                                                        FireworkEffect.Type.valueOf(effectTypeElement.getAsString());

                                                if (effectType != null) {
                                                    List<Color> colors = new ArrayList<>();
                                                    if (colorsElement != null && colorsElement.isJsonArray()) {
                                                        colorsElement.getAsJsonArray().forEach(colorElement -> {
                                                            if (colorElement.isJsonPrimitive()) {
                                                                colors.add(Color.fromRGB(Integer.parseInt(colorElement.getAsString(), 16)));
                                                            }
                                                        });
                                                    }

                                                    List<Color> fadeColors = new ArrayList<>();
                                                    if (fadeColorsElement != null && fadeColorsElement.isJsonArray()) {
                                                        fadeColorsElement.getAsJsonArray().forEach(colorElement -> {
                                                            if (colorElement.isJsonPrimitive()) {
                                                                fadeColors.add(Color.fromRGB(Integer.parseInt(colorElement.getAsString(), 16)));
                                                            }
                                                        });
                                                    }

                                                    FireworkEffect.Builder builder =
                                                            FireworkEffect.builder().with(effectType);

                                                    if (flickerElement != null && flickerElement.isJsonPrimitive()) {
                                                        builder.flicker(flickerElement.getAsBoolean());
                                                    }
                                                    if (trailElement != null && trailElement.isJsonPrimitive()) {
                                                        builder.trail(trailElement.getAsBoolean());
                                                    }

                                                    if (!colors.isEmpty()) {
                                                        builder.withColor(colors);
                                                    }
                                                    if (!fadeColors.isEmpty()) {
                                                        builder.withFade(fadeColors);
                                                    }

                                                    fmeta.addEffect(builder.build());
                                                }
                                            }
                                        }
                                    });
                                }
                            } else if (meta instanceof MapMeta) {
                                MapMeta mmeta = (MapMeta) meta;

                                JsonElement scalingElement = extraJson.get("scaling");
                                if (scalingElement != null && scalingElement.isJsonPrimitive()) {
                                    mmeta.setScaling(scalingElement.getAsBoolean());
                                }

                                /* 1.11
                                JsonElement locationNameElement = extraJson.get("location-name");
                                if(locationNameElement != null && locationNameElement.isJsonPrimitive()) {
                                    mmeta.setLocationName(locationNameElement.getAsString());
                                }

                                JsonElement colorElement = extraJson.get("color");
                                if(colorElement != null && colorElement.isJsonPrimitive()) {
                                    mmeta.setColor(Color.fromRGB(Integer.parseInt(colorElement.getAsString(), 16)));
                                }*/
                            }
                        } catch (Exception e) {
                            return null;
                        }
                    }
                    itemStack.setItemMeta(meta);
                }
                return itemStack;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }
}

It included changes made in the way 1.9+ potions work

@felix-schindler
Copy link

Is there an updated version of this?

@titivermeesch
Copy link

@felix-schindler Are you having issues with it? Any errors?

@Dagrond
Copy link

Dagrond commented Oct 27, 2022

@titivermeesch
I have a problem with this.
I use JsonItemStack.toJson(ItemStack) and save it to the database - There is valid json saved, with all of lore etc.
This is example which I take from my database - ItemStack saved by JsonItemStack:
{"type":"PAPER","item-meta":{"displayname":"§e§lFragment Mapy","lore":["§7「❖」 Rodzaj §8- §fSPECJALNY","§8§m--------------------------------","","§fJest to fragment mapy, nie jesteś w stanie","§fnic odczytać z pojedynczego kawałka.","§fMoże ktoś w Gildii Podróżników będzie wiedział","§fco z nim zrobić?"],"flags":["HIDE_ATTRIBUTES"]}}
But when I call JsonItemStack.fromJson(json retrieved from database), the ItemStack will not have any lore etc, only the type of the item is loaded from this Json. After some testing I think that the problem is with line if (itemMetaElement != null && itemMetaElement.isJsonObject()) { because this line is blocking execution of all code below it.
PS. I've also tried a simple item with ASCII characters, without color codes, strange symbols etc, but it does not work either.

@felix-schindler
Copy link

When I called JsonItemStack.fromJson the enchants where gone. Also this Code is using many deprecated functions but that might be because I‘m using paper.

Also wouldn’t it be easier to use the Bukkit.YamlConfiguration and then just parse the Yaml string to JSON?

@Dagrond
Copy link

Dagrond commented Oct 27, 2022

I've also make sure that my database is returning a full saved json and not only it's part - and that's correct, my database is sending a full json to JsonItemStack, so I'm pretty sure that the problem is not on my side.

@titivermeesch
Copy link

I'll have a look at what is going on

@bouchbi
Copy link

bouchbi commented Jan 7, 2023

I want to make a suggestion to anyone struggling with ItemStacks serializing.
I found a workaround for this, well the outputed value is not really readable but I have to say it's working pretty well, I used to store player's inventories in a database with this and I was happy of the result.
What I did is that I encoded every ItemStack instance in Base64 that I sent as a blob in the DB.
Then taking it back in an string in Java and casting it to an ItemStack instance. It works pretty well I have to admit.
However when it's stored in the db you have no idea what the ItemStack is, that's the major drawback.
Just leaving this here if it fits anyone's requirements

@IlligalSpigot99
Copy link

Search for the line contains "if (meta.getClass().getSimpleName().equals(clazz)) {" and add after this line "itemStack.setItemMeta(meta);"

@nullexcept1on
Copy link

Came across this snippet just in time. Thanks for the provided code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment