From 4dbf7925cc92cd6c405b0b00545241079b83f362 Mon Sep 17 00:00:00 2001 From: blackilykat Date: Mon, 4 Nov 2024 19:07:36 +0100 Subject: [PATCH] Add saturation and value to rainbowname + a preview in gui settings --- .../chipmunkmod/modules/RainbowName.java | 15 ++++++- .../components/options/IntSliderOption.java | 2 +- .../modules/fun/RainbowNameModule.java | 45 +++++++++++++++++++ src/main/resources/chipmunkmod.accesswidener | 3 +- 4 files changed, 62 insertions(+), 3 deletions(-) diff --git a/src/main/java/land/chipmunk/chipmunkmod/modules/RainbowName.java b/src/main/java/land/chipmunk/chipmunkmod/modules/RainbowName.java index e0a78ad..f97189d 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/modules/RainbowName.java +++ b/src/main/java/land/chipmunk/chipmunkmod/modules/RainbowName.java @@ -1,13 +1,19 @@ package land.chipmunk.chipmunkmod.modules; import land.chipmunk.chipmunkmod.ChipmunkMod; +import land.chipmunk.chipmunkmod.testclient.modules.fun.RainbowNameModule; import land.chipmunk.chipmunkmod.util.ColorUtilities; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.TextColor; import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.widget.TextFieldWidget; +import net.minecraft.client.gui.widget.TextWidget; import net.minecraft.client.network.ClientPlayNetworkHandler; +import net.minecraft.text.MutableText; +import net.minecraft.text.Text; +import net.minecraft.text.TextContent; import java.util.Random; import java.util.Timer; @@ -30,6 +36,8 @@ public class RainbowName { private int startHue = 0; public double speed = 1; + public int saturation = 255; + public int value = 255; public void init () { final TimerTask task = new TimerTask() { @@ -120,16 +128,21 @@ public class RainbowName { Component component = Component.empty(); StringBuilder essentialsNickname = new StringBuilder(); + MutableText preview = Text.empty(); for (char character : displayName.toCharArray()) { - String color = String.format("%06x", ColorUtilities.hsvToRgb(hue, 100, 100)); + int colorRaw = ColorUtilities.hsvToRgb(hue, saturation, value); + String color = String.format("%06x", colorRaw); component = component.append(Component.text(character).color(TextColor.fromHexString("#" + color))); essentialsNickname.append("\u00a7#").append(color).append(character != ' ' ? character : '_'); + preview.append(Text.of(character+"").copy().withColor(colorRaw)); hue = (hue + increment) % 360; } CommandCore.INSTANCE.run("essentials:nick " + ChipmunkMod.CONFIG.defaultUsername + " " + essentialsNickname); + ((RainbowNameModule.MutableTextWidget)RainbowNameModule.preview.widget).setText(preview); + startHue = (startHue + increment) % 360; } catch (Exception e) { e.printStackTrace(); diff --git a/src/main/java/land/chipmunk/chipmunkmod/testclient/gui/components/options/IntSliderOption.java b/src/main/java/land/chipmunk/chipmunkmod/testclient/gui/components/options/IntSliderOption.java index d82a1f8..1b4fc78 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/testclient/gui/components/options/IntSliderOption.java +++ b/src/main/java/land/chipmunk/chipmunkmod/testclient/gui/components/options/IntSliderOption.java @@ -39,7 +39,7 @@ public class IntSliderOption extends Option { @Override public void setValueFromString(String string) { optionValue = Integer.valueOf(string); - sliderWidget.value = optionValue; + sliderWidget.value = (double) (optionValue - minValue) / (maxValue - minValue); sliderWidget.updateMessage(); } diff --git a/src/main/java/land/chipmunk/chipmunkmod/testclient/modules/fun/RainbowNameModule.java b/src/main/java/land/chipmunk/chipmunkmod/testclient/modules/fun/RainbowNameModule.java index bedcbe0..26ca124 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/testclient/modules/fun/RainbowNameModule.java +++ b/src/main/java/land/chipmunk/chipmunkmod/testclient/modules/fun/RainbowNameModule.java @@ -2,22 +2,67 @@ package land.chipmunk.chipmunkmod.testclient.modules.fun; import land.chipmunk.chipmunkmod.modules.RainbowName; import land.chipmunk.chipmunkmod.testclient.gui.components.Module; +import land.chipmunk.chipmunkmod.testclient.gui.components.Option; import land.chipmunk.chipmunkmod.testclient.gui.components.options.DoubleSliderOption; +import land.chipmunk.chipmunkmod.testclient.gui.components.options.IntSliderOption; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.font.TextRenderer; +import net.minecraft.client.gui.widget.TextWidget; +import net.minecraft.text.MutableText; +import net.minecraft.text.Text; public class RainbowNameModule extends Module { public static RainbowNameModule instance = new RainbowNameModule(); public static DoubleSliderOption speedOption; + public static IntSliderOption saturationOption; + public static IntSliderOption valueOption; + public static Preview preview; public RainbowNameModule() { super("Rainbow name"); speedOption = new DoubleSliderOption("Speed", 1, 0.1, 2, 2); withOption(speedOption); + saturationOption = new IntSliderOption("Saturation", 100, 0, 100); + withOption(saturationOption); + valueOption = new IntSliderOption("Value", 100, 0, 100); + withOption(valueOption); + preview = new Preview("Preview"); + withOption(preview); + needsInWorld = true; // onActivate(RainbowName.INSTANCE::enable); onDeactivate(RainbowName.INSTANCE::disable); atEndTick(() -> { RainbowName.INSTANCE.enable(); RainbowName.INSTANCE.speed = speedOption.optionValue; + RainbowName.INSTANCE.saturation = saturationOption.optionValue; + RainbowName.INSTANCE.value = valueOption.optionValue; }); } + + public static class Preview extends Option { + public Preview(String name) { + super(name, null); + widget = new MutableTextWidget(Text.empty(), MinecraftClient.getInstance().textRenderer); + } + + @Override + public void setValueFromString(String string) { } + + @Override + public String getValueAsString() { + return "null"; + } + } + + public static class MutableTextWidget extends TextWidget { + + public MutableTextWidget(Text message, TextRenderer textRenderer) { + super(100, 20, message, textRenderer); + } + + public void setText(Text text) { + this.message = text; + } + } } diff --git a/src/main/resources/chipmunkmod.accesswidener b/src/main/resources/chipmunkmod.accesswidener index 9659c7b..5dd6752 100644 --- a/src/main/resources/chipmunkmod.accesswidener +++ b/src/main/resources/chipmunkmod.accesswidener @@ -2,4 +2,5 @@ accessWidener v2 named accessible field net/minecraft/client/gui/screen/Screen drawables Ljava/util/List; accessible field net/minecraft/client/gui/widget/CheckboxWidget checked Z accessible field net/minecraft/client/gui/widget/SliderWidget value D -accessible method net/minecraft/client/gui/widget/SliderWidget updateMessage ()V \ No newline at end of file +accessible method net/minecraft/client/gui/widget/SliderWidget updateMessage ()V +accessible field net/minecraft/client/gui/widget/ClickableWidget message Lnet/minecraft/text/Text; \ No newline at end of file