aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/eu/olli/cowmoonication/Utils.java
diff options
context:
space:
mode:
authorCow <cow@volloeko.de>2020-03-01 06:49:05 +0100
committerCow <cow@volloeko.de>2020-03-01 06:49:05 +0100
commitea5963ad660c91640ec72bddd046d2e465f55f3f (patch)
tree9d628daaaaa9c9697ff420bf108f64345acc1296 /src/main/java/eu/olli/cowmoonication/Utils.java
parent48138d74623d3603f89a4de18a35266197a3b46d (diff)
downloadCowlection-ea5963ad660c91640ec72bddd046d2e465f55f3f.tar.gz
Cowlection-ea5963ad660c91640ec72bddd046d2e465f55f3f.tar.bz2
Cowlection-ea5963ad660c91640ec72bddd046d2e465f55f3f.zip
Initial code commit
Diffstat (limited to 'src/main/java/eu/olli/cowmoonication/Utils.java')
-rw-r--r--src/main/java/eu/olli/cowmoonication/Utils.java57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/main/java/eu/olli/cowmoonication/Utils.java b/src/main/java/eu/olli/cowmoonication/Utils.java
new file mode 100644
index 0000000..ac3167c
--- /dev/null
+++ b/src/main/java/eu/olli/cowmoonication/Utils.java
@@ -0,0 +1,57 @@
+package eu.olli.cowmoonication;
+
+import net.minecraft.client.Minecraft;
+import net.minecraft.util.ChatComponentText;
+import net.minecraft.util.IChatComponent;
+import net.minecraftforge.client.event.ClientChatReceivedEvent;
+import net.minecraftforge.common.MinecraftForge;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class Utils {
+ public static final Pattern VALID_USERNAME = Pattern.compile("^[\\w]{1,16}$");
+ private static final Pattern USELESS_JSON_CONTENT_PATTERN = Pattern.compile("\"[A-Za-z]+\":false,?");
+ private final Cowmoonication main;
+ private String[] aboveChatMessage;
+ private long aboveChatMessageExpiration;
+
+ public Utils(Cowmoonication main) {
+ this.main = main;
+ }
+
+ public void sendMessage(String text) {
+ sendMessage(new ChatComponentText(text));
+ }
+
+ public void sendMessage(IChatComponent chatComponent) {
+ ClientChatReceivedEvent event = new ClientChatReceivedEvent((byte) 1, chatComponent);
+ MinecraftForge.EVENT_BUS.post(event);
+ if (!event.isCanceled()) {
+ Minecraft.getMinecraft().thePlayer.addChatMessage(event.message);
+ }
+ }
+
+ public void sendAboveChatMessage(String... text) {
+ aboveChatMessage = text;
+ aboveChatMessageExpiration = Minecraft.getSystemTime() + 5000;
+ }
+
+ public String[] getAboveChatMessage() {
+ if (aboveChatMessageExpiration < Minecraft.getSystemTime()) {
+ // message expired
+ aboveChatMessage = null;
+ }
+ return aboveChatMessage;
+ }
+
+ public boolean isValidMcName(String username) {
+ return VALID_USERNAME.matcher(username).matches();
+ }
+
+ public String cleanChatComponent(IChatComponent chatComponent) {
+ String component = IChatComponent.Serializer.componentToJson(chatComponent);
+ Matcher jsonMatcher = USELESS_JSON_CONTENT_PATTERN.matcher(component);
+ return jsonMatcher.replaceAll("");
+ }
+}