Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added book color functionality to ColorSigns #338

Merged
merged 2 commits into from
Apr 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 44 additions & 8 deletions src/main/java/anticope/rejects/modules/ColorSigns.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +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 All @@ -35,7 +68,10 @@ public void onActivate() {
}

private void checkWarning() {
String brand = mc.player.getServer().getServerModName();
assert mc.player != null;
MinecraftServer server = mc.player.getServer();
if (server == null) return;
String brand = server.getServerModName();
if (brand == null) return;
if (brand.contains("Paper")) warning("You are on a paper server. Color signs won't work here");
}
Expand Down
Loading