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

This commit is contained in:
Kristian S. Stangeland 2013-08-06 19:27:58 +02:00
parent 6d152707cf
commit 50c2931484
2 changed files with 21 additions and 7 deletions

View File

@ -1175,8 +1175,10 @@ public class MinecraftReflection {
/**
* 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.
* @return The NMS ItemStack.
* @return The NMS ItemStack, or NULL if the stack represents air.
*/
public static Object getMinecraftItemStack(ItemStack stack) {
// Make sure this is a CraftItemStack

View File

@ -118,12 +118,12 @@ public class NbtFactory {
* <p>
* The item stack must be a wrapper for a CraftItemStack. Use
* {@link MinecraftReflection#getBukkitItemStack(ItemStack)} if not.
* @param stack - the item stack.
* @param compound - the new NBT compound.
* @param stack - the item stack, cannot be air.
* @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) {
if (!MinecraftReflection.isCraftItemStack(stack))
throw new IllegalArgumentException("Stack must be a CraftItemStack.");
checkItemStack(stack);
StructureModifier<NbtBase<?>> modifier = getStackModifier(stack);
modifier.write(0, compound);
@ -140,8 +140,7 @@ public class NbtFactory {
* @return A wrapper for its NBT tag.
*/
public static NbtWrapper<?> fromItemTag(ItemStack stack) {
if (!MinecraftReflection.isCraftItemStack(stack))
throw new IllegalArgumentException("Stack must be a CraftItemStack.");
checkItemStack(stack);
StructureModifier<NbtBase<?>> modifier = getStackModifier(stack);
NbtBase<?> result = modifier.read(0);
@ -154,6 +153,19 @@ public class NbtFactory {
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.
* @param stack - the stack that will store the NBT compound.