Created
September 10, 2013 14:43
-
-
Save Batschkoto/6510454 to your computer and use it in GitHub Desktop.
Example for forwarding data via plugin messages
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 de.batschkoto.pluginmessagetest; | |
import org.bukkit.Bukkit; | |
import org.bukkit.entity.Player; | |
import org.bukkit.plugin.java.JavaPlugin; | |
import org.bukkit.plugin.messaging.PluginMessageListener; | |
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.DataInputStream; | |
import java.io.DataOutputStream; | |
public class Main extends JavaPlugin implements PluginMessageListener { | |
private static Main instance; | |
@Override | |
public void onEnable() { | |
instance = this; | |
this.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord"); | |
this.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", this); | |
} | |
@Override | |
public void onPluginMessageReceived(String channel, Player player, byte[] message) { | |
if (!channel.equals("BungeeCord")) { | |
return; | |
} | |
try{ | |
DataInputStream in = new DataInputStream(new ByteArrayInputStream(message)); | |
String subchannel = in.readUTF(); | |
if (subchannel.equals("customchannel")) { | |
short len = in.readShort(); | |
byte[] data = new byte[len]; | |
in.readFully(data); | |
//bytearray to string | |
String s = new String(data); | |
System.out.println(s); | |
} | |
}catch(Exception ex){ | |
ex.printStackTrace(); | |
} | |
} | |
public void forwardString(String subchannel, String target, String s){ | |
try{ | |
ByteArrayOutputStream b = new ByteArrayOutputStream(); | |
DataOutputStream out = new DataOutputStream(b); | |
out.writeUTF("Forward"); | |
out.writeUTF(target); | |
out.writeUTF(subchannel); // "customchannel" for example | |
byte[] data = s.getBytes(); | |
out.writeShort(data.length); | |
out.write(data); | |
Player p = Bukkit.getPlayer("someplayer"); | |
p.sendPluginMessage(instance, "BungeeCord", b.toByteArray()); | |
}catch(Exception ex){ | |
ex.printStackTrace(); | |
} | |
} | |
public static Main getInstance(){ | |
return instance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment