diff options
author | Cow <cow@volloeko.de> | 2020-04-18 18:31:36 +0200 |
---|---|---|
committer | Cow <cow@volloeko.de> | 2020-04-18 18:31:36 +0200 |
commit | a76d00bec256964083a636e443748f70a76e47cd (patch) | |
tree | 4dcb2b300bd710b4b9af48481c8bcaa7c82b8ae1 /src/main | |
parent | b0403a905f6abffb44d1bf3502ae4616f2cf68a9 (diff) | |
download | Cowlection-a76d00bec256964083a636e443748f70a76e47cd.tar.gz Cowlection-a76d00bec256964083a636e443748f70a76e47cd.tar.bz2 Cowlection-a76d00bec256964083a636e443748f70a76e47cd.zip |
Adapted to new login/logout notifications of Hypixel
also:
- Added ¯\_(ツ)_/¯
- Improved copying of chat components
Diffstat (limited to 'src/main')
6 files changed, 124 insertions, 37 deletions
diff --git a/src/main/java/eu/olli/cowmoonication/Cowmoonication.java b/src/main/java/eu/olli/cowmoonication/Cowmoonication.java index 544e692..1175541 100644 --- a/src/main/java/eu/olli/cowmoonication/Cowmoonication.java +++ b/src/main/java/eu/olli/cowmoonication/Cowmoonication.java @@ -1,6 +1,7 @@ package eu.olli.cowmoonication; import eu.olli.cowmoonication.command.MooCommand; +import eu.olli.cowmoonication.command.ShrugCommand; import eu.olli.cowmoonication.config.MooConfig; import eu.olli.cowmoonication.friends.Friends; import eu.olli.cowmoonication.listener.ChatListener; @@ -25,7 +26,7 @@ import java.io.File; updateJSON = "https://raw.githubusercontent.com/cow-mc/Cowmoonication/master/update.json") public class Cowmoonication { public static final String MODID = "cowmoonication"; - public static final String VERSION = "1.8.9-0.3.1"; + public static final String VERSION = "1.8.9-0.4.0"; public static final String MODNAME = "Cowmoonication"; private File modsDir; private MooConfig config; @@ -52,10 +53,10 @@ public class Cowmoonication { @EventHandler public void init(FMLInitializationEvent e) { - MinecraftForge.EVENT_BUS.register(new ChatListener(this)); MinecraftForge.EVENT_BUS.register(new PlayerListener(this)); ClientCommandHandler.instance.registerCommand(new MooCommand(this)); + ClientCommandHandler.instance.registerCommand(new ShrugCommand(this)); } @EventHandler diff --git a/src/main/java/eu/olli/cowmoonication/command/MooCommand.java b/src/main/java/eu/olli/cowmoonication/command/MooCommand.java index f265d8a..81c8c15 100644 --- a/src/main/java/eu/olli/cowmoonication/command/MooCommand.java +++ b/src/main/java/eu/olli/cowmoonication/command/MooCommand.java @@ -32,7 +32,7 @@ public class MooCommand extends CommandBase { @Override public void processCommand(ICommandSender sender, String[] args) throws CommandException { if (args.length == 0) { - main.getChatHelper().sendMessage(new ChatComponentTranslation(getCommandUsage(sender))); + sendCommandUsage(sender); return; } // sub commands: friends @@ -53,12 +53,9 @@ public class MooCommand extends CommandBase { } else if (args[0].equalsIgnoreCase("nameChangeCheck")) { main.getChatHelper().sendMessage(EnumChatFormatting.GOLD, "Looking for best friends that have changed their name... This will take a few seconds..."); main.getFriends().updateBestFriends(true); - } else if (args[0].equalsIgnoreCase("toggle")) { - main.getConfig().toggleNotifications(); - main.getChatHelper().sendMessage(EnumChatFormatting.GREEN, "\u2714 Switched all non-best friend login/logout notifications " + (MooConfig.filterFriendNotifications ? EnumChatFormatting.DARK_GREEN + "off" : EnumChatFormatting.DARK_RED + "on")); } // sub-commands: miscellaneous - else if (args[0].equalsIgnoreCase("config")) { + else if (args[0].equalsIgnoreCase("config") || args[0].equalsIgnoreCase("toggle")) { new TickDelay(() -> Minecraft.getMinecraft().displayGuiScreen(new MooGuiConfig(null)), 1); // delay by 1 tick, because the chat closing would close the new gui instantly as well. } else if (args[0].equalsIgnoreCase("guiscale")) { int currentGuiScale = (Minecraft.getMinecraft()).gameSettings.guiScale; @@ -69,6 +66,8 @@ public class MooCommand extends CommandBase { Minecraft.getMinecraft().gameSettings.guiScale = scale; main.getChatHelper().sendMessage(EnumChatFormatting.GREEN, "\u2714 New GUI scale: " + EnumChatFormatting.DARK_GREEN + scale + EnumChatFormatting.GREEN + " (previous: " + EnumChatFormatting.DARK_GREEN + currentGuiScale + EnumChatFormatting.GREEN + ")"); } + } else if (args[0].equalsIgnoreCase("shrug")) { + main.getChatHelper().sendShrug(buildString(args, 1)); } else if (args[0].equalsIgnoreCase("apikey")) { handleApiKey(args); } @@ -110,7 +109,7 @@ public class MooCommand extends CommandBase { } // "catch-all" remaining sub-commands else { - main.getChatHelper().sendMessage(new ChatComponentTranslation(getCommandUsage(sender))); + sendCommandUsage(sender); } } @@ -229,6 +228,10 @@ public class MooCommand extends CommandBase { @Override public String getCommandUsage(ICommandSender sender) { + return "/moo help"; + } + + private void sendCommandUsage(ICommandSender sender) { IChatComponent usage = new ChatComponentText("\u279C Cowmoonication commands:").setChatStyle(new ChatStyle().setColor(EnumChatFormatting.GOLD).setBold(true)) .appendSibling(createCmdHelpSection(1, "Friends")) .appendSibling(createCmdHelpEntry("stalk", "Get info of player's status")) @@ -236,17 +239,17 @@ public class MooCommand extends CommandBase { .appendSibling(createCmdHelpEntry("remove", "Remove best friends")) .appendSibling(createCmdHelpEntry("list", "View list of best friends")) .appendSibling(createCmdHelpEntry("nameChangeCheck", "Force a scan for changed names of best friends")) - .appendSibling(createCmdHelpEntry("toggle", "Toggle show/hide all join/leave notifications")) + .appendSibling(createCmdHelpEntry("toggle", "Toggle join/leave notifications")) .appendSibling(createCmdHelpSection(2, "Miscellaneous")) .appendSibling(createCmdHelpEntry("config", "Open mod's configuration")) .appendSibling(createCmdHelpEntry("guiScale", "Change GUI scale")) + .appendSibling(createCmdHelpEntry("shrug", "\u00AF\\_(\u30C4)_/\u00AF")) // ¯\_(ツ)_/¯ .appendSibling(createCmdHelpSection(3, "Update mod")) .appendSibling(createCmdHelpEntry("update", "Check for new mod updates")) .appendSibling(createCmdHelpEntry("updateHelp", "Show mod update instructions")) .appendSibling(createCmdHelpEntry("version", "View results of last mod update check")) .appendSibling(createCmdHelpEntry("folder", "Open Minecraft's mods folder")); sender.addChatMessage(usage); - return ""; } private IChatComponent createCmdHelpSection(int nr, String title) { @@ -273,7 +276,7 @@ public class MooCommand extends CommandBase { if (args.length == 1) { return getListOfStringsMatchingLastWord(args, /* friends */ "stalk", "add", "remove", "list", "nameChangeCheck", "toggle", - /* miscellaneous */ "config", "guiscale", "apikey", + /* miscellaneous */ "config", "guiscale", "shrug", "apikey", /* update mod */ "update", "updateHelp", "version", "folder", /* help */ "help"); } diff --git a/src/main/java/eu/olli/cowmoonication/command/ShrugCommand.java b/src/main/java/eu/olli/cowmoonication/command/ShrugCommand.java new file mode 100644 index 0000000..5275e54 --- /dev/null +++ b/src/main/java/eu/olli/cowmoonication/command/ShrugCommand.java @@ -0,0 +1,34 @@ +package eu.olli.cowmoonication.command; + +import eu.olli.cowmoonication.Cowmoonication; +import net.minecraft.command.CommandBase; +import net.minecraft.command.CommandException; +import net.minecraft.command.ICommandSender; + +public class ShrugCommand extends CommandBase { + private final Cowmoonication main; + + public ShrugCommand(Cowmoonication main) { + this.main = main; + } + + @Override + public String getCommandName() { + return "shrug"; + } + + @Override + public String getCommandUsage(ICommandSender sender) { + return "/shrug [message]"; + } + + @Override + public void processCommand(ICommandSender sender, String[] args) throws CommandException { + main.getChatHelper().sendShrug(args); + } + + @Override + public int getRequiredPermissionLevel() { + return 0; + } +} diff --git a/src/main/java/eu/olli/cowmoonication/config/MooConfig.java b/src/main/java/eu/olli/cowmoonication/config/MooConfig.java index 73e8bbc..a401dbb 100644 --- a/src/main/java/eu/olli/cowmoonication/config/MooConfig.java +++ b/src/main/java/eu/olli/cowmoonication/config/MooConfig.java @@ -14,7 +14,9 @@ import java.util.List; public class MooConfig { public static boolean doUpdateCheck; - public static boolean filterFriendNotifications; + public static boolean showBestFriendNotifications; + public static boolean showFriendNotifications; + public static boolean showGuildNotifications; public static String moo; private static Configuration cfg = null; @@ -70,8 +72,14 @@ public class MooConfig { final boolean DO_UPDATE_CHECK = true; Property propDoUpdateCheck = cfg.get(Configuration.CATEGORY_CLIENT, "doUpdateCheck", DO_UPDATE_CHECK, "Check for mod updates?"); - final boolean FILTER_FRIEND_NOTIFICATIONS = true; - Property propFilterFriendNotify = cfg.get(Configuration.CATEGORY_CLIENT, "filterFriendNotifications", FILTER_FRIEND_NOTIFICATIONS, "Set to false to receive all login/logout messages, set to true to only get notifications of 'best friends' joining/leaving"); + final boolean SHOW_BEST_FRIEND_NOTIFICATIONS = true; + Property propShowBestFriendNotifications = cfg.get(Configuration.CATEGORY_CLIENT, "showBestFriendNotifications", SHOW_BEST_FRIEND_NOTIFICATIONS, "Set to true to receive best friends' login/logout messages, set to false hide them."); + + final boolean SHOW_FRIEND_NOTIFICATIONS = false; + Property propShowFriendNotifications = cfg.get(Configuration.CATEGORY_CLIENT, "showFriendNotifications", SHOW_FRIEND_NOTIFICATIONS, "Set to true to receive friends' login/logout messages, set to false hide them."); + + final boolean SHOW_GUILD_NOTIFICATIONS = false; + Property propShowGuildNotifications = cfg.get(Configuration.CATEGORY_CLIENT, "showGuildNotifications", SHOW_GUILD_NOTIFICATIONS, "Set to true to receive guild members' login/logout messages, set to false hide them."); final String MOO = ""; Property propMoo = cfg.get(Configuration.CATEGORY_CLIENT, "moo", MOO, "The answer to life the universe and everything. Don't edit this entry manually!", Utils.VALID_UUID_PATTERN); @@ -79,18 +87,24 @@ public class MooConfig { List<String> propOrderGeneral = new ArrayList<>(); propOrderGeneral.add(propDoUpdateCheck.getName()); - propOrderGeneral.add(propFilterFriendNotify.getName()); + propOrderGeneral.add(propShowBestFriendNotifications.getName()); + propOrderGeneral.add(propShowFriendNotifications.getName()); + propOrderGeneral.add(propShowGuildNotifications.getName()); propOrderGeneral.add(propMoo.getName()); cfg.setCategoryPropertyOrder(Configuration.CATEGORY_CLIENT, propOrderGeneral); if (readFieldsFromConfig) { doUpdateCheck = propDoUpdateCheck.getBoolean(DO_UPDATE_CHECK); - filterFriendNotifications = propFilterFriendNotify.getBoolean(FILTER_FRIEND_NOTIFICATIONS); + showBestFriendNotifications = propShowBestFriendNotifications.getBoolean(SHOW_BEST_FRIEND_NOTIFICATIONS); + showFriendNotifications = propShowFriendNotifications.getBoolean(SHOW_FRIEND_NOTIFICATIONS); + showGuildNotifications = propShowGuildNotifications.getBoolean(SHOW_GUILD_NOTIFICATIONS); moo = propMoo.getString(); } propDoUpdateCheck.set(doUpdateCheck); - propFilterFriendNotify.set(filterFriendNotifications); + propShowBestFriendNotifications.set(showBestFriendNotifications); + propShowFriendNotifications.set(showFriendNotifications); + propShowGuildNotifications.set(showGuildNotifications); propMoo.set(moo); if (cfg.hasChanged()) { @@ -98,9 +112,13 @@ public class MooConfig { } } - public void toggleNotifications() { - filterFriendNotifications = !filterFriendNotifications; - syncFromFields(); + /** + * Should login/logout notifications be modified and thus monitored? + * + * @return true if notifications should be monitored + */ + public static boolean doMonitorNotifications() { + return showBestFriendNotifications || !showFriendNotifications || !showGuildNotifications; } public class ConfigEventHandler { diff --git a/src/main/java/eu/olli/cowmoonication/listener/ChatListener.java b/src/main/java/eu/olli/cowmoonication/listener/ChatListener.java index 78a8f29..96f3a4a 100644 --- a/src/main/java/eu/olli/cowmoonication/listener/ChatListener.java +++ b/src/main/java/eu/olli/cowmoonication/listener/ChatListener.java @@ -5,9 +5,11 @@ import eu.olli.cowmoonication.config.MooConfig; import eu.olli.cowmoonication.util.Utils; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiChat; +import net.minecraft.client.gui.GuiControls; import net.minecraft.client.gui.GuiNewChat; import net.minecraft.util.EnumChatFormatting; import net.minecraft.util.IChatComponent; +import net.minecraft.util.StringUtils; import net.minecraftforge.client.event.ClientChatReceivedEvent; import net.minecraftforge.client.event.GuiOpenEvent; import net.minecraftforge.client.event.GuiScreenEvent; @@ -17,13 +19,16 @@ import org.apache.commons.lang3.CharUtils; import org.lwjgl.input.Keyboard; import org.lwjgl.input.Mouse; -import java.awt.*; -import java.awt.datatransfer.Clipboard; -import java.awt.datatransfer.StringSelection; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ChatListener { + /** + * Examples: + * - §aFriend > §r§aNAME §r§eleft.§r + * - §2Guild > §r§aNAME §r§eleft.§r + */ + private static final Pattern LOGIN_LOGOUT_NOTIFICATION = Pattern.compile("^(?<type>§aFriend|§2Guild) > §r(?<rank>§[0-9a-f])(?<playerName>[\\w]+)(?<joinLeave> §r§e(?:joined|left)\\.)§r$"); private static final Pattern PRIVATE_MESSAGE_RECEIVED_PATTERN = Pattern.compile("^From (?:\\[.*?] )?(\\w+): "); private final Cowmoonication main; private String lastTypedChars = ""; @@ -35,18 +40,31 @@ public class ChatListener { @SubscribeEvent public void onLogInOutMessage(ClientChatReceivedEvent e) { - if (e.type != 2) { // normal chat or system msg + if (e.type != 2) { // normal chat or system msg (not above action bar) String text = e.message.getUnformattedText(); - if (MooConfig.filterFriendNotifications && text.length() < 42 && // to prevent the party disbanded message from being filtered: "The party was disbanded because all invites have expired and all members have left." - (text.endsWith(" joined.") || text.endsWith(" left.") // Hypixel - || text.endsWith(" joined the game") || text.endsWith(" left the game."))) { // Spigot - // TODO maybe check which server thePlayer is on and check for logout pattern accordingly - int nameEnd = text.indexOf(" joined"); - if (nameEnd == -1) { - nameEnd = text.indexOf(" left"); + Matcher notificationMatcher = LOGIN_LOGOUT_NOTIFICATION.matcher(e.message.getFormattedText()); + + if (MooConfig.doMonitorNotifications() && text.length() < 42 && notificationMatcher.matches()) { + // we got a login or logout notification! + main.getLogger().info(text); + + String type = notificationMatcher.group("type"); + String rank = notificationMatcher.group("rank"); + String playerName = notificationMatcher.group("playerName"); + String joinLeave = notificationMatcher.group("joinLeave"); + + if (MooConfig.showBestFriendNotifications) { + boolean isBestFriend = main.getFriends().isBestFriend(playerName, false); + if (isBestFriend) { + // replace default (friend/guild) notification with best friend notification + main.getChatHelper().sendMessage(EnumChatFormatting.YELLOW, "" + EnumChatFormatting.DARK_GREEN + EnumChatFormatting.BOLD + "Best friend" + EnumChatFormatting.DARK_GREEN + " > " + EnumChatFormatting.RESET + rank + playerName + joinLeave); + e.setCanceled(true); + return; + } } - boolean isBestFriend = main.getFriends().isBestFriend(text.substring(0, nameEnd), false); - if (!isBestFriend) { + if (!MooConfig.showFriendNotifications && "§aFriend".equals(type)) { + e.setCanceled(true); + } else if (!MooConfig.showGuildNotifications && "§2Guild".equals(type)) { e.setCanceled(true); } } else if (text.length() == 56 && text.startsWith("Your new API key is ")) { @@ -67,10 +85,15 @@ public class ChatListener { if (!Mouse.getEventButtonState() && Mouse.getEventButton() == 1 && Keyboard.isKeyDown(Keyboard.KEY_LMENU)) { // alt key pressed and right mouse button being released IChatComponent chatComponent = Minecraft.getMinecraft().ingameGUI.getChatGUI().getChatComponent(Mouse.getX(), Mouse.getY()); if (chatComponent != null) { - String chatData = main.getChatHelper().cleanChatComponent(chatComponent); - Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); - clipboard.setContents(new StringSelection(chatData), null); - main.getChatHelper().sendAboveChatMessage(EnumChatFormatting.YELLOW + "Copied chat component to clipboard:", "" + EnumChatFormatting.BOLD + EnumChatFormatting.GOLD + "\u276E" + EnumChatFormatting.RESET + chatComponent.getUnformattedText() + EnumChatFormatting.BOLD + EnumChatFormatting.GOLD + "\u276F"); + boolean copyWithFormatting = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT); + String chatData; + if (copyWithFormatting) { + chatData = main.getChatHelper().cleanChatComponent(chatComponent); + } else { + chatData = StringUtils.stripControlCodes(chatComponent.getUnformattedText()); + } + GuiControls.setClipboardString(chatData); + main.getChatHelper().sendAboveChatMessage(EnumChatFormatting.YELLOW + "Copied chat component to clipboard:", "" + EnumChatFormatting.BOLD + EnumChatFormatting.GOLD + "\u276E" + EnumChatFormatting.RESET + (copyWithFormatting ? chatComponent.getUnformattedText() : chatData) + EnumChatFormatting.BOLD + EnumChatFormatting.GOLD + "\u276F"); } } } diff --git a/src/main/java/eu/olli/cowmoonication/util/ChatHelper.java b/src/main/java/eu/olli/cowmoonication/util/ChatHelper.java index a0820c0..b0196e4 100644 --- a/src/main/java/eu/olli/cowmoonication/util/ChatHelper.java +++ b/src/main/java/eu/olli/cowmoonication/util/ChatHelper.java @@ -71,4 +71,12 @@ public class ChatHelper { Matcher jsonMatcher = USELESS_JSON_CONTENT_PATTERN.matcher(component); return jsonMatcher.replaceAll(""); } + + public void sendShrug(String... args) { + String chatMsg = "\u00AF\\_(\u30C4)_/\u00AF"; // ¯\\_(ツ)_/¯" + if (args.length > 0) { + chatMsg = String.join(" ", args) + " " + chatMsg; + } + Minecraft.getMinecraft().thePlayer.sendChatMessage(chatMsg); + } } |