Skip to content

Commit 33bb127

Browse files
committed
Restore ItemStack to CraftItemStack conversion
1 parent d175a76 commit 33bb127

File tree

1 file changed

+25
-128
lines changed

1 file changed

+25
-128
lines changed

modules/API/src/main/java/com/comphenix/protocol/utility/MinecraftReflection.java

Lines changed: 25 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,121 +1847,6 @@ public static Class<?> getNonNullListClass() {
18471847
return getMinecraftClass("NonNullList");
18481848
}
18491849

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-
19651850
// ---- ItemStack conversions
19661851

19671852
private static Method asNMSCopy = null;
@@ -1973,24 +1858,38 @@ private static ItemStack getBukkitItemByMethod(Object minecraftItemStack) {
19731858
/**
19741859
* Retrieves the Bukkit equivalent of a NMS ItemStack. This method should
19751860
* 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.
19771862
*
19781863
* @param generic NMS ItemStack
19791864
* @return The Bukkit equivalent
19801865
*/
19811866
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
19881867
if (generic == null) {
1868+
// Convert null to AIR - 1.11 behavior
19891869
return new ItemStack(Material.AIR);
19901870
}
19911871

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+
19931891
try {
1892+
// Check null enforcement - 1.11 behavior
19941893
if (nullEnforced == null) {
19951894
isEmpty = getItemStackClass().getMethod("isEmpty");
19961895
nullEnforced = true;
@@ -2005,7 +1904,6 @@ public static ItemStack getBukkitItemStack(Object generic) {
20051904
nullEnforced = false;
20061905
}
20071906

2008-
// Find asCraftMirror
20091907
if (asCraftMirror == null) {
20101908
try {
20111909
asCraftMirror = getCraftItemStackClass().getMethod("asCraftMirror", getItemStackClass());
@@ -2014,8 +1912,8 @@ public static ItemStack getBukkitItemStack(Object generic) {
20141912
}
20151913
}
20161914

2017-
// Convert to a craft mirror to preserve NBT data
20181915
try {
1916+
// Convert to a craft mirror to preserve NBT data
20191917
return (ItemStack) asCraftMirror.invoke(nullEnforced, generic);
20201918
} catch (ReflectiveOperationException ex) {
20211919
throw new RuntimeException("Failed to obtain craft mirror of " + generic, ex);
@@ -2031,7 +1929,6 @@ public static ItemStack getBukkitItemStack(Object generic) {
20311929
* @return The NMS equivalent
20321930
*/
20331931
public static Object getMinecraftItemStack(ItemStack specific) {
2034-
// Grab asNMSCopy first
20351932
if (asNMSCopy == null) {
20361933
try {
20371934
asNMSCopy = getCraftItemStackClass().getMethod("asNMSCopy", ItemStack.class);
@@ -2040,13 +1937,13 @@ public static Object getMinecraftItemStack(ItemStack specific) {
20401937
}
20411938
}
20421939

2043-
// If it's already a CraftItemStack, use its handle
20441940
if (is(getCraftItemStackClass(), specific)) {
1941+
// If it's already a CraftItemStack, use its handle
20451942
return new BukkitUnwrapper().unwrapItem(specific);
20461943
}
20471944

2048-
// If it's not, grab a NMS copy
20491945
try {
1946+
// If not, grab a NMS copy
20501947
return asNMSCopy.invoke(null, specific);
20511948
} catch (ReflectiveOperationException ex) {
20521949
throw new RuntimeException("Failed to make NMS copy of " + specific, ex);

0 commit comments

Comments
 (0)