diff --git a/src/main/java/dev/stardust/mixin/BookEditScreenMixin.java b/src/main/java/dev/stardust/mixin/BookEditScreenMixin.java index 8c6c7f9..0db9bfc 100644 --- a/src/main/java/dev/stardust/mixin/BookEditScreenMixin.java +++ b/src/main/java/dev/stardust/mixin/BookEditScreenMixin.java @@ -2,10 +2,12 @@ import java.util.ArrayList; import net.minecraft.text.Text; +import javax.annotation.Nullable; import org.spongepowered.asm.mixin.*; import dev.stardust.util.StardustUtil; import dev.stardust.modules.BookTools; import net.minecraft.client.gui.screen.Screen; +import io.netty.util.internal.ThreadLocalRandom; import net.minecraft.client.gui.tooltip.Tooltip; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; @@ -13,6 +15,7 @@ import net.minecraft.client.gui.screen.ingame.BookEditScreen; import meteordevelopment.meteorclient.systems.modules.Modules; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; /** @@ -28,9 +31,15 @@ public abstract class BookEditScreenMixin extends Screen { // See BookTools.java protected BookEditScreenMixin(Text title) { super(title); } + @Unique + private boolean rainbowMode = false; @Unique private boolean didFormatPage = false; @Unique + private String activeFormatting = ""; + @Unique + private @Nullable StardustUtil.RainbowColor lastCC = null; + @Unique private final ArrayList buttons = new ArrayList<>(); @Unique @@ -49,7 +58,9 @@ private void onClickColorButton(ButtonWidget btn) { private void onClickFormatButton(ButtonWidget btn) { String format = btn.getMessage().getString().substring(0, 2); - if (this.signing) { + if (rainbowMode) { + activeFormatting = format; + }else if (this.signing) { ((BookEditScreenAccessor) this).getBookTitleSelectionManager().insert(format); } else { this.didFormatPage = true; @@ -57,6 +68,29 @@ private void onClickFormatButton(ButtonWidget btn) { } } + @Unique + private void onClickRainbowButton(ButtonWidget btn) { + rainbowMode = !rainbowMode; + if (rainbowMode) { + btn.setMessage(Text.of(uCC()+"🌈")); + btn.setTooltip(Tooltip.of(Text.of(uCC()+"R"+uCC()+"a"+uCC()+"i"+uCC()+"n"+uCC()+"b"+uCC()+"o"+uCC()+"w "+uCC()+"M"+uCC()+"o"+uCC()+"d"+uCC()+"e"+" §2On"))); + } else { + btn.setMessage(Text.of("🌈")); + btn.setTooltip(Tooltip.of(Text.of(uCC()+"R"+uCC()+"a"+uCC()+"i"+uCC()+"n"+uCC()+"b"+uCC()+"o"+uCC()+"w "+uCC()+"M"+uCC()+"o"+uCC()+"d"+uCC()+"e"+" §4Off"))); + } + } + + @Unique + private String uCC() { + // Return a random color code that follows the pattern of the rainbow. + if (lastCC == null) { + lastCC = StardustUtil.RainbowColor.getFirst(); + } else { + lastCC = StardustUtil.RainbowColor.getNext(lastCC); + } + return lastCC.labels[ThreadLocalRandom.current().nextInt(lastCC.labels.length)]; + } + @Inject(method = "init", at = @At("TAIL")) private void mixinInit(CallbackInfo ci) { BookTools bookTools = Modules.get().get(BookTools.class); @@ -111,6 +145,32 @@ private void mixinInit(CallbackInfo ci) { .build() ) ); + + if (odd) offset += 12; + odd = !odd; + this.buttons.add( + this.addDrawableChild( + ButtonWidget.builder( + Text.of("🌈"), + this::onClickRainbowButton + ) + .dimensions(odd ? this.width / 2 - 100 : this.width / 2 - 112, 47+offset, 22, 10) + .tooltip(Tooltip.of(Text.of(uCC()+"R"+uCC()+"a"+uCC()+"i"+uCC()+"n"+uCC()+"b"+uCC()+"o"+uCC()+"w "+uCC()+"M"+uCC()+"o"+uCC()+"d"+uCC()+"e"+" §4Off"))) + .build() + ) + ); + } + + @Inject(method = "charTyped", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/util/SelectionManager;insert(Ljava/lang/String;)V", shift = At.Shift.BEFORE)) + private void mixinCharTyped(char chr, int modifiers, CallbackInfoReturnable cir) { + if (!rainbowMode || signing) return; + didFormatPage = true; + if (activeFormatting.equals("§r")) { + activeFormatting = ""; + ((BookEditScreenAccessor) this).getCurrentPageSelectionManager().insert("§r" + uCC()); + } else { + ((BookEditScreenAccessor) this).getCurrentPageSelectionManager().insert(uCC() + activeFormatting); + } } @Inject(method = "finalizeBook", at = @At("HEAD")) diff --git a/src/main/java/dev/stardust/util/StardustUtil.java b/src/main/java/dev/stardust/util/StardustUtil.java index 2ed8b75..07370c2 100644 --- a/src/main/java/dev/stardust/util/StardustUtil.java +++ b/src/main/java/dev/stardust/util/StardustUtil.java @@ -19,6 +19,34 @@ * @author Tas [@0xTas] **/ public class StardustUtil { + public enum RainbowColor { + Reds(new String[]{"§c", "§4"}), + Yellows(new String[]{"§e", "§6"}), + Greens(new String[]{"§a", "§2"}), + Cyans(new String[]{"§b", "§3"}), + Blues(new String[]{"§9", "§1"}), + Purples(new String[]{"§d", "§5"}); + + public final String[] labels; + + RainbowColor(String[] labels) { this.labels = labels; } + + public static RainbowColor getFirst() { + return RainbowColor.values()[ThreadLocalRandom.current().nextInt(RainbowColor.values().length)]; + } + + public static RainbowColor getNext(RainbowColor previous) { + return switch (previous) { + case Reds -> Yellows; + case Yellows -> Greens; + case Greens -> Cyans; + case Cyans -> Blues; + case Blues -> Purples; + case Purples -> Reds; + }; + } + } + public enum TextColor { Black("§0"), White("§f"), Gray("§8"), Light_Gray("§7"), Dark_Green("§2"), Green("§a"), Dark_Aqua("§3"), Aqua("§b"),