From 316ad8dd3425ef252c1b379fc34ad31325edd7dd Mon Sep 17 00:00:00 2001 From: Sentropic <60368610+Sentropic@users.noreply.github.com> Date: Sun, 11 Oct 2020 12:53:59 -0600 Subject: [PATCH] Usage of CustomModleData can be toggled Resorts to disabled for versions below 1.14 --- .../sucy/skill/api/player/PlayerCombos.java | 1 + .../java/com/sucy/skill/api/util/Data.java | 31 ++++++++---- .../java/com/sucy/skill/data/Settings.java | 48 +++++++++++++------ 3 files changed, 58 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/sucy/skill/api/player/PlayerCombos.java b/src/main/java/com/sucy/skill/api/player/PlayerCombos.java index 8f4bf89d..3ef7efc5 100644 --- a/src/main/java/com/sucy/skill/api/player/PlayerCombos.java +++ b/src/main/java/com/sucy/skill/api/player/PlayerCombos.java @@ -159,6 +159,7 @@ private void checkExpired() { public String getCurrentComboString() { if (clickIndex == 0) { return ""; } else if (clickIndex == clicks.length) { final int id = SkillAPI.getComboManager().convertCombo(clicks); + checkExpired(); if (skills.containsKey(id)) { return SkillAPI.getSkill(skills.get(id)).getName(); } else { return ""; } diff --git a/src/main/java/com/sucy/skill/api/util/Data.java b/src/main/java/com/sucy/skill/api/util/Data.java index 6d0a17e3..db5bb60d 100644 --- a/src/main/java/com/sucy/skill/api/util/Data.java +++ b/src/main/java/com/sucy/skill/api/util/Data.java @@ -28,11 +28,14 @@ import com.rit.sucy.config.parse.DataSection; import com.rit.sucy.text.TextFormatter; +import com.sucy.skill.SkillAPI; +import com.sucy.skill.data.Settings; import org.apache.commons.lang.ObjectUtils; import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.entity.Item; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.Damageable; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.material.MaterialData; @@ -57,11 +60,17 @@ private static ItemStack parse(final String mat, final short dur, final int data final ItemStack item = new ItemStack(material); final ItemMeta meta = item.getItemMeta(); - if (data!=0) { - meta.setCustomModelData(data); - } - item.setDurability(dur); + if (meta instanceof Damageable) { + ((Damageable) meta).setDamage(dur); + } + if (SkillAPI.getSettings().useCustomModelData()) { + if (data!=0) { + meta.setCustomModelData(data); + } + } else { + item.setData(new MaterialData(material, (byte) data)); + } if (lore != null && !lore.isEmpty()) { final List colored = TextFormatter.colorStringList(lore); meta.setDisplayName(colored.remove(0)); @@ -82,11 +91,17 @@ private static ItemStack parse(final String mat, final short dur, final int data */ public static void serializeIcon(ItemStack item, DataSection config) { config.set(MAT, item.getType().name()); - config.set(DURABILITY, item.getDurability()); - ItemMeta meta = item.getItemMeta(); - config.set(DATA, meta.hasCustomModelData() ? meta.getCustomModelData() : 0); - if (item.hasItemMeta() && item.getItemMeta().hasDisplayName()) { + ItemMeta meta = item.getItemMeta(); + if (SkillAPI.getSettings().useCustomModelData()) { + config.set(DATA, meta.hasCustomModelData() ? meta.getCustomModelData() : 0); + } else { + config.set(DATA, item.getData().getData()); + } + if (meta instanceof Damageable) { + config.set(DURABILITY, ((Damageable) meta).getDamage()); + } + if (meta.hasDisplayName()) { List lore = item.getItemMeta().getLore(); if (lore == null) { lore = new ArrayList<>(); } lore.add(0, item.getItemMeta().getDisplayName()); diff --git a/src/main/java/com/sucy/skill/data/Settings.java b/src/main/java/com/sucy/skill/data/Settings.java index b1e09134..e1bb78cf 100644 --- a/src/main/java/com/sucy/skill/data/Settings.java +++ b/src/main/java/com/sucy/skill/data/Settings.java @@ -48,21 +48,13 @@ import com.sucy.skill.log.Logger; import org.bukkit.Material; import org.bukkit.World; -import org.bukkit.entity.Animals; -import org.bukkit.entity.LivingEntity; -import org.bukkit.entity.Monster; -import org.bukkit.entity.Player; -import org.bukkit.entity.Tameable; +import org.bukkit.entity.*; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.Damageable; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.material.MaterialData; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; /** *

The management class for SkillAPI's config.yml settings.

@@ -1464,6 +1456,7 @@ private void loadExpSettings() { private ItemStack unassigned; private boolean[] defaultBarLayout = new boolean[9]; private boolean[] lockedSlots = new boolean[9]; + private boolean customModelData; /** * Checks whether or not the skill bar is enabled @@ -1510,10 +1503,28 @@ public boolean[] getLockedSlots() { return lockedSlots; } + /** + * Checks whether or not CustomModelData is enabled + * + * @return true if enabled, false otherwise + */ + public boolean useCustomModelData() { + return customModelData; + } + private void loadSkillBarSettings() { DataSection bar = config.getSection("Skill Bar"); skillBarEnabled = bar.getBoolean("enabled", false) && !castEnabled; skillBarCooldowns = bar.getBoolean("show-cooldown", true); + customModelData = bar.getBoolean("use-custommodeldata", false); + if (customModelData) { + try { + ItemMeta.class.getMethod("hasCustomModelData",null); + } catch (NoSuchMethodException e) { + customModelData = false; + Logger.log("CustomModelData not supported below 1.14+. Using item durability/data instead."); + } + } DataSection icon = bar.getSection("empty-icon"); Material mat = Material.matchMaterial(icon.getString("material", "PUMPKIN_SEEDS")); @@ -1521,9 +1532,18 @@ private void loadSkillBarSettings() { unassigned = new ItemStack(mat); ItemMeta meta = unassigned.getItemMeta(); - int customModelData = icon.getInt("data", 0); - if (customModelData!=0) { - meta.setCustomModelData(icon.getInt("data", 0)); + + if (meta instanceof org.bukkit.inventory.meta.Damageable) { + ((Damageable) meta).setDamage((short) icon.getInt("durability", 0)); + } + + final int data = icon.getInt("data", 0); + if (customModelData) { + if (data!=0) { + meta.setCustomModelData(data); + } + } else { + unassigned.setData(new MaterialData(mat, (byte) data)); } if (icon.isList("text")) {