1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
package com.arslaancodes.arslaansmagichax.modules;
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
import com.arslaancodes.arslaansmagichax.ArslaansMagicHax;
import com.arslaancodes.arslaansmagichax.ArslaansMagicHaxClient;
import com.arslaancodes.arslaansmagichax.modules.settings.Setting;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.render.RenderTickCounter;
import net.minecraft.client.util.InputUtil;
import net.minecraft.text.Text;
import net.minecraft.util.math.MathHelper;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
public class ModuleManager {
private boolean hudRegistered = false;
private static final String CONFIG_FILE = "ArslaansMagicHax.properties";
private static Map<Module, Boolean> previousKeyState = new HashMap<>();
public static void handleKeyPresses() {
MinecraftClient client = MinecraftClient.getInstance();
if (client.player == null || client.currentScreen != null) return;
for (Module module : modules) {
int key = module.getGlfwKeybinding();
if (key == -1) continue;
boolean isPressed = InputUtil.isKeyPressed(client.getWindow().getHandle(), key);
boolean wasPressed = previousKeyState.getOrDefault(module, false);
if (isPressed && !wasPressed) {
boolean wasEnabled = module.isEnabled(); // use !wasEnabled in the chat message for edge cases e.g. modules that toggle themselves off on enable
module.toggle();
if (!Objects.equals(module.getName(), "ClickGUI")) {
client.player.sendMessage(Text.literal(
"Toggled module " + module.getName() + " to " + (!wasEnabled ? "ON" : "OFF")
), false);
}
}
previousKeyState.put(module, isPressed);
}
}
private static List<Module> modules = new ArrayList<>();
public static ModuleManager INSTANCE = new ModuleManager();
public void register(Module module) {
modules.add(module);
}
public static List<Module> getAllModules() {
return modules;
}
public static List<Module> getEnabledModules() {
return modules.stream().filter(Module::isEnabled).collect(Collectors.toList());
}
public List<Module> getModulesWithCategory(Category category) {
return modules.stream()
.filter(module -> module.getCategory() == category)
.collect(Collectors.toList());
}
public static void tick() {
for (Module module : modules) {
module.tick();
}
handleKeyPresses();
}
public void registerHUD() {
if (!hudRegistered) {
HudRenderCallback.EVENT.register(this::onRenderHud);
hudRegistered = true;
}
}
private void onRenderHud(DrawContext drawContext, RenderTickCounter renderTickCounter) {
if (!ArslaansMagicHaxClient.hideModulesHUD.isEnabled()) {
TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer;
float alpha = 1.0F;
int i = MathHelper.ceil(alpha * 255.0F) << 24; // I don't know what this dark magic is but it works
int x = 5;
int y = 5;
int titleColor = 16568909 | i; // weird magic
drawContext.drawTextWithBackground(textRenderer, Text.literal("Arslaan's Magic Hax"), x, y, 50, titleColor);
y += textRenderer.fontHeight + 4;
for (Module module : ModuleManager.getEnabledModules()) {
int color = 16777215 | i; // more magic I don't understand
drawContext.drawTextWithShadow(textRenderer, Text.literal(module.getName()), x, y, color);
y += textRenderer.fontHeight + 2;
}
}
}
public void close() {
// save then disable all
saveModuleStates();
for (Module module : modules) {
module.setEnabled(false);
}
}
public void loadModuleStates() {
File file = new File(CONFIG_FILE);
if (!file.exists()) return;
try (FileInputStream fis = new FileInputStream(file)) {
Properties properties = new Properties();
properties.load(fis);
for (Module module : modules) {
String enabled = properties.getProperty(module.getName() + ".enabled");
if (Objects.equals(enabled, "true")) {
module.setEnabled(true);
} else {
module.setEnabled(false);
}
String keyStr = properties.getProperty(module.getName() + ".key");
if (keyStr != null) {
try {
module.setGlfwKeybinding(Integer.parseInt(keyStr));
} catch (NumberFormatException ignored) {}
}
for (Setting setting : module.getSettings()) {
String value = properties.getProperty(module.getName() + "." + setting.getName());
if (value != null) {
setting.setValueFromString(value);
}
}
}
} catch (IOException e) {
ArslaansMagicHax.LOGGER.info("Failed to load module states and settings", e);
}
}
public void saveModuleStates() {
Properties properties = new Properties();
for (Module module : modules) {
properties.setProperty(module.getName() + ".enabled", String.valueOf(module.isEnabled()));
properties.setProperty(module.getName() + ".key", String.valueOf(module.getGlfwKeybinding()));
for (Setting setting : module.getSettings()) {
properties.setProperty(module.getName() + "." + setting.getName(), setting.getValueAsString());
}
}
try (FileOutputStream fos = new FileOutputStream(CONFIG_FILE)) {
properties.store(fos, "Arslaan's Magic Hax - Module States & Settings");
} catch (IOException e) {
ArslaansMagicHax.LOGGER.error("Failed to save module states and settings", e);
}
}
}
|