blob: eef9ca78bb74d73a1cbd16067dee2ca39a985031 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
package de.cowtipper.cowlection.util;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
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 net.minecraftforge.event.entity.EntityJoinWorldEvent;
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 ChatHelper {
private static final Pattern USELESS_JSON_CONTENT_PATTERN = Pattern.compile("\"[A-Za-z]+\":false,?");
private static final int DISPLAY_DURATION = 5000;
private final 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) {
if (Minecraft.getMinecraft().thePlayer == null) {
putOfflineMessage(chatComponent);
} else {
ClientChatReceivedEvent event = new ClientChatReceivedEvent((byte) 1, chatComponent);
MinecraftForge.EVENT_BUS.post(event);
if (!event.isCanceled()) {
Minecraft.getMinecraft().thePlayer.addChatMessage(event.message);
}
}
}
private void putOfflineMessage(IChatComponent chatComponent) {
if (offlineMessages.size() == 0) {
// had no offline messages before
MinecraftForge.EVENT_BUS.register(this);
}
offlineMessages.add(chatComponent);
}
@SubscribeEvent
public void onPlayerWorldJoin(EntityJoinWorldEvent e) {
if (e.entity == Minecraft.getMinecraft().thePlayer) {
new TickDelay(this::sendOfflineMessages, 6 * 20);
}
}
private void sendOfflineMessages() {
if (Minecraft.getMinecraft().thePlayer != null) {
if (offlineMessages.size() > 0) {
Minecraft.getMinecraft().thePlayer.playSound("random.levelup", 0.4F, 0.8F);
}
for (IChatComponent offlineMessage : offlineMessages) {
sendMessage(offlineMessage);
}
offlineMessages.clear();
MinecraftForge.EVENT_BUS.unregister(this);
}
}
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("");
}
public void sendShrug(String... args) {
String chatMsg = "¯\\_(ツ)_/¯";
if (args.length > 0) {
chatMsg = String.join(" ", args) + " " + chatMsg;
}
Minecraft.getMinecraft().thePlayer.sendChatMessage(chatMsg);
}
public void sendServerCommand(String command) {
EntityPlayerSP thePlayer = Minecraft.getMinecraft().thePlayer;
if (thePlayer != null) {
thePlayer.sendChatMessage(command);
}
}
}
|