|
41 | 41 | import javax.annotation.Nonnull;
|
42 | 42 |
|
43 | 43 | import org.bukkit.Bukkit;
|
| 44 | +import org.bukkit.Material; |
44 | 45 | import org.bukkit.Server;
|
45 | 46 | import org.bukkit.inventory.ItemStack;
|
46 | 47 |
|
@@ -128,13 +129,13 @@ public class MinecraftReflection {
|
128 | 129 | private static String packageVersion;
|
129 | 130 |
|
130 | 131 | // Item stacks
|
131 |
| - private static Method craftNMSMethod; |
| 132 | + /* private static Method craftNMSMethod; |
132 | 133 | private static Method craftBukkitNMS;
|
133 | 134 | private static Method craftBukkitOBC;
|
134 | 135 | private static boolean craftItemStackFailed;
|
135 | 136 |
|
136 | 137 | private static Constructor<?> craftNMSConstructor;
|
137 |
| - private static Constructor<?> craftBukkitConstructor; |
| 138 | + private static Constructor<?> craftBukkitConstructor; */ |
138 | 139 |
|
139 | 140 | // net.minecraft.server
|
140 | 141 | private static Class<?> itemStackArrayClass;
|
@@ -1840,7 +1841,7 @@ public static boolean signUpdateExists() {
|
1840 | 1841 | * @param bukkitItemStack - the Bukkit ItemStack to convert.
|
1841 | 1842 | * @return A CraftItemStack as an ItemStack.
|
1842 | 1843 | */
|
1843 |
| - public static ItemStack getBukkitItemStack(ItemStack bukkitItemStack) { |
| 1844 | + /* public static ItemStack getBukkitItemStack(ItemStack bukkitItemStack) { |
1844 | 1845 | // Delegate this task to the method that can execute it
|
1845 | 1846 | if (craftBukkitNMS != null)
|
1846 | 1847 | return getBukkitItemByMethod(bukkitItemStack);
|
@@ -1890,7 +1891,7 @@ private static ItemStack getBukkitItemByMethod(ItemStack bukkitItemStack) {
|
1890 | 1891 | * @param minecraftItemStack - the NMS ItemStack to wrap.
|
1891 | 1892 | * @return The wrapped ItemStack.
|
1892 | 1893 | */
|
1893 |
| - public static ItemStack getBukkitItemStack(Object minecraftItemStack) { |
| 1894 | + /* public static ItemStack getBukkitItemStack(Object minecraftItemStack) { |
1894 | 1895 | // Delegate this task to the method that can execute it
|
1895 | 1896 | if (craftNMSMethod != null)
|
1896 | 1897 | return getBukkitItemByMethod(minecraftItemStack);
|
@@ -1941,13 +1942,104 @@ private static ItemStack getBukkitItemByMethod(Object minecraftItemStack) {
|
1941 | 1942 | * @param stack - the Bukkit ItemStack to convert.
|
1942 | 1943 | * @return The NMS ItemStack, or NULL if the stack represents air.
|
1943 | 1944 | */
|
1944 |
| - public static Object getMinecraftItemStack(ItemStack stack) { |
| 1945 | + /* public static Object getMinecraftItemStack(ItemStack stack) { |
1945 | 1946 | // Make sure this is a CraftItemStack
|
1946 | 1947 | if (!isCraftItemStack(stack))
|
1947 | 1948 | stack = getBukkitItemStack(stack);
|
1948 | 1949 |
|
1949 | 1950 | BukkitUnwrapper unwrapper = new BukkitUnwrapper();
|
1950 | 1951 | return unwrapper.unwrapItem(stack);
|
| 1952 | + } */ |
| 1953 | + |
| 1954 | + // ---- ItemStack conversions |
| 1955 | + |
| 1956 | + private static Method asNMSCopy = null; |
| 1957 | + private static Method asCraftMirror = null; |
| 1958 | + |
| 1959 | + private static Boolean nullEnforced = null; |
| 1960 | + private static Method isEmpty = null; |
| 1961 | + |
| 1962 | + /** |
| 1963 | + * Retrieves the Bukkit equivalent of a NMS ItemStack. This method should |
| 1964 | + * preserve NBT data and will never return null when supplied with a valid |
| 1965 | + * ItemStack. Empty ItemStacks are returned as AIR. |
| 1966 | + * |
| 1967 | + * @param generic NMS ItemStack |
| 1968 | + * @return The Bukkit equivalent |
| 1969 | + */ |
| 1970 | + public static ItemStack getBukkitItemStack(Object generic) { |
| 1971 | + // Make sure it actually is an ItemStack |
| 1972 | + if (!is(getItemStackClass(), generic)) { |
| 1973 | + return null; |
| 1974 | + } |
| 1975 | + |
| 1976 | + // Convert null to AIR |
| 1977 | + if (generic == null) { |
| 1978 | + return new ItemStack(Material.AIR); |
| 1979 | + } |
| 1980 | + |
| 1981 | + // Check null enforcement |
| 1982 | + try { |
| 1983 | + if (nullEnforced == null) { |
| 1984 | + isEmpty = getItemStackClass().getMethod("isEmpty"); |
| 1985 | + nullEnforced = true; |
| 1986 | + } |
| 1987 | + |
| 1988 | + if (nullEnforced) { |
| 1989 | + if ((boolean) isEmpty.invoke(generic)) { |
| 1990 | + return new ItemStack(Material.AIR); |
| 1991 | + } |
| 1992 | + } |
| 1993 | + } catch (ReflectiveOperationException ex) { |
| 1994 | + nullEnforced = false; |
| 1995 | + } |
| 1996 | + |
| 1997 | + // Find asCraftMirror |
| 1998 | + if (asCraftMirror == null) { |
| 1999 | + try { |
| 2000 | + asCraftMirror = getCraftItemStackClass().getMethod("asCraftMirror", getItemStackClass()); |
| 2001 | + } catch (ReflectiveOperationException ex) { |
| 2002 | + throw new RuntimeException("Failed to obtain CraftItemStack.asCraftMirror", ex); |
| 2003 | + } |
| 2004 | + } |
| 2005 | + |
| 2006 | + // Convert to a craft mirror to preserve NBT data |
| 2007 | + try { |
| 2008 | + return (ItemStack) asCraftMirror.invoke(nullEnforced, generic); |
| 2009 | + } catch (ReflectiveOperationException ex) { |
| 2010 | + throw new RuntimeException("Failed to obtain craft mirror of " + generic, ex); |
| 2011 | + } |
| 2012 | + } |
| 2013 | + |
| 2014 | + /** |
| 2015 | + * Retrieves the NMS equivalent of a Bukkit ItemStack. This method will |
| 2016 | + * never return null and should preserve NBT data. Null inputs are treated |
| 2017 | + * as empty (AIR) ItemStacks. |
| 2018 | + * |
| 2019 | + * @param specific Bukkit ItemStack |
| 2020 | + * @return The NMS equivalent |
| 2021 | + */ |
| 2022 | + public static Object getMinecraftItemStack(ItemStack specific) { |
| 2023 | + // Grab asNMSCopy first |
| 2024 | + if (asNMSCopy == null) { |
| 2025 | + try { |
| 2026 | + asNMSCopy = getCraftItemStackClass().getMethod("asNMSCopy", ItemStack.class); |
| 2027 | + } catch (ReflectiveOperationException ex) { |
| 2028 | + throw new RuntimeException("Failed to obtain CraftItemStack.asNMSCopy", ex); |
| 2029 | + } |
| 2030 | + } |
| 2031 | + |
| 2032 | + // If it's already a CraftItemStack, use its handle |
| 2033 | + if (is(getCraftItemStackClass(), specific)) { |
| 2034 | + return new BukkitUnwrapper().unwrapItem(specific); |
| 2035 | + } |
| 2036 | + |
| 2037 | + // If it's not, grab a NMS copy |
| 2038 | + try { |
| 2039 | + return asNMSCopy.invoke(null, specific); |
| 2040 | + } catch (ReflectiveOperationException ex) { |
| 2041 | + throw new RuntimeException("Failed to make NMS copy of " + specific, ex); |
| 2042 | + } |
1951 | 2043 | }
|
1952 | 2044 |
|
1953 | 2045 | /**
|
|
0 commit comments