-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
238 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/client/java/dev/symo/finz/events/listeners/KeyPressListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
src/client/java/dev/symo/finz/mixin/client/KeyboardMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.