|
17 | 17 |
|
18 | 18 | package com.comphenix.protocol.utility;
|
19 | 19 |
|
20 |
| -import java.lang.reflect.Constructor; |
21 | 20 | import java.lang.reflect.InvocationTargetException;
|
22 |
| -import java.lang.reflect.Modifier; |
| 21 | +import java.util.ArrayList; |
23 | 22 | import java.util.List;
|
| 23 | +import java.util.UUID; |
24 | 24 |
|
| 25 | +import com.comphenix.protocol.wrappers.WrappedChatComponent; |
25 | 26 | import org.bukkit.command.CommandSender;
|
26 | 27 | import org.bukkit.entity.Player;
|
27 | 28 |
|
28 | 29 | import com.comphenix.protocol.PacketType;
|
29 | 30 | import com.comphenix.protocol.ProtocolManager;
|
30 | 31 | import com.comphenix.protocol.events.PacketContainer;
|
31 |
| -import com.comphenix.protocol.injector.PacketConstructor; |
32 |
| -import com.comphenix.protocol.injector.packet.PacketRegistry; |
33 | 32 | import com.comphenix.protocol.reflect.FieldAccessException;
|
34 |
| -import com.comphenix.protocol.reflect.FuzzyReflection; |
35 |
| -import com.comphenix.protocol.reflect.accessors.Accessors; |
36 |
| -import com.comphenix.protocol.reflect.accessors.MethodAccessor; |
37 |
| -import com.comphenix.protocol.reflect.fuzzy.FuzzyMatchers; |
38 |
| -import com.comphenix.protocol.reflect.fuzzy.FuzzyMethodContract; |
39 | 33 | import com.google.common.base.Strings;
|
40 |
| -import com.google.common.collect.Iterables; |
41 | 34 |
|
42 | 35 | /**
|
43 | 36 | * Utility methods for sending chat messages.
|
|
46 | 39 | */
|
47 | 40 | public class ChatExtensions {
|
48 | 41 | // Used to sent chat messages
|
49 |
| - private ProtocolManager manager; |
50 |
| - |
51 |
| - // The chat packet constructor |
52 |
| - private static volatile PacketConstructor chatConstructor; |
53 |
| - |
54 |
| - // Whether or not we have to use the post-1.6.1 chat format |
55 |
| - private static volatile Constructor<?> jsonConstructor = getJsonFormatConstructor(); |
56 |
| - private static volatile MethodAccessor messageFactory; |
57 |
| - |
| 42 | + private final ProtocolManager manager; |
| 43 | + |
| 44 | + private static final UUID SERVER_UUID = new UUID(0L, 0L); |
| 45 | + |
58 | 46 | public ChatExtensions(ProtocolManager manager) {
|
59 | 47 | this.manager = manager;
|
60 | 48 | }
|
@@ -100,53 +88,21 @@ private void sendMessageSilently(Player player, String message) throws Invocatio
|
100 | 88 | * @param message - the message to send.
|
101 | 89 | * @return The packets.
|
102 | 90 | */
|
103 |
| - public static PacketContainer[] createChatPackets(String message) { |
104 |
| - if (jsonConstructor != null) { |
105 |
| - if (chatConstructor == null) { |
106 |
| - Class<?> messageClass = jsonConstructor.getParameterTypes()[0]; |
107 |
| - chatConstructor = PacketConstructor.DEFAULT.withPacket(PacketType.Play.Server.CHAT, new Object[] { messageClass }); |
108 |
| - |
109 |
| - // Try one of the string constructors |
110 |
| - if (MinecraftReflection.isUsingNetty()) { |
111 |
| - messageFactory = Accessors.getMethodAccessor( |
112 |
| - MinecraftReflection.getCraftMessageClass(), "fromString", String.class); |
113 |
| - } else { |
114 |
| - messageFactory = Accessors.getMethodAccessor( |
115 |
| - FuzzyReflection.fromClass(messageClass).getMethod( |
116 |
| - FuzzyMethodContract.newBuilder(). |
117 |
| - requireModifier(Modifier.STATIC). |
118 |
| - parameterCount(1). |
119 |
| - parameterExactType(String.class). |
120 |
| - returnTypeMatches(FuzzyMatchers.matchParent()). |
121 |
| - build()) |
122 |
| - ); |
123 |
| - } |
124 |
| - } |
125 |
| - |
126 |
| - // Minecraft 1.7.2 and later |
127 |
| - if (MinecraftReflection.isUsingNetty()) { |
128 |
| - Object[] components = (Object[]) messageFactory.invoke(null, message); |
129 |
| - PacketContainer[] packets = new PacketContainer[components.length]; |
130 |
| - |
131 |
| - for (int i = 0; i < components.length; i++) { |
132 |
| - packets[i] = chatConstructor.createPacket(components[i]); |
133 |
| - } |
134 |
| - return packets; |
135 |
| - |
136 |
| - // Minecraft 1.6.1 - 1.6.4 |
137 |
| - } else { |
138 |
| - return new PacketContainer[] { chatConstructor.createPacket(messageFactory.invoke(null, message)) }; |
139 |
| - } |
140 |
| - |
141 |
| - } else { |
142 |
| - if (chatConstructor == null) { |
143 |
| - chatConstructor = PacketConstructor.DEFAULT.withPacket(PacketType.Play.Server.CHAT, new Object[] { message }); |
| 91 | + public static List<PacketContainer> createChatPackets(String message) { |
| 92 | + List<PacketContainer> packets = new ArrayList<>(); |
| 93 | + WrappedChatComponent[] components = WrappedChatComponent.fromChatMessage(message); |
| 94 | + for (WrappedChatComponent component : components) { |
| 95 | + PacketContainer packet = new PacketContainer(PacketType.Play.Server.CHAT); |
| 96 | + packet.getChatComponents().write(0, component); |
| 97 | + if (MinecraftVersion.NETHER_UPDATE_2.atOrAbove()) { |
| 98 | + packet.getUUIDs().write(0, SERVER_UUID); |
144 | 99 | }
|
145 |
| - // Minecraft 1.6.0 and earlier |
146 |
| - return new PacketContainer[] { chatConstructor.createPacket(message) }; |
| 100 | + packets.add(packet); |
147 | 101 | }
|
| 102 | + |
| 103 | + return packets; |
148 | 104 | }
|
149 |
| - |
| 105 | + |
150 | 106 | /**
|
151 | 107 | * Broadcast a message without invoking any packet listeners.
|
152 | 108 | * @param message - message to send.
|
@@ -203,27 +159,11 @@ private static int getMaximumLength(String[] lines) {
|
203 | 159 | int current = 0;
|
204 | 160 |
|
205 | 161 | // Find the longest line
|
206 |
| - for (int i = 0; i < lines.length; i++) { |
207 |
| - if (current < lines[i].length()) |
208 |
| - current = lines[i].length(); |
| 162 | + for (String line : lines) { |
| 163 | + if (current < line.length()) |
| 164 | + current = line.length(); |
209 | 165 | }
|
| 166 | + |
210 | 167 | return current;
|
211 | 168 | }
|
212 |
| - |
213 |
| - /** |
214 |
| - * Retrieve a constructor for post-1.6.1 chat packets. |
215 |
| - * @return A constructor for JSON-based packets. |
216 |
| - */ |
217 |
| - static Constructor<?> getJsonFormatConstructor() { |
218 |
| - Class<?> chatPacket = PacketRegistry.getPacketClassFromType(PacketType.Play.Server.CHAT, true); |
219 |
| - List<Constructor<?>> list = FuzzyReflection.fromClass(chatPacket).getConstructorList( |
220 |
| - FuzzyMethodContract.newBuilder(). |
221 |
| - parameterCount(1). |
222 |
| - parameterMatches(MinecraftReflection.getMinecraftObjectMatcher()). |
223 |
| - build() |
224 |
| - ); |
225 |
| - |
226 |
| - // First element or NULL |
227 |
| - return Iterables.getFirst(list, null); |
228 |
| - } |
229 | 169 | }
|
0 commit comments