Skip to content

Commit fa3a577

Browse files
committed
Simplified keybind isolation
1 parent 5de7e26 commit fa3a577

File tree

11 files changed

+39
-175
lines changed

11 files changed

+39
-175
lines changed

common/src/main/java/dev/terminalmc/clientsort/client/ClientSort.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,13 @@ public static void afterConfigSaved(Config config) {
7070
InteractionManager.setTickRate(options.interactionInterval);
7171
// Update class cache
7272
PolicyManager.reloadPolicyClasses(options.classPolicies.keySet());
73+
// Isolate keybinds
74+
if (options().isolateKeybinds)
75+
KeybindManager.isolateKeybinds();
7376
}
7477

7578
public static void afterGameStart() {
76-
if (options().isolateKeybinds) {
77-
KeybindManager.configToKeybinds();
78-
}
79-
}
80-
81-
public static void afterKeyMapReset() {
82-
if (!options().isolateKeybinds) {
83-
KeybindManager.keybindsToConfig();
84-
}
79+
if (options().isolateKeybinds)
80+
KeybindManager.isolateKeybinds();
8581
}
8682
}

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

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,10 @@
1818

1919
import com.google.gson.Gson;
2020
import com.google.gson.GsonBuilder;
21-
import com.mojang.blaze3d.platform.InputConstants;
22-
import com.mojang.blaze3d.platform.InputConstants.Type;
2321
import dev.terminalmc.clientsort.client.ClientSort;
2422
import dev.terminalmc.clientsort.client.config.legacy.ButtonLayout;
2523
import dev.terminalmc.clientsort.client.inventory.operator.Operation;
2624
import dev.terminalmc.clientsort.client.order.SortOrder;
27-
import dev.terminalmc.clientsort.client.util.KeybindManager;
2825
import dev.terminalmc.clientsort.platform.Services;
2926
import net.minecraft.resources.ResourceLocation;
3027
import net.minecraft.world.entity.player.Inventory;
@@ -179,30 +176,9 @@ public enum ExtraSlotScope {
179176

180177
// Keybind options
181178

182-
public static final boolean isolateKeybindsDefault = false;
179+
public static final boolean isolateKeybindsDefault = true;
183180
public boolean isolateKeybinds = isolateKeybindsDefault;
184181

185-
public static final String editKeyDefault = InputConstants.UNKNOWN.getName();
186-
public String editKey = editKeyDefault;
187-
public static Validator<String> editKeyValidator = (val) ->
188-
KeybindManager.fromName(val).getName();
189-
190-
public static final String sortKeyDefault =
191-
Type.MOUSE.getOrCreate(InputConstants.MOUSE_BUTTON_MIDDLE).getName();
192-
public String sortKey = sortKeyDefault;
193-
public static Validator<String> sortKeyValidator = (val) ->
194-
KeybindManager.fromName(val).getName();
195-
196-
public static final String stackFillKeyDefault = InputConstants.UNKNOWN.getName();
197-
public String stackFillKey = stackFillKeyDefault;
198-
public static Validator<String> stackFillKeyValidator = (val) ->
199-
KeybindManager.fromName(val).getName();
200-
201-
public static final String transferKeyDefault = InputConstants.UNKNOWN.getName();
202-
public String transferKey = transferKeyDefault;
203-
public static Validator<String> transferKeyValidator = (val) ->
204-
KeybindManager.fromName(val).getName();
205-
206182
// Button options
207183

208184
public static final boolean showButtonsDefault = false;
@@ -431,14 +407,6 @@ private void validate() {
431407
Options.soundPitchMaxValidator.validate(options.soundPitchMax, options);
432408
options.soundVolume =
433409
Options.soundVolumeValidator.validate(options.soundVolume);
434-
options.editKey =
435-
Options.editKeyValidator.validate(options.editKey);
436-
options.sortKey =
437-
Options.sortKeyValidator.validate(options.sortKey);
438-
options.stackFillKey =
439-
Options.stackFillKeyValidator.validate(options.stackFillKey);
440-
options.transferKey =
441-
Options.transferKeyValidator.validate(options.transferKey);
442410
options.firstButtonOp =
443411
Options.firstButtonOpValidator.validate(options.firstButtonOp, options);
444412
options.secondButtonOp =

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

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -312,51 +312,42 @@ else if (val > Config.Options.SOUND_VOLUME_MAX)
312312
.setTooltip(localized("option", "isolateKeybinds.tooltip"))
313313
.setDefaultValue(Config.Options.isolateKeybindsDefault)
314314
.setSaveConsumer(val -> options.isolateKeybinds = val)
315-
.requireRestart()
316315
.build());
317316

318317
keybinds.addEntry((eb.startKeyCodeField(
319318
localized("key", "edit"),
320319
((KeyMappingAccessor) KeybindManager.EDIT_KEY).clientsort$getKey()
321320
)
322-
.setDefaultValue(KeybindManager.fromName(Options.editKeyDefault))
323-
.setKeySaveConsumer((key) -> {
324-
KeybindManager.bindKey(KeybindManager.EDIT_KEY, key);
325-
options.editKey = key.getName();
326-
})
321+
.setDefaultValue(KeybindManager.EDIT_KEY.getDefaultKey())
322+
.setKeySaveConsumer((key) ->
323+
KeybindManager.bindKey(KeybindManager.EDIT_KEY, key))
327324
.build()));
328325

329326
keybinds.addEntry((eb.startKeyCodeField(
330327
localized("key", "op.sort"),
331328
((KeyMappingAccessor) KeybindManager.SORT_KEY).clientsort$getKey()
332329
)
333-
.setDefaultValue(KeybindManager.fromName(Options.sortKeyDefault))
334-
.setKeySaveConsumer((key) -> {
335-
KeybindManager.bindKey(KeybindManager.SORT_KEY, key);
336-
options.sortKey = key.getName();
337-
})
330+
.setDefaultValue(KeybindManager.SORT_KEY.getDefaultKey())
331+
.setKeySaveConsumer((key) ->
332+
KeybindManager.bindKey(KeybindManager.SORT_KEY, key))
338333
.build()));
339334

340335
keybinds.addEntry((eb.startKeyCodeField(
341336
localized("key", "op.stackFill"),
342337
((KeyMappingAccessor) KeybindManager.STACK_FILL_KEY).clientsort$getKey()
343338
)
344-
.setDefaultValue(KeybindManager.fromName(Options.stackFillKeyDefault))
345-
.setKeySaveConsumer((key) -> {
346-
KeybindManager.bindKey(KeybindManager.STACK_FILL_KEY, key);
347-
options.stackFillKey = key.getName();
348-
})
339+
.setDefaultValue(KeybindManager.STACK_FILL_KEY.getDefaultKey())
340+
.setKeySaveConsumer((key) ->
341+
KeybindManager.bindKey(KeybindManager.STACK_FILL_KEY, key))
349342
.build()));
350343

351344
keybinds.addEntry((eb.startKeyCodeField(
352345
localized("key", "op.transfer"),
353346
((KeyMappingAccessor) KeybindManager.TRANSFER_KEY).clientsort$getKey()
354347
)
355-
.setDefaultValue(KeybindManager.fromName(Options.transferKeyDefault))
356-
.setKeySaveConsumer((key) -> {
357-
KeybindManager.bindKey(KeybindManager.TRANSFER_KEY, key);
358-
options.transferKey = key.getName();
359-
})
348+
.setDefaultValue(KeybindManager.TRANSFER_KEY.getDefaultKey())
349+
.setKeySaveConsumer((key) ->
350+
KeybindManager.bindKey(KeybindManager.TRANSFER_KEY, key))
360351
.build()));
361352

362353
ConfigCategory buttons = builder.getOrCreateCategory(localized("option", "buttons"));

common/src/main/java/dev/terminalmc/clientsort/client/util/KeybindManager.java

Lines changed: 9 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,14 @@
1717
package dev.terminalmc.clientsort.client.util;
1818

1919
import com.mojang.blaze3d.platform.InputConstants;
20-
import dev.terminalmc.clientsort.client.config.Config;
2120
import dev.terminalmc.clientsort.mixin.client.accessor.KeyMappingAccessor;
2221
import net.minecraft.client.KeyMapping;
2322
import net.minecraft.client.Minecraft;
24-
import org.jetbrains.annotations.Nullable;
2523

2624
import java.util.List;
2725

28-
import static dev.terminalmc.clientsort.client.config.Config.options;
2926
import static dev.terminalmc.clientsort.util.Localization.translationKey;
3027

31-
/**
32-
* To allow optionally isolating mod keybinds from MC keybinds, the value of all keybinds must be
33-
* synced between mod config and MC config.
34-
* <p>
35-
* If {@link Config.Options#isolateKeybinds} is {@code true}, the keybinds will be set initially
36-
* by MC when MC config is loaded. The mod config values will not be read at all.
37-
* <p>
38-
* If {@link Config.Options#isolateKeybinds} is {@code false}, keybinds will be set initially by
39-
* the mod after MC has finished loading. The mod config values will not be read again.
40-
* <p>
41-
* The mod config values are kept up-to-date with mod config changes by the CC option handers, and
42-
* with MC config changes by the {@link KeyMapping#resetMapping} listener.
43-
*/
4428
public class KeybindManager {
4529

4630
public static final KeyMapping EDIT_KEY = new KeyMapping(
@@ -75,55 +59,22 @@ public class KeybindManager {
7559
);
7660

7761
/**
78-
* Saves the keybind values to mod config.
62+
* Attempts to remove all mod keybinds from the MC keybind maps.
63+
* <p>
64+
* Does not affect the MC keybind options list, since that references
65+
* {@link net.minecraft.client.Options#keyMappings} which is (mostly) unrelated.
7966
*/
80-
public static void keybindsToConfig() {
81-
options().editKey = toName(EDIT_KEY);
82-
options().sortKey = toName(SORT_KEY);
83-
options().stackFillKey = toName(STACK_FILL_KEY);
84-
options().transferKey = toName(TRANSFER_KEY);
85-
Config.save();
86-
}
87-
88-
/**
89-
* Loads the keybind values from mod config.
90-
*/
91-
public static void configToKeybinds() {
92-
EDIT_KEY.setKey(fromName(options().editKey));
93-
SORT_KEY.setKey(fromName(options().sortKey));
94-
STACK_FILL_KEY.setKey(fromName(options().stackFillKey));
95-
TRANSFER_KEY.setKey(fromName(options().transferKey));
67+
public static void isolateKeybinds() {
68+
KEYBINDS.forEach((kb) -> KeyMappingAccessor.clientsort$getAll().remove(kb.getName()));
69+
KeyMapping.resetMapping();
9670
}
9771

9872
/**
9973
* Sets and saves the keybind. Does not affect mod config.
10074
*/
10175
public static void bindKey(KeyMapping keybind, InputConstants.Key key) {
10276
keybind.setKey(key);
103-
if (!options().isolateKeybinds) {
104-
KeyMapping.resetMapping();
105-
Minecraft.getInstance().options.save();
106-
}
107-
}
108-
109-
/**
110-
* @return the name of the keybind's current key.
111-
*/
112-
public static String toName(KeyMapping keybind) {
113-
return ((KeyMappingAccessor) keybind).clientsort$getKey().getName();
114-
}
115-
116-
/**
117-
* @return the key identified by the name, or {@link InputConstants#UNKNOWN} if the name is
118-
* invalid.
119-
*/
120-
public static InputConstants.Key fromName(@Nullable String name) {
121-
if (name == null)
122-
return InputConstants.UNKNOWN;
123-
try {
124-
return InputConstants.getKey(name);
125-
} catch (IllegalArgumentException e) {
126-
return InputConstants.UNKNOWN;
127-
}
77+
KeyMapping.resetMapping();
78+
Minecraft.getInstance().options.save();
12879
}
12980
}

common/src/main/java/dev/terminalmc/clientsort/mixin/client/KeyMappingMixin.java

Lines changed: 0 additions & 40 deletions
This file was deleted.

common/src/main/java/dev/terminalmc/clientsort/mixin/client/accessor/KeyMappingAccessor.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,16 @@
2121
import org.spongepowered.asm.mixin.Mixin;
2222
import org.spongepowered.asm.mixin.gen.Accessor;
2323

24+
import java.util.Map;
25+
2426
@Mixin(KeyMapping.class)
2527
public interface KeyMappingAccessor {
2628

29+
@Accessor("ALL")
30+
static Map<String, KeyMapping> clientsort$getAll() {
31+
throw new UnsupportedOperationException();
32+
}
33+
2734
@Accessor("key")
2835
Key clientsort$getKey();
2936
}

common/src/main/resources/assets/clientsort/lang/en_us.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888

8989
"option.clientsort.keybinds": "Keybinds",
9090
"option.clientsort.isolateKeybinds": "Isolate Keybinds",
91-
"option.clientsort.isolateKeybinds.tooltip": "If this is enabled, the keybinds will only be shown here and will not conflict with other keybinds outside of inventories. You must restart the game to apply changes to this option.",
91+
"option.clientsort.isolateKeybinds.tooltip": "If this is enabled, ClientSort will attempt to remove its own keybinds from Minecraft's map. This may help prevent conflicts, but may not be compatible with mods that alter the keybind system.",
9292

9393
"option.clientsort.buttons": "Buttons",
9494
"option.clientsort.showButtons": "Show Buttons",

common/src/main/resources/assets/clientsort/lang/ru_ru.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888

8989
"option.clientsort.keybinds": "Keybinds",
9090
"option.clientsort.isolateKeybinds": "Isolate Keybinds",
91-
"option.clientsort.isolateKeybinds.tooltip": "If this is enabled, the keybinds will only be shown here and will not conflict with other keybinds outside of inventories. You must restart the game to apply changes to this option.",
91+
"option.clientsort.isolateKeybinds.tooltip": "If this is enabled, ClientSort will attempt to remove its own keybinds from Minecraft's map. This may help prevent conflicts, but may not be compatible with mods that alter the keybind system.",
9292

9393
"option.clientsort.buttons": "Интерфейс",
9494
"option.clientsort.showButtons": "Показывать кнопки",

common/src/main/resources/clientsort.mixins.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"client": [
1212
"client.AbstractContainerScreenMixin",
1313
"client.ClientPacketListenerMixin",
14-
"client.KeyMappingMixin",
1514
"client.LocalPlayerMixin",
1615
"client.MinecraftMixin",
1716
"client.accessor.AbstractContainerScreenAccessor",

fabric/src/main/java/dev/terminalmc/clientsort/client/ClientSortFabric.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,12 @@
2525
import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents;
2626
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
2727

28-
import static dev.terminalmc.clientsort.client.config.Config.options;
29-
3028
public class ClientSortFabric implements ClientModInitializer {
3129

3230
@Override
3331
public void onInitializeClient() {
34-
if (!options().isolateKeybinds) {
35-
// Register all keybinds
36-
KeybindManager.KEYBINDS.forEach(KeyBindingHelper::registerKeyBinding);
37-
}
32+
// Register all keybinds
33+
KeybindManager.KEYBINDS.forEach(KeyBindingHelper::registerKeyBinding);
3834

3935
// Register after-tick event
4036
ClientTickEvents.END_CLIENT_TICK.register(ClientSort::afterClientTick);

0 commit comments

Comments
 (0)