Skip to content

Commit c49d37f

Browse files
committed
Fixed bugs where methods does not exist
Also added small detection what type of item when only provide metadata on legacy.
1 parent bb01df2 commit c49d37f

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/utility/UnbreakableUtil.java

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
import org.bukkit.Bukkit;
55
import org.bukkit.Material;
66
import org.bukkit.inventory.ItemStack;
7-
import org.bukkit.inventory.meta.ItemMeta;
7+
import org.bukkit.inventory.meta.*;
88

9+
import javax.annotation.Nonnull;
10+
import javax.annotation.Nullable;
911
import java.lang.reflect.Constructor;
12+
import java.lang.reflect.Field;
1013
import java.lang.reflect.InvocationTargetException;
1114
import java.lang.reflect.Method;
1215

@@ -19,7 +22,6 @@ public final class UnbreakableUtil {
1922
private static Method setTagMethod = null;
2023
private static Method setBooleanMethod = null;
2124
private static Method getBooleanMethod = null;
22-
private static Method metaGetMaterialMethod = null;
2325
private static Constructor<?> nbtTagConstructor = null;
2426

2527
private static boolean reflectionReady = false;
@@ -43,11 +45,6 @@ public final class UnbreakableUtil {
4345
asNMSCopyMethod = craftItemStackClass.getMethod("asNMSCopy", ItemStack.class);
4446
asBukkitCopyMethod = craftItemStackClass.getMethod("asBukkitCopy", Class.forName(getMinecraftPath() + ".ItemStack"));
4547

46-
Class<?> craftMetaItem = Class.forName(craftPath + ".inventory.CraftMetaItem"
47-
);
48-
metaGetMaterialMethod = craftMetaItem.getDeclaredMethod("getMaterial");
49-
metaGetMaterialMethod.setAccessible(true);
50-
5148
final Class<?> nbtTagCompoundClass = Class.forName(getMinecraftPath() + ".NBTTagCompound");
5249

5350
nbtTagConstructor = nbtTagCompoundClass.getConstructor();
@@ -87,19 +84,13 @@ public static ItemMeta applyToMeta(final ItemMeta meta, final boolean unbreakabl
8784
}
8885
if (!reflectionReady) return meta;
8986

90-
try {
91-
Material original = (Material) metaGetMaterialMethod.invoke(meta);
92-
if (original == null || original == Material.AIR) original = Material.STONE;
93-
94-
ItemStack stack = new ItemStack(original);
95-
stack.setItemMeta(meta);
87+
Material original = getMetaType(meta);
88+
if (original == null || original == Material.AIR) original = Material.STONE;
89+
ItemStack stack = new ItemStack(original);
90+
stack.setItemMeta(meta);
9691

97-
stack = applyToItem(stack, unbreakable);
98-
return stack.getItemMeta();
99-
} catch (InvocationTargetException | IllegalAccessException e) {
100-
logger.logError(e, () -> "Failed to invoke the material for this meta via legacy fallback");
101-
return meta;
102-
}
92+
stack = applyToItem(stack, unbreakable);
93+
return stack.getItemMeta();
10394
}
10495

10596
/**
@@ -193,4 +184,20 @@ private static String getPackageVersion() {
193184
private static Object getNmsStack(final ItemStack item) throws IllegalAccessException, InvocationTargetException {
194185
return asNMSCopyMethod.invoke(null, item);
195186
}
187+
188+
@Nullable
189+
private static Material getMetaType(@Nonnull final ItemMeta meta){
190+
Material material;
191+
if (meta instanceof SkullMeta) material = Material.getMaterial("SKULL_ITEM");
192+
else if (meta instanceof LeatherArmorMeta) material = Material.LEATHER_HELMET;
193+
else if (meta instanceof BannerMeta) material = Material.getMaterial("BANNER");
194+
else if (meta instanceof FireworkEffectMeta) material = Material.FIREWORK_STAR;
195+
else if (meta instanceof FireworkMeta) material = Material.FIREWORK_ROCKET;
196+
else if (meta instanceof MapMeta) material = Material.MAP;
197+
else if (meta instanceof BookMeta) material = Material.WRITTEN_BOOK;
198+
else if (meta instanceof PotionMeta) material = Material.POTION;
199+
else material = Material.STONE;
200+
201+
return material;
202+
}
196203
}

0 commit comments

Comments
 (0)