Move key registration to module

This commit is contained in:
c0de 2023-03-21 20:42:42 -05:00
parent d651d41d8f
commit 86bc75991a
3 changed files with 28 additions and 18 deletions

View File

@ -2,6 +2,9 @@ package dev.c0de.minecraft.client;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding; import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
public abstract class Module { public abstract class Module {
@ -14,15 +17,24 @@ public abstract class Module {
public void onToggle(MinecraftClient client) { public void onToggle(MinecraftClient client) {
} }
public void registerKey(String translationKey, int glfw_key, String category) {
this.key = KeyBindingHelper.registerKeyBinding(new KeyBinding(
translationKey,
InputUtil.Type.KEYSYM,
glfw_key,
category
));
}
public void setState(boolean state) { public void setState(boolean state) {
this.state = state; this.state = state;
} }
public KeyBinding getKey() { public KeyBinding getKey() {
return this.key; return key;
} }
public boolean isState() { public boolean isState() {
return this.state; return state;
} }
} }

View File

@ -7,26 +7,25 @@ import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import dev.c0de.minecraft.client.modules.Fullbright; import dev.c0de.minecraft.client.modules.Fullbright;
import java.util.List;
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public class c0deFoxModClient implements ClientModInitializer { public class c0deFoxModClient implements ClientModInitializer {
public static final Logger LOGGER = LoggerFactory.getLogger("c0defox"); public static final Logger LOGGER = LoggerFactory.getLogger("c0defox");
private static c0deFoxModClient self; private static c0deFoxModClient self;
protected Fullbright fullbright; Module fullbright;
@Override @Override
public void onInitializeClient() { public void onInitializeClient() {
ClientTickEvents.END_CLIENT_TICK.register(this::tick); ClientTickEvents.END_CLIENT_TICK.register(this::tick);
self = this;
fullbright = new Fullbright(); fullbright = new Fullbright();
LOGGER.info("Ready"); LOGGER.info("Ready");
@ -38,7 +37,7 @@ public class c0deFoxModClient implements ClientModInitializer {
// Called on every game tick // Called on every game tick
public void tick(MinecraftClient client) { public void tick(MinecraftClient client) {
fullbright.onTick(client); this.fullbright.onTick(client);
} }
} }

View File

@ -3,11 +3,7 @@ package dev.c0de.minecraft.client.modules;
import dev.c0de.minecraft.c0deFoxMod; import dev.c0de.minecraft.c0deFoxMod;
import dev.c0de.minecraft.client.Module; import dev.c0de.minecraft.client.Module;
import dev.c0de.minecraft.client.c0deFoxModClient; import dev.c0de.minecraft.client.c0deFoxModClient;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import org.lwjgl.glfw.GLFW; import org.lwjgl.glfw.GLFW;
@ -16,24 +12,27 @@ public class Fullbright extends Module {
double DEFAULT = 1.0; double DEFAULT = 1.0;
public void FullBright() { public void FullBright() {
this.key = KeyBindingHelper.registerKeyBinding(new KeyBinding( this.registerKey("key.c0defox.fullbright", GLFW.GLFW_KEY_B, "key.categories.c0defox");
"key.c0defox.fullbright",
InputUtil.Type.KEYSYM,
GLFW.GLFW_KEY_B,
"key.categories.c0defox"
));
} }
@Override @Override
public void onTick(MinecraftClient client) { public void onTick(MinecraftClient client) {
update(client, DEFAULT); if (key.wasPressed()) {
this.setState(!this.isState());
}
if (this.isState()) { if (this.isState()) {
c0deFoxModClient.LOGGER.warn("Active State"); c0deFoxModClient.LOGGER.warn("Active State");
update(client, c0deFoxMod.config.getConfig().FULLBRIGHT_GAMMA); update(client, c0deFoxMod.config.getConfig().FULLBRIGHT_GAMMA);
return;
} }
update(client, DEFAULT);
} }
private void update(MinecraftClient client, double value) { private void update(MinecraftClient client, double value) {
c0deFoxModClient.LOGGER.warn(String.valueOf(value));
if (client == null) return; if (client == null) return;
if (client.options.getGamma().getValue() == value) return; if (client.options.getGamma().getValue() == value) return;