diff --git a/build.gradle b/build.gradle index fafc1a6..33b14ae 100644 --- a/build.gradle +++ b/build.gradle @@ -25,11 +25,9 @@ dependencies { modCompile "io.github.ladysnake:Satin:${satin_version}" include "io.github.ladysnake:Satin:${satin_version}" - modImplementation ("me.sargunvohra.mcmods:autoconfig1u:${project.auto_config_version}") - include ("me.sargunvohra.mcmods:autoconfig1u:${project.auto_config_version}") - modImplementation ("me.shedaniel.cloth:config-2:${project.cloth_config_version}") - include ("me.shedaniel.cloth:config-2:${project.cloth_config_version}") - modImplementation ("io.github.prospector:modmenu:${project.mod_menu_version}") + modImplementation ("io.github.prospector:modmenu:${project.mod_menu_version}") { + exclude group: "net.fabricmc.fabric-api" + } implementation 'com.google.code.findbugs:jsr305:3.0.2' } diff --git a/gradle.properties b/gradle.properties index a800cc0..efbdd4b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,21 +3,17 @@ org.gradle.jvmargs=-Xmx1G # Fabric Properties # check these on https://fabricmc.net/use - minecraft_version=20w51a - yarn_mappings=20w51a+build.5 - loader_version=0.10.8 - fabric_version=0.28.3+1.17 + minecraft_version=21w08b + yarn_mappings=21w08b+build.20 + loader_version=0.11.2 # Mod Properties - mod_version = 2.0.0-20w51a+fabric + mod_version = 2.1.0-21w08b+fabric maven_group = com.tterrag.blur archives_base_name = Blur # Dependencies # currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api - fabric_version=0.26.1+1.16 - - auto_config_version = 3.2.0-unstable - cloth_config_version = 4.7.0-unstable - mod_menu_version = 1.14.6+build.31 + fabric_version=0.31.2+1.17 + mod_menu_version = 2.0.0-beta.1+build.2 satin_version = 1.6.0-nightly.20w48a diff --git a/src/main/java/com/tterrag/blur/Blur.java b/src/main/java/com/tterrag/blur/Blur.java index 3056864..8d51525 100644 --- a/src/main/java/com/tterrag/blur/Blur.java +++ b/src/main/java/com/tterrag/blur/Blur.java @@ -1,12 +1,11 @@ package com.tterrag.blur; import com.tterrag.blur.config.BlurConfig; +import com.tterrag.blur.config.MidnightConfig; import ladysnake.satin.api.event.ShaderEffectRenderCallback; import ladysnake.satin.api.managed.ManagedShaderEffect; import ladysnake.satin.api.managed.ShaderEffectManager; import ladysnake.satin.api.managed.uniform.Uniform1f; -import me.sargunvohra.mcmods.autoconfig1u.AutoConfig; -import me.sargunvohra.mcmods.autoconfig1u.serializer.JanksonConfigSerializer; import net.fabricmc.api.ClientModInitializer; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.Screen; @@ -14,7 +13,6 @@ import org.apache.commons.lang3.ArrayUtils; public class Blur implements ClientModInitializer { - public static BlurConfig BLUR_CONFIG; public static final String MODID = "blur"; @@ -22,15 +20,14 @@ public class Blur implements ClientModInitializer { public int colorFirst, colorSecond; private final ManagedShaderEffect blur = ShaderEffectManager.getInstance().manage(new Identifier(MODID, "shaders/post/fade_in_blur.json"), - shader -> shader.setUniformValue("Radius", (float) getRadius())); + shader -> shader.setUniformValue("Radius", BlurConfig.radius)); private final Uniform1f blurProgress = blur.findUniform1f("Progress"); public static final Blur INSTANCE = new Blur(); @Override public void onInitializeClient() { - AutoConfig.register(BlurConfig.class, JanksonConfigSerializer::new); - BLUR_CONFIG = AutoConfig.getConfigHolder(BlurConfig.class).getConfig(); + MidnightConfig.init("blur", BlurConfig.class); ShaderEffectRenderCallback.EVENT.register((deltaTick) -> { if (start > 0) { @@ -43,12 +40,12 @@ public void onInitializeClient() { private boolean doFade = false; public void onScreenChange(Screen newGui) { if (MinecraftClient.getInstance().world != null) { - boolean excluded = newGui == null || ArrayUtils.contains(BLUR_CONFIG.blurExclusions, newGui.getClass().getName()); + boolean excluded = newGui == null || ArrayUtils.contains(BlurConfig.blurExclusions, newGui.getClass().getName()); if (!excluded) { - blur.setUniformValue("Radius", (float) getRadius()); - colorFirst = Integer.parseUnsignedInt(BLUR_CONFIG.gradientStartColor, 16); - colorSecond = Integer.parseUnsignedInt(BLUR_CONFIG.gradientEndColor, 16); - if (doFade == true) { + blur.setUniformValue("Radius", BlurConfig.radius); + colorFirst = Integer.parseUnsignedInt(String.valueOf(BlurConfig.gradientStartColor), 16); + colorSecond = Integer.parseUnsignedInt(String.valueOf(BlurConfig.gradientEndColor), 16); + if (doFade) { start = System.currentTimeMillis(); doFade = false; } @@ -59,12 +56,8 @@ public void onScreenChange(Screen newGui) { } } - public int getRadius() { - return BLUR_CONFIG.radius; - } - private float getProgress() { - return Math.min((System.currentTimeMillis() - start) / (float) BLUR_CONFIG.fadeTimeMillis, 1); + return Math.min((System.currentTimeMillis() - start) / (float) BlurConfig.fadeTimeMillis, 1); } public int getBackgroundColor(boolean second) { diff --git a/src/main/java/com/tterrag/blur/config/BlurConfig.java b/src/main/java/com/tterrag/blur/config/BlurConfig.java index 3ccae63..c52fb6f 100644 --- a/src/main/java/com/tterrag/blur/config/BlurConfig.java +++ b/src/main/java/com/tterrag/blur/config/BlurConfig.java @@ -1,16 +1,16 @@ package com.tterrag.blur.config; -import me.sargunvohra.mcmods.autoconfig1u.ConfigData; -import me.sargunvohra.mcmods.autoconfig1u.annotation.Config; -import me.sargunvohra.mcmods.autoconfig1u.annotation.ConfigEntry; import net.minecraft.client.gui.screen.ChatScreen; -@Config(name = "blur") -public class BlurConfig implements ConfigData { - @ConfigEntry.Gui.Excluded - public String[] blurExclusions = new String[]{ ChatScreen.class.getName() }; - public int fadeTimeMillis = 200; - public int radius = 8; - public String gradientStartColor = "75000000"; - public String gradientEndColor = "75000000"; +public class BlurConfig extends MidnightConfig { + @Entry + public static String[] blurExclusions = new String[]{ ChatScreen.class.getName() }; + @Entry(min = 0, max = 5000) + public static int fadeTimeMillis = 200; + @Entry(min = 0, max = 500) + public static int radius = 8; + @Entry(min = 0, max = 99999999) + public static int gradientStartColor = 75000000; + @Entry(min = 0, max = 99999999) + public static int gradientEndColor = 75000000; } diff --git a/src/main/java/com/tterrag/blur/config/MidnightConfig.java b/src/main/java/com/tterrag/blur/config/MidnightConfig.java new file mode 100644 index 0000000..c44cadf --- /dev/null +++ b/src/main/java/com/tterrag/blur/config/MidnightConfig.java @@ -0,0 +1,293 @@ +package com.tterrag.blur.config; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.tterrag.blur.Blur; +import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.gui.screen.ScreenTexts; +import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.client.gui.widget.TextFieldWidget; +import net.minecraft.client.resource.language.I18n; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.text.*; +import net.minecraft.util.Formatting; + +import java.lang.annotation.*; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.*; +import java.util.function.BiFunction; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.regex.Pattern; + +// MidnightConfig v0.1.0 - Modified for Blur // + +/* Based on https://github.com/Minenash/TinyConfig + Credits to Minenash - CC0-1.0 + You can copy this class to get a standalone version of MidnightConfig */ + +@SuppressWarnings("rawtypes") +public class MidnightConfig { + + private static final Pattern INTEGER_ONLY = Pattern.compile("(-?[0-9]*)"); + private static final Pattern DECIMAL_ONLY = Pattern.compile("-?([\\d]+\\.?[\\d]*|[\\d]*\\.?[\\d]+|\\.)"); + + private static final List entries = new ArrayList<>(); + + protected static class EntryInfo { + Field field; + Object widget; + int width; + Method dynamicTooltip; + Map.Entry error; + Object defaultValue; + Object value; + String tempValue; + boolean inLimits = true; + } + + private static Class configClass; + private static String translationPrefix; + private static Path path; + + private static final Gson gson = new GsonBuilder() + .excludeFieldsWithModifiers(Modifier.TRANSIENT) + .excludeFieldsWithModifiers(Modifier.PRIVATE) + .setPrettyPrinting() + .create(); + + public static void init(String modid, Class config) { + translationPrefix = modid + ".midnightconfig."; + path = FabricLoader.getInstance().getConfigDir().resolve(modid + ".json"); + configClass = config; + + for (Field field : config.getFields()) { + Class type = field.getType(); + EntryInfo info = new EntryInfo(); + + Entry e; + try { e = field.getAnnotation(Entry.class); } + catch (Exception ignored) { continue; } + + info.width = e.width(); + info.field = field; + + if (type == int.class) textField(info, Integer::parseInt, INTEGER_ONLY, e.min(), e.max(), true); + else if (type == double.class) textField(info, Double::parseDouble, DECIMAL_ONLY, e.min(), e.max(),false); + else if (type == String.class) textField(info, String::length, null, Math.min(e.min(),0), Math.max(e.max(),1),true); + else if (type == boolean.class) { + Function func = value -> new LiteralText((Boolean) value ? "True" : "False").formatted((Boolean) value ? Formatting.GREEN : Formatting.RED); + info.widget = new AbstractMap.SimpleEntry>(button -> { + info.value = !(Boolean) info.value; + button.setMessage(func.apply(info.value)); + }, func); + } + else if (type.isEnum()) { + List values = Arrays.asList(field.getType().getEnumConstants()); + Function func = value -> new TranslatableText(translationPrefix + "enum." + type.getSimpleName() + "." + info.value.toString()); + info.widget = new AbstractMap.SimpleEntry>( button -> { + int index = values.indexOf(info.value) + 1; + info.value = values.get(index >= values.size()? 0 : index); + button.setMessage(func.apply(info.value)); + }, func); + } + else + continue; + + entries.add(info); + + try { info.defaultValue = field.get(null); } + catch (IllegalAccessException ignored) {} + + try { + info.dynamicTooltip = config.getMethod(e.dynamicTooltip()); + info.dynamicTooltip.setAccessible(true); + } catch (Exception ignored) {} + + } + + try { gson.fromJson(Files.newBufferedReader(path), config); } + catch (Exception e) { write(); } + + for (EntryInfo info : entries) { + try { + info.value = info.field.get(null); + info.tempValue = info.value.toString(); + } + catch (IllegalAccessException ignored) {} + } + + } + + private static void textField(EntryInfo info, Function f, Pattern pattern, double min, double max, boolean cast) { + boolean isNumber = pattern != null; + info.widget = (BiFunction>) (t, b) -> s -> { + s = s.trim(); + if (!(s.isEmpty() || !isNumber || pattern.matcher(s).matches())) + return false; + + Number value = 0; + boolean inLimits = false; + System.out.println(((isNumber ^ s.isEmpty()))); + System.out.println(!s.equals("-") && !s.equals(".")); + info.error = null; + if (!(isNumber && s.isEmpty()) && !s.equals("-") && !s.equals(".") && Double.parseDouble(s) < 999999999) { + value = f.apply(s); + inLimits = value.doubleValue() >= min && value.doubleValue() <= max; + info.error = inLimits? null : new AbstractMap.SimpleEntry<>(t, new LiteralText(value.doubleValue() < min ? + "§cMinimum " + (isNumber? "value" : "length") + (cast? " is " + (int)min : " is " + min) : + "§cMaximum " + (isNumber? "value" : "length") + (cast? " is " + (int)max : " is " + max))); + } + + info.tempValue = s; + t.setEditableColor(inLimits? 0xFFFFFFFF : 0xFFFF7777); + info.inLimits = inLimits; + b.active = entries.stream().allMatch(e -> e.inLimits); + + if (inLimits) + info.value = isNumber? value : s; + + return true; + }; + } + + public static void write() { + try { + if (!Files.exists(path)) Files.createFile(path); + Files.write(path, gson.toJson(configClass.newInstance()).getBytes()); + } catch (Exception e) { + e.printStackTrace(); + } + + } + + public Screen getScreen(Screen parent) { + return new TinyConfigScreen(parent); + } + + private static class TinyConfigScreen extends Screen { + protected TinyConfigScreen(Screen parent) { + super(new TranslatableText(MidnightConfig.translationPrefix + "title")); + this.parent = parent; + } + private final Screen parent; + + // Real Time config update // + @Override + public void tick() { + for (EntryInfo info : entries) + try { info.field.set(null, info.value); } + catch (IllegalAccessException ignore) {} + Blur.INSTANCE.onScreenChange(this); + } + + @Override + protected void init() { + super.init(); + this.addButton(new ButtonWidget(this.width / 2 - 154, this.height - 28, 150, 20, ScreenTexts.CANCEL, button -> { + try { gson.fromJson(Files.newBufferedReader(path), configClass); } + catch (Exception e) { write(); } + + for (EntryInfo info : entries) { + try { + info.value = info.field.get(null); + info.tempValue = info.value.toString(); + } + catch (IllegalAccessException ignored) {} + } + Objects.requireNonNull(client).openScreen(parent); + })); + + ButtonWidget done = this.addButton(new ButtonWidget(this.width / 2 + 4, this.height - 28, 150, 20, ScreenTexts.DONE, (button) -> { + for (EntryInfo info : entries) + try { info.field.set(null, info.value); } + catch (IllegalAccessException ignore) {} + write(); + Objects.requireNonNull(client).openScreen(parent); + })); + + int y = 45; + for (EntryInfo info : entries) { + addButton(new ButtonWidget(width - 155, y, 40,20, new LiteralText("Reset").formatted(Formatting.RED), (button -> { + info.value = info.defaultValue; + info.tempValue = info.value.toString(); + Objects.requireNonNull(client).openScreen(this); + }))); + + if (info.widget instanceof Map.Entry) { + Map.Entry> widget = (Map.Entry>) info.widget; + addButton(new ButtonWidget(width-110,y,info.width,20, widget.getValue().apply(info.value), widget.getKey())); + } + else { + TextFieldWidget widget = addButton(new TextFieldWidget(textRenderer, width-110, y, info.width, 20, null)); + widget.setText(info.tempValue); + + Predicate processor = ((BiFunction>) info.widget).apply(widget,done); + + widget.setTextPredicate(processor); + + children.add(widget); + } + y += 25; + } + + } + int aniX = this.width / 2; + @Override + public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) { + this.renderBackground(matrices); + + if (aniX < this.width / 2) { + aniX = aniX +40; + } + + int stringWidth = (int) (title.getString().length() * 2.75f); + this.fillGradient(matrices, this.width / 2 - stringWidth, 10, this.width /2 + stringWidth, 29, -1072689136, -804253680); + if (client.world == null) { + this.fillGradient(matrices, this.width / 2 - aniX, 35, width / 2 + aniX, this.height - 40, -1072689136, -804253680); + } + + super.render(matrices, mouseX, mouseY, delta); + drawCenteredText(matrices, textRenderer, title, width/2, 15, 0xFFFFFF); + + int y = 40; + for (EntryInfo info : entries) { + drawTextWithShadow(matrices, textRenderer, new TranslatableText(translationPrefix + info.field.getName()), 12, y + 10, 0xFFFFFF); + + if (info.error != null && info.error.getKey().isMouseOver(mouseX,mouseY)) + renderTooltip(matrices, info.error.getValue(), mouseX, mouseY); + else if (mouseY >= y && mouseY < (y + 25)) { + if (info.dynamicTooltip != null) { + try { + renderTooltip(matrices, (List) info.dynamicTooltip.invoke(null, entries), mouseX, mouseY); + y += 25; + continue; + } catch (Exception e) { e.printStackTrace(); } + } + String key = translationPrefix + info.field.getName() + ".tooltip"; + if (I18n.hasTranslation(key)) { + List list = new ArrayList<>(); + for (String str : I18n.translate(key).split("\n")) + list.add(new LiteralText(str)); + renderTooltip(matrices, list, mouseX, mouseY); + } + } + y += 25; + } + } + } + + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.FIELD) + public @interface Entry { + String dynamicTooltip() default ""; + int width() default 100; + double min() default Double.MIN_NORMAL; + double max() default Double.MAX_VALUE; + } +} \ No newline at end of file diff --git a/src/main/java/com/tterrag/blur/config/ModMenuIntegration.java b/src/main/java/com/tterrag/blur/config/ModMenuIntegration.java index 259cd21..ac025f7 100644 --- a/src/main/java/com/tterrag/blur/config/ModMenuIntegration.java +++ b/src/main/java/com/tterrag/blur/config/ModMenuIntegration.java @@ -2,7 +2,6 @@ import io.github.prospector.modmenu.api.ConfigScreenFactory; import io.github.prospector.modmenu.api.ModMenuApi; -import me.sargunvohra.mcmods.autoconfig1u.AutoConfig; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; @@ -11,6 +10,6 @@ public class ModMenuIntegration implements ModMenuApi { @Override public ConfigScreenFactory getModConfigScreenFactory() { - return parent -> AutoConfig.getConfigScreen(BlurConfig.class, parent).get(); + return parent -> new BlurConfig().getScreen(parent); } } diff --git a/src/main/resources/assets/blur/lang/de_de.json b/src/main/resources/assets/blur/lang/de_de.json index 859bad7..f186bd1 100644 --- a/src/main/resources/assets/blur/lang/de_de.json +++ b/src/main/resources/assets/blur/lang/de_de.json @@ -1,7 +1,7 @@ { - "text.autoconfig.blur.title": "Blur Konfiguration", - "text.autoconfig.blur.option.fadeTimeMillis": "Überblendzeit (in Millisekunden)", - "text.autoconfig.blur.option.radius": "Radius", - "text.autoconfig.blur.option.gradientStartColor": "Farbverlauf-Anfangsfarbe", - "text.autoconfig.blur.option.gradientEndColor": "Farbverlauf-Endfarbe" + "blur.midnightconfig.title": "Blur Konfiguration", + "blur.midnightconfig.fadeTimeMillis": "Überblendzeit (in Millisekunden)", + "blur.midnightconfig.radius": "Radius", + "blur.midnightconfig.gradientStartColor": "Farbverlauf-Anfangsfarbe", + "blur.midnightconfig.gradientEndColor": "Farbverlauf-Endfarbe" } \ No newline at end of file diff --git a/src/main/resources/assets/blur/lang/en_gb.json b/src/main/resources/assets/blur/lang/en_gb.json index 4b76cc1..61a93ff 100644 --- a/src/main/resources/assets/blur/lang/en_gb.json +++ b/src/main/resources/assets/blur/lang/en_gb.json @@ -1,7 +1,7 @@ { - "text.autoconfig.blur.title": "Blur Config", - "text.autoconfig.blur.option.fadeTimeMillis": "Fade Time (in milliseconds)", - "text.autoconfig.blur.option.radius": "Radius", - "text.autoconfig.blur.option.gradientStartColor": "Gradient Start Colour", - "text.autoconfig.blur.option.gradientEndColor": "Gradient End Colour" + "blur.midnightconfig.title": "Blur Config", + "blur.midnightconfig.fadeTimeMillis": "Fade Time (in milliseconds)", + "blur.midnightconfig.radius": "Radius", + "blur.midnightconfig.gradientStartColor": "Gradient Start Colour", + "blur.midnightconfig.gradientEndColor": "Gradient End Colour" } \ No newline at end of file diff --git a/src/main/resources/assets/blur/lang/en_us.json b/src/main/resources/assets/blur/lang/en_us.json index f181006..30eace1 100644 --- a/src/main/resources/assets/blur/lang/en_us.json +++ b/src/main/resources/assets/blur/lang/en_us.json @@ -1,7 +1,7 @@ { - "text.autoconfig.blur.title": "Blur Config", - "text.autoconfig.blur.option.fadeTimeMillis": "Fade Time (in milliseconds)", - "text.autoconfig.blur.option.radius": "Radius", - "text.autoconfig.blur.option.gradientStartColor": "Gradient Start Color", - "text.autoconfig.blur.option.gradientEndColor": "Gradient End Color" + "blur.midnightconfig.title": "Blur Config", + "blur.midnightconfig.fadeTimeMillis": "Fade Time (in milliseconds)", + "blur.midnightconfig.radius": "Radius", + "blur.midnightconfig.gradientStartColor": "Gradient Start Color", + "blur.midnightconfig.gradientEndColor": "Gradient End Color" } \ No newline at end of file diff --git a/src/main/resources/assets/blur/lang/zh_cn.json b/src/main/resources/assets/blur/lang/zh_cn.json index 2bbb57b..0738eea 100644 --- a/src/main/resources/assets/blur/lang/zh_cn.json +++ b/src/main/resources/assets/blur/lang/zh_cn.json @@ -1,7 +1,7 @@ { - "text.autoconfig.blur.title": "模糊配置", - "text.autoconfig.blur.option.fadeTimeMillis": "淡入时间(毫秒)", - "text.autoconfig.blur.option.radius": "半径", - "text.autoconfig.blur.option.gradientStartColor": "渐变起始色", - "text.autoconfig.blur.option.gradientEndColor": "渐变结束色" + "blur.midnightconfig.title": "模糊配置", + "blur.midnightconfig.fadeTimeMillis": "淡入时间(毫秒)", + "blur.midnightconfig.radius": "半径", + "blur.midnightconfig.gradientStartColor": "渐变起始色", + "blur.midnightconfig.gradientEndColor": "渐变结束色" } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 42f5f4a..0b23269 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -2,7 +2,7 @@ "schemaVersion": 1, "id": "blur", "name": "Blur (Fabric)", - "version": "2.0.0", + "version": "2.1.0", "environment": "client", "license": "MIT", "icon": "assets/blur/icon.png",