Fixed custom model data for unidentified items in 1.20.6+

This commit is contained in:
Jules 2024-08-14 13:48:43 -07:00
parent e97e7a6ccd
commit f610ea8269
5 changed files with 102 additions and 79 deletions

View File

@ -18,7 +18,7 @@ import net.Indyuce.mmoitems.api.item.util.identify.UnidentifiedItem;
import net.Indyuce.mmoitems.api.player.PlayerData;
import net.Indyuce.mmoitems.manager.TypeManager;
import net.Indyuce.mmoitems.stat.type.ItemStat;
import org.apache.commons.lang.Validate;
import net.Indyuce.mmoitems.util.MMOUtils;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
@ -148,7 +148,7 @@ public class Type implements CooldownObject, PreloadedObject {
postLoadAction.cacheConfig(config);
name = config.getString("name", name);
item = read(config.getString("display", item == null ? Material.STONE.toString() : item.getType().toString()));
item = MMOUtils.readIcon(config.getString("display", item == null ? Material.STONE.toString() : item.getType().toString()));
(unidentifiedTemplate = new UnidentifiedItem(this)).update(config.getConfigurationSection("unident-item"));
loreFormat = config.getString("LoreFormat", (parent != null ? parent.loreFormat : null));
attackCooldownKey = config.getString("attack-cooldown-key", "default");
@ -286,14 +286,6 @@ public class Type implements CooldownObject, PreloadedObject {
return unidentifiedTemplate;
}
private ItemStack read(String str) {
Validate.notNull(str, "Input must not be null");
String[] split = str.split(":");
Material material = Material.valueOf(split[0]);
return split.length > 1 ? MythicLib.plugin.getVersion().getWrapper().textureItem(material, Integer.parseInt(split[1])) : new ItemStack(material);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;

View File

@ -19,8 +19,10 @@ import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
// TODO Refactor this mess
public class ConfigItem {
private final String id;
@Deprecated
private final ItemStack icon;
// updated when the plugin reloads
@ -28,6 +30,7 @@ public class ConfigItem {
private List<String> lore;
// generated
@Deprecated
private ItemStack item;
public ConfigItem(String id, Material material) {
@ -44,8 +47,8 @@ public class ConfigItem {
this.lore = Arrays.asList(lore);
}
/*
* used as util to load an item stack from a config
/**
* Used as util to load an item stack from a config
*/
public ConfigItem(ConfigurationSection config) {
Validate.notNull(config, "Config cannot be null");
@ -65,11 +68,13 @@ public class ConfigItem {
return id;
}
@Deprecated
public void setup(ConfigurationSection config) {
config.set("name", getName());
config.set("lore", getLore());
}
@Deprecated
public void update(ConfigurationSection config) {
Validate.notNull(config, "Config cannot be null");
@ -128,10 +133,12 @@ public class ConfigItem {
return lore != null;
}
@Deprecated
public ItemStack getItem() {
return item;
}
@Deprecated
public ItemStack getNewItem() {
return item.clone();
}
@ -144,11 +151,11 @@ public class ConfigItem {
this.lore = lore;
}
@Deprecated
protected void setItem(ItemStack item) {
this.item = item;
}
/**
* Unidentified items are ruined when, using a custom resourcepack, they get the material and
* custom model data of what they should be, making them not really unidentified.... this will
@ -159,11 +166,13 @@ public class ConfigItem {
* @author Gunging
* @see #setModel(Integer)
*/
@Deprecated
protected void setMaterial(@Nullable Material mat) {
material = mat;
}
@Nullable
@Deprecated
protected Material material = null;
/**
@ -176,10 +185,12 @@ public class ConfigItem {
* @author Gunging
* @see #setMaterial(Material)
*/
@Deprecated
protected void setModel(@Nullable Integer cmd) {
customModelData = cmd;
}
@Nullable
@Deprecated
protected Integer customModelData = null;
}

View File

@ -1,56 +1,58 @@
package net.Indyuce.mmoitems.api.item.util.identify;
import io.lumine.mythic.lib.api.item.NBTItem;
import net.Indyuce.mmoitems.api.item.mmoitem.LiveMMOItem;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.io.BukkitObjectInputStream;
import org.jetbrains.annotations.NotNull;
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import net.Indyuce.mmoitems.api.item.mmoitem.LiveMMOItem;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.io.BukkitObjectInputStream;
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
import io.lumine.mythic.lib.api.item.NBTItem;
public class IdentifiedItem {
private final NBTItem item;
private final NBTItem item;
public IdentifiedItem(NBTItem item) {
this.item = item;
}
public IdentifiedItem(NBTItem item) {
this.item = item;
}
/*
* the identified item is stored in an item NBTTag, identifying the item
* basically replaces the item for the one saved in the NBT
*/
public ItemStack identify() {
return deserialize(item.getString("MMOITEMS_UNIDENTIFIED_ITEM"));
}
/**
* The identified item is stored in an item NBTTag, identifying the
* item basically replaces the item for the one saved in the NBT
*/
@NotNull
public ItemStack identify() {
return deserialize(item.getString("MMOITEMS_UNIDENTIFIED_ITEM"));
}
private ItemStack deserialize(String data) {
try {
ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
ItemStack stack = (ItemStack) dataInput.readObject();
dataInput.close();
@NotNull
private ItemStack deserialize(String stringInput) {
try {
ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(stringInput));
BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
ItemStack stack = (ItemStack) dataInput.readObject();
dataInput.close();
/*
* For some reason, unidentified items keep having slightly different NBT tags
* than items generated from mob drops or the GUI, I suppose it has to do with
* the serialization-deserialization, It seems to get fixed when rebuilding
* the item stack though.
*
* Its annoying because it prevents stacking.
*/
NBTItem toRebuild = NBTItem.get(stack);
if (toRebuild.hasType()) {
/*
* For some reason, unidentified items keep having slightly different NBT tags
* than items generated from mob drops or the GUI, I suppose it has to do with
* the serialization-deserialization, It seems to get fixed when rebuilding
* the item stack though.
*
* Its annoying because it prevents stacking.
*/
NBTItem toRebuild = NBTItem.get(stack);
if (toRebuild.hasType()) {
// Rebuild
LiveMMOItem rebuilt = new LiveMMOItem(stack);
return rebuilt.newBuilder().build(); }
// Rebuild
LiveMMOItem rebuilt = new LiveMMOItem(stack);
return rebuilt.newBuilder().build();
}
return stack;
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
return null;
}
}
return stack;
} catch (ClassNotFoundException | IOException exception) {
throw new RuntimeException(exception);
}
}
}

View File

@ -15,6 +15,7 @@ import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.util.io.BukkitObjectOutputStream;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
@ -23,11 +24,17 @@ import java.util.*;
public class UnidentifiedItem extends ConfigItem {
public UnidentifiedItem(Type type) {
// Default options
super("unidentified", type.getItem().getType());
setName("#prefix#Unidentified " + type.getName());
setLore(Arrays.asList("&7This item is unidentified. I must", "&7find a way to identify it!", "{tier}", "{tier}&8Item Info:",
"{range}&8- &7Lvl Range: &e#range#", "{tier}&8- &7Item Tier: #prefix##tier#"));
setLore(Arrays.asList(
"&7This item is unidentified. I must",
"&7find a way to identify it!",
"{tier}",
"{tier}&8Item Info:",
"{range}&8- &7Lvl Range: &e#range#",
"{tier}&8- &7Item Tier: #prefix##tier#"
));
}
public ItemBuilder newBuilder(NBTItem item) {
@ -86,27 +93,27 @@ public class UnidentifiedItem extends ConfigItem {
// Apply changes to item
item.getItem().setAmount(1);
ItemStack unidentified = MythicLib.plugin.getVersion().getWrapper().copyTexture(item)
.addTag(new ItemTag("MMOITEMS_UNIDENTIFIED_ITEM", serialize(item.toItem()))).toItem();
// Save serialized item inside of NBT
final ItemStack unidentified = NBTItem.get(new ItemStack(material != null ? material : item.getItem().getType()))
.addTag(new ItemTag("MMOITEMS_UNIDENTIFIED_ITEM", serialize(item.getItem())))
.toItem();
final ItemMeta meta = unidentified.getItemMeta();
if (customModelData != null) meta.setCustomModelData(customModelData);
else if (item.getItem().hasItemMeta() && item.getItem().getItemMeta().hasCustomModelData())
meta.setCustomModelData(item.getItem().getItemMeta().getCustomModelData());
unidentified.setAmount(amount);
ItemMeta meta = unidentified.getItemMeta();
meta.addItemFlags(ItemFlag.values());
meta.setUnbreakable(true);
AdventureUtils.setDisplayName(meta, name);
AdventureUtils.setLore(meta, lore);
if (customModelData != null) {
meta.setCustomModelData(customModelData);
}
unidentified.setItemMeta(meta);
// Has model?
if (material != null && material.isItem()) {
unidentified.setType(material);
}
return unidentified;
}
@NotNull
private String serialize(ItemStack item) {
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
@ -114,9 +121,8 @@ public class UnidentifiedItem extends ConfigItem {
dataOutput.writeObject(item);
dataOutput.close();
return Base64Coder.encodeLines(outputStream.toByteArray());
} catch (Exception e) {
e.printStackTrace();
return null;
} catch (Exception exception) {
throw new RuntimeException(exception);
}
}
}

View File

@ -11,6 +11,7 @@ import io.lumine.mythic.lib.util.annotation.BackwardsCompatibility;
import io.lumine.mythic.lib.version.VPotionEffectType;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.Type;
import org.apache.commons.lang.Validate;
import org.bukkit.*;
import org.bukkit.attribute.Attribute;
import org.bukkit.enchantments.Enchantment;
@ -21,6 +22,7 @@ import org.bukkit.entity.Projectile;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityShootBowEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;
@ -38,6 +40,22 @@ public class MMOUtils {
return particle.getDataType() == Particle.DustOptions.class;
}
@NotNull
public static ItemStack readIcon(@NotNull String stringInput) {
Validate.notNull(stringInput, "Input must not be null");
final String[] split = stringInput.split(":");
final ItemStack stack = new ItemStack(Material.valueOf(UtilityMethods.enumName(split[0])));
if (split.length > 1) {
final ItemMeta meta = stack.getItemMeta();
meta.setCustomModelData(Integer.parseInt(split[1]));
stack.setItemMeta(meta);
}
return stack;
}
@BackwardsCompatibility(version = "1.21")
public static double getForce(@NotNull EntityShootBowEvent event) {
final double force = event.getForce();
@ -456,10 +474,4 @@ public class MMOUtils {
return entities;
}
public static ItemStack readIcon(String string) throws IllegalArgumentException {
String[] split = string.split(":");
Material material = Material.valueOf(split[0].toUpperCase().replace("-", "_").replace(" ", "_"));
return split.length > 1 ? MythicLib.plugin.getVersion().getWrapper().textureItem(material, Integer.parseInt(split[1])) : new ItemStack(material);
}
}