Container
is a superset of IItemHandler
- almost every Container
(GUI with slots) in both modded and vanilla can "handle items".
And example of a block which is a Container
but isn't an IItemHandler
would be a liquid tank which has a GUI showing how full it is but doesn't have item slots (i.e. accept buckets).
-
-
Save Caellian/05bb0116c1c97e2bcc529737564507a4 to your computer and use it in GitHub Desktop.
MC v1.20.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected static class ContainerItemHandler implements Container { | |
@NonNull | |
IItemHandler itemHandler; | |
@Setter | |
@Nullable | |
IChangeHandler changeHandler; | |
public ContainerHandler(@NonNull IItemHandler handler, @NonNull IChangeHandler changeHandler) { | |
this.itemHandler = handler; | |
this.changeHandler = changeHandler; | |
} | |
private ContainerHandler(@NonNull IItemHandler handler) { | |
this(handler, null); | |
} | |
@Override | |
public int getContainerSize() { | |
return itemHandler.getSlots(); | |
} | |
@Override | |
public boolean isEmpty() { | |
for (int i = 0; i < this.getContainerSize(); i++) { | |
if (!itemHandler.getStackInSlot(i).isEmpty()) { | |
return false; | |
} | |
} | |
return true; | |
} | |
@Override | |
public ItemStack getItem(int pSlot) { | |
return itemHandler.getStackInSlot(pSlot); | |
} | |
@Override | |
public ItemStack removeItem(int pSlot, int pAmount) { | |
return itemHandler.extractItem(pSlot, pAmount, false); | |
} | |
@Override | |
public ItemStack removeItemNoUpdate(int pSlot) { | |
return itemHandler.getStackInSlot(pSlot).copyAndClear(); | |
} | |
@Override | |
public void setItem(int pSlot, ItemStack pStack) { | |
itemHandler.getStackInSlot(pSlot).setCount(0); | |
itemHandler.insertItem(pSlot, pStack, false); | |
} | |
@Override | |
public void setChanged() { | |
if (this.changeHandler != null) this.changeHandler.onChange(); | |
} | |
@Override | |
public boolean stillValid(Player pPlayer) { | |
return true; // IItemHandler is always valid, so Container is as well. | |
} | |
@Override | |
public void clearContent() { | |
for (int i = 0; i < this.getContainerSize(); i++) { | |
itemHandler.getStackInSlot(i).setCount(0); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment