Files
MinecraftForge/patches/minecraft/net/minecraft/client/Options.java.patch
T
2026-06-17 19:35:27 -07:00

145 lines
6.6 KiB
Diff

--- a/net/minecraft/client/Options.java
+++ b/net/minecraft/client/Options.java
@@ -986,6 +_,7 @@
);
public boolean syncWrites;
public boolean startedCleanly = true;
+ private final Map<String, String> unknownKeys = new java.util.HashMap<>();
public static boolean isSoundDeviceDefault(final String deviceName) {
return deviceName.equals("");
@@ -1474,6 +_,7 @@
}
public Options(final Minecraft minecraft, final File workingDirectory) {
+ setForgeKeybindProperties();
this.minecraft = minecraft;
this.optionsFile = new File(workingDirectory, "options.txt");
boolean largeDistances = Runtime.getRuntime().maxMemory() >= 1000000000L;
@@ -1624,15 +_,28 @@
this.startedCleanly = access.process("startedCleanly", this.startedCleanly);
access.process("musicToast", this.musicToast);
access.process("musicFrequency", this.musicFrequency);
+ processOptionsKeysOnly(access);
+ processOptionsEnd(access);
+ }
+ // FORGE: split off to allow reloading keys after mod loading is done
+ private void processOptionsKeysOnly(Options.FieldAccess access) {
for (KeyMapping keyMapping : this.keyMappings) {
- String currentValue = keyMapping.saveString();
+ String currentValue = keyMapping.saveString() + (keyMapping.getKeyModifier() != net.minecraftforge.client.settings.KeyModifier.NONE ? ":" + keyMapping.getKeyModifier() : "");
String newValue = access.process("key_" + keyMapping.getName(), currentValue);
if (!currentValue.equals(newValue)) {
keyMapping.setKey(InputConstants.getKey(newValue));
+ if (newValue.indexOf(':') != -1) {
+ String[] pts = newValue.split(":");
+ keyMapping.setKeyModifierAndCode(net.minecraftforge.client.settings.KeyModifier.valueFromString(pts[1]), InputConstants.getKey(pts[0]));
+ } else {
+ keyMapping.setKeyModifierAndCode(net.minecraftforge.client.settings.KeyModifier.NONE, InputConstants.getKey(newValue));
+ }
}
}
+ }
+ private void processOptionsEnd(Options.FieldAccess access) {
for (SoundSource source : SoundSource.values()) {
access.process("soundCategory_" + source.getName(), this.soundSourceVolumes.get(source));
}
@@ -1647,6 +_,10 @@
}
public void load() {
+ load(false);
+ }
+
+ public void load(boolean limited) {
try {
if (!this.optionsFile.exists()) {
return;
@@ -1666,7 +_,8 @@
}
final CompoundTag options = this.dataFix(rawOptions);
- this.processOptions(
+ java.util.function.Consumer<FieldAccess> processor = limited ? this::processOptionsKeysOnly : this::processOptions;
+ processor.accept(
new Options.FieldAccess() {
private @Nullable String getValue(final String name) {
Tag tag = options.get(name);
@@ -1748,6 +_,17 @@
);
options.getString("fullscreenResolution").ifPresent(fullscreenResolution -> this.fullscreenVideoModeString = fullscreenResolution);
KeyMapping.resetMapping();
+
+ if (limited) {
+ this.unknownKeys.clear();
+ } else {
+ var knownKeys = Arrays.stream(this.keyMappings).map(k -> "key_" + k.getName()).collect(Collectors.toSet());
+ for (var entry : options.entrySet()) {
+ if (entry.getKey().startsWith("key_") && entry.getValue().asString().isPresent() && !knownKeys.contains(entry.getKey()))
+ this.unknownKeys.put(entry.getKey(), entry.getValue().asString().get());
+ }
+ }
+
} catch (Exception e) {
LOGGER.error("Failed to load options", e);
}
@@ -1778,9 +_,11 @@
public void save() {
try (final PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(this.optionsFile), StandardCharsets.UTF_8))) {
writer.println("version:" + SharedConstants.getCurrentVersion().dataVersion().version());
+ var seen = new java.util.HashSet<String>();
this.processOptions(
new Options.FieldAccess() {
public void writePrefix(final String name) {
+ seen.add(name);
writer.print(name);
writer.print(':');
}
@@ -1836,6 +_,12 @@
if (fullscreenVideoModeString != null) {
writer.println("fullscreenResolution:" + fullscreenVideoModeString);
}
+ // Forge add any unknown keys so that Mods can add keybindings between vanilla loading/saving its config.
+ for (var entry : this.unknownKeys.entrySet()) {
+ if (!seen.contains(entry.getKey()))
+ writer.println(entry.getKey() + ":" + entry.getValue());
+ }
+
} catch (Exception e) {
LOGGER.error("Failed to save options", e);
}
@@ -1873,6 +_,7 @@
}
public void broadcastOptions() {
+ if (net.minecraftforge.client.loading.ClientModLoader.isLoading()) return; //Don't save settings before mods add keybindigns and the like to prevent them from being deleted.
if (this.minecraft.player != null) {
this.minecraft.player.connection.broadcastClientInformation(this.buildPlayerInformation());
}
@@ -1986,6 +_,23 @@
public static Component genericValueLabel(final Component caption, final int value) {
return genericValueLabel(caption, Component.literal(Integer.toString(value)));
+ }
+
+ private void setForgeKeybindProperties() {
+ var inGame = net.minecraftforge.client.settings.KeyConflictContext.IN_GAME;
+ keyUp.setKeyConflictContext(inGame);
+ keyLeft.setKeyConflictContext(inGame);
+ keyDown.setKeyConflictContext(inGame);
+ keyRight.setKeyConflictContext(inGame);
+ keyJump.setKeyConflictContext(inGame);
+ keyShift.setKeyConflictContext(inGame);
+ keySprint.setKeyConflictContext(inGame);
+ keyAttack.setKeyConflictContext(inGame);
+ keyChat.setKeyConflictContext(inGame);
+ keyPlayerList.setKeyConflictContext(inGame);
+ keyCommand.setKeyConflictContext(inGame);
+ keyTogglePerspective.setKeyConflictContext(inGame);
+ keySmoothCamera.setKeyConflictContext(inGame);
}
public static Component genericValueOrOffLabel(final Component caption, final int value) {