PluginMessages (solved)

Discussion in 'BungeeCord Plugin Development' started by Mezy, Apr 17, 2017.

Thread Status:
Not open for further replies.
  1. Hey,

    Could someone explain me what I'm doing wrong?

    Thanks :3


    Code (Text):

    public class OnlinePlayers extends JavaPlugin implements PluginMessageListener {

        @Override
        public void onEnable() {
            this.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
            this.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", this);
        }

        @Override
        public void onPluginMessageReceived(String channel, Player player, byte[] message) {
            Bukkit.broadcastMessage("in");
            if (!channel.equals("BungeeCord")) {
                return;
            }
            ByteArrayDataInput in = ByteStreams.newDataInput(message);
            String subchannel = in.readUTF();
            if (subchannel.equals("onlinePlayers")) {
                // Use the code sample in the 'Response' sections below to read
                String server = in.readUTF();
                int playercount = in.readInt();
                Bukkit.broadcastMessage(playercount + " players on " + server);
            }
        }

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getLabel().equalsIgnoreCase("test")){
                Bukkit.broadcastMessage("out");
                ByteArrayDataOutput out = ByteStreams.newDataOutput();
                out.writeUTF("Subchannel");
                out.writeUTF("onlinePlayers");

                // get player count

                out.writeUTF("PlayerCount");
                out.writeUTF("Hub");


                Player player = Iterables.getFirst(Bukkit.getOnlinePlayers(), null);
                player.sendPluginMessage(this, "BungeeCord", out.toByteArray());
            }
            return true;
        }
    }
     
     
    #1 Mezy, Apr 17, 2017
    Last edited: Apr 17, 2017
  2. Does anyone know? Still don't know how to fix it :p
     
  3. I have the same problem, im getting 873333 as result ;(
     
  4. I'm getting no response at all
     
  5. Try this

    Code (Text):
    public abstract class PlayerCount implements PluginMessageListener {

        Plugin plugin;
        public PlayerCount(Plugin plugin){
            this.plugin = plugin;
            Bukkit.getMessenger().registerIncomingPluginChannel(plugin,"BungeeCord", this);
            Bukkit.getMessenger().registerOutgoingPluginChannel(plugin, "BungeeCord");
        }

        public void getPlayerCount(String server) throws IOException {
            ByteArrayOutputStream b = new ByteArrayOutputStream();
            DataOutputStream out = new DataOutputStream(b);

            out.writeUTF("PlayerCount");
            out.writeUTF(server);

            Bukkit.getServer().sendPluginMessage(plugin,"BungeeCord",b.toByteArray());
        }

        @Override
        public void onPluginMessageReceived(String s, Player player, byte[] bytes) {
            ByteArrayDataInput in = ByteStreams.newDataInput(bytes);

            if(!(s.equals("BungeeCord"))){
                return;
            }
            String sub = in.readUTF();
            if(sub.equals("PlayerCount")) {
                String server = in.readUTF();
                int playerCount = in.readInt();

                onRecivied(server, playerCount, player);
            }
        }

        public abstract void onRecivied(String server, int playercount, Player sender);
    }
     
  6. It sais public PlayerCount needs to return something.
     
  7. Public PlayerCount is my constructor
     
  8. Try this as reciving Method
    Code (Text):

    @Override
     public void onPluginMessageReceived(String channel, Player player, byte[] message) {
     ByteArrayDataInput in = ByteStreams.newDataInput(message);
     String command = in.readUTF();
     if (command.equals("PlayerCount")) {
      String server = in.readUTF();
      playercountsg = in.readInt();
     }
     }
     
    And this:
    Code (Text):

    private void askPlayerCount(String server){
     ByteArrayOutputStream b = new ByteArrayOutputStream();
     DataOutputStream out = new DataOutputStream(b);
     try{
     out.writeUTF("PlayerCount");
     out.writeUTF(server);
     }catch(IOException ex){
     ex.printStackTrace();
     }
     Bukkit.getServer().sendPluginMessage(plugin, "BungeeCord", b.toByteArray());
     }
     
     
  9. That worked thanks :D

    I'm trying forwarding now but it also doesn't work :p

    Code (Text):
    public class BungeeCordMessaging extends JavaPlugin implements PluginMessageListener{

        @Override
        public void onEnable() {
            getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
            getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", this);
        }

        @Override
        public void onPluginMessageReceived(String channel, Player player, byte[] message) {
            Bukkit.broadcastMessage("in");
            if (!channel.equals("BungeeCord")) {
                return;
            }
            ByteArrayDataInput in = ByteStreams.newDataInput(message);
            String subchannel = in.readUTF();
            if (subchannel.equals("test")) {
                try {
                    short len = in.readShort();
                    byte[] msgbytes = new byte[len];
                    in.readFully(msgbytes);

                    DataInputStream msgin = new DataInputStream(new ByteArrayInputStream(msgbytes));
                    String somedata = msgin.readUTF();
                    short somenumber = msgin.readShort();
                    Bukkit.broadcastMessage(somedata);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }

        public void sendStartData(){
            ByteArrayDataOutput out = ByteStreams.newDataOutput();
            try {
                out.writeUTF("Forward");
                out.writeUTF("ALL");
                out.writeUTF("test");

                ByteArrayOutputStream msgbytes = new ByteArrayOutputStream();
                DataOutputStream msgout = new DataOutputStream(msgbytes);
                msgout.writeUTF("Some kind of data here");
                msgout.writeShort(123);
                out.writeShort(msgbytes.toByteArray().length);
                out.write(msgbytes.toByteArray());
                Bukkit.broadcastMessage("out");
            }catch(IOException ex){
                ex.printStackTrace();
                Bukkit.broadcastMessage("catch");
            }

            Bukkit.getServer().sendPluginMessage(this, "BungeeCord", out.toByteArray());
        }

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getLabel().equalsIgnoreCase("test")){
                sendStartData();
            }
            return true;
        }
    }
     
  10. Do I have to replace that test with Forward?
     
Thread Status:
Not open for further replies.