Add foundation and first module (fullbright)

This commit is contained in:
c0de 2023-03-20 21:16:49 -05:00
parent 79c81d7772
commit e3f7b1ae19
12 changed files with 316 additions and 11 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,51 @@
package dev.c0de.minecraft.client;
import net.minecraft.client.MinecraftClient;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.TreeMap;
public class KeybindManager {
private final HashMap<String, Boolean> states = new HashMap<>();
private final List<Module> modules;
public KeybindManager(List<Module> modules) {
this.modules = modules;
}
public void tick(MinecraftClient client) {
// Execute the onTick function of each module
modules.forEach(module -> module.onTick(this, client));
// Toggle the module state with keybind
modules.stream().filter(module -> module.getKey() != null).forEach(module -> {
if (module.getKey().wasPressed()) {
final String name = module.getClass().getName();
final boolean newState = states.get(name);
states.put(name, !states.getOrDefault(name, false));
module.setState(newState);
module.onToggle(this, client);
}
});
}
public boolean getState(Class<?> cls) {
return states.getOrDefault(cls.getName(), false);
}
public TreeMap<String, Boolean> getSortedStates() {
final TreeMap<String, Boolean> keyStates = new TreeMap<>();
states.forEach((key, value) -> {
final Optional<Module> foundModule = modules.stream().filter(module -> module.getClass().getName().equals(key)).findFirst();
foundModule.ifPresent(module -> keyStates.put(module.getKey().getTranslationKey(), value));
});
return keyStates;
}
}

View File

@ -0,0 +1,30 @@
package dev.c0de.minecraft.client;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
public abstract class Module {
protected KeyBinding key;
protected boolean state;
public void onTick(KeybindManager manager, MinecraftClient client) {
}
public void onToggle(KeybindManager manager, MinecraftClient client) {
}
public void setState(boolean state) {
this.state = state;
}
public KeyBinding getKey() {
return key;
}
public boolean isState() {
return state;
}
}

View File

@ -0,0 +1,56 @@
package dev.c0de.minecraft.client;
import dev.c0de.minecraft.modules.Fullbright;
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 java.util.List;
@Environment(EnvType.CLIENT)
public class c0deFoxModClient implements ClientModInitializer {
public static final Logger LOGGER = LoggerFactory.getLogger("c0defox");
private static c0deFoxModClient self;
private static KeybindManager keybindManager;
// Load in modules as independent parts of the mod
private static List<Module> modules;
@Override
public void onInitializeClient() {
ClientTickEvents.END_CLIENT_TICK.register(this::tick);
self = this;
// Add new modules here
modules = List.of(
new Fullbright()
);
keybindManager = new KeybindManager(modules);
LOGGER.info("Ready");
}
public static c0deFoxModClient getSelf() {
return self;
}
public static KeybindManager getKeybindManager() {
return keybindManager;
}
// Called on every game tick
public void tick(MinecraftClient client) {
keybindManager.tick(client);
}
}

View File

@ -0,0 +1,6 @@
package dev.c0de.minecraft.config;
// Configuration options for the client
public class ClientConfig {
public double FULLBRIGHT_GAMMA = 2.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,6 @@
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;

View File

@ -0,0 +1,43 @@
package dev.c0de.minecraft.modules;
import dev.c0de.minecraft.c0deFoxMod;
import dev.c0de.minecraft.client.Module;
import dev.c0de.minecraft.client.KeybindManager;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.text.Text;
import org.lwjgl.glfw.GLFW;
public class Fullbright extends Module {
double DEFAULT = 1.0;
public void FullBright() {
this.key = KeyBindingHelper.registerKeyBinding(new KeyBinding(
"key.c0defox.fullbright",
InputUtil.Type.KEYSYM,
GLFW.GLFW_KEY_B,
"category.c0defox.default"
));
}
@Override
public void onTick(KeybindManager manager, MinecraftClient client) {
if (state)
update(client, c0deFoxMod.config.getConfig().FULLBRIGHT_GAMMA);
else
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,3 @@
{
"key.c0defox.fullbright": "Fullbright"
}

View File

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

View File

@ -20,6 +20,9 @@
"entrypoints": {
"main": [
"dev.c0de.minecraft.c0deFoxMod"
],
"client": [
"dev.c0de.minecraft.client.c0deFoxModClient"
]
},
"mixins": [