-
-
Save aadnk/8129389 to your computer and use it in GitHub Desktop.
package com.comphenix.example; | |
import org.bukkit.ChatColor; | |
import org.bukkit.event.Listener; | |
import org.bukkit.plugin.java.JavaPlugin; | |
import org.json.simple.JSONArray; | |
import org.json.simple.JSONObject; | |
import org.json.simple.JSONValue; | |
import org.json.simple.parser.JSONParser; | |
import org.json.simple.parser.ParseException; | |
import com.comphenix.protocol.PacketType; | |
import com.comphenix.protocol.ProtocolLibrary; | |
import com.comphenix.protocol.events.PacketAdapter; | |
import com.comphenix.protocol.events.PacketContainer; | |
import com.comphenix.protocol.events.PacketEvent; | |
import com.comphenix.protocol.reflect.StructureModifier; | |
import com.comphenix.protocol.wrappers.WrappedChatComponent; | |
public class ChangingUnknownCommand extends JavaPlugin implements Listener { | |
private interface Processor { | |
public Object process(Object value, Object parent); | |
} | |
@Override | |
public void onEnable() { | |
ProtocolLibrary.getProtocolManager().addPacketListener( | |
new PacketAdapter(this, PacketType.Play.Server.CHAT) { | |
private JSONParser parser = new JSONParser(); | |
@Override | |
public void onPacketSending(PacketEvent event) { | |
PacketContainer packet = event.getPacket(); | |
StructureModifier<WrappedChatComponent> componets = packet.getChatComponents(); | |
try { | |
Object data = parser.parse(componets.read(0).getJson()); | |
final boolean[] result = new boolean[1]; | |
transformPrimitives(data, null, new Processor() { | |
@SuppressWarnings("unchecked") | |
@Override | |
public Object process(Object value, Object parent) { | |
if (value instanceof String) { | |
String stripped = ChatColor.stripColor((String) value); | |
if ("Unknown command. Type \"/help\" for help.".equals(stripped)) { | |
result[0] = true; | |
// Add color to the parent object | |
if (parent instanceof JSONObject) { | |
((JSONObject) parent).put("color", "red"); | |
} | |
return "Wrong. Try again! Also note that we can apply a color to the " + | |
"entire multi-lined messages, unlike ChatColor.RED."; | |
} | |
} | |
return value; | |
} | |
}); | |
// Write back the changed string | |
if (result[0]) { | |
componets.write(0, WrappedChatComponent.fromJson(JSONValue.toJSONString(data))); | |
} | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
} | |
} | |
}); | |
} | |
private Object transformPrimitives(Object value, Object parent, Processor processor) { | |
// Check its type | |
if (value instanceof JSONObject) { | |
return transformPrimitives((JSONObject) value, processor); | |
} else if (value instanceof JSONArray) { | |
return transformPrimitives((JSONArray) value, processor); | |
} else { | |
return processor.process(value, parent); | |
} | |
} | |
@SuppressWarnings("unchecked") | |
private JSONObject transformPrimitives(JSONObject source, Processor processor) { | |
for (Object key : source.keySet().toArray()) { | |
Object value = source.get(key); | |
source.put(key, transformPrimitives(value, source, processor)); | |
} | |
return source; | |
} | |
@SuppressWarnings("unchecked") | |
private JSONArray transformPrimitives(JSONArray source, Processor processor) { | |
for (int i = 0; i < source.size(); i++) { | |
Object value = source.get(i); | |
source.set(i, transformPrimitives(value, source, processor)); | |
} | |
return source; | |
} | |
} |
This method works, however if colours are used in the string, only the top line will be sent in that colour, the rest will be sent in white;
return ChatColor.RED + "Wrong. Try again!";
Result; http://i.imgur.com/J7SsGlN.png
I also noticed occasional errors were sent to the console;
http://pastebin.com/gvZxr0wq
Line 217 being; JSONObject json = (JSONObject) parser.parse(componets.read(0).getJson());
@DoctorDark: I believe I've fixed that error now. The trouble was that I assumed the root node of the JSON message would be a JSONObject, while in actuality it can be a simple string as well.
I've also addressed your chat color problem - if you add a "color" property to the parent JSONObject, the entire message will be colorized (see example).
@aadnk Why need transformPrimitives?
@JermeyPark123999: Sorry for the late response. You have to tag me in your comment, though, otherwise I won't get notified by GitHub and by email.
This is working fine in both CraftBukkit 1.7.2 and Spigot 1.7.2 (using ProtocolLib #211). Have you made sure your plugin didn't throw an exception, perhaps? And that it's sending the exact string "Unknown command. Type "/help" for help."?
@CraftThatBlock: Nice. Still, this is just an example. You could use it to modify hard-coded plugin messages as well.