From efa2c0dd2c1500edff663a2efd0b1d1ea902ce34 Mon Sep 17 00:00:00 2001 From: bowser0000 Date: Sun, 2 May 2021 20:24:51 -0400 Subject: Add custom name colours --- src/main/java/me/Danker/DankersSkyblockMod.java | 10 ++- .../java/me/Danker/commands/DankerGuiCommand.java | 1 + .../java/me/Danker/commands/ToggleCommand.java | 12 +++- .../java/me/Danker/features/ColouredNames.java | 78 ++++++++++++++++++++++ src/main/java/me/Danker/gui/DankerGui.java | 7 ++ .../java/me/Danker/handlers/ConfigHandler.java | 1 + 6 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 src/main/java/me/Danker/features/ColouredNames.java (limited to 'src/main/java/me') diff --git a/src/main/java/me/Danker/DankersSkyblockMod.java b/src/main/java/me/Danker/DankersSkyblockMod.java index f176bbe..d2b8017 100644 --- a/src/main/java/me/Danker/DankersSkyblockMod.java +++ b/src/main/java/me/Danker/DankersSkyblockMod.java @@ -58,13 +58,15 @@ import org.lwjgl.input.Mouse; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.UnsupportedAudioFileException; import java.io.IOException; +import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; @Mod(modid = DankersSkyblockMod.MODID, version = DankersSkyblockMod.VERSION, clientSideOnly = true) public class DankersSkyblockMod { public static final String MODID = "Danker's Skyblock Mod"; - public static final String VERSION = "1.8.7-beta1"; + public static final String VERSION = "1.8.7-beta2"; public static int titleTimer = -1; public static boolean showTitle = false; public static String titleText = ""; @@ -99,6 +101,7 @@ public class DankersSkyblockMod { MinecraftForge.EVENT_BUS.register(new CakeTimer()); MinecraftForge.EVENT_BUS.register(new ChronomatronSolver()); MinecraftForge.EVENT_BUS.register(new ClickInOrderSolver()); + MinecraftForge.EVENT_BUS.register(new ColouredNames()); MinecraftForge.EVENT_BUS.register(new CreeperSolver()); MinecraftForge.EVENT_BUS.register(new CustomMusic()); MinecraftForge.EVENT_BUS.register(new DungeonTimer()); @@ -154,8 +157,11 @@ public class DankersSkyblockMod { } new Thread(() -> { - DankersSkyblockMod.data = APIHandler.getResponse("https://raw.githubusercontent.com/bowser0000/SkyblockMod-REPO/main/data.json"); + data = APIHandler.getResponse("https://raw.githubusercontent.com/bowser0000/SkyblockMod-REPO/main/data.json"); System.out.println("Loaded data from GitHub?: " + (DankersSkyblockMod.data != null && DankersSkyblockMod.data.has("trivia"))); + ColouredNames.users = data.get("colourednames").getAsJsonObject().entrySet().stream() + .map(Map.Entry::getKey) + .collect(Collectors.toCollection(ArrayList::new)); }).start(); } diff --git a/src/main/java/me/Danker/commands/DankerGuiCommand.java b/src/main/java/me/Danker/commands/DankerGuiCommand.java index 6f548fa..a92cb80 100644 --- a/src/main/java/me/Danker/commands/DankerGuiCommand.java +++ b/src/main/java/me/Danker/commands/DankerGuiCommand.java @@ -95,6 +95,7 @@ public class DankerGuiCommand extends CommandBase { debug.append("[teammatesinradius][").append(ToggleCommand.teammatesInRadius).append("]\n"); debug.append("[gianthp][").append(ToggleCommand.giantHP).append("]\n"); debug.append("[hidepetcandy][").append(ToggleCommand.hidePetCandy).append("]\n"); + debug.append("[customcolourednames][").append(ToggleCommand.customColouredNames).append("]\n"); debug.append("[dungeonbossmusic][").append(ToggleCommand.dungeonBossMusic).append("]\n"); debug.append("[bloodroommusic][").append(ToggleCommand.bloodRoomMusic).append("]\n"); debug.append("[dungeonmusic][").append(ToggleCommand.dungeonMusic).append("]\n"); diff --git a/src/main/java/me/Danker/commands/ToggleCommand.java b/src/main/java/me/Danker/commands/ToggleCommand.java index 27167b6..1f2fe5c 100644 --- a/src/main/java/me/Danker/commands/ToggleCommand.java +++ b/src/main/java/me/Danker/commands/ToggleCommand.java @@ -37,6 +37,7 @@ public class ToggleCommand extends CommandBase implements ICommand { public static boolean teammatesInRadius; public static boolean giantHP; public static boolean hidePetCandy; + public static boolean customColouredNames; // Chat Messages public static boolean sceptreMessages; public static boolean midasStaffMessages; @@ -97,7 +98,7 @@ public class ToggleCommand extends CommandBase implements ICommand { "startswithterminal/selectallterminal/clickinorderterminal/" + "ultrasequencer/chronomatron/superpairs/hidetooltipsinaddons/pickblock/" + "melodytooltips/highlightslayers/highlightarachne/highlightskeletonmasters/teammatesinradius/" + - "gianthp/hidepetcandy/dungeonbossmusic/bloodroommusic/dungeonmusic/list>"; + "gianthp/hidepetcandy/customcolorednames/dungeonbossmusic/bloodroommusic/dungeonmusic/list>"; } public static String usage(ICommandSender arg0) { @@ -126,7 +127,7 @@ public class ToggleCommand extends CommandBase implements ICommand { "ultrasequencer", "chronomatron", "superpairs", "hidetooltipsinaddons", "pickblock", "melodytooltips", "highlightslayers", "highlightskeletonmasters", "dungeonbossmusic", "bloodroommusic", "dungeonmusic", - "teammatesinradius", "gianthp", "hidepetcandy", "list"); + "teammatesinradius", "gianthp", "hidepetcandy", "customcolorednames", "list"); } return null; } @@ -422,6 +423,12 @@ public class ToggleCommand extends CommandBase implements ICommand { ConfigHandler.writeBooleanConfig("toggles", "HidePetCandy", hidePetCandy); player.addChatMessage(new ChatComponentText(DankersSkyblockMod.MAIN_COLOUR + "Hide pet candy has been set to " + DankersSkyblockMod.SECONDARY_COLOUR + hidePetCandy + DankersSkyblockMod.MAIN_COLOUR + ".")); break; + case "customcolorednames": + case "customcolourednames": + customColouredNames = !customColouredNames; + ConfigHandler.writeBooleanConfig("toggles", "CustomColouredNames", customColouredNames); + player.addChatMessage(new ChatComponentText(DankersSkyblockMod.MAIN_COLOUR + "Custom name colors has been set to " + DankersSkyblockMod.SECONDARY_COLOUR + customColouredNames + DankersSkyblockMod.MAIN_COLOUR + ".")); + break; case "dungeonbossmusic": dungeonBossMusic = !dungeonBossMusic; CustomMusic.dungeonboss.stop(); @@ -489,6 +496,7 @@ public class ToggleCommand extends CommandBase implements ICommand { DankersSkyblockMod.TYPE_COLOUR + " Teammates in radius: " + DankersSkyblockMod.VALUE_COLOUR + teammatesInRadius + "\n" + DankersSkyblockMod.TYPE_COLOUR + " Giant HP display: " + DankersSkyblockMod.VALUE_COLOUR + giantHP + "\n" + DankersSkyblockMod.TYPE_COLOUR + " Hide pet candy: " + DankersSkyblockMod.VALUE_COLOUR + hidePetCandy + "\n" + + DankersSkyblockMod.TYPE_COLOUR + " Custom name colors: " + DankersSkyblockMod.VALUE_COLOUR + customColouredNames + "\n" + DankersSkyblockMod.TYPE_COLOUR + " Custom dungeon boss music: " + DankersSkyblockMod.VALUE_COLOUR + dungeonBossMusic + "\n" + DankersSkyblockMod.TYPE_COLOUR + " Custom blood room music: " + DankersSkyblockMod.VALUE_COLOUR + bloodRoomMusic + "\n" + DankersSkyblockMod.TYPE_COLOUR + " Custom dungeon music: " + DankersSkyblockMod.VALUE_COLOUR + dungeonMusic diff --git a/src/main/java/me/Danker/features/ColouredNames.java b/src/main/java/me/Danker/features/ColouredNames.java new file mode 100644 index 0000000..eec2d5c --- /dev/null +++ b/src/main/java/me/Danker/features/ColouredNames.java @@ -0,0 +1,78 @@ +package me.Danker.features; + +import me.Danker.DankersSkyblockMod; +import me.Danker.commands.ToggleCommand; +import me.Danker.utils.Utils; +import net.minecraft.util.ChatComponentText; +import net.minecraft.util.EnumChatFormatting; +import net.minecraft.util.IChatComponent; +import net.minecraftforge.client.event.ClientChatReceivedEvent; +import net.minecraftforge.event.entity.player.ItemTooltipEvent; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; + +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class ColouredNames { + + public static List users = new ArrayList<>(); + + @SubscribeEvent + public void onChat(ClientChatReceivedEvent event) { + if (!ToggleCommand.customColouredNames || !Utils.inSkyblock || event.type != 0) return; + long startTime = System.currentTimeMillis(); + + for (String user : users) { + if (event.message.getFormattedText().contains(user)) { + event.message = replaceChat(event.message, user); + } + } + System.out.println("Chat time: " + ((System.currentTimeMillis() - startTime) / 1000D) + "s"); + } + + @SubscribeEvent + public void onTooltip(ItemTooltipEvent event) { + if (!ToggleCommand.customColouredNames || !Utils.inSkyblock) return; + + for (String user : users) { + for (int i = 0; i < event.toolTip.size(); i++) { + if (event.toolTip.get(i).contains(user)) { + event.toolTip.set(i, replaceName(event.toolTip.get(i), user, getColourFromName(user))); + } + } + } + } + + // https://github.com/SteveKunG/SkyBlockcatia/blob/1.8.9/src/main/java/com/stevekung/skyblockcatia/utils/SupporterUtils.java#L53 + public static String replaceName(String text, String name, String colour) { + String namePattern = "(?:(?:\\u00a7[0-9a-fbr])\\B(?:" + name + ")\\b)|(?:\\u00a7[rb]" + name + "\\u00a7r)|\\b" + name + "\\b"; + Matcher prevColourMat = Pattern.compile("(?:.*(?:(?\\u00a7[0-9a-fbr])" + name + ")\\b.*)").matcher(text); + + if (prevColourMat.matches()) { + return text.replaceAll(namePattern, colour + name + prevColourMat.group("colour")); + } + return text.replaceAll(namePattern, colour + name + EnumChatFormatting.WHITE); + } + + // https://github.com/Moulberry/Hychat/blob/master/src/main/java/io/github/moulberry/hychat/util/TextProcessing.java#L23 + public static IChatComponent replaceChat(IChatComponent component, String user) { + IChatComponent newComponent; + ChatComponentText text = (ChatComponentText) component; + + newComponent = new ChatComponentText(replaceName(text.getUnformattedTextForChat(), user, getColourFromName(user))); + newComponent.setChatStyle(text.getChatStyle().createShallowCopy()); + + for (IChatComponent sibling : text.getSiblings()) { + newComponent.appendSibling(replaceChat(sibling, user)); + } + + return newComponent; + } + + public static String getColourFromName(String name) { + return "ยง" + DankersSkyblockMod.data.get("colourednames").getAsJsonObject().get(name).getAsString(); + } + +} diff --git a/src/main/java/me/Danker/gui/DankerGui.java b/src/main/java/me/Danker/gui/DankerGui.java index 16e0640..ce795f3 100644 --- a/src/main/java/me/Danker/gui/DankerGui.java +++ b/src/main/java/me/Danker/gui/DankerGui.java @@ -66,6 +66,7 @@ public class DankerGui extends GuiScreen { private GuiButton teammatesInRadius; private GuiButton giantHP; private GuiButton hidePetCandy; + private GuiButton customColouredNames; // Chat Messages private GuiButton sceptreMessages; private GuiButton midasStaffMessages; @@ -153,6 +154,7 @@ public class DankerGui extends GuiScreen { teammatesInRadius = new FeatureButton("Display Players in 30 Block Radius: " + Utils.getColouredBoolean(ToggleCommand.teammatesInRadius), "Displays dungeon teammates in 30 block radius for tether and diversion."); giantHP = new FeatureButton("Display Giant HP: " + Utils.getColouredBoolean(ToggleCommand.giantHP), "Displays health of Sadan's giants during F6 bossfight and F7 blood room."); hidePetCandy = new FeatureButton("Hide Pet Candy: " + Utils.getColouredBoolean(ToggleCommand.hidePetCandy), "Hide pet candy in pet tooltips."); + customColouredNames = new FeatureButton("Custom Name Colors: " + Utils.getColouredBoolean(ToggleCommand.customColouredNames), "Replaces some player's usernames with a custom color."); allButtons.clear(); allButtons.add(changeDisplay); @@ -198,6 +200,7 @@ public class DankerGui extends GuiScreen { allButtons.add(teammatesInRadius); allButtons.add(giantHP); allButtons.add(hidePetCandy); + allButtons.add(customColouredNames); search.setText(initSearchText); search.setVisible(true); @@ -440,6 +443,10 @@ public class DankerGui extends GuiScreen { ToggleCommand.hidePetCandy = !ToggleCommand.hidePetCandy; ConfigHandler.writeBooleanConfig("toggles", "HidePetCandy", ToggleCommand.hidePetCandy); hidePetCandy.displayString = "Hide Pet Candy: " + Utils.getColouredBoolean(ToggleCommand.hidePetCandy); + } else if (button == customColouredNames) { + ToggleCommand.customColouredNames = !ToggleCommand.customColouredNames; + ConfigHandler.writeBooleanConfig("toggles", "CustomColouredNames", ToggleCommand.customColouredNames); + customColouredNames.displayString = "Custom Name Colors: " + Utils.getColouredBoolean(ToggleCommand.customColouredNames); } } diff --git a/src/main/java/me/Danker/handlers/ConfigHandler.java b/src/main/java/me/Danker/handlers/ConfigHandler.java index db33cb2..6d32f99 100644 --- a/src/main/java/me/Danker/handlers/ConfigHandler.java +++ b/src/main/java/me/Danker/handlers/ConfigHandler.java @@ -214,6 +214,7 @@ public class ConfigHandler { ToggleCommand.teammatesInRadius = initBoolean("toggles", "TeammatesInRadius", false); ToggleCommand.giantHP = initBoolean("toggles", "GiantHP", false); ToggleCommand.hidePetCandy = initBoolean("toggles", "HidePetCandy", false); + ToggleCommand.customColouredNames = initBoolean("toggles", "CustomColouredNames", true); // Chat Messages ToggleCommand.sceptreMessages = initBoolean("toggles", "SceptreMessages", true); ToggleCommand.midasStaffMessages = initBoolean("toggles", "MidasStaffMessages", true); -- cgit