Skip to content

Commit

Permalink
chore: refactor SingleItemStackHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
Elenterius committed Apr 2, 2024
1 parent 63e55a2 commit 446d146
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
import com.github.elenterius.biomancy.api.serum.SerumContainer;
import com.github.elenterius.biomancy.inventory.itemhandler.LargeSingleItemStackHandler;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.IItemHandler;

import javax.annotation.Nonnull;

public class InjectorItemInventory extends BaseInventory<LargeSingleItemStackHandler> {

private final ItemStack cachedInventoryHost;
Expand All @@ -18,14 +15,12 @@ private InjectorItemInventory(short maxSlotSize, ItemStack inventoryHost) {
itemHandler = new LargeSingleItemStackHandler(maxSlotSize) {

@Override
public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
Item item = stack.getItem();
return super.isItemValid(slot, stack) && item instanceof SerumContainer;
public boolean isItemValid(ItemStack stack) {
return stack.getItem() instanceof SerumContainer;
}

@Override
protected void onContentsChanged() {
super.onContentsChanged();
setChanged();
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import net.minecraft.util.Mth;
import net.minecraft.world.item.ItemStack;

import javax.annotation.Nonnull;

public class LargeSingleItemStackHandler extends SingleItemStackHandler {

public static final String ITEM_AMOUNT_TAG = "ItemAmount";
Expand Down Expand Up @@ -53,9 +51,8 @@ public void setStack(ItemStack stack) {
onContentsChanged();
}

@Nonnull
@Override
public ItemStack insertItem(int slot, @Nonnull ItemStack stackIn, boolean simulate) {
public ItemStack insertItem(int slot, ItemStack stackIn, boolean simulate) {
ItemStack remainder = internalInsertItem(slot, stackIn, simulate);
if (!simulate) {
itemAmount = (short) cachedStack.getCount();
Expand All @@ -64,7 +61,6 @@ public ItemStack insertItem(int slot, @Nonnull ItemStack stackIn, boolean simula
return remainder;
}

@Nonnull
@Override
public ItemStack extractItem(int slot, int amount, boolean simulate) {
ItemStack remainder = internalExtractItem(slot, amount, simulate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.ItemHandlerHelper;

import javax.annotation.Nonnull;

public class SingleItemStackHandler implements IItemHandler, IItemHandlerModifiable, INBTSerializable<CompoundTag> {

public static final String ITEM_TAG = "Item";
Expand All @@ -33,13 +31,13 @@ public int getSlotLimit(int slot) {
return cachedStack.getMaxStackSize();
}

public boolean isItemValid(@Nonnull ItemStack stack) {
return isItemValid(0, stack);
public boolean isItemValid(ItemStack stack) {
return true;
}

@Override
public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
return slot == 0;
public boolean isItemValid(int slot, ItemStack stack) {
return slot == 0 && isItemValid(stack);
}

public boolean isEmpty() {
Expand Down Expand Up @@ -73,39 +71,37 @@ public ItemStack getStack() {
return cachedStack;
}

@Nonnull
public void setStack(ItemStack stack) {
cachedStack = stack;
onContentsChanged();
}

@Override
public ItemStack getStackInSlot(int slot) {
validateSlotIndex(slot);
return cachedStack;
}

public void setStack(ItemStack stack) {
cachedStack = stack;
onContentsChanged();
}

@Override
public void setStackInSlot(int slot, @Nonnull ItemStack stack) {
public void setStackInSlot(int slot, ItemStack stack) {
validateSlotIndex(slot);
setStack(stack);
}

public ItemStack insertItem(@Nonnull ItemStack stack, boolean simulate) {
public ItemStack insertItem(ItemStack stack, boolean simulate) {
return insertItem(0, stack, simulate);
}

@Nonnull
@Override
public ItemStack insertItem(int slot, @Nonnull ItemStack stackIn, boolean simulate) {
public ItemStack insertItem(int slot, ItemStack stackIn, boolean simulate) {
ItemStack remainder = internalInsertItem(slot, stackIn, simulate);
if (!simulate) {
onContentsChanged();
}
return remainder;
}

protected ItemStack internalInsertItem(int slot, @Nonnull ItemStack stackIn, boolean simulate) {
protected ItemStack internalInsertItem(int slot, ItemStack stackIn, boolean simulate) {
validateSlotIndex(slot);
if (stackIn.isEmpty()) return ItemStack.EMPTY;
if (!isItemValid(slot, stackIn)) return stackIn;
Expand All @@ -130,7 +126,6 @@ public ItemStack extractItem(int amount, boolean simulate) {
return extractItem(0, amount, simulate);
}

@Nonnull
@Override
public ItemStack extractItem(int slot, int amount, boolean simulate) {
ItemStack remainder = internalExtractItem(slot, amount, simulate);
Expand Down

0 comments on commit 446d146

Please sign in to comment.