Skip to content

Commit 4bce8f9

Browse files
committed
move the actually expensive chemical insert checks to after the basic ones
1 parent 177298d commit 4bce8f9

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

src/api/java/mekanism/api/chemical/BasicChemicalTank.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -108,35 +108,35 @@ private void setStack(STACK stack, boolean validateStack) {
108108

109109
@Override
110110
public STACK insert(@NotNull STACK stack, Action action, AutomationType automationType) {
111-
if (stack.isEmpty() || !isValid(stack) || !canInsert.test(stack.getChemical(), automationType)) {
112-
//"Fail quick" if the given stack is empty, or we can never insert the chemical or currently are unable to insert it
111+
boolean sameType = false;
112+
if (stack.isEmpty() || !(isEmpty() || (sameType = isTypeEqual(stack)))) {
113+
//"Fail quick" if the given stack is empty
113114
return stack;
114115
}
115116
long needed = Math.min(getInsertRate(automationType), getNeeded());
116117
if (needed <= 0) {
117118
//Fail if we are a full tank or our rate is zero
118119
return stack;
119120
}
120-
boolean sameType = false;
121-
if (isEmpty() || (sameType = isTypeEqual(stack))) {
122-
long toAdd = Math.min(stack.getAmount(), needed);
123-
if (action.execute()) {
124-
//If we want to actually insert the chemical, then update the current chemical
125-
if (sameType) {
126-
//We can just grow our stack by the amount we want to increase it
127-
stored.grow(toAdd);
128-
onContentsChanged();
129-
} else {
130-
//If we are not the same type then we have to copy the stack and set it
131-
// Just set it unchecked as we have already validated it
132-
// Note: this also will mark that the contents changed
133-
setStackUnchecked(createStack(stack, toAdd));
134-
}
121+
if (!isValid(stack) || !canInsert.test(stack.getChemical(), automationType)) {
122+
//we can never insert the chemical or currently are unable to insert it
123+
return stack;
124+
}
125+
long toAdd = Math.min(stack.getAmount(), needed);
126+
if (action.execute()) {
127+
//If we want to actually insert the chemical, then update the current chemical
128+
if (sameType) {
129+
//We can just grow our stack by the amount we want to increase it
130+
stored.grow(toAdd);
131+
onContentsChanged();
132+
} else {
133+
//If we are not the same type then we have to copy the stack and set it
134+
// Just set it unchecked as we have already validated it
135+
// Note: this also will mark that the contents changed
136+
setStackUnchecked(createStack(stack, toAdd));
135137
}
136-
return createStack(stack, stack.getAmount() - toAdd);
137138
}
138-
//If we didn't accept this chemical, then just return the given stack
139-
return stack;
139+
return createStack(stack, stack.getAmount() - toAdd);
140140
}
141141

142142
@Override

0 commit comments

Comments
 (0)