Skip to content

Commit

Permalink
Simplified the code
Browse files Browse the repository at this point in the history
  • Loading branch information
Edivad99 committed Oct 11, 2023
1 parent 0edffb4 commit 5b6515b
Showing 1 changed file with 20 additions and 117 deletions.
137 changes: 20 additions & 117 deletions src/main/java/mods/railcraft/util/container/CombinedInvWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,168 +4,71 @@
import net.minecraft.world.Container;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.ItemHandlerHelper;
import net.minecraftforge.items.wrapper.InvWrapper;

public class CombinedInvWrapper implements IItemHandlerModifiable {

private final Container inputInv;
private final Container outputInv;
private final InvWrapper inputInv;
private final InvWrapper outputInv;

public CombinedInvWrapper(Container inputInv, Container outputInv) {
this.inputInv = inputInv;
this.outputInv = outputInv;
this.inputInv = new InvWrapper(inputInv);
this.outputInv = new InvWrapper(outputInv);
}

@Override
public void setStackInSlot(int slot, @NotNull ItemStack stack) {
if (slot < this.inputInv.getContainerSize()) {
this.inputInv.setItem(slot, stack);
if (slot < this.inputInv.getSlots()) {
this.inputInv.setStackInSlot(slot, stack);
}
}

@Override
public int getSlots() {
return this.inputInv.getContainerSize() + this.outputInv.getContainerSize();
return this.inputInv.getSlots() + this.outputInv.getSlots();
}

@Override
@NotNull
public ItemStack getStackInSlot(int slot) {
if (slot < this.inputInv.getContainerSize()) {
return this.inputInv.getItem(slot);
if (slot < this.inputInv.getSlots()) {
return this.inputInv.getStackInSlot(slot);
} else {
return this.outputInv.getItem(slot - this.inputInv.getContainerSize());
return this.outputInv.getStackInSlot(slot - this.inputInv.getSlots());
}
}

@Override
@NotNull
public ItemStack insertItem(int slot, @NotNull ItemStack stack, boolean simulate) {
if (slot >= this.inputInv.getContainerSize()) {
if (slot >= this.inputInv.getSlots()) {
return stack;
}

if (stack.isEmpty()) {
return ItemStack.EMPTY;
}

ItemStack stackInSlot = this.inputInv.getItem(slot);

int m;
if (!stackInSlot.isEmpty()) {
if (stackInSlot.getCount() >= Math.min(stackInSlot.getMaxStackSize(), getSlotLimit(slot))) {
return stack;
}

if (!ItemHandlerHelper.canItemStacksStack(stack, stackInSlot)) {
return stack;
}

if (!this.inputInv.canPlaceItem(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());
this.inputInv.setItem(slot, copy);
this.inputInv.setChanged();
}

return ItemStack.EMPTY;
} else {
// copy the stack to not modify the original one
stack = stack.copy();
if (!simulate) {
ItemStack copy = stack.split(m);
copy.grow(stackInSlot.getCount());
this.inputInv.setItem(slot, copy);
this.inputInv.setChanged();
return stack;
} else {
stack.shrink(m);
return stack;
}
}
} else {
if (!this.inputInv.canPlaceItem(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) {
this.inputInv.setItem(slot, stack.split(m));
this.inputInv.setChanged();
return stack;
} else {
stack.shrink(m);
return stack;
}
} else {
if (!simulate) {
this.inputInv.setItem(slot, stack);
this.inputInv.setChanged();
}
return ItemStack.EMPTY;
}
}
return this.inputInv.insertItem(slot, stack, simulate);
}

@Override
@NotNull
public ItemStack extractItem(int slot, int amount, boolean simulate) {
if (slot < this.inputInv.getContainerSize()) {
if (slot < this.inputInv.getSlots()) {
return ItemStack.EMPTY;
}

slot = slot - this.inputInv.getContainerSize();

if (amount == 0) {
return ItemStack.EMPTY;
}

ItemStack stackInSlot = this.outputInv.getItem(slot);

if (stackInSlot.isEmpty()) {
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);

ItemStack decrStackSize = this.outputInv.removeItem(slot, m);
this.outputInv.setChanged();
return decrStackSize;
}
return this.outputInv.extractItem(slot - this.inputInv.getSlots(), amount, simulate);
}

@Override
public int getSlotLimit(int slot) {
if (slot < this.inputInv.getContainerSize()) {
return this.inputInv.getMaxStackSize();
if (slot < this.inputInv.getSlots()) {
return this.inputInv.getSlotLimit(slot);
} else {
return this.outputInv.getMaxStackSize();
return this.outputInv.getSlotLimit(slot - this.inputInv.getSlots());
}
}

@Override
public boolean isItemValid(int slot, @NotNull ItemStack stack) {
if (slot < this.inputInv.getContainerSize()) {
return this.inputInv.canPlaceItem(slot, stack);
if (slot < this.inputInv.getSlots()) {
return this.inputInv.isItemValid(slot, stack);
} else {
return false;
}
Expand Down

0 comments on commit 5b6515b

Please sign in to comment.