Add configuration

This commit is contained in:
Timofey Gelazoniya 2022-05-25 23:24:16 +03:00
parent 74c953eac3
commit 7cdcf1fec7
Signed by: zeldon
GPG Key ID: 047886915281DD2A
6 changed files with 123 additions and 9 deletions

View File

@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.14.6 loader_version=0.14.6
# Mod Properties # Mod Properties
mod_version = 1.0.0-1.18.2 mod_version = 1.1.0-1.18.2
maven_group = ru.xzeldon.removeblockoutline maven_group = ru.xzeldon.removeblockoutline
archives_base_name = removeblockoutline archives_base_name = removeblockoutline

View File

@ -5,17 +5,10 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
public class RemoveBlockOutline implements ModInitializer { 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"); public static final Logger LOGGER = LoggerFactory.getLogger("removeblockoutline");
@Override @Override
public void onInitialize() { 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"); LOGGER.info("RemoveBlockOutline mod initialized");
} }
} }

View File

@ -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);
}
}
}
});
}
}

View File

@ -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);
}
}

View File

@ -10,11 +10,19 @@ import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import ru.xzeldon.removeblockoutline.RemoveBlockOutlineClient;
import ru.xzeldon.removeblockoutline.config.RemoveBlockOutlineConfig;
@Mixin(WorldRenderer.class) @Mixin(WorldRenderer.class)
public class RemoveBlockOutlineMixin { public class RemoveBlockOutlineMixin {
private static RemoveBlockOutlineConfig config;
@Inject(method = "drawBlockOutline", at = @At("HEAD"), cancellable = true) @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) { 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();
}
} }
} }

View File

@ -20,6 +20,10 @@
"entrypoints": { "entrypoints": {
"main": [ "main": [
"ru.xzeldon.removeblockoutline.RemoveBlockOutline" "ru.xzeldon.removeblockoutline.RemoveBlockOutline"
],
"client": [
"ru.xzeldon.removeblockoutline.RemoveBlockOutlineClient"
] ]
}, },
"mixins": [ "mixins": [