Skip to content

Instantly share code, notes, and snippets.

@aadnk
Last active February 23, 2023 03:48
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aadnk/8129389 to your computer and use it in GitHub Desktop.
Save aadnk/8129389 to your computer and use it in GitHub Desktop.
Change the "unknown command" message in 1.7.2 with ProtocolLib.
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;
}
}
@JeremyPark123999
Copy link

This doesn't seem to work. I am using ProtocolLib 3.1.0, and it makes no difference, Do I need to change the main class to read something from this? Or perhaps something similar.
Could you please upload your edited ProtocolLib File, so I can see if there is something odd going on between the two complications?

@Cretezy
Copy link

Cretezy commented Jan 10, 2014

This is built into Spigot :P

@aadnk
Copy link
Author

aadnk commented Feb 4, 2014

@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.

Copy link

ghost commented Feb 11, 2014

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());

@aadnk
Copy link
Author

aadnk commented Feb 13, 2014

@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).

@pittersnider
Copy link

@aadnk Why need transformPrimitives?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment