diff --git a/src/main/java/com/gamingmesh/jobs/CMILib/CMIAsyncHead.java b/src/main/java/com/gamingmesh/jobs/CMILib/CMIAsyncHead.java new file mode 100644 index 00000000..16340034 --- /dev/null +++ b/src/main/java/com/gamingmesh/jobs/CMILib/CMIAsyncHead.java @@ -0,0 +1,28 @@ +package com.gamingmesh.jobs.CMILib; + +import org.bukkit.inventory.ItemStack; + +public class CMIAsyncHead { + private boolean head = false; + private boolean force = false; + + public void afterAsyncUpdate(ItemStack item) { + + } + + public boolean isAsyncHead() { + return head; + } + + public void setAsyncHead(boolean head) { + this.head = head; + } + + public boolean isForce() { + return force; + } + + public void setForce(boolean force) { + this.force = force; + } +} diff --git a/src/main/java/com/gamingmesh/jobs/CMILib/CMIItemStack.java b/src/main/java/com/gamingmesh/jobs/CMILib/CMIItemStack.java index e18c04aa..41de66f7 100644 --- a/src/main/java/com/gamingmesh/jobs/CMILib/CMIItemStack.java +++ b/src/main/java/com/gamingmesh/jobs/CMILib/CMIItemStack.java @@ -6,11 +6,15 @@ import java.util.List; import java.util.Map.Entry; import org.bukkit.Material; +import org.bukkit.block.CreatureSpawner; import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.EntityType; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.EnchantmentStorageMeta; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.PotionMeta; +import org.bukkit.inventory.meta.SkullMeta; +import org.bukkit.material.SpawnEgg; import org.bukkit.potion.PotionData; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; @@ -19,7 +23,9 @@ import com.gamingmesh.jobs.Jobs; public class CMIItemStack { + @Deprecated private int id = 0; + @Deprecated private short data = 0; private short durability = 0; private int amount = 0; @@ -28,6 +34,7 @@ public class CMIItemStack { private String mojangName = null; private CMIMaterial cmiMaterial = null; private Material material = null; + private CMIEntityType entityType = null; private ItemStack item; public CMIItemStack(Material material) { @@ -48,6 +55,7 @@ public class CMIItemStack { @Override public CMIItemStack clone() { CMIItemStack cm = new CMIItemStack(material); + cm.entityType = this.entityType; cm.setId(id); cm.setData(data); cm.setAmount(amount); @@ -60,14 +68,17 @@ public class CMIItemStack { return cm; } + @Deprecated public int getId() { return id; } + @Deprecated public void setId(Integer id) { this.id = id; } + @Deprecated public short getData() { return data; } @@ -76,6 +87,12 @@ public class CMIItemStack { return getMaxDurability() > 0; } + public boolean isArmor() { + if (this.getCMIType() != null && this.getCMIType().isArmor()) + return true; + return CMIMaterial.isArmor(this.getType()); + } + public short getDurability() { return Jobs.getNms().getDurability(getItemStack()); } @@ -85,9 +102,17 @@ public class CMIItemStack { } public void setData(short data) { -// CMIMaterial got = CMIMaterial.get(id, data); -// if (got != null && got.getLegacyData() == data) this.data = data; + if (this.getCMIType() != null) { + ItemMeta meta = null; + if (item != null && item.hasItemMeta()) { + meta = item.getItemMeta(); + } + this.item = null; + if (meta != null && this.getItemStack() != null) { + this.getItemStack().setItemMeta(meta); + } + } } public CMIItemStack setDisplayName(String name) { @@ -122,9 +147,11 @@ public class CMIItemStack { public CMIItemStack clearLore() { ItemMeta meta = getItemStack().getItemMeta(); - List t = new ArrayList<>(); - meta.setLore(t); - getItemStack().setItemMeta(meta); + if (meta != null) { + List t = new ArrayList(); + meta.setLore(t); + this.getItemStack().setItemMeta(meta); + } return this; } @@ -176,13 +203,11 @@ public class CMIItemStack { public List getLore() { ItemMeta meta = this.getItemStack().getItemMeta(); -// List lore = null; if (meta != null) { List lore = meta.getLore(); if (lore == null) { lore = new ArrayList<>(); meta.setLore(lore); -// this.getItemStack().setItemMeta(meta); } return meta.getLore() == null ? new ArrayList<>() : meta.getLore(); @@ -193,7 +218,7 @@ public class CMIItemStack { public String getRealName() { return getCMIType() == null || getCMIType() == CMIMaterial.NONE ? getType().name() : getCMIType().getName(); // if (this.getItemStack() != null) { - // + // //// String translated = CMI.getInstance().getItemManager().getTranslatedName(this.getItemStack()); //// if (translated != null) //// return translated; @@ -219,7 +244,7 @@ public class CMIItemStack { // try { // mojangName = CMI.getInstance().getRef().getItemMinecraftName(getItemStack()).replace("minecraft:", ""); // } catch (Exception e) { - // + // // } return mojangName == null || mojangName.isEmpty() ? getCMIType().getMaterial().name() : mojangName; } @@ -263,6 +288,13 @@ public class CMIItemStack { @SuppressWarnings("deprecation") public ItemStack getItemStack() { if (item == null) { + try { + if (!this.getType().isItem()) { + return null; + } + } catch (Throwable e) { + } + if (cmiMaterial.isMonsterEgg()) { if (Version.isCurrentEqualOrHigher(Version.v1_13_R1)) { item = new ItemStack(getType()); @@ -299,23 +331,24 @@ public class CMIItemStack { if (item != null) { this.amount = item.getAmount(); this.material = item.getType(); - this.cmiMaterial = CMIMaterial.get(this.material); - if (Version.isCurrentEqualOrLower(Version.v1_13_R2)) + this.cmiMaterial = CMIMaterial.get(item); + if (Version.isCurrentEqualOrLower(Version.v1_13_R2)) { this.id = item.getType().getId(); - else if (cmiMaterial != null) { + if ((this.getType().isBlock() || this.getType().isSolid())) { + data = item.getData().getData(); + } + if (item.getType().getMaxDurability() - item.getDurability() < 0) { + data = item.getData().getData(); + } + } else if (cmiMaterial != null) { this.id = cmiMaterial.getId(); } - if ((getType().isBlock() || getType().isSolid())) - data = item.getData().getData(); - - if (item.getType().getMaxDurability() - item.getDurability() < 0) - data = item.getData().getData(); if (item.getType().getMaxDurability() > 15) data = (short) 0; if (item.getType() == Material.POTION || item.getType().name().contains("SPLASH_POTION") - || item.getType().name().contains("TIPPED_ARROW")) { + || item.getType().name().contains("TIPPED_ARROW")) { PotionMeta potion = (PotionMeta) item.getItemMeta(); try { if (potion != null && potion.getBasePotionData().getType().getEffectType() != null) { @@ -394,15 +427,61 @@ public class CMIItemStack { if ((item.getCMIType() == CMIMaterial.SPAWNER || item.getCMIType().isMonsterEgg()) && (getCMIType() == CMIMaterial.SPAWNER || getCMIType().isMonsterEgg())) { if (this.cmiMaterial != item.cmiMaterial) return false; + if (getEntityType() != item.getEntityType()) + return false; return true; } + if (item.getCMIType() == CMIMaterial.PLAYER_HEAD && this.getCMIType() == CMIMaterial.PLAYER_HEAD) { + try { + SkullMeta skullMeta = (SkullMeta) item.getItemStack().getItemMeta(); + SkullMeta skullMeta2 = (SkullMeta) getItemStack().getItemMeta(); + + if (skullMeta.getOwner() != null && skullMeta2.getOwner() == null || skullMeta.getOwner() == null && skullMeta2.getOwner() != null) + return false; + if (skullMeta.getOwner() != null && skullMeta2.getOwner() != null && !skullMeta.getOwner().equals(skullMeta2.getOwner())) + return false; + } catch (Throwable e) { + e.printStackTrace(); + } + } + if (Version.isCurrentEqualOrHigher(Version.v1_13_R1)) return this.cmiMaterial == item.cmiMaterial; return this.cmiMaterial == item.cmiMaterial && this.getData() == item.getData(); } + public EntityType getEntityType() { + if (this.getItemStack() == null) + return null; + + if (this.entityType != null) + return this.entityType.getType(); + + ItemStack is = this.getItemStack().clone(); + + if (Version.isCurrentEqualOrHigher(Version.v1_8_R1) && is.getItemMeta() instanceof org.bukkit.inventory.meta.BlockStateMeta) { + org.bukkit.inventory.meta.BlockStateMeta bsm = (org.bukkit.inventory.meta.BlockStateMeta) is.getItemMeta(); + if (bsm.getBlockState() instanceof CreatureSpawner) { + CreatureSpawner bs = (CreatureSpawner) bsm.getBlockState(); + return bs.getSpawnedType(); + } + } + + if (is.getData() instanceof SpawnEgg) { + return CMIReflections.getEggType(is); + } + + if (CMIMaterial.get(is) != null && CMIMaterial.get(is).isMonsterEgg()) { + return CMIReflections.getEggType(is); + } + + if (Version.isCurrentEqualOrLower(Version.v1_12_R1)) + return EntityType.fromId(is.getData().getData()); + return null; + } + public void setDurability(short durability) { this.durability = durability; } @@ -442,4 +521,26 @@ public class CMIItemStack { return liner; } + public static ItemStack getHead(String texture) { + if (texture == null || texture.isEmpty()) + return null; + if (texture.length() < 120) + texture = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUv" + texture; + ItemStack cached = CMIEntityType.cache.get(texture); + if (cached != null) { + return cached.clone(); + } + ItemStack item = CMIMaterial.PLAYER_HEAD.newItemStack(); + item = CMIReflections.setSkullTexture(item, null, texture); + CMIEntityType.cache.put(texture, item); + return item.clone(); + } + + public void setEntityType(CMIEntityType entityType) { + this.entityType = entityType; + } + + public void setEntityType(EntityType entityType) { + setEntityType(CMIEntityType.getByType(entityType)); + } } diff --git a/src/main/java/com/gamingmesh/jobs/CMILib/CMIMaterial.java b/src/main/java/com/gamingmesh/jobs/CMILib/CMIMaterial.java index 62278609..7c85907d 100644 --- a/src/main/java/com/gamingmesh/jobs/CMILib/CMIMaterial.java +++ b/src/main/java/com/gamingmesh/jobs/CMILib/CMIMaterial.java @@ -1152,6 +1152,7 @@ public enum CMIMaterial { private Integer legacyId; private Integer legacyData; + private boolean legacy = false; private Integer id; private String name; private List legacyName; @@ -1191,6 +1192,10 @@ public enum CMIMaterial { if (criteria != null) this.criteria = new HashSet<>(criteria); + + if (this.toString().startsWith("LEGACY_")) { + legacy = true; + } } public String getName() { @@ -1215,41 +1220,57 @@ public enum CMIMaterial { } public void updateMaterial() { - if (mat == null) { - for (Material one : Material.class.getEnumConstants()) { - if (!one.name().replaceAll("LEGACY_|_", "").equalsIgnoreCase(this.name().replace("_", ""))) - continue; - mat = one; - break; - } - } - if (mat == null) { - for (Material one : Material.class.getEnumConstants()) { - if (!one.name().replaceAll("LEGACY_|_", "").equalsIgnoreCase(this.getName().replace(" ", ""))) - continue; - mat = one; - break; - } - } - if (mat == null && !this.getLegacyNames().isEmpty()) { - main: for (Material one : Material.class.getEnumConstants()) { - for (String oneL : this.getLegacyNames()) { - if (!one.name().replaceAll("LEGACY_|_", "").equalsIgnoreCase(oneL.replace(" ", "").replace("_", ""))) - continue main; - } - mat = one; - break; - } - } - if (Version.isCurrentEqualOrLower(Version.v1_13_R2)) { - if (mat == null && this.getId() != null) { - for (Material one : Material.class.getEnumConstants()) { - if (one.getId() != this.getId()) + mat = null; + if (Version.isCurrentEqualOrHigher(Version.v1_13_R1)) { + if (mat == null) { + for (Material one : Material.values()) { + if (!one.name().replace("_", "").equalsIgnoreCase(this.name().replace("_", ""))) continue; mat = one; break; } } + } else { + if (Version.isCurrentEqualOrLower(Version.v1_12_R1) && this.equals(CMIMaterial.PODZOL)) { + mat = null; + return; + } + if (Version.isCurrentEqualOrLower(Version.v1_13_R2)) { + if (mat == null && this.getId() != null) { + for (Material one : Material.class.getEnumConstants()) { + if (one.getId() != this.getId()) + continue; + mat = one; + break; + } + } + } + if (mat == null) { + for (Material one : Material.class.getEnumConstants()) { + if (!one.name().replace("LEGACY_", "").replace("_", "").equalsIgnoreCase(this.name().replace("_", ""))) + continue; + mat = one; + break; + } + } + if (mat == null) { + for (Material one : Material.class.getEnumConstants()) { + if (!one.name().replace("LEGACY_", "").replace("_", "").equalsIgnoreCase(this.getName().replace(" ", ""))) + continue; + mat = one; + break; + } + } + if (mat == null && !this.getLegacyNames().isEmpty()) { + main: for (Material one : Material.class.getEnumConstants()) { + for (String oneL : this.getLegacyNames()) { + if (!one.name().replace("LEGACY_", "").replace("_", "").equalsIgnoreCase(oneL.replace(" ", "").replace("_", ""))) + continue main; + } + mat = one; + break; + } + } } } @@ -1321,6 +1342,8 @@ public enum CMIMaterial { List ls = new ArrayList<>(); for (CMIMaterial one : CMIMaterial.values()) { + if (one.getLegacyId() == null) + continue; if (one.getLegacyId() != mat.getLegacyId()) continue; ls.add(one); @@ -1350,6 +1373,8 @@ public enum CMIMaterial { if (mat == null) return CMIMaterial.NONE; for (CMIMaterial one : CMIMaterial.values()) { + if (one.getLegacyId() == null) + continue; if (one.getLegacyId() != mat.getLegacyId()) continue; if (one.getLegacyData() == id) @@ -1437,7 +1462,7 @@ public enum CMIMaterial { return CMIMaterial.NONE; CMIMaterial mat = null; if (Version.isCurrentEqualOrLower(Version.v1_13_R2)) { - mat = Version.isCurrentEqualOrHigher(Version.v1_13_R1) ? get(item.getType().getId()) : get(item.getType().getId(), item.getData().getData()); + mat = Version.isCurrentEqualOrHigher(Version.v1_13_R1) ? get(item.getType()) : get(item.getType().getId(), item.getData().getData()); if (mat == null) { mat = ItemManager.byName.get(item.getType().toString().toLowerCase().replace("_", "")); } @@ -1449,11 +1474,19 @@ public enum CMIMaterial { } public static CMIMaterial get(Block block) { - if (block == null || Bukkit.getWorld(block.getWorld().getUID()) == null) + if (block == null) return CMIMaterial.NONE; + try { + if (Bukkit.getWorld(block.getWorld().getUID()) == null) + return CMIMaterial.NONE; + } catch (Throwable e) { + e.printStackTrace(); + } + if (Version.isCurrentEqualOrHigher(Version.v1_14_R1)) { - return ItemManager.byRealMaterial.get(block.getType()); + CMIMaterial res = ItemManager.byRealMaterial.get(block.getType()); + return res == null ? CMIMaterial.NONE : res; } byte data = Version.isCurrentEqualOrLower(Version.v1_13_R1) ? block.getData() : 0; @@ -1478,6 +1511,7 @@ public enum CMIMaterial { return mat == null ? CMIMaterial.NONE : mat; } + @Deprecated public static CMIMaterial get(int id, int data) { CMIMaterial mat = ItemManager.byName.get(id + ":" + data); if (mat != null) { @@ -1566,7 +1600,6 @@ public enum CMIMaterial { case PUFFERFISH_SPAWN_EGG: case SALMON_SPAWN_EGG: case TROPICAL_FISH_SPAWN_EGG: - case TURTLE_EGG: case TURTLE_SPAWN_EGG: // 1.14 @@ -2063,6 +2096,28 @@ public enum CMIMaterial { return this != CMIMaterial.NONE && !isAir() && getMaterial() != null; } + public static boolean isValidAsItemStack(Material mat) { + CMIMaterial m = CMIMaterial.get(mat); + if (m == null) + return false; + return m.isValidItem(); + } + + public boolean isValidAsItemStack() { + + ItemStack item = newItemStack(); + if (item == null || getMaterial() == null) + return false; + + try { + if (!getMaterial().isItem()) + return false; + } catch (Throwable e) { + } + + return isValidItem(); + } + public boolean isNone() { return this == CMIMaterial.NONE; } @@ -2659,7 +2714,10 @@ public enum CMIMaterial { } public boolean equals(Material mat) { - return getMaterial() == mat; + if (getMaterial() == null) { + return false; + } + return this.getMaterial().equals(mat); } public List getLegacyNames() { @@ -2703,4 +2761,8 @@ public enum CMIMaterial { public boolean containsCriteria(CMIMaterialCriteria criteria) { return this.criteria != null && criteria != null && this.criteria.contains(criteria); } -} + + public boolean isLegacy() { + return legacy; + } +} \ No newline at end of file diff --git a/src/main/java/com/gamingmesh/jobs/CMILib/CMINBT.java b/src/main/java/com/gamingmesh/jobs/CMILib/CMINBT.java new file mode 100644 index 00000000..9bfbcf4a --- /dev/null +++ b/src/main/java/com/gamingmesh/jobs/CMILib/CMINBT.java @@ -0,0 +1,344 @@ +package com.gamingmesh.jobs.CMILib; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.bukkit.inventory.ItemStack; + +public class CMINBT { + + Object tag; + Object object; + + nmbtType type; + + public enum nmbtType { + item, block, entity; + } + + static { + + } + + public CMINBT(ItemStack item) { + tag = CMIReflections.getNbt(item); + object = item; + type = nmbtType.item; + } + + public Integer getInt(String path) { + if (tag == null) + return null; + if (!this.hasNBT(path)) + return null; + try { + return (Integer) tag.getClass().getMethod("getInt", String.class).invoke(tag, path); + } catch (Exception e) { + return null; + } + } + + public Byte getByte(String path) { + if (tag == null) + return null; + if (!this.hasNBT(path)) + return null; + try { + return (Byte) tag.getClass().getMethod("getByte", String.class).invoke(tag, path); + } catch (Exception e) { + return null; + } + } + + public Long getLong(String path) { + if (tag == null) + return null; + if (!this.hasNBT(path)) + return null; + try { + return (Long) tag.getClass().getMethod("getLong", String.class).invoke(tag, path); + } catch (Exception e) { + return null; + } + } + + public Boolean getBoolean(String path) { + if (tag == null) + return null; + if (!this.hasNBT(path)) + return null; + try { + return (Boolean) tag.getClass().getMethod("getBoolean", String.class).invoke(tag, path); + } catch (Exception e) { + return null; + } + } + + public String getString(String path) { + if (tag == null) + return null; + if (!this.hasNBT(path)) + return null; + try { + + if (tag != null && path.contains(".")) { + List keys = new ArrayList(); + keys.addAll(Arrays.asList(path.split("\\."))); + try { + Object nbtbase = tag.getClass().getMethod("get", String.class).invoke(tag, keys.get(0)); + for (int i = 1; i < keys.size(); i++) { + if (i + 1 < keys.size()) { + nbtbase = nbtbase.getClass().getMethod("get", String.class).invoke(nbtbase, keys.get(i)); + } else { + return nbtbase != null ? (String) nbtbase.getClass().getMethod("getString", String.class).invoke(nbtbase, keys.get(i)) : null; + } + } + } catch (Throwable e) { + } + } + + return (String) tag.getClass().getMethod("getString", String.class).invoke(tag, path); + } catch (Exception e) { + return null; + } + } + + public List getList(String path) { + if (tag == null) + return null; + if (!this.hasNBT(path)) + return null; + List list = new ArrayList(); + try { + Object ls = tag.getClass().getMethod("getList", String.class, int.class).invoke(tag, path, 8); + int size = (int) ls.getClass().getMethod("size").invoke(ls); + Method method = ls.getClass().getMethod("get", int.class); + + if (Version.isCurrentEqualOrLower(Version.v1_12_R1)) { + method = ls.getClass().getMethod("getString", int.class); + for (int i = 0; i < size; i++) { + Object ress = method.invoke(ls, i); + String line = (String) ress; + list.add(line); + } + } else { + Object nbtbase = tag.getClass().getMethod("get", String.class).invoke(tag, path); + Method baseMethod = nbtbase.getClass().getMethod(Version.isCurrentEqualOrLower(Version.v1_12_R1) ? "toString" : "asString"); + for (int i = 0; i < size; i++) { + list.add((String) baseMethod.invoke(method.invoke(ls, i))); + } + } + return list; + } catch (Exception e) { + return null; + } + } + + public Short getShort(String path) { + if (tag == null) + return null; + try { + return (Short) tag.getClass().getMethod("getShort", String.class).invoke(tag, path); + } catch (Exception e) { + return null; + } + } + + public Object setBoolean(String path, Boolean value) { + switch (type) { + case block: + break; + case entity: + break; + case item: + try { + if (value == null) { + Method meth = tag.getClass().getMethod("remove", String.class); + meth.invoke(tag, path); + } else { + Method meth = tag.getClass().getMethod("setBoolean", String.class, boolean.class); + meth.invoke(tag, path, value); + } + return CMIReflections.setTag((ItemStack) object, tag); + } catch (Throwable e) { + if (Version.isCurrentEqualOrHigher(Version.v1_7_R4)) + e.printStackTrace(); + return object; + } + default: + break; + } + return object; + } + + public Object setByte(String path, Byte value) { + switch (type) { + case block: + break; + case entity: + break; + case item: + try { + if (value == null) { + Method meth = tag.getClass().getMethod("remove", String.class); + meth.invoke(tag, path); + } else { + Method meth = tag.getClass().getMethod("setByte", String.class, byte.class); + meth.invoke(tag, path, value); + } + return CMIReflections.setTag((ItemStack) object, tag); + } catch (Throwable e) { + if (Version.isCurrentEqualOrHigher(Version.v1_7_R4)) + e.printStackTrace(); + return object; + } + default: + break; + } + return object; + } + + public Object setShort(String path, Short value) { + switch (type) { + case block: + break; + case entity: + break; + case item: + try { + if (value == null) { + Method meth = tag.getClass().getMethod("remove", String.class); + meth.invoke(tag, path); + } else { + Method meth = tag.getClass().getMethod("setShort", String.class, short.class); + meth.invoke(tag, path, value); + } + return CMIReflections.setTag((ItemStack) object, tag); + } catch (Throwable e) { + if (Version.isCurrentEqualOrHigher(Version.v1_7_R4)) + e.printStackTrace(); + return object; + } + default: + break; + } + return object; + } + + public Object setString(String path, String value) { + switch (type) { + case block: + break; + case entity: + break; + case item: + try { + if (value == null) { + Method meth = tag.getClass().getMethod("remove", String.class); + meth.invoke(tag, path); + } else { + Method meth = tag.getClass().getMethod("setString", String.class, String.class); + meth.invoke(tag, path, value); + } + return CMIReflections.setTag((ItemStack) object, tag); + } catch (Throwable e) { + if (Version.isCurrentEqualOrHigher(Version.v1_7_R4)) + e.printStackTrace(); + return object; + } + default: + break; + } + return object; + } + + public Object setInt(String path, Integer value) { + switch (type) { + case block: + break; + case entity: + break; + case item: + try { + + if (value == null) { + Method meth = tag.getClass().getMethod("remove", String.class); + meth.invoke(tag, path); + } else { + Method meth = tag.getClass().getMethod("setInt", String.class, int.class); + meth.invoke(tag, path, value); + } + return CMIReflections.setTag((ItemStack) object, tag); + } catch (Throwable e) { + if (Version.isCurrentEqualOrHigher(Version.v1_7_R4)) + e.printStackTrace(); + return object; + } + default: + break; + } + return object; + } + + public Object setLong(String path, Long value) { + switch (type) { + case block: + break; + case entity: + break; + case item: + try { + if (value == null) { + Method meth = tag.getClass().getMethod("remove", String.class); + meth.invoke(tag, path, value); + } else { + Method meth = tag.getClass().getMethod("setLong", String.class, long.class); + meth.invoke(tag, path, value); + } + return CMIReflections.setTag((ItemStack) object, tag); + } catch (Throwable e) { + if (Version.isCurrentEqualOrHigher(Version.v1_7_R4)) + e.printStackTrace(); + return object; + } + default: + break; + } + return object; + } + + public boolean hasNBT() { + return tag != null; + } + + public boolean hasNBT(String key) { + if (tag != null && key.contains(".")) { + List keys = new ArrayList(); + keys.addAll(Arrays.asList(key.split("\\."))); + try { + Object nbtbase = tag.getClass().getMethod("get", String.class).invoke(tag, keys.get(0)); + for (int i = 1; i < keys.size(); i++) { + if (i + 1 < keys.size()) { + nbtbase = nbtbase.getClass().getMethod("get", String.class).invoke(nbtbase, keys.get(i)); + } else { + return nbtbase != null && (Boolean) nbtbase.getClass().getMethod("hasKey", String.class).invoke(nbtbase, keys.get(i)); + } + } + } catch (Throwable e) { + } + return false; + } + try { + return tag != null && (Boolean) tag.getClass().getMethod("hasKey", String.class).invoke(tag, key); + } catch (Throwable e) { + e.printStackTrace(); + } + return false; + } + + public Object getNbt() { + return tag; + } +} diff --git a/src/main/java/com/gamingmesh/jobs/CMILib/CMIReflections.java b/src/main/java/com/gamingmesh/jobs/CMILib/CMIReflections.java index c9c75f3e..7d032346 100644 --- a/src/main/java/com/gamingmesh/jobs/CMILib/CMIReflections.java +++ b/src/main/java/com/gamingmesh/jobs/CMILib/CMIReflections.java @@ -6,8 +6,14 @@ package com.gamingmesh.jobs.CMILib; import java.lang.reflect.Field; import java.lang.reflect.Method; +import java.util.UUID; +import org.bukkit.entity.EntityType; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.SkullMeta; + +import com.mojang.authlib.GameProfile; +import com.mojang.authlib.properties.Property; public class CMIReflections { @@ -289,4 +295,112 @@ public class CMIReflections { public static ItemStack getItemInOffHand(org.bukkit.entity.Player player) { return Version.getCurrent().isLower(Version.v1_9_R1) ? null : player.getInventory().getItemInOffHand(); } + + public static int getEggId(ItemStack item) { + EntityType type = getEggType(item); + return type == null ? 0 : type.getTypeId(); + } + + public static EntityType getEggType(ItemStack item) { + if (!CMIMaterial.isMonsterEgg(item.getType())) + return null; + + if (Version.isCurrentEqual(Version.v1_12_R1)) { + try { + if (Version.isCurrentEqualOrLower(Version.v1_11_R1)) { + CMIEntityType cmiType = CMIEntityType.getById(item.getData().getData()); + if (cmiType != null) + return cmiType.getType(); + } + Object tag = getNbt(item); + Object base = tag.getClass().getMethod("getCompound", String.class).invoke(tag, "EntityTag"); + String type = (String) base.getClass().getMethod("getString", String.class).invoke(base, "id"); + return EntityType.fromName(type.replace("minecraft:", "").toUpperCase()); + } catch (Exception e) { + return null; + } + } + + CMIEntityType type = CMIEntityType.getByName(item.getType().toString().replace("_SPAWN_EGG", "")); + return type == null ? null : type.getType(); + } + + public static ItemStack setEggType(ItemStack item, EntityType etype) { + if (!item.getType().toString().contains("_EGG")) + return null; + try { + Object tag = getNbt(item); + + Object ttag = tag.getClass().getMethod("getCompound", String.class).invoke(tag, "EntityTag"); + + if (ttag == null) + ttag = NBTTagCompound.newInstance(); + + CMIEntityType ce = CMIEntityType.getByType(etype); + if (ce == null) + return item; + + ttag.getClass().getMethod("setString", String.class, String.class).invoke(ttag, "id", ce.getName()); + + tag.getClass().getMethod("set", String.class, NBTTagCompound).invoke(tag, "EntityTag", ttag); + setTag(item, tag); + + return (ItemStack) asBukkitCopy(item); + } catch (Exception e) { + return null; + } + } + + public static ItemStack setTag(ItemStack item, Object tag) { + try { + Object nmsStack = asNMSCopy(item); + if (nmsStack == null) { + return null; + } + Method meth2 = nmsStack.getClass().getMethod("setTag", NBTTagCompound); + meth2.invoke(nmsStack, tag); + return (ItemStack) asBukkitCopy(nmsStack); + } catch (Throwable e) { + if (Version.isCurrentEqualOrHigher(Version.v1_7_R4)) + e.printStackTrace(); + return item; + } + } + + public static ItemStack setSkullTexture(ItemStack item, String customProfileName, String texture) { + if (item == null) + return null; + try { + + GameProfile prof = new GameProfile(UUID.nameUUIDFromBytes(texture.getBytes()), null); + prof.getProperties().removeAll("textures"); + prof.getProperties().put("textures", new Property("textures", texture)); + +// ItemMeta headMeta = item.getItemMeta(); + SkullMeta headMeta = (SkullMeta) item.getItemMeta(); + + Field profileField = null; + try { + profileField = headMeta.getClass().getDeclaredField("profile"); + } catch (NoSuchFieldException | SecurityException e) { + e.printStackTrace(); + } + if (profileField != null) { + profileField.setAccessible(true); + try { + profileField.set(headMeta, prof); + } catch (IllegalArgumentException | IllegalAccessException e) { + e.printStackTrace(); + } + item.setItemMeta(headMeta); + } + + Object i = new CMINBT(item).setString("Id", UUID.nameUUIDFromBytes(texture.getBytes()).toString()); + + return i == null ? null : (ItemStack) i; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } } diff --git a/src/main/java/com/gamingmesh/jobs/CMILib/ItemManager.java b/src/main/java/com/gamingmesh/jobs/CMILib/ItemManager.java index 4588c929..e3b9164a 100644 --- a/src/main/java/com/gamingmesh/jobs/CMILib/ItemManager.java +++ b/src/main/java/com/gamingmesh/jobs/CMILib/ItemManager.java @@ -1,15 +1,24 @@ package com.gamingmesh.jobs.CMILib; import java.util.HashMap; +import java.util.UUID; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.bukkit.Bukkit; import org.bukkit.Material; +import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Player; +import org.bukkit.event.inventory.InventoryType; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.MapMeta; import org.bukkit.inventory.meta.PotionMeta; +import org.bukkit.inventory.meta.SkullMeta; import org.bukkit.potion.PotionData; import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionType; -import com.gamingmesh.jobs.stuff.Util; +import com.gamingmesh.jobs.Jobs; public class ItemManager { @@ -26,15 +35,24 @@ public class ItemManager { } static { + byRealMaterial.clear(); for (CMIMaterial one : CMIMaterial.values()) { - one.updateMaterial(); + if (one == null) + continue; + // Ignoring legacy materials on new servers + if (Version.isCurrentEqualOrHigher(Version.v1_13_R1) && one.isLegacy()) { + continue; + } + + one.updateMaterial(); Material mat = one.getMaterial(); + if (mat == null) { continue; } - Integer id = one.getId(); +// Integer id = one.getId(); short data = one.getLegacyData(); Integer legacyId = one.getLegacyId(); String cmiName = one.getName().replace("_", "").replace(" ", "").toLowerCase(); @@ -42,7 +60,8 @@ public class ItemManager { String mojangName = null; try { - mojangName = CMIReflections.getItemMinecraftName(new ItemStack(mat)); + if (Version.isCurrentEqualOrLower(Version.v1_14_R1) || mat.isItem()) + mojangName = CMIReflections.getItemMinecraftName(new ItemStack(mat)); } catch (Exception e) { e.printStackTrace(); } @@ -50,53 +69,55 @@ public class ItemManager { mojangName = mat.toString(); } - mojangName = mojangName.replace("_", "").replace(" ", "").toLowerCase(); + mojangName = mojangName == null ? mat.toString().replace("_", "").replace(" ", "").toLowerCase() : mojangName.replace("_", "").replace(" ", "").toLowerCase(); if (one.isCanHavePotionType()) { for (PotionType potType : PotionType.values()) { byName.put(cmiName + ":" + potType.toString().toLowerCase(), one); } - } else if (byName.containsKey(cmiName)) { + } + if (byName.containsKey(cmiName) && Version.isCurrentEqualOrLower(Version.v1_13_R1)) { byName.put(cmiName + ":" + data, one); } else byName.put(cmiName, one); - if (byName.containsKey(materialName)) - byName.put(materialName + ":" + data, one); - else - byName.put(materialName, one); + byName.put(materialName, one); + if (Version.isCurrentEqualOrLower(Version.v1_13_R1) && !byName.containsKey(cmiName + ":" + data)) + byName.put(cmiName + ":" + data, one); if (!one.getLegacyNames().isEmpty()) { for (String oneL : one.getLegacyNames()) { String legacyName = oneL.replace("_", "").replace(" ", "").toLowerCase(); - if (byName.containsKey(legacyName) || data > 0) + if (Version.isCurrentEqualOrLower(Version.v1_13_R1) && (byName.containsKey(legacyName) || data > 0)) { byName.put(legacyName + ":" + data, one); - else - byName.put(legacyName, one); + } + byName.put(legacyName, one); } } - if (byName.containsKey(mojangName)) + if (byName.containsKey(mojangName) && Version.isCurrentEqualOrLower(Version.v1_13_R1)) byName.put(mojangName + ":" + data, one); else byName.put(mojangName, one); - if (byName.containsKey(String.valueOf(id)) || data > 0) - byName.put(id + ":" + data, one); - else - byName.put(String.valueOf(id), one); + if (Version.isCurrentEqualOrLower(Version.v1_13_R1)) { + Integer id = one.getId(); + if (byName.containsKey(String.valueOf(id)) || data > 0) + byName.put(id + ":" + data, one); + else + byName.put(String.valueOf(id), one); + if (!byId.containsKey(id)) + byId.put(id, one); + if (!byId.containsKey(one.getLegacyId())) + byId.put(one.getLegacyId(), one); + if (one.getLegacyData() == 0) + byId.put(one.getLegacyId(), one); + if (byName.containsKey(String.valueOf(legacyId)) || data > 0) + byName.put(legacyId + ":" + data, one); + else + byName.put(String.valueOf(legacyId), one); + } - if (byName.containsKey(String.valueOf(legacyId)) || data > 0) - byName.put(legacyId + ":" + data, one); - else - byName.put(String.valueOf(legacyId), one); - - if (!byId.containsKey(id)) - byId.put(id, one); - if (!byId.containsKey(one.getLegacyId())) - byId.put(one.getLegacyId(), one); - if (one.getLegacyData() == 0) - byId.put(one.getLegacyId(), one); byRealMaterial.put(mat, one); } } @@ -124,36 +145,65 @@ public class ItemManager { HashMap headCache = new HashMap<>(); public CMIItemStack getItem(String name) { + return getItem(name, null); + } + + public CMIItemStack getItem(String name, CMIAsyncHead ahead) { + if (name == null) + return null; // if (byBukkitName.isEmpty()) -// load(); +// load(); CMIItemStack cm = null; + String original = name.replace("minecraft:", ""); name = name.toLowerCase().replace("minecraft:", ""); - String original = name; name = name.replace("_", ""); Integer amount = null; + CMIEntityType entityType = null; + + String tag = null; + + if (name.contains("{") && name.contains("}")) { + Pattern ptr = Pattern.compile("(\\{).+(\\})"); + Matcher match = ptr.matcher(name); + if (match.find()) { + tag = match.group(); + name = name.replace(match.group(), ""); + } + name = name.replace(" ", " "); + } String subdata = null; if (name.contains(":")) { CMIMaterial mat = byName.get(name); if (mat != null) return new CMIItemStack(mat); - subdata = name.split(":")[1]; + try { + subdata = name.split(":")[1]; + } catch (Throwable e) { + + } } if (name.contains("-")) { - String a = name.split("-")[1]; - try { - amount = Integer.parseInt(a); - } catch (NumberFormatException e) { + String[] split = name.split("-"); + if (split.length > 1) { + String a = name.split("-")[1]; + try { + amount = Integer.parseInt(a); + } catch (Exception e) { + } } name = name.split("-")[0]; } if (name.contains(">")) { - String a = name.split(">")[1]; - try { - amount = Integer.parseInt(a); - } catch (NumberFormatException e) { + String[] split = name.split(">"); + if (split.length > 1) { + String a = name.split(">")[1]; + try { + amount = Integer.parseInt(a); + } catch (Exception e) { + } } name = name.split(">")[0]; } @@ -166,9 +216,9 @@ public class ItemManager { } catch (Exception e) { } try { - CMIEntityType e = CMIEntityType.getByName(name.split(":")[1]); - if (e != null) - data = (short) e.getId(); + entityType = CMIEntityType.getByName(name.split(":")[1]); + if (entityType != null) + data = (short) entityType.getId(); } catch (Exception e) { } name = name.split(":")[0]; @@ -185,51 +235,137 @@ public class ItemManager { cm = CMIMaterial.PLAYER_HEAD.newCMIItemStack(); data = 3; - main: if (original.contains(":")) { + if (original.contains(":")) { ItemStack old = headCache.get(original); if (old != null) { cm.setItemStack(old); } else { - String d = original.split(":")[1]; - ItemStack skull = Util.getSkull(d); - if (skull == null) { - break main; - } + String[] split = original.split(":"); + if (split.length > 1) { + String d = split[1]; - headCache.put(original, skull); - cm.setItemStack(skull); + if (d.length() > 36 || d.startsWith("eyJ0ZXh0dXJlcy")) { + ItemStack skull = CMIItemStack.getHead(d); + headCache.put(original, skull); + cm.setItemStack(skull); + } else { + ItemStack skull = CMIMaterial.PLAYER_HEAD.newItemStack(); + SkullMeta skullMeta = (SkullMeta) skull.getItemMeta(); + if (d.length() == 36) { + try { + OfflinePlayer offPlayer = Bukkit.getOfflinePlayer(UUID.fromString(d)); + skullMeta.setOwningPlayer(offPlayer); + skull.setItemMeta(skullMeta); + } catch (Exception e) { + } + } else { + if (Version.isCurrentEqualOrHigher(Version.v1_16_R3)) { + + if ((ahead != null && !ahead.isForce() || ahead == null) && Bukkit.getPlayer(d) != null) { + Player player = Bukkit.getPlayer(d); + skullMeta.setOwningPlayer(player); + skull.setItemMeta(skullMeta); + headCache.put(original, skull); + } else { + + if (ahead != null) { + ahead.setAsyncHead(true); + } + Bukkit.getScheduler().runTaskAsynchronously(Jobs.getInstance(), () -> { + OfflinePlayer offlineP = Bukkit.getOfflinePlayer(d); + if (offlineP != null) { + skullMeta.setOwningPlayer(offlineP); + skull.setItemMeta(skullMeta); + headCache.put(original, skull); + + // Forcing server to load skin information + Bukkit.createInventory(null, InventoryType.CHEST, "").addItem(skull); + + skull.setItemMeta(skullMeta); + headCache.put(original, skull); + + if (ahead != null) + ahead.afterAsyncUpdate(skull); + } + }); + } + + } else { + skullMeta.setOwner(d); + skull.setItemMeta(skullMeta); + } + + if (ahead == null || !ahead.isAsyncHead()) { + skull.setItemMeta(skullMeta); + headCache.put(original, skull); + } + } + } + } } } + break; + case "map": + cm = CMIMaterial.FILLED_MAP.newCMIItemStack(); + if (original.contains(":") && data > 0) { + ItemStack stack = cm.getItemStack(); + MapMeta map = (MapMeta) stack.getItemMeta(); + map.setMapId(data); + stack.setItemMeta(map); + cm.setItemStack(stack); + return cm; + } break; default: break; } CMIMaterial cmat = CMIMaterial.get(subdata == null ? name : name + ":" + subdata); - if (cmat == null || cmat == CMIMaterial.NONE) { + if (cmat == null || cmat.equals(CMIMaterial.NONE)) { cmat = CMIMaterial.get(name); } - if (cmat != null && cmat != CMIMaterial.NONE) { + if (cmat != null && !cmat.equals(CMIMaterial.NONE)) { cm = cmat.newCMIItemStack(); } else cmat = CMIMaterial.get(subdata == null ? original : original + ":" + subdata); - if (cmat != null && cmat != CMIMaterial.NONE) + if (cmat != null && !cmat.equals(CMIMaterial.NONE)) cm = cmat.newCMIItemStack(); + if (cm == null) { + try { + Material match = Material.matchMaterial(original); + if (match != null) { + if (Version.isCurrentLower(Version.v1_13_R1) || !CMIMaterial.get(match).isLegacy() && CMIMaterial.get(match) != CMIMaterial.NONE) { + cm = new CMIItemStack(match); + } + } + } catch (Throwable e) { + e.printStackTrace(); + } + } + + if (cm != null && entityType != null) + cm.setEntityType(entityType); + CMIItemStack ncm = null; if (cm != null) ncm = cm.clone(); if (ncm != null && data != -999) { - if (ncm.getMaxDurability() > 15) + if (ncm.getMaxDurability() > 15) { ncm.setData((short) 0); - else { + } else { ncm.setData(data); } } + +// if (ncm != null && tag != null) { +// ncm.setTag(CMIChatColor.translate(tag)); +// } + if (ncm != null && amount != null) ncm.setAmount(amount); diff --git a/src/main/java/com/gamingmesh/jobs/CMILib/Version.java b/src/main/java/com/gamingmesh/jobs/CMILib/Version.java index afbe4a9f..4550eb14 100644 --- a/src/main/java/com/gamingmesh/jobs/CMILib/Version.java +++ b/src/main/java/com/gamingmesh/jobs/CMILib/Version.java @@ -101,4 +101,8 @@ public enum Version { public static boolean isCurrentEqualOrLower(Version v) { return getCurrent().getValue() <= v.getValue(); } + + public static boolean isCurrentEqual(Version v) { + return getCurrent().getValue() == v.getValue(); + } } \ No newline at end of file