Created
May 30, 2014 20:50
-
-
Save aadnk/3749a78828ddd8a9c10b to your computer and use it in GitHub Desktop.
Send custom ping data.
This file contains 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 java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.DataInputStream; | |
import java.io.DataOutputStream; | |
import java.io.IOException; | |
import org.bukkit.plugin.Plugin; | |
import org.bukkit.plugin.java.JavaPlugin; | |
import org.json.simple.JSONObject; | |
import org.json.simple.JSONValue; | |
import com.comphenix.protocol.PacketType; | |
import com.comphenix.protocol.ProtocolLibrary; | |
import com.comphenix.protocol.events.ListenerPriority; | |
import com.comphenix.protocol.events.PacketAdapter; | |
import com.comphenix.protocol.events.PacketEvent; | |
import com.comphenix.protocol.events.PacketOutputHandler; | |
import com.comphenix.protocol.utility.StreamSerializer; | |
public class CustomPingData extends JavaPlugin { | |
@Override | |
public void onEnable() { | |
ProtocolLibrary.getProtocolManager().addPacketListener( | |
new PacketAdapter(this, PacketType.Status.Server.OUT_SERVER_INFO) { | |
private StreamSerializer serializer = new StreamSerializer(); | |
@Override | |
public void onPacketSending(PacketEvent event) { | |
// There might be some way to encode data in ServerPing (Steganography?) | |
// but you would be limited in how much you could send. | |
// It's better to modify the raw JSON string. | |
event.getNetworkMarker().addOutputHandler(new PacketOutputHandler() { | |
@SuppressWarnings("unchecked") | |
@Override | |
public byte[] handle(PacketEvent event, byte[] buffer) { | |
try { | |
DataInputStream input = new DataInputStream(new ByteArrayInputStream(buffer)); | |
boolean hasHeader = event.getNetworkMarker().requireOutputHeader(); | |
if (hasHeader) | |
serializer.deserializeVarInt(input); | |
String json = serializer.deserializeString(new DataInputStream(input), 32767); | |
// We assume it's a JSON object | |
JSONObject parsed = (JSONObject) JSONValue.parse(json); | |
// Add our custom data here | |
parsed.put("custom_data", "This can be any JSON."); | |
// Note that the maximum size is 32767 characters | |
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); | |
DataOutputStream dataOutput = new DataOutputStream(byteOutput); | |
// Include the header | |
if (hasHeader) | |
serializer.serializeVarInt(dataOutput, event.getPacket().getType().getCurrentId()); | |
serializer.serializeString(dataOutput, parsed.toJSONString()); | |
return byteOutput.toByteArray(); | |
} catch (IOException e) { | |
throw new RuntimeException("Cannot modify outgoing JSON.", e); | |
} | |
} | |
@Override | |
public ListenerPriority getPriority() { | |
return ListenerPriority.HIGH; | |
} | |
@Override | |
public Plugin getPlugin() { | |
return ExampleMod.this; | |
} | |
}); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment