Skip to content

Commit

Permalink
keybindings for everyone yay
Browse files Browse the repository at this point in the history
  • Loading branch information
SymoHTL committed Apr 18, 2024
1 parent 6c2d294 commit 94eb1b5
Show file tree
Hide file tree
Showing 13 changed files with 238 additions and 30 deletions.
10 changes: 7 additions & 3 deletions src/client/java/dev/symo/finz/FinZClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import dev.symo.finz.config.FinZSettings;
import dev.symo.finz.config.FriendList;
import dev.symo.finz.events.impl.EventManager;
import dev.symo.finz.events.listeners.KeyPressListener;
import dev.symo.finz.config.KeyBindSettings;
import dev.symo.finz.modules.ModuleManager;
import dev.symo.finz.ui.ConfigScreen;
import net.fabricmc.api.ClientModInitializer;
Expand All @@ -22,7 +24,6 @@
public class FinZClient implements ClientModInitializer {

public static final Logger LOGGER = LogManager.getLogger("FinZ");
public static final String MOD_ID = "finz";
public static final String CONFIG_VERSION = "1";

public static EventManager eventManager = new EventManager();
Expand All @@ -36,14 +37,17 @@ public class FinZClient implements ClientModInitializer {

public static final FinZSettings settings = new FinZSettings(FinZPath.resolve("config.json").toString());

public static final KeyBindSettings keyBindSettings = new KeyBindSettings(FinZPath.resolve("keybinds.json").toString());

@Override
public void onInitializeClient() {
createFinZFolder();

ModuleManager.init();

settings.load();
friendList.load();
keyBindSettings.load();
eventManager.add(KeyPressListener.class, keyBindSettings);
FinZClient.MODS = ImmutableList.copyOf(FabricLoader.getInstance().getAllMods());
}

Expand All @@ -54,7 +58,7 @@ private void createFinZFolder() {
try {
Files.createDirectories(finzDir);
} catch (IOException e) {
e.printStackTrace();
LOGGER.error("Failed to create FinZ folder", e);
}
}
}
Expand Down
16 changes: 6 additions & 10 deletions src/client/java/dev/symo/finz/config/FinZSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,16 @@ public void load() {
var modules = Modules.all;
// load the settings for each module
modules.forEach(module -> {
var moduleJson = json.getAsJsonObject(module._name.toLowerCase());
var moduleJson = json.getAsJsonObject(module.name.toLowerCase());
if (moduleJson == null) return;
module.getSettings().forEach(setting -> {
setting.fromJson(moduleJson.get(setting.getName().toLowerCase()));
});
module.getSettings().forEach(setting -> setting.fromJson(moduleJson.get(setting.getName().toLowerCase())));
module.checkEnabled();
});
} catch (IOException e) {
try {
backupAndDelete();
} catch (IOException ex) {
throw new RuntimeException(ex);
FinZClient.LOGGER.error("Failed to backup and delete config file", ex);
}
}
}
Expand All @@ -63,10 +61,8 @@ public void save() {

modules.forEach(module -> {
JsonObject moduleJson = new JsonObject();
module.getSettings().forEach(setting -> {
moduleJson.add(setting.getName().toLowerCase(), setting.toJson());
});
json.add(module._name.toLowerCase(), moduleJson);
module.getSettings().forEach(setting -> moduleJson.add(setting.getName().toLowerCase(), setting.toJson()));
json.add(module.name.toLowerCase(), moduleJson);
});

try {
Expand All @@ -75,7 +71,7 @@ public void save() {
gson.toJson(json, writer);
writer.close();
} catch (IOException e) {
throw new RuntimeException(e);
FinZClient.LOGGER.error("Failed to save config file", e);
}
}
}
5 changes: 3 additions & 2 deletions src/client/java/dev/symo/finz/config/FriendList.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.symo.finz.config;

import com.google.gson.Gson;
import dev.symo.finz.FinZClient;

import java.io.FileReader;
import java.io.IOException;
Expand Down Expand Up @@ -29,7 +30,7 @@ public void load() {
}

} catch (IOException e) {
e.printStackTrace();
FinZClient.LOGGER.error("Failed to load friends list", e);
}
}
}
Expand Down Expand Up @@ -57,7 +58,7 @@ public void save() {
try {
Files.write(Paths.get(path), new Gson().toJson(Friends).getBytes());
} catch (IOException e) {
e.printStackTrace();
FinZClient.LOGGER.error("Failed to save friends list", e);
}
}
}
33 changes: 33 additions & 0 deletions src/client/java/dev/symo/finz/config/KeyBind.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package dev.symo.finz.config;

import dev.symo.finz.modules.AModule;

public class KeyBind {
private final String keyName;
private final AModule module;

public KeyBind(String keyName, AModule module) {
this.keyName = keyName;
this.module = module;
}

public void onKeyPress(String keyName) {
if (this.keyName.equalsIgnoreCase(keyName))
this.module.toggle();
}

public String getKeyName() {
return keyName;
}

public AModule getModule() {
return module;
}

public boolean equals(Object o) {
if (this == o) return true;
else if (!(o instanceof KeyBind keyBind)) return false;
else return this.keyName.equalsIgnoreCase(keyBind.keyName) && this.module.equals(keyBind.module);
}

}
94 changes: 94 additions & 0 deletions src/client/java/dev/symo/finz/config/KeyBindSettings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package dev.symo.finz.config;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import dev.symo.finz.FinZClient;
import dev.symo.finz.events.listeners.KeyPressListener;
import dev.symo.finz.modules.Modules;
import net.minecraft.client.util.InputUtil;
import org.lwjgl.glfw.GLFW;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;

public final class KeyBindSettings implements KeyPressListener {

private final String path;

private final ArrayList<KeyBind> keyBinds = new ArrayList<>();

public KeyBindSettings(String path) {
this.path = path;
}

@Override
public void onKeyPress(KeyPressEvent event) {
if(event.getAction() != GLFW.GLFW_PRESS) return;
if(InputUtil.isKeyPressed(FinZClient.mc.getWindow().getHandle(), GLFW.GLFW_KEY_F3))return;
if(FinZClient.mc.currentScreen != null) return;

var keyName = InputUtil.fromKeyCode(event.getKeyCode(), event.getScanCode()).getTranslationKey();
for (KeyBind keyBind : keyBinds) keyBind.onKeyPress(keyName);
}

public void load() {
try {
Path path = Paths.get(this.path);
if (!path.toFile().exists()) return;

var reader = Files.newBufferedReader(path);
var json = JsonParser.parseReader(reader).getAsJsonObject();

var modules = Modules.all;

var keyBinds = json.getAsJsonArray("keyBinds");
for (var keyBind : keyBinds) {
var keyBindObj = keyBind.getAsJsonObject();
var keyCode = keyBindObj.get("keyName").getAsString();
var moduleName = keyBindObj.get("module").getAsString().toLowerCase();
var module = modules.stream().filter(m -> m.name.toLowerCase().equals(moduleName)).findFirst().orElse(null);
if (module == null) {
FinZClient.LOGGER.error("Failed to find module for keybind: {}", moduleName);
continue;
}
add(new KeyBind(keyCode, module));
}
} catch (Exception e) {
FinZClient.LOGGER.error("Failed to load keybinds", e);
}
}

public void save() {
JsonObject json = new JsonObject();
var keyBinds = new JsonArray();
for (KeyBind keyBind : this.keyBinds) {
var keyBindObj = new JsonObject();
keyBindObj.addProperty("keyName", keyBind.getKeyName());
keyBindObj.addProperty("module", keyBind.getModule().name.toLowerCase());
keyBinds.add(keyBindObj);
}
json.add("keyBinds", keyBinds);

try {
var writer = Files.newBufferedWriter(Path.of(path));
var gson = new com.google.gson.GsonBuilder().setPrettyPrinting().create();
gson.toJson(json, writer);
writer.close();
} catch (Exception e) {
FinZClient.LOGGER.error("Failed to save keybinds", e);
}
}

public void add(KeyBind keyBind) {
for (KeyBind bind : keyBinds) if (bind.equals(keyBind)) return;
keyBinds.add(keyBind);
}

public void addAndSave(KeyBind keyBind) {
add(keyBind);
save();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package dev.symo.finz.events.listeners;

import dev.symo.finz.events.impl.Event;
import dev.symo.finz.events.impl.Listener;

import java.util.ArrayList;

public interface KeyPressListener extends Listener {
void onKeyPress(KeyPressEvent event);

class KeyPressEvent extends Event<KeyPressListener> {
private final int keyCode;
private final int scanCode;
private final int action;

public KeyPressEvent(int keyCode, int scanCode, int action) {
this.keyCode = keyCode;
this.scanCode = scanCode;
this.action = action;
}

@Override
public void fire(ArrayList<KeyPressListener> listeners) {
for (KeyPressListener listener : listeners)
listener.onKeyPress(this);
}

@Override
public Class<KeyPressListener> getListenerType() {
return KeyPressListener.class;
}

public int getKeyCode() {
return keyCode;
}

public int getScanCode() {
return scanCode;
}

public int getAction() {
return action;
}
}
}
19 changes: 19 additions & 0 deletions src/client/java/dev/symo/finz/mixin/client/KeyboardMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dev.symo.finz.mixin.client;

import dev.symo.finz.events.impl.EventManager;
import dev.symo.finz.events.listeners.KeyPressListener;
import net.minecraft.client.Keyboard;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(Keyboard.class)

public class KeyboardMixin {
@Inject(at = @At("HEAD"), method = "onKey(JIIII)V")
private void onOnKey(long windowHandle, int key, int scancode, int action,
int modifiers, CallbackInfo ci) {
EventManager.fire(new KeyPressListener.KeyPressEvent(key, scancode, action));
}
}
26 changes: 21 additions & 5 deletions src/client/java/dev/symo/finz/modules/AModule.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.symo.finz.modules;

import dev.symo.finz.FinZClient;
import dev.symo.finz.config.KeyBind;
import dev.symo.finz.events.impl.EventManager;
import dev.symo.finz.modules.settings.BoolSetting;
import dev.symo.finz.modules.settings.ModuleSetting;
Expand All @@ -18,16 +19,16 @@ public abstract class AModule {
protected static final EventManager EVENTS = FinZClient.eventManager;
protected static final MinecraftClient mc = FinZClient.mc;

public String _name;
public Category _category;
public String name;
public Category category;

public KeyBinding _keybind;
public KeyBinding keybind;

private final LinkedHashMap<String, ModuleSetting> settings = new LinkedHashMap<>();

public AModule(String name, Category category) {
this._name = name;
this._category = category;
this.name = name;
this.category = category;

BoolSetting enabled = new BoolSetting(ENABLED, "Enable", false);
enabled.onChanged(this::checkEnabled);
Expand All @@ -39,6 +40,10 @@ public void addSetting(ModuleSetting setting) {
setting.onChanged(this::onSettingsChanged);
}

public void registerKeyBind(String keyName){
FinZClient.keyBindSettings.addAndSave(new KeyBind(keyName, this));
}

public Collection<ModuleSetting> getSettings() {
return settings.values();
}
Expand All @@ -59,6 +64,10 @@ public final void checkEnabled() {
else onDisable();
}

public void toggle() {
setEnabled(!isEnabled());
}

public void onEnable() {
}

Expand All @@ -67,4 +76,11 @@ public void onDisable() {

protected void onSettingsChanged() {
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof AModule module)) return false;
return name.equals(module.name);
}
}
5 changes: 2 additions & 3 deletions src/client/java/dev/symo/finz/modules/ModuleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.util.ActionResult;

public class ModuleManager {

Expand All @@ -26,8 +25,8 @@ public static void init() {
_openGuiKey = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.finz.opengui", InputUtil.Type.KEYSYM, 77, "category.finz.config"));

for (AModule module : Modules.all)
if (module._keybind != null)
KeyBindingHelper.registerKeyBinding(module._keybind);
if (module.keybind != null)
KeyBindingHelper.registerKeyBinding(module.keybind);


ClientTickEvents.END_CLIENT_TICK.register(mc -> {
Expand Down
Loading

0 comments on commit 94eb1b5

Please sign in to comment.