diff --git a/core/src/main/java/com/nisovin/magicspells/MagicSpells.java b/core/src/main/java/com/nisovin/magicspells/MagicSpells.java index bc8a2602f..679da3e74 100644 --- a/core/src/main/java/com/nisovin/magicspells/MagicSpells.java +++ b/core/src/main/java/com/nisovin/magicspells/MagicSpells.java @@ -175,6 +175,7 @@ public class MagicSpells extends JavaPlugin { private boolean ignoreCastItemAuthor; private boolean ignoreCastItemLore; private boolean ignoreCastItemCustomModelData; + private boolean ignoreCastItemItemModel; private boolean castOnAnimate; private boolean enableManaSystem; @@ -336,6 +337,7 @@ public void load() { ignoreCastItemAuthor = config.getBoolean(path + "ignore-cast-item-author", true); ignoreCastItemLore = config.getBoolean(path + "ignore-cast-item-lore", true); ignoreCastItemCustomModelData = config.getBoolean(path + "ignore-cast-item-custom-model-data", true); + ignoreCastItemItemModel = config.getBoolean(path + "ignore-cast-item-item-model", true); ignoreCastItemDurability = Util.getMaterialList(config.getStringList(path + "ignore-cast-item-durability", new ArrayList<>()), ArrayList::new); checkWorldPvpFlag = config.getBoolean(path + "check-world-pvp-flag", true); @@ -1172,6 +1174,10 @@ public static boolean ignoreCastItemCustomModelData() { return plugin.ignoreCastItemCustomModelData; } + public static boolean ignoreCastItemItemModel() { + return plugin.ignoreCastItemItemModel; + } + public static boolean ignoreCastItemNames() { return plugin.ignoreCastItemNames; } diff --git a/core/src/main/java/com/nisovin/magicspells/util/CastItem.java b/core/src/main/java/com/nisovin/magicspells/util/CastItem.java index d1d65e510..477c22519 100644 --- a/core/src/main/java/com/nisovin/magicspells/util/CastItem.java +++ b/core/src/main/java/com/nisovin/magicspells/util/CastItem.java @@ -10,6 +10,7 @@ import org.bukkit.Color; import org.bukkit.Material; +import org.bukkit.NamespacedKey; import org.bukkit.potion.PotionType; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -26,6 +27,7 @@ import com.nisovin.magicspells.util.itemreader.DurabilityHandler; import com.nisovin.magicspells.util.itemreader.WrittenBookHandler; import com.nisovin.magicspells.util.itemreader.LeatherArmorHandler; + import static com.nisovin.magicspells.util.magicitems.MagicItemData.MagicItemAttribute.*; public class CastItem { @@ -38,6 +40,8 @@ public class CastItem { private int customModelData = 0; private boolean unbreakable = false; + private NamespacedKey itemModel = null; + private Color color = null; private PotionType potionType = null; private String title = null; @@ -65,6 +69,7 @@ public CastItem(ItemStack item) { if (!MagicSpells.ignoreCastItemAmount()) amount = item.getAmount(); if (!MagicSpells.ignoreCastItemDurability(type) && type.getMaxDurability() > 0) durability = DurabilityHandler.getDurability(meta); if (!MagicSpells.ignoreCastItemCustomModelData()) customModelData = ItemUtil.getCustomModelData(meta); + if (!MagicSpells.ignoreCastItemItemModel()) itemModel = meta.getItemModel(); if (!MagicSpells.ignoreCastItemBreakability()) unbreakable = meta.isUnbreakable(); if (!MagicSpells.ignoreCastItemColor()) color = LeatherArmorHandler.getColor(meta); if (!MagicSpells.ignoreCastItemPotionType()) potionType = PotionHandler.getPotionType(meta); @@ -99,6 +104,9 @@ public CastItem(String string) { if (!MagicSpells.ignoreCastItemCustomModelData() && data.hasAttribute(CUSTOM_MODEL_DATA)) customModelData = (int) data.getAttribute(CUSTOM_MODEL_DATA); + if (!MagicSpells.ignoreCastItemItemModel() && data.hasAttribute(ITEM_MODEL)) + itemModel = (NamespacedKey) data.getAttribute(ITEM_MODEL); + if (!MagicSpells.ignoreCastItemBreakability() && data.hasAttribute(UNBREAKABLE)) unbreakable = (boolean) data.getAttribute(UNBREAKABLE); @@ -148,6 +156,7 @@ public boolean equalsCastItem(CastItem i) { && (MagicSpells.ignoreCastItemAmount() || amount == i.amount) && (MagicSpells.ignoreCastItemNames() || Objects.equals(name, i.name)) && (MagicSpells.ignoreCastItemCustomModelData() || customModelData == i.customModelData) + && (MagicSpells.ignoreCastItemItemModel() || itemModel == i.itemModel) && (MagicSpells.ignoreCastItemBreakability() || unbreakable == i.unbreakable) && (MagicSpells.ignoreCastItemColor() || Objects.equals(color, i.color)) && (MagicSpells.ignoreCastItemPotionType() || Objects.equals(potionType, i.potionType)) @@ -159,7 +168,7 @@ public boolean equalsCastItem(CastItem i) { @Override public int hashCode() { - return Objects.hash(type, name, amount, durability, customModelData, unbreakable, color, potionType, title, author, enchants, lore); + return Objects.hash(type, name, amount, durability, customModelData, itemModel, unbreakable, color, potionType, title, author, enchants, lore); } @Override @@ -180,6 +189,9 @@ public String toString() { if (!MagicSpells.ignoreCastItemCustomModelData()) castItem.addProperty("custommodeldata", customModelData); + if (!MagicSpells.ignoreCastItemItemModel() && itemModel != null) + castItem.addProperty("itemmodel", itemModel.asString()); + if (!MagicSpells.ignoreCastItemBreakability()) castItem.addProperty("unbreakable", unbreakable); diff --git a/core/src/main/java/com/nisovin/magicspells/util/itemreader/ItemModelHandler.java b/core/src/main/java/com/nisovin/magicspells/util/itemreader/ItemModelHandler.java new file mode 100644 index 000000000..2ad4715f7 --- /dev/null +++ b/core/src/main/java/com/nisovin/magicspells/util/itemreader/ItemModelHandler.java @@ -0,0 +1,33 @@ +package com.nisovin.magicspells.util.itemreader; + +import org.bukkit.NamespacedKey; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.configuration.ConfigurationSection; + +import com.nisovin.magicspells.util.magicitems.MagicItemData; + +import static com.nisovin.magicspells.util.magicitems.MagicItemData.MagicItemAttribute.ITEM_MODEL; + +public class ItemModelHandler { + + private static final String CONFIG_NAME = ITEM_MODEL.toString(); + + public static void process(ConfigurationSection config, ItemMeta meta, MagicItemData data) { + if (!config.isString(CONFIG_NAME)) return; + + String itemModel = config.getString(CONFIG_NAME); + NamespacedKey itemModelKey = NamespacedKey.fromString(itemModel); + + meta.setItemModel(itemModelKey); + data.setAttribute(ITEM_MODEL, itemModelKey); + } + + public static void processItemMeta(ItemMeta meta, MagicItemData data) { + if (data.hasAttribute(ITEM_MODEL)) meta.setItemModel((NamespacedKey) data.getAttribute(ITEM_MODEL)); + } + + public static void processMagicItemData(ItemMeta meta, MagicItemData data) { + if (meta.hasItemModel()) data.setAttribute(ITEM_MODEL, meta.getItemModel()); + } + +} diff --git a/core/src/main/java/com/nisovin/magicspells/util/magicitems/MagicItemData.java b/core/src/main/java/com/nisovin/magicspells/util/magicitems/MagicItemData.java index 9b1e09af6..4da9bc16e 100644 --- a/core/src/main/java/com/nisovin/magicspells/util/magicitems/MagicItemData.java +++ b/core/src/main/java/com/nisovin/magicspells/util/magicitems/MagicItemData.java @@ -15,6 +15,7 @@ import com.google.gson.JsonArray; import com.google.gson.JsonObject; + import net.kyori.adventure.text.Component; import org.bukkit.*; @@ -231,6 +232,7 @@ public enum MagicItemAttribute { DURABILITY(Integer.class), REPAIR_COST(Integer.class), CUSTOM_MODEL_DATA(Integer.class), + ITEM_MODEL(NamespacedKey.class), POWER(Integer.class), UNBREAKABLE(Boolean.class), HIDE_TOOLTIP(Boolean.class), @@ -291,6 +293,9 @@ public String toString() { if (hasAttribute(MagicItemAttribute.CUSTOM_MODEL_DATA)) magicItem.addProperty("custom-model-data", (int) getAttribute(MagicItemAttribute.CUSTOM_MODEL_DATA)); + if (hasAttribute(MagicItemAttribute.ITEM_MODEL)) + magicItem.addProperty("item-model", ((NamespacedKey) getAttribute(MagicItemAttribute.ITEM_MODEL)).asString()); + if (hasAttribute(MagicItemAttribute.POWER)) magicItem.addProperty("power", (int) getAttribute(MagicItemAttribute.POWER)); diff --git a/core/src/main/java/com/nisovin/magicspells/util/magicitems/MagicItemDataParser.java b/core/src/main/java/com/nisovin/magicspells/util/magicitems/MagicItemDataParser.java index 6b5393294..111339e7c 100644 --- a/core/src/main/java/com/nisovin/magicspells/util/magicitems/MagicItemDataParser.java +++ b/core/src/main/java/com/nisovin/magicspells/util/magicitems/MagicItemDataParser.java @@ -125,6 +125,20 @@ public static MagicItemData parseMagicItemData(String str) { case "custom-model-data": case "custom_model_data": data.setAttribute(CUSTOM_MODEL_DATA, value.getAsInt()); + break; + case "itemmodel": + case "item-model": + case "item_model": + String itemModelString = value.getAsString(); + + try { + NamespacedKey itemKey = NamespacedKey.fromString(itemModelString); + data.setAttribute(ITEM_MODEL, itemKey); + } catch (IllegalArgumentException e) { + MagicSpells.error("Invalid item model key '" + itemModelString + "'."); + continue; + } + break; case "power": data.setAttribute(POWER, value.getAsInt()); diff --git a/core/src/main/java/com/nisovin/magicspells/util/magicitems/MagicItems.java b/core/src/main/java/com/nisovin/magicspells/util/magicitems/MagicItems.java index 2ce78d090..48dcef366 100644 --- a/core/src/main/java/com/nisovin/magicspells/util/magicitems/MagicItems.java +++ b/core/src/main/java/com/nisovin/magicspells/util/magicitems/MagicItems.java @@ -100,6 +100,9 @@ private static MagicItemData getMagicItemDataFromItemStackInternal(ItemStack ite // customModelData CustomModelDataHandler.processMagicItemData(meta, data); + // itemModel + ItemModelHandler.processMagicItemData(meta, data); + // power, fireworkEffects FireworkHandler.processMagicItemData(meta, data); @@ -200,6 +203,9 @@ public static MagicItem getMagicItemFromData(MagicItemData data) { // Custom Model Data CustomModelDataHandler.processItemMeta(meta, data); + // Item Model + ItemModelHandler.processItemMeta(meta, data); + // Enchantments if (data.hasAttribute(ENCHANTS)) { Map enchantments = (Map) data.getAttribute(ENCHANTS); @@ -362,6 +368,9 @@ public static MagicItem getMagicItemFromSection(ConfigurationSection section) { // CustomModelData CustomModelDataHandler.process(section, meta, itemData); + // ItemModel + ItemModelHandler.process(section, meta, itemData); + // Enchants // if (section.isList("enchants")) { diff --git a/core/src/main/resources/general.yml b/core/src/main/resources/general.yml index f0d20540f..4fdcd828d 100644 --- a/core/src/main/resources/general.yml +++ b/core/src/main/resources/general.yml @@ -40,6 +40,7 @@ ignore-cast-item-title: true ignore-cast-item-author: true ignore-cast-item-lore: true ignore-cast-item-custom-model-data: true +ignore-cast-item-item-model: true ignore-cast-item-durability: - "leather_helmet" - "leather_chestplate"