Base functions and first module (#1)

This is a working baseline

Reviewed-on: #1
This commit is contained in:
2023-03-22 23:20:13 +00:00
parent 3185cfb26d
commit 28dcfbeac3
14 changed files with 279 additions and 140 deletions

View File

@@ -1,19 +1,14 @@
package dev.c0de.minecraft;
import dev.c0de.minecraft.config.SimpleConfig;
import net.fabricmc.api.ModInitializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class c0deFoxMod implements ModInitializer {
public static final Logger LOGGER = LoggerFactory.getLogger("c0defox");
public static SimpleConfig config;
@Override
public void onInitialize() {
// This code runs as soon as Minecraft is in a mod-load-ready state.
// However, some things (like resources) may still be uninitialized.
// Proceed with mild caution.
LOGGER.info("Hello world!");
config = new SimpleConfig("c0defox");
}
}

View File

@@ -0,0 +1,48 @@
package dev.c0de.minecraft.client;
import net.minecraft.client.MinecraftClient;
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 {
protected KeyBinding key;
protected boolean state;
public void onTick(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) {
this.state = state;
}
public KeyBinding getKey() {
return this.key;
}
public void toggleKey() {
if (this.key != null) {
if (this.key.wasPressed()) {
this.setState(!this.isState());
}
}
}
public boolean isState() {
return this.state;
}
}

View File

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

View File

@@ -0,0 +1,37 @@
package dev.c0de.minecraft.client.modules;
import dev.c0de.minecraft.c0deFoxMod;
import dev.c0de.minecraft.client.Module;
import net.minecraft.client.MinecraftClient;
import net.minecraft.text.Text;
import org.lwjgl.glfw.GLFW;
public class Fullbright extends Module {
double DEFAULT = 1.0;
public Fullbright() {
this.registerKey("key.c0defox.fullbright", GLFW.GLFW_KEY_B, "key.categories.c0defox");
}
@Override
public void onTick(MinecraftClient client) {
this.toggleKey();
if (this.isState()) {
this.update(client, c0deFoxMod.config.getConfig().FULLBRIGHT_GAMMA);
return;
}
this.update(client, DEFAULT);
}
private void update(MinecraftClient client, double value) {
if (client == null) return;
if (client.options.getGamma().getValue() == value) return;
client.options.getGamma().setValue(value);
if (client.player != null) client.player.sendMessage(Text.of(String.valueOf(value)), true);
}
}

View File

@@ -0,0 +1,6 @@
package dev.c0de.minecraft.config;
// Configuration options for the client
public class ClientConfig {
public double FULLBRIGHT_GAMMA = 5.5;
}

View File

@@ -0,0 +1,80 @@
package dev.c0de.minecraft.config;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.fabricmc.loader.api.FabricLoader;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
public class SimpleConfig {
private static final Logger LOGGER = LogManager.getLogger("SimpleConfig");
private boolean broken = false;
private final File file;
private ClientConfig config = new ClientConfig();
public ClientConfig getConfig() {
return config;
}
public void createConfig() throws IOException {
// try creating missing files
file.getParentFile().mkdirs();
if (Files.notExists(file.toPath()))
Files.createFile(file.toPath());
// write default config data
Gson gson = new GsonBuilder()
.enableComplexMapKeySerialization()
.setPrettyPrinting()
.create();
PrintWriter writer = new PrintWriter(file, StandardCharsets.UTF_8);
gson.toJson(config, writer);
writer.close();
}
private void loadConfig() throws IOException {
Gson gson = new Gson();
Reader reader = Files.newBufferedReader(file.toPath());
config = gson.fromJson(reader, ClientConfig.class);
reader.close();
}
public SimpleConfig(String modID) {
Path path = FabricLoader.getInstance().getConfigDir();
file = new File(path.toAbsolutePath().toString(), modID + ".json");
if (!file.exists()) {
LOGGER.info(modID + " is missing, generating default one...");
try {
createConfig();
} catch (IOException e) {
LOGGER.error(modID + " failed to generate!");
LOGGER.trace(e);
broken = true;
}
}
if (!broken) {
try {
loadConfig();
} catch (Exception e) {
LOGGER.error(modID + " failed to load!");
LOGGER.trace(e);
broken = true;
}
}
}
}

View File

@@ -0,0 +1,37 @@
package dev.c0de.minecraft.mixin;
import com.mojang.serialization.Codec;
import net.minecraft.client.option.SimpleOption;
import net.minecraft.client.resource.language.I18n;
import net.minecraft.text.Text;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@Mixin(SimpleOption.class)
public class BrightnessMixin<T> {
@Shadow @Final
Text text;
@Shadow
T value;
@Inject(method = "getCodec", at = @At("HEAD"), cancellable = true)
private void returnFakeCodec(CallbackInfoReturnable<Codec<Double>> info) {
if (text.getString().equals(I18n.translate("options.gamma"))) {
info.setReturnValue(Codec.DOUBLE);
}
}
@Inject(method = "setValue", at = @At("HEAD"), cancellable = true)
private void setRealValue(T value, CallbackInfo info) {
if (text.getString().equals(I18n.translate("options.gamma"))) {
this.value = value;
info.cancel();
}
}
}

View File

@@ -1,6 +1,7 @@
package dev.c0de.minecraft.mixin;
import dev.c0de.minecraft.c0deFoxMod;
// import dev.c0de.minecraft.c0deFoxMod;
import net.minecraft.client.gui.screen.TitleScreen;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
@@ -11,7 +12,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
public class c0deFoxMixin {
@Inject(at = @At("HEAD"), method = "init()V")
private void init(CallbackInfo info) {
c0deFoxMod.LOGGER.info("This line is printed by an example mod mixin!");
// c0deFoxMod.LOGGER.info("This line is printed by an example mod mixin!");
}
}

View File

@@ -0,0 +1,4 @@
{
"key.categories.c0defox": "c0de's mods",
"key.c0defox.fullbright": "Fullbright"
}

View File

@@ -6,6 +6,7 @@
"mixins": [
],
"client": [
"BrightnessMixin",
"c0deFoxMixin"
],
"injectors": {

View File

@@ -14,22 +14,25 @@
},
"license": "MIT",
"icon": "assets/modid/icon.png",
"icon": "assets/c0defox/icon.png",
"environment": "*",
"environment": "client",
"entrypoints": {
"main": [
"dev.c0de.minecraft.c0deFoxMod"
],
"client": [
"dev.c0de.minecraft.client.c0deFoxModClient"
]
},
"mixins": [
"modid.mixins.json"
"c0defox.mixins.json"
],
"depends": {
"fabricloader": ">=0.14.17",
"fabric-api": "*",
"minecraft": "~1.19.4",
"minecraft": ">=1.19.4",
"java": ">=17"
},
"suggests": {