Archiviert
13
0

Added support for the new way of converting CraftItemStacks in 1.4.5

Dieser Commit ist enthalten in:
Kristian S. Stangeland 2012-12-19 17:51:21 +01:00
Ursprung b1bd7e9fcd
Commit d7d6e24506
2 geänderte Dateien mit 61 neuen und 6 gelöschten Zeilen

Datei anzeigen

@ -55,7 +55,6 @@
<goal>shade</goal> <goal>shade</goal>
</goals> </goals>
<configuration> <configuration>
<minimizeJar>true</minimizeJar>
<shadedArtifactAttached>false</shadedArtifactAttached> <shadedArtifactAttached>false</shadedArtifactAttached>
<createDependencyReducedPom>true</createDependencyReducedPom> <createDependencyReducedPom>true</createDependencyReducedPom>
<relocations> <relocations>

Datei anzeigen

@ -35,10 +35,14 @@ public class MinecraftReflection {
private static CachedPackage craftbukkitPackage; private static CachedPackage craftbukkitPackage;
// org.bukkit.craftbukkit // org.bukkit.craftbukkit
private static Class<?> craftItemStackClass;
private static Constructor<?> craftNMSConstructor; private static Constructor<?> craftNMSConstructor;
private static Constructor<?> craftBukkitConstructor; private static Constructor<?> craftBukkitConstructor;
// New in 1.4.5
private static Method craftNMSMethod;
private static Method craftBukkitMethod;
private static boolean craftItemStackFailed;
// net.minecraft.server // net.minecraft.server
private static Class<?> itemStackArrayClass; private static Class<?> itemStackArrayClass;
@ -370,9 +374,7 @@ public class MinecraftReflection {
*/ */
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public static Class getCraftItemStackClass() { public static Class getCraftItemStackClass() {
if (craftItemStackClass == null) return getCraftBukkitClass("inventory.CraftItemStack");
craftItemStackClass = getCraftBukkitClass("inventory.CraftItemStack");
return craftItemStackClass;
} }
/** /**
@ -382,10 +384,18 @@ public class MinecraftReflection {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static ItemStack getBukkitItemStack(ItemStack bukkitItemStack) { public static ItemStack getBukkitItemStack(ItemStack bukkitItemStack) {
// Delegate this task to the method that can execute it
if (craftBukkitMethod != null)
return getBukkitItemByMethod(bukkitItemStack);
if (craftBukkitConstructor == null) { if (craftBukkitConstructor == null) {
try { try {
craftBukkitConstructor = getCraftItemStackClass().getConstructor(ItemStack.class); craftBukkitConstructor = getCraftItemStackClass().getConstructor(ItemStack.class);
} catch (Exception e) { } catch (Exception e) {
// See if this method works
if (!craftItemStackFailed)
return getBukkitItemByMethod(bukkitItemStack);
throw new RuntimeException("Cannot find CraftItemStack(org.bukkit.inventory.ItemStack).", e); throw new RuntimeException("Cannot find CraftItemStack(org.bukkit.inventory.ItemStack).", e);
} }
} }
@ -398,6 +408,25 @@ public class MinecraftReflection {
} }
} }
@SuppressWarnings("unchecked")
private static ItemStack getBukkitItemByMethod(ItemStack bukkitItemStack) {
if (craftBukkitMethod == null) {
try {
craftBukkitMethod = getCraftItemStackClass().getMethod("asCraftCopy", ItemStack.class);
} catch (Exception e) {
craftItemStackFailed = true;
throw new RuntimeException("Cannot find CraftItemStack.asCraftCopy(org.bukkit.inventory.ItemStack).", e);
}
}
// Next, construct it
try {
return (ItemStack) craftBukkitMethod.invoke(null, bukkitItemStack);
} catch (Exception e) {
throw new RuntimeException("Cannot construct CraftItemStack.", e);
}
}
/** /**
* Retrieve the Bukkit ItemStack from a given net.minecraft.server ItemStack. * Retrieve the Bukkit ItemStack from a given net.minecraft.server ItemStack.
* @param minecraftItemStack - the NMS ItemStack to wrap. * @param minecraftItemStack - the NMS ItemStack to wrap.
@ -405,10 +434,18 @@ public class MinecraftReflection {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static ItemStack getBukkitItemStack(Object minecraftItemStack) { public static ItemStack getBukkitItemStack(Object minecraftItemStack) {
// Delegate this task to the method that can execute it
if (craftNMSMethod != null)
return getBukkitItemByMethod(minecraftItemStack);
if (craftNMSConstructor == null) { if (craftNMSConstructor == null) {
try { try {
craftNMSConstructor = getCraftItemStackClass().getConstructor(minecraftItemStack.getClass()); craftNMSConstructor = getCraftItemStackClass().getConstructor(minecraftItemStack.getClass());
} catch (Exception e) { } catch (Exception e) {
// Give it a try
if (!craftItemStackFailed)
return getBukkitItemByMethod(minecraftItemStack);
throw new RuntimeException("Cannot find CraftItemStack(net.mineraft.server.ItemStack).", e); throw new RuntimeException("Cannot find CraftItemStack(net.mineraft.server.ItemStack).", e);
} }
} }
@ -421,6 +458,25 @@ public class MinecraftReflection {
} }
} }
@SuppressWarnings("unchecked")
private static ItemStack getBukkitItemByMethod(Object minecraftItemStack) {
if (craftNMSMethod == null) {
try {
craftNMSMethod = getCraftItemStackClass().getMethod("asCraftMirror", minecraftItemStack.getClass());
} catch (Exception e) {
craftItemStackFailed = true;
throw new RuntimeException("Cannot find CraftItemStack.asCraftMirror(net.mineraft.server.ItemStack).", e);
}
}
// Next, construct it
try {
return (ItemStack) craftNMSMethod.invoke(null, minecraftItemStack);
} catch (Exception e) {
throw new RuntimeException("Cannot construct CraftItemStack.", e);
}
}
/** /**
* Retrieve the net.minecraft.server ItemStack from a Bukkit ItemStack. * Retrieve the net.minecraft.server ItemStack from a Bukkit ItemStack.
* @param stack - the Bukkit ItemStack to convert. * @param stack - the Bukkit ItemStack to convert.