Archiviert
13
0

ItemStacks that represents AIR cannot store NMS tags. Use exceptions.

Dieser Commit ist enthalten in:
Kristian S. Stangeland 2013-08-06 19:27:58 +02:00
Ursprung 6d152707cf
Commit 50c2931484
2 geänderte Dateien mit 21 neuen und 7 gelöschten Zeilen

Datei anzeigen

@ -1175,8 +1175,10 @@ public class MinecraftReflection {
/** /**
* Retrieve the net.minecraft.server ItemStack from a Bukkit ItemStack. * Retrieve the net.minecraft.server ItemStack from a Bukkit ItemStack.
* <p>
* By convention, item stacks that contain air are usually represented as NULL.
* @param stack - the Bukkit ItemStack to convert. * @param stack - the Bukkit ItemStack to convert.
* @return The NMS ItemStack. * @return The NMS ItemStack, or NULL if the stack represents air.
*/ */
public static Object getMinecraftItemStack(ItemStack stack) { public static Object getMinecraftItemStack(ItemStack stack) {
// Make sure this is a CraftItemStack // Make sure this is a CraftItemStack

Datei anzeigen

@ -118,12 +118,12 @@ public class NbtFactory {
* <p> * <p>
* The item stack must be a wrapper for a CraftItemStack. Use * The item stack must be a wrapper for a CraftItemStack. Use
* {@link MinecraftReflection#getBukkitItemStack(ItemStack)} if not. * {@link MinecraftReflection#getBukkitItemStack(ItemStack)} if not.
* @param stack - the item stack. * @param stack - the item stack, cannot be air.
* @param compound - the new NBT compound. * @param compound - the new NBT compound, or NULL to remove it.
* @throws IllegalArgumentException If the stack is not a CraftItemStack, or it represents air.
*/ */
public static void setItemTag(ItemStack stack, NbtCompound compound) { public static void setItemTag(ItemStack stack, NbtCompound compound) {
if (!MinecraftReflection.isCraftItemStack(stack)) checkItemStack(stack);
throw new IllegalArgumentException("Stack must be a CraftItemStack.");
StructureModifier<NbtBase<?>> modifier = getStackModifier(stack); StructureModifier<NbtBase<?>> modifier = getStackModifier(stack);
modifier.write(0, compound); modifier.write(0, compound);
@ -140,8 +140,7 @@ public class NbtFactory {
* @return A wrapper for its NBT tag. * @return A wrapper for its NBT tag.
*/ */
public static NbtWrapper<?> fromItemTag(ItemStack stack) { public static NbtWrapper<?> fromItemTag(ItemStack stack) {
if (!MinecraftReflection.isCraftItemStack(stack)) checkItemStack(stack);
throw new IllegalArgumentException("Stack must be a CraftItemStack.");
StructureModifier<NbtBase<?>> modifier = getStackModifier(stack); StructureModifier<NbtBase<?>> modifier = getStackModifier(stack);
NbtBase<?> result = modifier.read(0); NbtBase<?> result = modifier.read(0);
@ -154,6 +153,19 @@ public class NbtFactory {
return fromBase(result); return fromBase(result);
} }
/**
* Ensure that the given stack can store arbitrary NBT information.
* @param stack - the stack to check.
*/
private static void checkItemStack(ItemStack stack) {
if (stack == null)
throw new IllegalArgumentException("Stack cannot be NULL.");
if (!MinecraftReflection.isCraftItemStack(stack))
throw new IllegalArgumentException("Stack must be a CraftItemStack.");
if (stack.getTypeId() == 0)
throw new IllegalArgumentException("ItemStacks representing air cannot store NMS information.");
}
/** /**
* Retrieve a structure modifier that automatically marshalls between NBT wrappers and their NMS counterpart. * Retrieve a structure modifier that automatically marshalls between NBT wrappers and their NMS counterpart.
* @param stack - the stack that will store the NBT compound. * @param stack - the stack that will store the NBT compound.