Skip to content

Commit

Permalink
fix slot grid when the amount of slots is a prime number (#195)
Browse files Browse the repository at this point in the history
* fix slot grid when the amount of slots is a prime number

* dont add slots indexes bigger than the handler

special case for 3 items, for aesthetic purposes.
  • Loading branch information
PrototypeTrousers authored Oct 22, 2021
1 parent d9052a3 commit ab47807
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/main/java/gregtech/api/recipes/RecipeMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ protected void addInventorySlotGroup(ModularUI.Builder builder, IItemHandlerModi
for (int i = 0; i < itemSlotsToDown; i++) {
for (int j = 0; j < itemSlotsToLeft; j++) {
int slotIndex = i * itemSlotsToLeft + j;
if (slotIndex >= itemInputsCount) break;
int x = startInputsX + 18 * j;
int y = startInputsY + 18 * i;
addSlot(builder, x, y, slotIndex, itemHandler, fluidHandler, invertFluids, isOutputs);
Expand Down Expand Up @@ -446,20 +447,27 @@ protected TextureArea[] getOverlaysForSlot(boolean isOutput, boolean isFluid, bo
}

protected static int[] determineSlotsGrid(int itemInputsCount) {
int itemSlotsToLeft = 0;
int itemSlotsToDown = 0;
int itemSlotsToLeft;
int itemSlotsToDown;
double sqrt = Math.sqrt(itemInputsCount);
if (sqrt % 1 == 0) { //check if square root is integer
//case for 1, 4, 9 slots - it's square inputs (the most common case)
//if the number of input has an integer root
//return it.
if (sqrt % 1 == 0) {
itemSlotsToLeft = itemSlotsToDown = (int) sqrt;
} else if (itemInputsCount % 3 == 0) {
//case for 3 and 6 slots - 3 by horizontal and i / 3 by vertical (common case too)
itemSlotsToDown = itemInputsCount / 3;
} else if (itemInputsCount == 3) {
itemSlotsToLeft = 3;
} else if (itemInputsCount % 2 == 0) {
//case for 2 inputs - 2 by horizontal and i / 3 by vertical (for 2 slots)
itemSlotsToDown = itemInputsCount / 2;
itemSlotsToLeft = 2;
itemSlotsToDown = 1;
}
else {
//if we couldn't fit all into a perfect square,
//increase the amount of slots to the left
itemSlotsToLeft = (int) Math.ceil(sqrt);
itemSlotsToDown = itemSlotsToLeft - 1;
//if we still can't fit all the slots in a grid,
//increase the amount of slots on the bottom
if (itemInputsCount > itemSlotsToLeft * itemSlotsToDown) {
itemSlotsToDown = itemSlotsToLeft;
}
}
return new int[]{itemSlotsToLeft, itemSlotsToDown};
}
Expand Down

0 comments on commit ab47807

Please sign in to comment.