Skip to content

Commit 0363328

Browse files
committed
persist reactionTicks to config file (config/aimless.json)
1 parent fc8dfe5 commit 0363328

2 files changed

Lines changed: 54 additions & 6 deletions

File tree

src/client/java/org/codersoft/mohenjo/aimless/client/AimlessClient.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class AimlessClient implements ClientModInitializer {
2323
private static KeyMapping aimKeyBind;
2424

2525
private static final double MAX_RANGE = 3.0;
26-
static int reactionTicks = 6;
26+
private static final AimlessConfig CONFIG = AimlessConfig.load();
2727
private int tickCounter = 0;
2828
private boolean aiming = false;
2929

@@ -40,13 +40,14 @@ public void onInitializeClient() {
4040
dispatcher.register(ClientCommands.literal("aimless")
4141
.then(ClientCommands.argument("ticks", IntegerArgumentType.integer(1, 100))
4242
.executes(ctx -> {
43-
reactionTicks = IntegerArgumentType.getInteger(ctx, "ticks");
44-
ctx.getSource().sendFeedback(Component.literal("§aReaction ticks set to " + reactionTicks));
43+
int value = IntegerArgumentType.getInteger(ctx, "ticks");
44+
CONFIG.setReactionTicks(value);
45+
ctx.getSource().sendFeedback(Component.literal("§aReaction ticks set to " + value));
4546
return 1;
4647
})
4748
)
4849
.executes(ctx -> {
49-
ctx.getSource().sendFeedback(Component.literal("§eReaction ticks: " + reactionTicks));
50+
ctx.getSource().sendFeedback(Component.literal("§eReaction ticks: " + CONFIG.getReactionTicks()));
5051
return 1;
5152
})
5253
)
@@ -60,7 +61,7 @@ public void onInitializeClient() {
6061

6162
if (aimKeyBind.consumeClick()) {
6263
aiming = !aiming;
63-
player.sendOverlayMessage(Component.literal(aiming ? "Aimless §aEnabled §7(rt: " + reactionTicks + ")" : "Aimless §cDisabled"));
64+
player.sendOverlayMessage(Component.literal(aiming ? "Aimless §aEnabled §7(rt: " + CONFIG.getReactionTicks() + ")" : "Aimless §cDisabled"));
6465
}
6566

6667
if (!aiming) {
@@ -69,7 +70,7 @@ public void onInitializeClient() {
6970
}
7071

7172
tickCounter++;
72-
if (tickCounter < reactionTicks) return;
73+
if (tickCounter < CONFIG.getReactionTicks()) return;
7374
tickCounter = 0;
7475

7576
Player closestTarget = findClosestPlayer(player, level);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.codersoft.mohenjo.aimless.client;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
5+
import net.fabricmc.loader.api.FabricLoader;
6+
7+
import java.io.IOException;
8+
import java.io.Reader;
9+
import java.io.Writer;
10+
import java.nio.file.Files;
11+
import java.nio.file.Path;
12+
13+
public class AimlessConfig {
14+
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
15+
private static final Path CONFIG_PATH = FabricLoader.getInstance().getConfigDir().resolve("aimless.json");
16+
17+
private int reactionTicks = 6;
18+
19+
public int getReactionTicks() {
20+
return reactionTicks;
21+
}
22+
23+
public void setReactionTicks(int reactionTicks) {
24+
this.reactionTicks = reactionTicks;
25+
save();
26+
}
27+
28+
public static AimlessConfig load() {
29+
if (Files.exists(CONFIG_PATH)) {
30+
try (Reader reader = Files.newBufferedReader(CONFIG_PATH)) {
31+
return GSON.fromJson(reader, AimlessConfig.class);
32+
} catch (IOException ignored) {
33+
}
34+
}
35+
return new AimlessConfig();
36+
}
37+
38+
public void save() {
39+
try {
40+
Files.createDirectories(CONFIG_PATH.getParent());
41+
try (Writer writer = Files.newBufferedWriter(CONFIG_PATH)) {
42+
GSON.toJson(this, writer);
43+
}
44+
} catch (IOException ignored) {
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)