Skip to content

Commit

Permalink
Added book color functionality to ColorSigns
Browse files Browse the repository at this point in the history
  • Loading branch information
vitphire committed Apr 9, 2024
1 parent ca1bbde commit 133eae9
Showing 1 changed file with 39 additions and 7 deletions.
46 changes: 39 additions & 7 deletions src/main/java/anticope/rejects/modules/ColorSigns.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,63 @@

import anticope.rejects.MeteorRejectsAddon;
import meteordevelopment.meteorclient.events.packets.PacketEvent;
import meteordevelopment.meteorclient.settings.BoolSetting;
import meteordevelopment.meteorclient.settings.Setting;
import meteordevelopment.meteorclient.settings.SettingGroup;
import meteordevelopment.meteorclient.systems.modules.Module;
import meteordevelopment.orbit.EventHandler;
import net.minecraft.network.packet.c2s.play.BookUpdateC2SPacket;
import net.minecraft.network.packet.c2s.play.UpdateSignC2SPacket;
import net.minecraft.network.packet.s2c.play.GameJoinS2CPacket;
import net.minecraft.server.MinecraftServer;

import java.util.List;

public class ColorSigns extends Module {

private final SettingGroup sgGeneral = settings.getDefaultGroup();

private final Setting<Boolean> signs = sgGeneral.add(new BoolSetting.Builder()
.name("signs")
.description("Allows you to use colors in signs.")
.defaultValue(true)
.build()
);

private final Setting<Boolean> books = sgGeneral.add(new BoolSetting.Builder()
.name("books")
.description("Allows you to use colors in books.")
.defaultValue(false)
.build()
);

public ColorSigns() {
super(MeteorRejectsAddon.CATEGORY, "color-signs", "Allows you to use colors on signs on NON-PAPER servers (use \"&\" for color symbols)");
}

@EventHandler
private void onPacketSend(PacketEvent.Send event) {
if (event.packet instanceof GameJoinS2CPacket) {
checkWarning();
return;
}
if (!(event.packet instanceof UpdateSignC2SPacket)) return;
UpdateSignC2SPacket p = (UpdateSignC2SPacket)event.packet;
for (int l = 0; l < p.getText().length; l++) {
String newText = p.getText()[l].replaceAll("(?i)\u00a7|&([0-9A-FK-OR])", "\u00a7\u00a7$1$1");
p.getText()[l] = newText;
if (signs.get() && event.packet instanceof UpdateSignC2SPacket packet) {
for (int line = 0; line < packet.getText().length; line++) {
packet.getText()[line] = packet.getText()[line]
.replaceAll("(?i)(?:&|(?<!§)§)([0-9A-Z])", "§§$1$1");
}
}
if (books.get() && event.packet instanceof BookUpdateC2SPacket packet) {
List<String> newPages = packet.getPages().stream().map(text ->
text.replaceAll("(?i)&([0-9A-Z])", "§$1")).toList();
// BookUpdateC2SPacket.pages is final, so we need to create a new packet
if (!packet.getPages().equals(newPages)) {
assert mc.getNetworkHandler() != null;
mc.getNetworkHandler().sendPacket(new BookUpdateC2SPacket(
packet.getSlot(), newPages, packet.getTitle()));
event.cancel();
}
}
event.packet = p;
}

@Override
Expand Down

0 comments on commit 133eae9

Please sign in to comment.