Your First Plugin
Here a short introduction how you can use XComServer for your plugins. At first you have to add XServer as a referenced library to your project, if you are using eclipse.
Updated for Version 2.0.0
Receiving
XServerListener
public class MyXListener implements XServerListener { private final XServerTestPlugin plugin; public MyXListener(XServerTestPlugin plugin) { this.plugin = plugin; } @XEventHandler(sync = true) public void onMessage(XServerMessageIncomingEvent event) { Message m = event.getMessage(); if (m.getSubChannel().equals("mySubChannel")) { try { DataInputStream in = new DataInputStream(new ByteArrayInputStream( m.getContent())); String command = in.readUTF(); Bukkit.getLogger().info("Command will be executed: " + command); plugin.getServer().dispatchCommand( plugin.getServer().getConsoleSender(), command); } catch (IOException e) { Bukkit.getLogger().info(e.toString()); } } } }
Register XServerListener
- Register the XServerListener in the XServer - EventHandler when your Plugin enables.
@Override public void onEnable() { try { XServerManager.getInstance().getEventHandler().registerListener(this, new MyXListener(this)); } catch (NotInitializedException e) { log.warning("XServer not initialized correctly!"); } }
Sending
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){ if (plugin.getServer().getPluginManager().isPluginEnabled("XServer")) { if (args.length == 0) { return false; } StringBuilder sb = new StringBuilder(); for (int i = 0; i < args.length; i++) { if (i > 0) { sb.append(" "); } sb.append(args[i]); } byte[] data = new byte[0]; ByteArrayOutputStream b = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(b); try { //plugin.getLogger().info("Sending message: \"" + sb.toString()); // + "\""); out.writeUTF(sb.toString()); data = b.toByteArray(); for (XServer xs : XServerManager.getInstance().getServers()) { try { Message m = XServerManager.getInstance().createMessage("mySubChannel", data); //plugin.getLogger().info("\nNew Message:\nTo: " + m.getReceiver_Name() + "\nLength: " + m.getContent().length); xs.sendMessage(m); } catch (IOException e) { plugin.getLogger().warning("TESTING!!! ERROR!"); plugin.getLogger().warning(e.getStackTrace().toString()); } } } catch (IOException e1) { plugin.getLogger().warning(e1.getStackTrace().toString()); } catch (NotInitializedException e) { plugin.getLogger().warning("XServer not initialized correctly!"); } } return true; }
Plugin.yml
- Add to your "plugin.yml" the command:
commands: test: description: testing usage: /<command> [command] [args] permission: mypermissions permission-message: You don't have <permission>
End
- Copy this plugin to both servers.
- Send commands to other servers for example: /test broadcast Hello World!
More Infos
XServer Events
This plugin uses an own Event Handler. To register a XServerListener use:
XServerManager.getInstance().getEventHandler().registerListener(yourplugin, new MyXListener(yourplugin));
The Event Method should be like:
@XEventHandler(sync = true) public void methodname(XServerMessageIncomingEvent event) { }
If sync is true, the "methodname" method runs synchron. If sync is false, it runs assynchron. Standard sync is true.
Optional you can extend the annotation with the subchannel that is used. This increases the performance minimal. Example:
@XEventHandler(sync = true, channel = "mySubChannel")
List of Events
- XServerConnectionDenied
- XServerDisconnectEvent
- XServerEvent
- XServerLoggedInEvent
- XServerMessageEvent
- XServerMessageIncomingEvent
- XServerMessageOutgoingEvent
Comments