diff options
author | Cow <cow@volloeko.de> | 2020-03-28 04:11:23 +0100 |
---|---|---|
committer | Cow <cow@volloeko.de> | 2020-03-28 04:11:23 +0100 |
commit | 25714bc7ec6295630506b4ce0e6d4c4d8341ab34 (patch) | |
tree | ffa6ae145b4c1e26398bf446fa33e098cfacaa10 /src/main/java/eu/olli/cowmoonication/util/ChatHelper.java | |
parent | 0a105c807a3f8040ada76c4e4edac4a79fe32fe6 (diff) | |
download | Cowlection-25714bc7ec6295630506b4ce0e6d4c4d8341ab34.tar.gz Cowlection-25714bc7ec6295630506b4ce0e6d4c4d8341ab34.tar.bz2 Cowlection-25714bc7ec6295630506b4ce0e6d4c4d8341ab34.zip |
Reworked best friends list
- Saving best friends' UUIDs now (instead of just the name), also checking for name changes periodically to keep best friends list up to date
- Moved best friends add/remove functionality from config GUI back to commands (`/moo <add|remove> <name>`)
Diffstat (limited to 'src/main/java/eu/olli/cowmoonication/util/ChatHelper.java')
-rw-r--r-- | src/main/java/eu/olli/cowmoonication/util/ChatHelper.java | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/main/java/eu/olli/cowmoonication/util/ChatHelper.java b/src/main/java/eu/olli/cowmoonication/util/ChatHelper.java new file mode 100644 index 0000000..a0820c0 --- /dev/null +++ b/src/main/java/eu/olli/cowmoonication/util/ChatHelper.java @@ -0,0 +1,74 @@ +package eu.olli.cowmoonication.util; + +import net.minecraft.client.Minecraft; +import net.minecraft.util.ChatComponentText; +import net.minecraft.util.ChatStyle; +import net.minecraft.util.EnumChatFormatting; +import net.minecraft.util.IChatComponent; +import net.minecraftforge.client.event.ClientChatReceivedEvent; +import net.minecraftforge.common.MinecraftForge; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class ChatHelper { + private static final Pattern USELESS_JSON_CONTENT_PATTERN = Pattern.compile("\"[A-Za-z]+\":false,?"); + private static final int DISPLAY_DURATION = 5000; + private List<IChatComponent> offlineMessages = new ArrayList<>(); + private String[] aboveChatMessage; + private long aboveChatMessageExpiration; + + public ChatHelper() { + } + + public void sendMessage(EnumChatFormatting color, String text) { + sendMessage(new ChatComponentText(text).setChatStyle(new ChatStyle().setColor(color))); + } + + public void sendMessage(IChatComponent chatComponent) { + ClientChatReceivedEvent event = new ClientChatReceivedEvent((byte) 1, chatComponent); + MinecraftForge.EVENT_BUS.post(event); + if (!event.isCanceled()) { + if (Minecraft.getMinecraft().thePlayer == null) { + offlineMessages.add(event.message); + } else { + Minecraft.getMinecraft().thePlayer.addChatMessage(event.message); + } + } + } + + public void sendOfflineMessages() { + if (Minecraft.getMinecraft().thePlayer != null) { + Iterator<IChatComponent> offlineMessages = this.offlineMessages.iterator(); + if (offlineMessages.hasNext()) { + Minecraft.getMinecraft().thePlayer.playSound("random.levelup", 0.4F, 0.8F); + } + while (offlineMessages.hasNext()) { + Minecraft.getMinecraft().thePlayer.addChatMessage(offlineMessages.next()); + offlineMessages.remove(); + } + } + } + + public void sendAboveChatMessage(String... text) { + aboveChatMessage = text; + aboveChatMessageExpiration = Minecraft.getSystemTime() + DISPLAY_DURATION; + } + + public String[] getAboveChatMessage() { + if (aboveChatMessageExpiration < Minecraft.getSystemTime()) { + // message expired + aboveChatMessage = null; + } + return aboveChatMessage; + } + + public String cleanChatComponent(IChatComponent chatComponent) { + String component = IChatComponent.Serializer.componentToJson(chatComponent); + Matcher jsonMatcher = USELESS_JSON_CONTENT_PATTERN.matcher(component); + return jsonMatcher.replaceAll(""); + } +} |