@@ -1847,121 +1847,6 @@ public static Class<?> getNonNullListClass() {
1847
1847
return getMinecraftClass ("NonNullList" );
1848
1848
}
1849
1849
1850
- /**
1851
- * Retrieve a CraftItemStack from a given ItemStack.
1852
- * @param bukkitItemStack - the Bukkit ItemStack to convert.
1853
- * @return A CraftItemStack as an ItemStack.
1854
- */
1855
- /* public static ItemStack getBukkitItemStack(ItemStack bukkitItemStack) {
1856
- // Delegate this task to the method that can execute it
1857
- if (craftBukkitNMS != null)
1858
- return getBukkitItemByMethod(bukkitItemStack);
1859
-
1860
- if (craftBukkitConstructor == null) {
1861
- try {
1862
- craftBukkitConstructor = getCraftItemStackClass().getConstructor(ItemStack.class);
1863
- } catch (Exception e) {
1864
- // See if this method works
1865
- if (!craftItemStackFailed)
1866
- return getBukkitItemByMethod(bukkitItemStack);
1867
-
1868
- throw new RuntimeException("Cannot find CraftItemStack(org.bukkit.inventory.ItemStack).", e);
1869
- }
1870
- }
1871
-
1872
- // Try to create the CraftItemStack
1873
- try {
1874
- return (ItemStack) craftBukkitConstructor.newInstance(bukkitItemStack);
1875
- } catch (Exception e) {
1876
- throw new RuntimeException("Cannot construct CraftItemStack.", e);
1877
- }
1878
- }
1879
-
1880
- private static ItemStack getBukkitItemByMethod(ItemStack bukkitItemStack) {
1881
- if (craftBukkitNMS == null) {
1882
- try {
1883
- craftBukkitNMS = getCraftItemStackClass().getMethod("asNMSCopy", ItemStack.class);
1884
- craftBukkitOBC = getCraftItemStackClass().getMethod("asCraftMirror", MinecraftReflection.getItemStackClass());
1885
- } catch (Exception e) {
1886
- craftItemStackFailed = true;
1887
- throw new RuntimeException("Cannot find CraftItemStack.asCraftCopy(org.bukkit.inventory.ItemStack).", e);
1888
- }
1889
- }
1890
-
1891
- // Next, construct it
1892
- try {
1893
- Object nmsItemStack = craftBukkitNMS.invoke(null, bukkitItemStack);
1894
- return (ItemStack) craftBukkitOBC.invoke(null, nmsItemStack);
1895
- } catch (Exception e) {
1896
- throw new RuntimeException("Cannot construct CraftItemStack.", e);
1897
- }
1898
- }
1899
-
1900
- /**
1901
- * Retrieve the Bukkit ItemStack from a given net.minecraft.server ItemStack.
1902
- * @param minecraftItemStack - the NMS ItemStack to wrap.
1903
- * @return The wrapped ItemStack.
1904
- */
1905
- /* public static ItemStack getBukkitItemStack(Object minecraftItemStack) {
1906
- // Delegate this task to the method that can execute it
1907
- if (craftNMSMethod != null)
1908
- return getBukkitItemByMethod(minecraftItemStack);
1909
-
1910
- if (craftNMSConstructor == null) {
1911
- try {
1912
- craftNMSConstructor = getCraftItemStackClass().getConstructor(minecraftItemStack.getClass());
1913
- } catch (Exception e) {
1914
- // Give it a try
1915
- if (!craftItemStackFailed)
1916
- return getBukkitItemByMethod(minecraftItemStack);
1917
-
1918
- throw new RuntimeException("Cannot find CraftItemStack(net.minecraft.server.ItemStack).", e);
1919
- }
1920
- }
1921
-
1922
- // Try to create the CraftItemStack
1923
- try {
1924
- return (ItemStack) craftNMSConstructor.newInstance(minecraftItemStack);
1925
- } catch (Exception e) {
1926
- throw new RuntimeException("Cannot construct CraftItemStack.", e);
1927
- }
1928
- }
1929
-
1930
- private static ItemStack getBukkitItemByMethod(Object minecraftItemStack) {
1931
- if (craftNMSMethod == null) {
1932
- try {
1933
- craftNMSMethod = getCraftItemStackClass().getMethod("asCraftMirror", minecraftItemStack.getClass());
1934
- } catch (Exception e) {
1935
- craftItemStackFailed = true;
1936
- throw new RuntimeException("Cannot find CraftItemStack.asCraftMirror(net.minecraft.server.ItemStack).", e);
1937
- }
1938
- }
1939
-
1940
- // Next, construct it
1941
- try {
1942
- return (ItemStack) craftNMSMethod.invoke(null, minecraftItemStack);
1943
- } catch (Exception e) {
1944
- throw new RuntimeException("Cannot construct CraftItemStack.", e);
1945
- }
1946
- }
1947
-
1948
- /**
1949
- * Retrieve the net.minecraft.server ItemStack from a Bukkit ItemStack.
1950
- * <p>
1951
- * By convention, item stacks that contain air are usually represented as NULL.
1952
- *
1953
- * @param stack - the Bukkit ItemStack to convert.
1954
- * @return The NMS ItemStack, or NULL if the stack represents air.
1955
- */
1956
- /* public static Object getMinecraftItemStack(ItemStack stack) {
1957
- // Make sure this is a CraftItemStack
1958
- if (!isCraftItemStack(stack))
1959
- stack = getBukkitItemStack(stack);
1960
-
1961
- BukkitUnwrapper unwrapper = new BukkitUnwrapper();
1962
- return unwrapper.unwrapItem(stack);
1963
- } */
1964
-
1965
1850
// ---- ItemStack conversions
1966
1851
1967
1852
private static Method asNMSCopy = null ;
@@ -1973,24 +1858,38 @@ private static ItemStack getBukkitItemByMethod(Object minecraftItemStack) {
1973
1858
/**
1974
1859
* Retrieves the Bukkit equivalent of a NMS ItemStack. This method should
1975
1860
* preserve NBT data and will never return null when supplied with a valid
1976
- * ItemStack. Empty ItemStacks are returned as AIR.
1861
+ * ItemStack. Empty ItemStacks are treated as AIR.
1977
1862
*
1978
1863
* @param generic NMS ItemStack
1979
1864
* @return The Bukkit equivalent
1980
1865
*/
1981
1866
public static ItemStack getBukkitItemStack (Object generic ) {
1982
- // Make sure it actually is an ItemStack
1983
- if (!is (getItemStackClass (), generic )) {
1984
- return null ;
1985
- }
1986
-
1987
- // Convert null to AIR
1988
1867
if (generic == null ) {
1868
+ // Convert null to AIR - 1.11 behavior
1989
1869
return new ItemStack (Material .AIR );
1990
1870
}
1991
1871
1992
- // Check null enforcement
1872
+ if (generic instanceof ItemStack ) {
1873
+ ItemStack bukkit = (ItemStack ) generic ;
1874
+
1875
+ // They're probably looking for the CraftItemStack
1876
+ // If it's one already our work is done
1877
+ if (is (getCraftItemStackClass (), generic )) {
1878
+ return bukkit ;
1879
+ }
1880
+
1881
+ // If not, convert it to one
1882
+ Object nmsStack = getMinecraftItemStack ((ItemStack ) generic );
1883
+ return getBukkitItemStack (nmsStack );
1884
+ }
1885
+
1886
+ if (!is (getItemStackClass (), generic )) {
1887
+ // We can't do anything with non-ItemStacks
1888
+ throw new IllegalArgumentException (generic + " is not an ItemStack!" );
1889
+ }
1890
+
1993
1891
try {
1892
+ // Check null enforcement - 1.11 behavior
1994
1893
if (nullEnforced == null ) {
1995
1894
isEmpty = getItemStackClass ().getMethod ("isEmpty" );
1996
1895
nullEnforced = true ;
@@ -2005,7 +1904,6 @@ public static ItemStack getBukkitItemStack(Object generic) {
2005
1904
nullEnforced = false ;
2006
1905
}
2007
1906
2008
- // Find asCraftMirror
2009
1907
if (asCraftMirror == null ) {
2010
1908
try {
2011
1909
asCraftMirror = getCraftItemStackClass ().getMethod ("asCraftMirror" , getItemStackClass ());
@@ -2014,8 +1912,8 @@ public static ItemStack getBukkitItemStack(Object generic) {
2014
1912
}
2015
1913
}
2016
1914
2017
- // Convert to a craft mirror to preserve NBT data
2018
1915
try {
1916
+ // Convert to a craft mirror to preserve NBT data
2019
1917
return (ItemStack ) asCraftMirror .invoke (nullEnforced , generic );
2020
1918
} catch (ReflectiveOperationException ex ) {
2021
1919
throw new RuntimeException ("Failed to obtain craft mirror of " + generic , ex );
@@ -2031,7 +1929,6 @@ public static ItemStack getBukkitItemStack(Object generic) {
2031
1929
* @return The NMS equivalent
2032
1930
*/
2033
1931
public static Object getMinecraftItemStack (ItemStack specific ) {
2034
- // Grab asNMSCopy first
2035
1932
if (asNMSCopy == null ) {
2036
1933
try {
2037
1934
asNMSCopy = getCraftItemStackClass ().getMethod ("asNMSCopy" , ItemStack .class );
@@ -2040,13 +1937,13 @@ public static Object getMinecraftItemStack(ItemStack specific) {
2040
1937
}
2041
1938
}
2042
1939
2043
- // If it's already a CraftItemStack, use its handle
2044
1940
if (is (getCraftItemStackClass (), specific )) {
1941
+ // If it's already a CraftItemStack, use its handle
2045
1942
return new BukkitUnwrapper ().unwrapItem (specific );
2046
1943
}
2047
1944
2048
- // If it's not, grab a NMS copy
2049
1945
try {
1946
+ // If not, grab a NMS copy
2050
1947
return asNMSCopy .invoke (null , specific );
2051
1948
} catch (ReflectiveOperationException ex ) {
2052
1949
throw new RuntimeException ("Failed to make NMS copy of " + specific , ex );
0 commit comments