Skip to content

Commit 91bcb36

Browse files
committed
Add support for slot position anchoring
1 parent 8e32fef commit 91bcb36

File tree

11 files changed

+264
-158
lines changed

11 files changed

+264
-158
lines changed

common/src/main/java/dev/terminalmc/clientsort/client/config/ClassPolicy.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@
3434
public record ClassPolicy(
3535
@NotNull String className,
3636
@Nullable Vec2i buttonOffset,
37+
boolean offsetFromSlot,
3738
@NotNull Policy sortPolicy,
3839
@NotNull Policy stackFillPolicy,
3940
@NotNull Policy matchTransferPolicy,
4041
@NotNull Policy transferPolicy,
4142
@Nullable Operation autoOp,
42-
boolean autoOpOther,
43+
boolean autoOpOther, // better as reverseAutoOp but that requires a legacy upgrader
4344
@NotNull TreeSet<Integer> ignoredSlots
4445
) {
4546

@@ -115,16 +116,17 @@ public boolean autoTransfer() {
115116

116117
// Config data-string serialization
117118

118-
public static final String DATA_FORMAT = "%s,(%s),%s,%s,%s,%s,%d/%d,(%s)";
119+
public static final String DATA_FORMAT = "%s,(%s)/%d,%s,%s,%s,%s,%d/%d,(%s)";
119120
public static final String DATA_PATTERN_STRING =
120-
"^(.+),\\((?:(-?\\d+),(-?\\d+))?\\),([012]),([012]),([012]),([012]),([01234])/([01]),\\(((?:\\d+(?:,\\d+)*)?)\\)$";
121+
"^(.+),\\((?:(-?\\d+),(-?\\d+))?\\)/([01]),([012]),([012]),([012]),([012]),([01234])/([01]),\\(((?:\\d+(?:,\\d+)*)?)\\)$";
121122
public static final Pattern DATA_PATTERN = Pattern.compile(DATA_PATTERN_STRING);
122123

123124
public String toDataString() {
124125
return String.format(
125126
DATA_FORMAT,
126127
className,
127128
buttonOffset == null ? "" : buttonOffset.x() + "," + buttonOffset.y(),
129+
offsetFromSlot ? 1 : 0,
128130
sortPolicy.toSimpleString(),
129131
stackFillPolicy.toSimpleString(),
130132
matchTransferPolicy.toSimpleString(),
@@ -176,15 +178,16 @@ public static ClassPolicy fromDataString(
176178
Integer.parseInt(matcher.group(2)),
177179
Integer.parseInt(matcher.group(3))
178180
),
179-
Policy.fromSimpleString(matcher.group(4)),
181+
matcher.group(4).equals("1"),
180182
Policy.fromSimpleString(matcher.group(5)),
181183
Policy.fromSimpleString(matcher.group(6)),
182184
Policy.fromSimpleString(matcher.group(7)),
183-
matcher.group(8).equals("0")
185+
Policy.fromSimpleString(matcher.group(8)),
186+
matcher.group(9).equals("0")
184187
? null
185-
: Operation.values()[Integer.parseInt(matcher.group(8)) - 1],
186-
matcher.group(9).equals("1"),
187-
new TreeSet<>(Arrays.stream(matcher.group(10).split(","))
188+
: Operation.values()[Integer.parseInt(matcher.group(9)) - 1],
189+
matcher.group(10).equals("1"),
190+
new TreeSet<>(Arrays.stream(matcher.group(11).split(","))
188191
.filter((s) -> !s.isBlank())
189192
.map(Integer::parseInt).sorted().toList())
190193
);

common/src/main/java/dev/terminalmc/clientsort/client/config/Config.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ private static Operation validateUniqueOp(
304304
new ClassPolicy(
305305
Inventory.class.getName(),
306306
null,
307+
false,
307308
Policy.KEYBIND_BUTTON,
308309
Policy.KEYBIND,
309310
Policy.KEYBIND_BUTTON,
@@ -315,6 +316,7 @@ private static Operation validateUniqueOp(
315316
new ClassPolicy(
316317
ChestMenu.class.getName(),
317318
null,
319+
false,
318320
Policy.KEYBIND_BUTTON,
319321
Policy.KEYBIND_BUTTON,
320322
Policy.KEYBIND_BUTTON,
@@ -326,6 +328,7 @@ private static Operation validateUniqueOp(
326328
new ClassPolicy(
327329
HopperMenu.class.getName(),
328330
null,
331+
false,
329332
Policy.KEYBIND,
330333
Policy.KEYBIND,
331334
Policy.KEYBIND_BUTTON,
@@ -337,6 +340,7 @@ private static Operation validateUniqueOp(
337340
new ClassPolicy(
338341
HorseInventoryMenu.class.getName(),
339342
null,
343+
false,
340344
Policy.KEYBIND_BUTTON,
341345
Policy.KEYBIND,
342346
Policy.KEYBIND,
@@ -348,6 +352,7 @@ private static Operation validateUniqueOp(
348352
new ClassPolicy(
349353
PlayerEnderChestContainer.class.getName(),
350354
null,
355+
false,
351356
Policy.KEYBIND_BUTTON,
352357
Policy.KEYBIND_BUTTON,
353358
Policy.KEYBIND_BUTTON,
@@ -359,6 +364,7 @@ private static Operation validateUniqueOp(
359364
new ClassPolicy(
360365
ShulkerBoxMenu.class.getName(),
361366
null,
367+
false,
362368
Policy.KEYBIND_BUTTON,
363369
Policy.KEYBIND_BUTTON,
364370
Policy.KEYBIND_BUTTON,
@@ -370,6 +376,7 @@ private static Operation validateUniqueOp(
370376
new ClassPolicy(
371377
RandomizableContainerBlockEntity.class.getName(),
372378
null,
379+
false,
373380
Policy.KEYBIND_BUTTON,
374381
Policy.KEYBIND_BUTTON,
375382
Policy.KEYBIND_BUTTON,
@@ -381,6 +388,7 @@ private static Operation validateUniqueOp(
381388
new ClassPolicy(
382389
"com.simibubi.create.content.equipment.toolbox.ToolboxMenu",
383390
null,
391+
false,
384392
Policy.NONE,
385393
Policy.NONE,
386394
Policy.NONE,
@@ -392,6 +400,7 @@ private static Operation validateUniqueOp(
392400
new ClassPolicy(
393401
"com.tiviacz.travelersbackpack.inventory.menu.BackpackSettingsMenu",
394402
null,
403+
false,
395404
Policy.NONE,
396405
Policy.NONE,
397406
Policy.NONE,
@@ -403,6 +412,7 @@ private static Operation validateUniqueOp(
403412
new ClassPolicy(
404413
"com.tom.storagemod.menu.CraftingTerminalMenu",
405414
null,
415+
false,
406416
Policy.NONE,
407417
Policy.NONE,
408418
Policy.NONE,
@@ -414,6 +424,7 @@ private static Operation validateUniqueOp(
414424
new ClassPolicy(
415425
"com.tom.storagemod.menu.StorageTerminalMenu",
416426
null,
427+
false,
417428
Policy.NONE,
418429
Policy.NONE,
419430
Policy.NONE,
@@ -441,6 +452,7 @@ private static Operation validateUniqueOp(
441452
new ClassPolicy(
442453
cp.className(),
443454
cp.buttonOffset(),
455+
cp.offsetFromSlot(),
444456
Options.policyValidator.validate(cp.sortPolicy()),
445457
Options.policyValidator.validate(cp.stackFillPolicy()),
446458
Options.policyValidator.validate(cp.matchTransferPolicy()),
@@ -554,6 +566,7 @@ private void upgradeLegacy() {
554566
new ClassPolicy(
555567
bl.className(),
556568
bl.offset(),
569+
false,
557570
Boolean.TRUE.equals(bl.sortEnabled())
558571
? Policy.KEYBIND_BUTTON : Policy.KEYBIND,
559572
Boolean.TRUE.equals(bl.stackFillEnabled())

common/src/main/java/dev/terminalmc/clientsort/client/gui/TriggerButtonManager.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import dev.terminalmc.clientsort.client.inventory.helper.ContainerScreenHelper;
3030
import dev.terminalmc.clientsort.client.util.KeybindManager;
3131
import dev.terminalmc.clientsort.client.util.PolicyManager;
32-
import dev.terminalmc.clientsort.mixin.client.accessor.AbstractContainerScreenAccessor;
3332
import dev.terminalmc.clientsort.mixin.client.accessor.ScreenAccessor;
3433
import net.minecraft.client.Minecraft;
3534
import net.minecraft.client.gui.screens.Screen;
@@ -476,16 +475,28 @@ public ScoredContainer(Container container, int score) {
476475
Scope scope = isPlayerInv ? Scope.PLAYER_INV : Scope.CONTAINER_INV;
477476
for (Slot slot : helper.getLargestSlotGroup(scope)) {
478477
// Calculate the weighted positional score
478+
479+
// x factor is how far from the left side of the menu the slot is, as a fraction
480+
// of the screen width
481+
double xFactor = Math.clamp(slot.x, 0, screen.width) / (double) screen.width;
482+
// y factor is how far from the top of the menu the slot is, as a fraction of the
483+
// screen height
484+
double yFactor = Math.clamp(slot.y, 0, screen.height) / (double) screen.height;
485+
479486
double x;
487+
double y;
480488
if (anchorButtonsLeft) {
481-
x = Math.clamp(slot.x, 0, screen.width) / (double) screen.width;
489+
// Prefer further left
490+
x = 1 - xFactor;
482491
} else {
483-
x = ((AbstractContainerScreenAccessor) screen).clientsort$getImageWidth()
484-
- Math.clamp(slot.x, 0, screen.width) / (double) screen.width;
492+
// Prefer further right
493+
x = xFactor;
485494
}
486-
double y = (screen.height - Math.clamp(slot.y, 0, screen.height))
487-
/ (double) screen.height;
488-
double score = x * 0.8D + y * 0.2D;
495+
// Prefer higher up
496+
y = 1 - yFactor;
497+
498+
// Assign weights
499+
double score = x * 0.8d + y * 0.2d;
489500

490501
if (score > bestScore) {
491502
bestSlot = slot;

common/src/main/java/dev/terminalmc/clientsort/client/gui/screen/config/ClothScreenProvider.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,10 @@ else if (val > Config.Options.SOUND_VOLUME_MAX)
648648
eb.startTextDescription(localized("option", "policies.description.9")).build());
649649
policiesInstructions.add(
650650
eb.startTextDescription(localized("option", "policies.description.10")).build());
651+
policiesInstructions.add(
652+
eb.startTextDescription(localized("option", "policies.description.11")).build());
653+
policiesInstructions.add(
654+
eb.startTextDescription(localized("option", "policies.description.12")).build());
651655

652656
policies.addEntry(policiesInstructions.build());
653657

0 commit comments

Comments
 (0)