Solved Plugin Message to Bungeecord (not work correct)

Discussion in 'Spigot Plugin Development' started by Doc, May 11, 2016.

  1. Doc

    Doc

    To start and saw the usage guide ...
    try to put together a small plugin to try but for some reason does not work, I mean not managed to obtain the list of servers bungeecord, the user is valid and the parameters too.

    Deputy Main part of the plugin.

    Code (Text):
    public class Main extends JavaPlugin  {
        protected static Main current;

        protected static BungeeCord bungee;

        public static String worldname;

        @Override
        public void onEnable() {
            current = this;
            bungee = new BungeeCord();
     
            Bukkit.getServer().getPluginManager().registerEvents(new Events(), this);
     
            this.getServer().getMessenger().registerOutgoingPluginChannel(Main.current, "BungeeCord");
            this.getServer().getMessenger().registerIncomingPluginChannel(Main.current, "BungeeCord", bungee);

            worldname = Bukkit.getWorlds().get(0).getWorldFolder().getName();
             
     
            System.out.println("------------------------------");
            System.out.println("Plugin TEST BUNGEE activado!");
            System.out.println("------------------------------");
        }
    }
    in the BungeeCord class..
    Code (Text):

    public class BungeeCord implements PluginMessageListener {
        public static String servername = null;
        public static String[] serverlist = null;
        @Override
        public void onPluginMessageReceived(String channel, Player player, byte[] message) {
                    Main.current.getLogger().log(Level.INFO, "[DEBUG] Got Plugin Message on {0} from {1} messge was: {2}", new Object[]{channel, player.getName(), message.toString()});
            if (!channel.equals("BungeeCord")) {
                return;
            }
             
               
            ByteArrayDataInput in = ByteStreams.newDataInput(message);
            String subchannel = in.readUTF();
             
            if (subchannel.equals("GetServer")) {
                servername = in.readUTF();
            }
            if (subchannel.equals("GetServers")) {
                        BungeeCord.serverlist = in.readUTF().split(", ");
                        if(BungeeCord.serverlist == null || BungeeCord.serverlist.length == 0) {
                            //not work :c
                        }
            }
        }
        protected static void getProxyInfo(Player Sender) { //Test
            SendUTF(Sender, "GetServer");
            SendUTF(Sender, "GetServers");
        }
        protected static void SendUTF(Player player, String string) {
            ByteArrayDataOutput out = ByteStreams.newDataOutput();
            out.writeUTF(string);
            Bukkit.broadcastMessage("SENDUTF PRE_SENDPLUGINMESSAGE");
            player.sendPluginMessage(Main.current, "BungeeCord", out.toByteArray());
        }
        public static void MovePlayer(Player p, String Server) { //Move player to other server
            Bukkit.getMessenger().registerOutgoingPluginChannel(Main.current, "BungeeCord");
            ByteArrayOutputStream b = new ByteArrayOutputStream();
            DataOutputStream out = new DataOutputStream(b);
            try {
                out.writeUTF("Connect");
                out.writeUTF(Server);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            p.sendPluginMessage(Main.current, "BungeeCord", b.toByteArray());
        }
    }
     
    Reason can not understand why I do not get results, even debug porlo not leave it appear that the message is not received my plugin.
    Any ideas?
     
    #1 Doc, May 11, 2016
    Last edited: May 12, 2016
  2. MiniDigger

    Supporter

    have I missed something or are you not sending anything at all? I saw the methods in your bungee class but they don't seem to get called anywhere ^^
     
  3. Also, why are you registering the outgoing plugin channel under the 'MovePlayer' method when you are already registering it on enable? It only needs to be registered once.
     
    • Agree Agree x 1
  4. Doc

    Doc

    Haps.. I forget to say...
    i have another class for events and there I check the variables then make request..

    Code (Text):

    //Class Events register in Main
    @EventHandler
        public void OnPlayerJoin(PlayerJoinEvent event){
            if(BungeeCord.serverlist == null || BungeeCord.servername == null){ //Null datas
                Bukkit.broadcastMessage("[DEBUG] Datos NULL, se procede a renovar datos");
                BungeeCord.getProxyInfo(event.getPlayer());
                Bukkit.broadcastMessage("[DEBUG] Datos NULL, se realizo accion anterior");
            }
        }
     
    Well.. my error.. but still it does not work as it should .. at least getproxy info because variables remain null and debug not go so you do not run for any reason.
     
  5. Doc

    Doc

    Well..
    Finally fixed the "error"
    I add a Runnable to 20L in onplayerjoin. and work :3

    Code (Text):

    @EventHandler
      public void OnPlayerJoin(PlayerJoinEvent event){
         if(BungeeCord.serverlist == null || BungeeCord.servername == null){
      new BukkitRunnable(){ //Generamos un runnable para que en un lapso se ejecute la solicitud de info
      @Override
      public void run() {
      BungeeCord.getProxyInfo(event.getPlayer());
      }
      }.runTaskLater(Main.current,20L); //Se ejecutara en 20 ticks
         }
      }