Skip to content

Commit

Permalink
Flooded the workplace with a good dose of dupe-be-gone(tm)
Browse files Browse the repository at this point in the history
-- um, is that stuff toxic?
  • Loading branch information
HenryLoenwind committed Dec 1, 2016
1 parent b987736 commit b33d6e7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import javax.annotation.Nullable;

import crazypants.util.Prep;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
Expand Down Expand Up @@ -106,7 +107,7 @@ public void readFromNBT(NBTTagCompound tag) {

public void setOwner(TileEntity owner) {
this.owner = owner;
for (InventorySlot slot : allSlots) {
for (InventorySlot slot : idents.values()) {
slot.setOwner(owner);
}
}
Expand Down Expand Up @@ -159,7 +160,7 @@ public ItemStack getStackInSlot(int slot) {
return inventorySlot.getStackInSlot(0);
}
}
return null;
return Prep.getEmpty();
}

@Override
Expand All @@ -181,7 +182,7 @@ public ItemStack extractItem(int slot, int amount, boolean simulate) {
return inventorySlot.extractItem(0, amount, simulate);
}
}
return null;
return Prep.getEmpty();
}

@Override
Expand Down
43 changes: 26 additions & 17 deletions src/main/java/crazypants/enderio/capability/InventorySlot.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

public class InventorySlot implements IItemHandler {

private ItemStack itemStack;
private ItemStack itemStack = Prep.getEmpty();
private final @Nonnull Predicate<ItemStack> filterIn, filterOut;
private final @Nonnull Callback<ItemStack> callback;
private final int limit;
Expand Down Expand Up @@ -95,7 +95,7 @@ public int getSlots() {

@Override
public ItemStack getStackInSlot(int slot) {
return slot == 0 ? itemStack : null;
return slot == 0 ? itemStack : Prep.getEmpty();
}

public boolean isItemValidForSlot(ItemStack stack) {
Expand All @@ -105,20 +105,20 @@ public boolean isItemValidForSlot(ItemStack stack) {
@Override
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) {
if (slot == 0 && filterIn.apply(stack)) {
if (itemStack == null) {
if (Prep.isInvalid(itemStack)) {
int max = Math.min(limit, stack.getMaxStackSize());
if (!simulate) {
itemStack = stack.copy();
}
if (stack.stackSize <= max) {
if (!simulate) {
onChange(null, itemStack);
onChange(Prep.getEmpty(), itemStack);
}
return null;
return Prep.getEmpty();
}
if (!simulate) {
itemStack.stackSize = max;
onChange(null, itemStack);
onChange(Prep.getEmpty(), itemStack);
}
ItemStack result = stack.copy();
result.stackSize -= max;
Expand All @@ -133,15 +133,16 @@ public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) {
itemStack.stackSize = target;
onChange(oldStack, itemStack);
}
return null;
return Prep.getEmpty();
}
int tomove = max - itemStack.stackSize;
if (!simulate) {
ItemStack oldStack = itemStack.copy();
itemStack.stackSize = max;
onChange(oldStack, itemStack);
}
ItemStack result = stack.copy();
result.stackSize -= max - target;
result.stackSize -= tomove;
return result;
}
}
Expand All @@ -150,24 +151,32 @@ public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) {

@Override
public ItemStack extractItem(int slot, int amount, boolean simulate) {
if (slot == 0 && itemStack != null && filterOut.apply(itemStack)) {
ItemStack result = itemStack.copy();
if (slot == 0 && Prep.isValid(itemStack) && filterOut.apply(itemStack)) {
if (amount >= itemStack.stackSize) {
if (!simulate) {
onChange(itemStack, null);
itemStack = null;
ItemStack oldStack = itemStack;
itemStack = Prep.getEmpty();
;
onChange(oldStack, itemStack);
return oldStack;
} else {
return itemStack.copy();
}
} else {
if (!simulate) {
ItemStack oldStack = itemStack.copy();
itemStack.stackSize -= amount;
onChange(oldStack, itemStack);
oldStack.stackSize = amount;
return oldStack;
} else {
ItemStack result = itemStack.copy();
result.stackSize = amount;
return result;
}
result.stackSize = amount;
}
return result;
}
return null;
return Prep.getEmpty();
}

private void onChange(ItemStack oldStack, ItemStack newStack) {
Expand All @@ -178,7 +187,7 @@ private void onChange(ItemStack oldStack, ItemStack newStack) {
}

public void writeToNBT(NBTTagCompound tag) {
if (itemStack != null) {
if (Prep.isValid(itemStack)) {
itemStack.writeToNBT(tag);
}
}
Expand All @@ -188,7 +197,7 @@ public void readFromNBT(NBTTagCompound tag) {
}

public void clear() {
itemStack = null;
itemStack = Prep.getEmpty();
}

public void set(ItemStack stack) {
Expand Down

0 comments on commit b33d6e7

Please sign in to comment.