Skip to content

Commit

Permalink
Add utility method to IInventorySlot that mimics our other handlers f…
Browse files Browse the repository at this point in the history
…or setting the contents to empty
  • Loading branch information
pupnewfster committed Dec 5, 2020
1 parent 3c45e00 commit 8f33f27
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/api/java/mekanism/api/MekanismAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private MekanismAPI() {
/**
* The version of the api classes - may not always match the mod's version
*/
public static final String API_VERSION = "10.0.14";
public static final String API_VERSION = "10.0.18";
public static final String MEKANISM_MODID = "mekanism";
/**
* Mekanism debug mode
Expand Down
4 changes: 4 additions & 0 deletions src/api/java/mekanism/api/chemical/BasicChemicalTank.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public void setStackUnchecked(STACK stack) {

private void setStack(STACK stack, boolean validateStack) {
if (stack.isEmpty()) {
if (stored.isEmpty()) {
//If we are already empty just exit, so as to not fire onContentsChanged
return;
}
stored = getEmptyStack();
} else if (!validateStack || isValid(stack)) {
stored = createStack(stack, stack.getAmount());
Expand Down
9 changes: 8 additions & 1 deletion src/api/java/mekanism/api/inventory/IInventorySlot.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ default int setStackSize(int amount, Action action) {
return 0;
} else if (amount <= 0) {
if (action.execute()) {
setStack(ItemStack.EMPTY);
setEmpty();
}
return 0;
}
Expand Down Expand Up @@ -274,6 +274,13 @@ default boolean isEmpty() {
return getStack().isEmpty();
}

/**
* Convenience method for emptying this {@link IInventorySlot}.
*/
default void setEmpty() {
setStack(ItemStack.EMPTY);
}

/**
* Convenience method for checking the size of the stack in this slot.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ private void vaporiseHohlraum() {
if (gasHandlerItem.getTanks() > 0) {
fuelTank.insert(gasHandlerItem.getChemicalInTank(0), Action.EXECUTE, AutomationType.INTERNAL);
lastPlasmaTemperature = getPlasmaTemp();
reactorSlot.setStack(ItemStack.EMPTY);
reactorSlot.setEmpty();
setBurning(true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ protected void setStackUnchecked(FluidStack stack) {

private void setStack(FluidStack stack, boolean validateStack) {
if (stack.isEmpty()) {
if (stored.isEmpty()) {
//If we are already empty just exit, so as to not fire onContentsChanged
return;
}
stored = FluidStack.EMPTY;
} else if (!validateStack || isFluidValid(stack)) {
stored = new FluidStack(stack, stack.getAmount());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ protected void setStackUnchecked(ItemStack stack) {

private void setStack(ItemStack stack, boolean validateStack) {
if (stack.isEmpty()) {
if (current.isEmpty()) {
//If we are already empty just exit, so as to not fire onContentsChanged
return;
}
current = ItemStack.EMPTY;
} else if (!validateStack || isItemValid(stack)) {
current = stack.copy();
Expand Down Expand Up @@ -233,7 +237,7 @@ public int setStackSize(int amount, Action action) {
return 0;
} else if (amount <= 0) {
if (action.execute()) {
setStack(ItemStack.EMPTY);
setEmpty();
}
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/mekanism/common/item/ItemConfigurator.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public ActionResultType onItemUse(ItemUseContext context) {
energyContainer.extract(energyPerItemDump, Action.EXECUTE, AutomationType.MANUAL);
}
Block.spawnAsEntity(world, pos, inventorySlot.getStack().copy());
inventorySlot.setStack(ItemStack.EMPTY);
inventorySlot.setEmpty();
}
}
return ActionResultType.SUCCESS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,9 +474,11 @@ private void organizeStock() {
//If we don't have the item stored anymore (already filled all previous slots with it),
// then we need to empty the slot as the items in it has been moved to a more "optimal" slot
//Note: We only set them to empty if they are not already empty to avoid onContentsChanged being called
// Technically our default implementation doesn't fire onContentsChanged if the stack was already empty
// but this is not an API contract
IInventorySlot slot = inputSlots.get(i);
if (!slot.isEmpty()) {
slot.setStack(ItemStack.EMPTY);
slot.setEmpty();
}
}
}
Expand All @@ -487,8 +489,10 @@ private void organizeStock() {
if (empty) {
//If we don't have any more items to sort, clear all the other slots that we haven't set something in
//Note: We only set them to empty if they are not already empty to avoid onContentsChanged being called
// Technically our default implementation doesn't fire onContentsChanged if the stack was already empty
// but this is not an API contract
if (!slot.isEmpty()) {
slot.setStack(ItemStack.EMPTY);
slot.setEmpty();
}
} else {
empty = setSlotIfChanged(storedMap, slot);
Expand Down

0 comments on commit 8f33f27

Please sign in to comment.