Skip to content

Commit

Permalink
move the actually expensive chemical insert checks to after the basic…
Browse files Browse the repository at this point in the history
… ones
  • Loading branch information
thiakil committed Aug 9, 2024
1 parent 177298d commit 4bce8f9
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions src/api/java/mekanism/api/chemical/BasicChemicalTank.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,35 +108,35 @@ private void setStack(STACK stack, boolean validateStack) {

@Override
public STACK insert(@NotNull STACK stack, Action action, AutomationType automationType) {
if (stack.isEmpty() || !isValid(stack) || !canInsert.test(stack.getChemical(), automationType)) {
//"Fail quick" if the given stack is empty, or we can never insert the chemical or currently are unable to insert it
boolean sameType = false;
if (stack.isEmpty() || !(isEmpty() || (sameType = isTypeEqual(stack)))) {
//"Fail quick" if the given stack is empty
return stack;
}
long needed = Math.min(getInsertRate(automationType), getNeeded());
if (needed <= 0) {
//Fail if we are a full tank or our rate is zero
return stack;
}
boolean sameType = false;
if (isEmpty() || (sameType = isTypeEqual(stack))) {
long toAdd = Math.min(stack.getAmount(), needed);
if (action.execute()) {
//If we want to actually insert the chemical, then update the current chemical
if (sameType) {
//We can just grow our stack by the amount we want to increase it
stored.grow(toAdd);
onContentsChanged();
} else {
//If we are not the same type then we have to copy the stack and set it
// Just set it unchecked as we have already validated it
// Note: this also will mark that the contents changed
setStackUnchecked(createStack(stack, toAdd));
}
if (!isValid(stack) || !canInsert.test(stack.getChemical(), automationType)) {
//we can never insert the chemical or currently are unable to insert it
return stack;
}
long toAdd = Math.min(stack.getAmount(), needed);
if (action.execute()) {
//If we want to actually insert the chemical, then update the current chemical
if (sameType) {
//We can just grow our stack by the amount we want to increase it
stored.grow(toAdd);
onContentsChanged();
} else {
//If we are not the same type then we have to copy the stack and set it
// Just set it unchecked as we have already validated it
// Note: this also will mark that the contents changed
setStackUnchecked(createStack(stack, toAdd));
}
return createStack(stack, stack.getAmount() - toAdd);
}
//If we didn't accept this chemical, then just return the given stack
return stack;
return createStack(stack, stack.getAmount() - toAdd);
}

@Override
Expand Down

0 comments on commit 4bce8f9

Please sign in to comment.