Validation annotations to make itemstack shenanigans more obvious

This commit is contained in:
Bernhard Bonigl committed 2017-04-29 18:05:43 +02:00
1 parent ffc8db9286
commit eaee115cc5
7 files changed
+29 -14

No files matched your search

@@ -187,6 +187,7 @@ public abstract class BaseContainer<T extends TileEntity> extends Container {
return super.addSlotToContainer(slotIn);
}
@Nonnull
@Override
public ItemStack transferStackInSlot(EntityPlayer playerIn, int index) {
// we can only support inventory <-> playerInventory
@@ -229,7 +230,7 @@ public abstract class BaseContainer<T extends TileEntity> extends Container {
// Fix for a vanilla bug: doesn't take Slot.getMaxStackSize into account
@Override
protected boolean mergeItemStack(ItemStack stack, int startIndex, int endIndex, boolean useEndIndex) {
protected boolean mergeItemStack(@Nonnull ItemStack stack, int startIndex, int endIndex, boolean useEndIndex) {
boolean ret = mergeItemStackRefill(stack, startIndex, endIndex, useEndIndex);
if(!stack.isEmpty() && stack.getCount() > 0) {
ret |= mergeItemStackMove(stack, startIndex, endIndex, useEndIndex);
@@ -238,7 +239,7 @@ public abstract class BaseContainer<T extends TileEntity> extends Container {
}
// only refills items that are already present
protected boolean mergeItemStackRefill(ItemStack stack, int startIndex, int endIndex, boolean useEndIndex) {
protected boolean mergeItemStackRefill(@Nonnull ItemStack stack, int startIndex, int endIndex, boolean useEndIndex) {
if(stack.getCount() <= 0) {
return false;
}
@@ -293,7 +294,7 @@ public abstract class BaseContainer<T extends TileEntity> extends Container {
}
// only moves items into empty slots
protected boolean mergeItemStackMove(ItemStack stack, int startIndex, int endIndex, boolean useEndIndex) {
protected boolean mergeItemStackMove(@Nonnull ItemStack stack, int startIndex, int endIndex, boolean useEndIndex) {
if(stack.getCount() <= 0) {
return false;
}
@@ -109,6 +109,7 @@ public class ContainerMultiModule<T extends TileEntity & IInventory> extends Bas
super.onContainerClosed(playerIn);
}
@Nonnull
@Override
public ItemStack slotClick(int slotId, int dragType, ClickType type, EntityPlayer player) {
if(slotId == -999 && type == ClickType.QUICK_CRAFT) {
@@ -122,6 +123,7 @@ public class ContainerMultiModule<T extends TileEntity & IInventory> extends Bas
// More sophisticated version of the one in BaseContainer
// Takes submodules into account when shiftclicking!
@Nonnull
@Override
public ItemStack transferStackInSlot(EntityPlayer playerIn, int index) {
Slot slot = this.inventorySlots.get(index);
@@ -175,7 +177,8 @@ public class ContainerMultiModule<T extends TileEntity & IInventory> extends Bas
return notifySlotAfterTransfer(playerIn, itemstack, ret, slot);
}
protected ItemStack notifySlotAfterTransfer(EntityPlayer player, ItemStack stack, ItemStack original, Slot slot) {
@Nonnull
protected ItemStack notifySlotAfterTransfer(EntityPlayer player, @Nonnull ItemStack stack, @Nonnull ItemStack original, Slot slot) {
// notify slot
slot.onSlotChange(stack, original);
@@ -194,7 +197,7 @@ public class ContainerMultiModule<T extends TileEntity & IInventory> extends Bas
return original;
}
protected boolean moveToTileInventory(ItemStack itemstack) {
protected boolean moveToTileInventory(@Nonnull ItemStack itemstack) {
if(itemstack.isEmpty()) {
return false;
}
@@ -206,7 +209,7 @@ public class ContainerMultiModule<T extends TileEntity & IInventory> extends Bas
return !this.mergeItemStack(itemstack, 0, end, false);
}
protected boolean moveToPlayerInventory(ItemStack itemstack) {
protected boolean moveToPlayerInventory(@Nonnull ItemStack itemstack) {
if(itemstack.isEmpty()) {
return false;
}
@@ -215,7 +218,7 @@ public class ContainerMultiModule<T extends TileEntity & IInventory> extends Bas
.mergeItemStack(itemstack, playerInventoryStart, this.inventorySlots.size(), true);
}
protected boolean moveToAnyContainer(ItemStack itemstack, Collection<Container> containers) {
protected boolean moveToAnyContainer(@Nonnull ItemStack itemstack, Collection<Container> containers) {
if(itemstack.isEmpty()) {
return false;
}
@@ -229,13 +232,13 @@ public class ContainerMultiModule<T extends TileEntity & IInventory> extends Bas
return false;
}
protected boolean moveToContainer(ItemStack itemstack, Container container) {
protected boolean moveToContainer(@Nonnull ItemStack itemstack, Container container) {
Pair<Integer, Integer> range = subContainerSlotRanges.get(container);
return !this.mergeItemStack(itemstack, range.getLeft(), range.getRight(), false);
}
protected boolean refillAnyContainer(ItemStack itemstack, Collection<Container> containers) {
protected boolean refillAnyContainer(@Nonnull ItemStack itemstack, Collection<Container> containers) {
if(itemstack.isEmpty()) {
return false;
}
@@ -249,7 +252,7 @@ public class ContainerMultiModule<T extends TileEntity & IInventory> extends Bas
return false;
}
protected boolean refillContainer(ItemStack itemstack, Container container) {
protected boolean refillContainer(@Nonnull ItemStack itemstack, Container container) {
Pair<Integer, Integer> range = subContainerSlotRanges.get(container);
return !this.mergeItemStackRefill(itemstack, range.getLeft(), range.getRight(), false);
}
@@ -27,6 +27,7 @@ public class SlotCraftingCustom extends SlotCrafting {
}
@Override
@Nonnull
public ItemStack onTake(EntityPlayer playerIn, @Nonnull ItemStack stack) {
net.minecraftforge.fml.common.FMLCommonHandler.instance().firePlayerCraftingEvent(playerIn, stack, craftMatrix);
this.onCrafting(stack);
@@ -4,6 +4,8 @@ import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import javax.annotation.Nonnull;
/** Slot that can only be taken out of */
public class SlotOut extends Slot {
@@ -12,7 +14,7 @@ public class SlotOut extends Slot {
}
@Override
public boolean isItemValid(ItemStack stack) {
public boolean isItemValid(@Nonnull ItemStack stack) {
return false;
}
}
@@ -5,6 +5,8 @@ import net.minecraft.inventory.Slot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import javax.annotation.Nonnull;
public class SlotRestrictedItem extends Slot {
private final Item allowedItem;
@@ -15,7 +17,7 @@ public class SlotRestrictedItem extends Slot {
}
@Override
public boolean isItemValid(ItemStack stack) {
public boolean isItemValid(@Nonnull ItemStack stack) {
return !stack.isEmpty() && stack.getItem() == allowedItem;
}
}
@@ -44,11 +44,12 @@ public class SlotWrapper extends Slot {
}
@Override
public void putStack(ItemStack stack) {
public void putStack(@Nonnull ItemStack stack) {
parent.putStack(stack);
}
@Override
@Nonnull
public ItemStack onTake(EntityPlayer playerIn, ItemStack stack) {
parent.onTake(playerIn, stack);
@@ -56,6 +57,7 @@ public class SlotWrapper extends Slot {
}
@Override
@Nonnull
public ItemStack getStack() {
return parent.getStack();
}
@@ -68,6 +68,7 @@ public class TileInventory extends MantleTileEntity implements IInventory {
/* Inventory management */
@Nonnull
@Override
public ItemStack getStackInSlot(int slot) {
if(slot < 0 || slot >= inventory.size()) {
@@ -101,7 +102,7 @@ public class TileInventory extends MantleTileEntity implements IInventory {
}
@Override
public void setInventorySlotContents(int slot, ItemStack itemstack) {
public void setInventorySlotContents(int slot, @Nonnull ItemStack itemstack) {
if(slot < 0 || slot >= inventory.size()) {
return;
}
@@ -113,6 +114,7 @@ public class TileInventory extends MantleTileEntity implements IInventory {
}
}
@Nonnull
@Override
public ItemStack decrStackSize(int slot, int quantity) {
ItemStack itemStack = getStackInSlot(slot);
@@ -140,6 +142,7 @@ public class TileInventory extends MantleTileEntity implements IInventory {
return itemStack;
}
@Nonnull
@Override
public ItemStack removeStackFromSlot(int slot) {
ItemStack itemStack = getStackInSlot(slot);
@@ -275,6 +278,7 @@ public class TileInventory extends MantleTileEntity implements IInventory {
}
/* Default implementations of hardly used methods */
@Nonnull
public ItemStack getStackInSlotOnClosing(int slot) {
return ItemStack.EMPTY;
}