Last active
February 23, 2023 03:48
-
-
Save aadnk/8129389 to your computer and use it in GitHub Desktop.
Change the "unknown command" message in 1.7.2 with ProtocolLib.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
@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?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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());