Skip to content

Commit

Permalink
Fix pattern dividing (#246)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Robertz <[email protected]>
  • Loading branch information
Alexdoru and Dream-Master authored Dec 6, 2024
1 parent be6dfac commit 8b1dff3
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.minecraft.inventory.Slot;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;

import com.glodblock.github.FluidCraft;
Expand Down Expand Up @@ -116,10 +117,11 @@ protected void actionPerformed(final GuiButton btn) {
"PatternTerminal.Combine",
this.combineDisableBtn == btn ? "1" : "0"));
} else if (ModAndClassUtil.isDoubleButton && doubleBtn == btn) {
final int eventButton = Mouse.getEventButton();
FluidCraft.proxy.netHandler.sendToServer(
new CPacketFluidPatternTermBtns(
"PatternTerminal.Double",
Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) ? "1" : "0"));
(Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) ? "1" : "0") + (eventButton == 1 ? "1" : "0")));
} else if (ModAndClassUtil.isBeSubstitutionsButton && beSubstitutionsDisabledBtn == btn) {
FluidCraft.proxy.netHandler
.sendToServer(new CPacketFluidPatternTermBtns("PatternTerminal.beSubstitute", "1"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ private void setCraftingMode(final boolean craftingMode) {
}

@Override
public void doubleStacks(boolean isShift) {
public void doubleStacks(boolean isShift, boolean divide) {
if (!isCraftingMode()) {
super.doubleStacks(isShift);
super.doubleStacks(isShift, divide);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -537,54 +537,79 @@ public boolean useRealItems() {
return false;
}

static boolean canDouble(SlotFake[] slots, int mult) {
for (Slot s : slots) {
ItemStack st = s.getStack();
if (st != null) {
if (st.getItem() instanceof ItemFluidPacket) {
long result = (long) ItemFluidPacket.getFluidAmount(st) * mult;
if (result > Integer.MAX_VALUE) {
return false;
}
} else {
long result = (long) s.getStack().stackSize * mult;
if (result > Integer.MAX_VALUE) {
return false;
}
}
public void doubleStacks(boolean isShift, boolean divide) {
if (!isCraftingMode()) {
final int mult = (isShift ? 8 : 2) * (divide ? -1 : 1);
if (canMultiplyOrDivide(this.craftingSlots, mult) && canMultiplyOrDivide(this.outputSlots, mult)) {
multiplyOrDivideStacksInternal(this.craftingSlots, mult);
multiplyOrDivideStacksInternal(this.outputSlots, mult);
}
this.detectAndSendChanges();
}
return true;
}

static void doubleStacksInternal(SlotFake[] slots, int mult) {
List<SlotFake> enabledSlots = Arrays.stream(slots).filter(SlotFake::isEnabled).collect(Collectors.toList());
for (final Slot s : enabledSlots) {
ItemStack st = s.getStack();
if (st != null) {
static boolean canMultiplyOrDivide(SlotFake[] slots, int mult) {
if (mult > 0) {
for (Slot s : slots) {
ItemStack st = s.getStack();
if (st == null) continue;
final long count;
if (st.getItem() instanceof ItemFluidPacket) {
ItemFluidPacket.setFluidAmount(st, ItemFluidPacket.getFluidAmount(st) * mult);
count = ItemFluidPacket.getFluidAmount(st);
} else {
st.stackSize *= mult;
count = s.getStack().stackSize;
}
long result = count * mult;
if (result > Integer.MAX_VALUE) {
return false;
}
}
return true;
} else if (mult < 0) {
mult = Math.abs(mult);
for (Slot s : slots) {
ItemStack st = s.getStack();
if (st == null) continue;
final int count;
if (st.getItem() instanceof ItemFluidPacket) {
count = ItemFluidPacket.getFluidAmount(st);
} else {
count = s.getStack().stackSize;
}
if (count % mult != 0) {
return false;
}
}
return true;
}
return false;
}

public void doubleStacks(boolean isShift) {
if (!isCraftingMode()) {
if (isShift) {
if (canDouble(this.craftingSlots, 8) && canDouble(this.outputSlots, 8)) {
doubleStacksInternal(this.craftingSlots, 8);
doubleStacksInternal(this.outputSlots, 8);
static void multiplyOrDivideStacksInternal(SlotFake[] slots, int mult) {
List<SlotFake> enabledSlots = Arrays.stream(slots).filter(SlotFake::isEnabled).collect(Collectors.toList());
if (mult > 0) {
for (final Slot s : enabledSlots) {
ItemStack st = s.getStack();
if (st != null) {
if (st.getItem() instanceof ItemFluidPacket) {
ItemFluidPacket.setFluidAmount(st, ItemFluidPacket.getFluidAmount(st) * mult);
} else {
st.stackSize *= mult;
}
}
} else {
if (canDouble(this.craftingSlots, 2) && canDouble(this.outputSlots, 2)) {
doubleStacksInternal(this.craftingSlots, 2);
doubleStacksInternal(this.outputSlots, 2);
}
} else if (mult < 0) {
mult = Math.abs(mult);
for (final Slot s : enabledSlots) {
ItemStack st = s.getStack();
if (st != null) {
if (st.getItem() instanceof ItemFluidPacket) {
ItemFluidPacket.setFluidAmount(st, ItemFluidPacket.getFluidAmount(st) / mult);
} else {
st.stackSize /= mult;
}
}
}
this.detectAndSendChanges();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,22 @@ public IMessage onMessage(CPacketFluidPatternTermBtns message, MessageContext ct
case "PatternTerminal.Clear" -> cpt.clear();
case "PatternTerminal.Substitute" -> cpt.getPatternTerminal().setSubstitution(Value.equals("1"));
case "PatternTerminal.Invert" -> cpt.getPatternTerminal().setInverted(Value.equals("1"));
case "PatternTerminal.Double" -> cpt.doubleStacks(Value.equals("1"));
case "PatternTerminal.Double" -> cpt.doubleStacks(Value.charAt(0) == '1', Value.charAt(1) == '1');
case "PatternTerminal.Combine" -> cpt.getPatternTerminal().setCombineMode(Value.equals("1"));
case "PatternTerminal.beSubstitute" -> cpt.getPatternTerminal().setBeSubstitute(Value.equals("1"));
case "PatternTerminal.ActivePage" -> cpt.getPatternTerminal()
.setActivePage(Integer.parseInt(Value));
case "PatternTerminal.ActivePage" ->
cpt.getPatternTerminal().setActivePage(Integer.parseInt(Value));
case "PatternTerminal.Prioritize" -> {
switch (Value) {
case "0", "1" -> cpt.getPatternTerminal().setPrioritization(Value.equals("1"));
case "2" -> cpt.getPatternTerminal().sortCraftingItems();
}
}
case "PatternTerminal.AutoFillerPattern" -> cpt.getPatternTerminal()
.setAutoFillPattern(Value.equals("1"));
case "PatternTerminal.AutoFillerPattern" ->
cpt.getPatternTerminal().setAutoFillPattern(Value.equals("1"));
}
cpt.getPatternTerminal().saveSettings();
} else if (Name.startsWith("StorageBus.") && c instanceof ContainerFluidStorageBus) {
final ContainerFluidStorageBus ccw = (ContainerFluidStorageBus) c;
} else if (Name.startsWith("StorageBus.") && c instanceof ContainerFluidStorageBus ccw) {
if (Name.equals("StorageBus.Action")) {
if (Value.equals("Partition")) {
ccw.partition();
Expand Down

0 comments on commit 8b1dff3

Please sign in to comment.