Skip to content

Commit

Permalink
BookTools - Added a rainbow text button to the book edit screen.
Browse files Browse the repository at this point in the history
  • Loading branch information
0xTas committed Apr 7, 2024
1 parent af29e0e commit b96e035
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
62 changes: 61 additions & 1 deletion src/main/java/dev/stardust/mixin/BookEditScreenMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

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;
import net.minecraft.client.gui.widget.ButtonWidget;
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;


/**
Expand All @@ -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<ButtonWidget> buttons = new ArrayList<>();

@Unique
Expand All @@ -49,14 +58,39 @@ 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;
((BookEditScreenAccessor) this).getCurrentPageSelectionManager().insert(format);
}
}

@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);
Expand Down Expand Up @@ -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<Boolean> 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"))
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/dev/stardust/util/StardustUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,34 @@
* @author Tas [@0xTas] <[email protected]>
**/
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"),
Expand Down

0 comments on commit b96e035

Please sign in to comment.