Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions core/src/main/java/com/nisovin/magicspells/MagicSpells.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
14 changes: 13 additions & 1 deletion core/src/main/java/com/nisovin/magicspells/util/CastItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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))
Expand All @@ -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
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import net.kyori.adventure.text.Component;

import org.bukkit.*;
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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<Enchantment, Integer> enchantments = (Map<Enchantment, Integer>) data.getAttribute(ENCHANTS);
Expand Down Expand Up @@ -362,6 +368,9 @@ public static MagicItem getMagicItemFromSection(ConfigurationSection section) {
// CustomModelData
CustomModelDataHandler.process(section, meta, itemData);

// ItemModel
ItemModelHandler.process(section, meta, itemData);

// Enchants
// <enchantmentName> <level>
if (section.isList("enchants")) {
Expand Down
1 change: 1 addition & 0 deletions core/src/main/resources/general.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading