use of javax.annotation.Nonnull in project MinecraftForge by MinecraftForge.
the class GameRegistry method makeItemStack.
/**
* Makes an {@link ItemStack} based on the itemName reference, with supplied meta, stackSize and nbt, if possible
* <p/>
* Will return null if the item doesn't exist (because it's not from a loaded mod for example)
* Will throw a {@link RuntimeException} if the nbtString is invalid for use in an {@link ItemStack}
*
* @param itemName a registry name reference
* @param meta the meta
* @param stackSize the stack size
* @param nbtString an nbt stack as a string, will be processed by {@link JsonToNBT}
* @return a new itemstack
*/
@Nonnull
public static ItemStack makeItemStack(String itemName, int meta, int stackSize, String nbtString) {
if (itemName == null) {
throw new IllegalArgumentException("The itemName cannot be null");
}
Item item = GameData.getItemRegistry().getObject(new ResourceLocation(itemName));
if (item == null) {
FMLLog.getLogger().log(Level.TRACE, "Unable to find item with name {}", itemName);
return ItemStack.EMPTY;
}
ItemStack is = new ItemStack(item, stackSize, meta);
if (!Strings.isNullOrEmpty(nbtString)) {
NBTBase nbttag = null;
try {
nbttag = JsonToNBT.getTagFromJson(nbtString);
} catch (NBTException e) {
FMLLog.getLogger().log(Level.WARN, "Encountered an exception parsing ItemStack NBT string {}", nbtString, e);
throw Throwables.propagate(e);
}
if (!(nbttag instanceof NBTTagCompound)) {
FMLLog.getLogger().log(Level.WARN, "Unexpected NBT string - multiple values {}", nbtString);
throw new RuntimeException("Invalid NBT JSON");
} else {
is.setTagCompound((NBTTagCompound) nbttag);
}
}
return is;
}
use of javax.annotation.Nonnull in project MinecraftForge by MinecraftForge.
the class CombinedInvWrapper method getStackInSlot.
@Override
@Nonnull
public ItemStack getStackInSlot(int slot) {
int index = getIndexForSlot(slot);
IItemHandlerModifiable handler = getHandlerFromIndex(index);
slot = getSlotFromIndex(slot, index);
return handler.getStackInSlot(slot);
}
use of javax.annotation.Nonnull in project MinecraftForge by MinecraftForge.
the class InvWrapper method insertItem.
@Override
@Nonnull
public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate) {
if (stack.isEmpty())
return ItemStack.EMPTY;
ItemStack stackInSlot = getInv().getStackInSlot(slot);
int m;
if (!stackInSlot.isEmpty()) {
if (!ItemHandlerHelper.canItemStacksStack(stack, stackInSlot))
return stack;
if (!getInv().isItemValidForSlot(slot, stack))
return stack;
m = Math.min(stack.getMaxStackSize(), getSlotLimit(slot)) - stackInSlot.getCount();
if (stack.getCount() <= m) {
if (!simulate) {
ItemStack copy = stack.copy();
copy.grow(stackInSlot.getCount());
getInv().setInventorySlotContents(slot, copy);
getInv().markDirty();
}
return ItemStack.EMPTY;
} else {
// copy the stack to not modify the original one
stack = stack.copy();
if (!simulate) {
ItemStack copy = stack.splitStack(m);
copy.grow(stackInSlot.getCount());
getInv().setInventorySlotContents(slot, copy);
getInv().markDirty();
return stack;
} else {
stack.shrink(m);
return stack;
}
}
} else {
if (!getInv().isItemValidForSlot(slot, stack))
return stack;
m = Math.min(stack.getMaxStackSize(), getSlotLimit(slot));
if (m < stack.getCount()) {
// copy the stack to not modify the original one
stack = stack.copy();
if (!simulate) {
getInv().setInventorySlotContents(slot, stack.splitStack(m));
getInv().markDirty();
return stack;
} else {
stack.shrink(m);
return stack;
}
} else {
if (!simulate) {
getInv().setInventorySlotContents(slot, stack);
getInv().markDirty();
}
return ItemStack.EMPTY;
}
}
}
use of javax.annotation.Nonnull in project MinecraftForge by MinecraftForge.
the class SidedInvWrapper method extractItem.
@Override
@Nonnull
public ItemStack extractItem(int slot, int amount, boolean simulate) {
if (amount == 0)
return ItemStack.EMPTY;
int slot1 = getSlot(inv, slot, side);
if (slot1 == -1)
return ItemStack.EMPTY;
ItemStack stackInSlot = inv.getStackInSlot(slot1);
if (stackInSlot.isEmpty())
return ItemStack.EMPTY;
if (!inv.canExtractItem(slot1, stackInSlot, side))
return ItemStack.EMPTY;
if (simulate) {
if (stackInSlot.getCount() < amount) {
return stackInSlot.copy();
} else {
ItemStack copy = stackInSlot.copy();
copy.setCount(amount);
return copy;
}
} else {
int m = Math.min(stackInSlot.getCount(), amount);
return inv.decrStackSize(slot1, m);
}
}
use of javax.annotation.Nonnull in project MinecraftForge by MinecraftForge.
the class ReflectionHelper method findMethod.
/**
* Finds a method with the specified name and parameters in the given class and makes it accessible.
* Note: for performance, store the returned value and avoid calling this repeatedly.
* <p>
* Throws an exception if the method is not found.
*
* @param clazz The class to find the method on.
* @param methodName The name of the method to find (used in developer environments, i.e. "getWorldTime").
* @param methodObfName The obfuscated name of the method to find (used in obfuscated environments, i.e. "func_72820_D").
* If the name you are looking for is on a class that is never obfuscated, this should be null.
* @param parameterTypes The parameter types of the method to find.
* @return The method with the specified name and parameters in the given class.
*/
@Nonnull
public static Method findMethod(@Nonnull Class<?> clazz, @Nonnull String methodName, @Nullable String methodObfName, Class<?>... parameterTypes) {
Preconditions.checkNotNull(clazz);
Preconditions.checkArgument(StringUtils.isNotEmpty(methodName), "Method name cannot be empty");
String nameToFind;
if (methodObfName == null || (Boolean) Launch.blackboard.get("fml.deobfuscatedEnvironment")) {
nameToFind = methodName;
} else {
nameToFind = methodObfName;
}
try {
Method m = clazz.getDeclaredMethod(nameToFind, parameterTypes);
m.setAccessible(true);
return m;
} catch (Exception e) {
throw new UnableToFindMethodException(e);
}
}
Aggregations