From 7cdcf1fec7bb8c20546d11963ee6abfd6238d416 Mon Sep 17 00:00:00 2001 From: xzeldon Date: Wed, 25 May 2022 23:24:16 +0300 Subject: [PATCH] Add configuration --- gradle.properties | 2 +- .../RemoveBlockOutline.java | 7 --- .../RemoveBlockOutlineClient.java | 58 +++++++++++++++++++ .../config/RemoveBlockOutlineConfig.java | 51 ++++++++++++++++ .../mixin/RemoveBlockOutlineMixin.java | 10 +++- src/main/resources/fabric.mod.json | 4 ++ 6 files changed, 123 insertions(+), 9 deletions(-) create mode 100644 src/main/java/ru/xzeldon/removeblockoutline/RemoveBlockOutlineClient.java create mode 100644 src/main/java/ru/xzeldon/removeblockoutline/config/RemoveBlockOutlineConfig.java diff --git a/gradle.properties b/gradle.properties index c1b0213..c2588b0 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G loader_version=0.14.6 # Mod Properties - mod_version = 1.0.0-1.18.2 + mod_version = 1.1.0-1.18.2 maven_group = ru.xzeldon.removeblockoutline archives_base_name = removeblockoutline diff --git a/src/main/java/ru/xzeldon/removeblockoutline/RemoveBlockOutline.java b/src/main/java/ru/xzeldon/removeblockoutline/RemoveBlockOutline.java index 75aceee..3542ed1 100644 --- a/src/main/java/ru/xzeldon/removeblockoutline/RemoveBlockOutline.java +++ b/src/main/java/ru/xzeldon/removeblockoutline/RemoveBlockOutline.java @@ -5,17 +5,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class RemoveBlockOutline implements ModInitializer { - // This logger is used to write text to the console and the log file. - // It is considered best practice to use your mod id as the logger's name. - // That way, it's clear which mod wrote info, warnings, and errors. public static final Logger LOGGER = LoggerFactory.getLogger("removeblockoutline"); @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("RemoveBlockOutline mod initialized"); } } diff --git a/src/main/java/ru/xzeldon/removeblockoutline/RemoveBlockOutlineClient.java b/src/main/java/ru/xzeldon/removeblockoutline/RemoveBlockOutlineClient.java new file mode 100644 index 0000000..9f2469d --- /dev/null +++ b/src/main/java/ru/xzeldon/removeblockoutline/RemoveBlockOutlineClient.java @@ -0,0 +1,58 @@ +package ru.xzeldon.removeblockoutline; + +import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; +import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; +import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.client.option.KeyBinding; +import net.minecraft.client.util.InputUtil; +import org.lwjgl.glfw.GLFW; +import ru.xzeldon.removeblockoutline.config.RemoveBlockOutlineConfig; + +import java.io.IOException; + +public class RemoveBlockOutlineClient implements ClientModInitializer { + private static KeyBinding toggleKeybind; + + public static RemoveBlockOutlineConfig CONFIG; + + @Override + public void onInitializeClient() { + CONFIG = new RemoveBlockOutlineConfig(FabricLoader.getInstance().getConfigDir().resolve("removeblockoutline.properties")); + + try { + CONFIG.initialize(); + } catch (IOException e) { + RemoveBlockOutline.LOGGER.error("Failed to initialize configuration, default values will be used instead"); + RemoveBlockOutline.LOGGER.error("", e); + } + + toggleKeybind = KeyBindingHelper.registerKeyBinding( + new KeyBinding("removeblockoutline.keybind.toggle", + InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_GRAVE_ACCENT, + "category.removeblockoutline.keybinds") + ); + + ClientTickEvents.END_CLIENT_TICK.register(client -> { + while (toggleKeybind.wasPressed()) { + if (CONFIG.isEnableBlockOutline()) { + CONFIG.setEnableBlockOutline(false); + try { + CONFIG.save(); + } catch (IOException e) { + RemoveBlockOutline.LOGGER.error("Failed to save configuration"); + RemoveBlockOutline.LOGGER.error("", e); + } + } else { + CONFIG.setEnableBlockOutline(true); + try { + CONFIG.save(); + } catch (IOException e) { + RemoveBlockOutline.LOGGER.error("Failed to save configuration"); + RemoveBlockOutline.LOGGER.error("", e); + } + } + } + }); + } +} diff --git a/src/main/java/ru/xzeldon/removeblockoutline/config/RemoveBlockOutlineConfig.java b/src/main/java/ru/xzeldon/removeblockoutline/config/RemoveBlockOutlineConfig.java new file mode 100644 index 0000000..6b25860 --- /dev/null +++ b/src/main/java/ru/xzeldon/removeblockoutline/config/RemoveBlockOutlineConfig.java @@ -0,0 +1,51 @@ +package ru.xzeldon.removeblockoutline.config; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Properties; + +public class RemoveBlockOutlineConfig { + private static final String COMMENT = + "This file stores configuration options for RemoveBlockOutline"; + + private final Path propertiesPath; + private boolean enableBlockOutline; + + public RemoveBlockOutlineConfig(Path propertiesPath) { + enableBlockOutline = false; + this.propertiesPath = propertiesPath; + } + + public void initialize() throws IOException { + load(); + + if (!Files.exists(propertiesPath)) { + save(); + } + } + + public boolean isEnableBlockOutline() { + return enableBlockOutline; + } + + public void setEnableBlockOutline(boolean enableBlockOutline) { + this.enableBlockOutline = enableBlockOutline; + } + + public void load() throws IOException { + if (!Files.exists(propertiesPath)) { + return; + } + + Properties properties = new Properties(); + properties.load(Files.newInputStream(propertiesPath)); + enableBlockOutline = !"true".equals(properties.getProperty("enableBlockOutline")); + } + + public void save() throws IOException { + Properties properties = new Properties(); + properties.setProperty("enableBlockOutline", enableBlockOutline ? "true" : "false"); + properties.store(Files.newOutputStream(propertiesPath), COMMENT); + } +} diff --git a/src/main/java/ru/xzeldon/removeblockoutline/mixin/RemoveBlockOutlineMixin.java b/src/main/java/ru/xzeldon/removeblockoutline/mixin/RemoveBlockOutlineMixin.java index e71c84a..d2c9905 100644 --- a/src/main/java/ru/xzeldon/removeblockoutline/mixin/RemoveBlockOutlineMixin.java +++ b/src/main/java/ru/xzeldon/removeblockoutline/mixin/RemoveBlockOutlineMixin.java @@ -10,11 +10,19 @@ 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; +import ru.xzeldon.removeblockoutline.RemoveBlockOutlineClient; +import ru.xzeldon.removeblockoutline.config.RemoveBlockOutlineConfig; @Mixin(WorldRenderer.class) public class RemoveBlockOutlineMixin { + private static RemoveBlockOutlineConfig config; + @Inject(method = "drawBlockOutline", at = @At("HEAD"), cancellable = true) private void onDrawBlockOutline(MatrixStack matrixStack, VertexConsumer vertexConsumer, Entity entity, double cameraX, double cameraY, double cameraZ, BlockPos blockPos, BlockState blockState, CallbackInfo ci) { - ci.cancel(); + config = RemoveBlockOutlineClient.CONFIG; + + if (!config.isEnableBlockOutline()) { + ci.cancel(); + } } } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 8f85282..450cf9d 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -20,6 +20,10 @@ "entrypoints": { "main": [ "ru.xzeldon.removeblockoutline.RemoveBlockOutline" + ], + + "client": [ + "ru.xzeldon.removeblockoutline.RemoveBlockOutlineClient" ] }, "mixins": [