Bungee/Bukkit Messaging - Vault [Prefix/Suffix] Example

Discussion in 'BungeeCord Plugin Development' started by Favorlock, Mar 10, 2013.

  1. Favorlock

    Sponsor

    Hey guys, here is a short post about how to send data between Bukkit and Bungee. Below is an example of how I am sending the prefix and suffix of a player. Yes, it looks messy and it may not be the most efficient, but its an example none the less. If you have any questions feel free to ask.

    Code (main (Unknown Language)):
    public class ExamplePlugin extends JavaPlugin {
       
        private Chat chat = null;
       
        public void onEnable() {
            if (setupChat()) {
                registerPluginChannels();
                registerListeners();
            } else {
                disable();
            }
        }
       
        private boolean setupChat()
        {
            RegisteredServiceProvider<Chat> chatProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.chat.Chat.class);
            if (chatProvider != null) {
                chat = chatProvider.getProvider();
            }
     
            return (chat != null);
        }
       
        public Chat getChat() {
            return this.chat;
        }
       
        private void registerPluginChannels() {
            Bukkit.getMessenger().registerOutgoingPluginChannel(this, "ExamplePlugin");
        }
       
        private void registerListeners() {
            getServer().getPluginManager().registerEvents(new VaultListener(this), this);
        }
       
        private void disable()
        {
            Bukkit.getPluginManager().disablePlugin(this);
        }
     
    }
     
    Code (listener (Unknown Language)):
    public class ExampleListener implements Listener {
       
        ExamplePlugin plugin;
       
        public VaultListener(ExamplePlugin plugin) {
            this.plugin = plugin;
        }
       
        @EventHandler
        public void login(PlayerJoinEvent event) {
            Player player = event.getPlayer();
            String group = plugin.getChat().getPrimaryGroup(player);
            String prefix = "";
            String suffix = "";
           
            if (plugin.getChat().getPlayerPrefix(player) != null) {
                prefix = plugin.getChat().getPlayerPrefix(player);
            } else if (plugin.getChat().getGroupPrefix(player.getWorld(), group) != null) {
                prefix = plugin.getChat().getGroupPrefix(player.getWorld(), group);
            }
            if (plugin.getChat().getPlayerSuffix(player) != null) {
                suffix = plugin.getChat().getPlayerSuffix(player);
            } else if (plugin.getChat().getGroupSuffix(player.getWorld(), group) != null) {
                suffix = plugin.getChat().getGroupSuffix(player.getWorld(), group);
            }
           
            ByteArrayOutputStream bStream = new ByteArrayOutputStream();
            DataOutputStream output = new DataOutputStream(bStream);
           
            try {
                output.writeUTF("VaultAffix");
                output.writeUTF(player.getName());
                output.writeUTF(prefix);
                output.writeUTF(suffix);
            } catch (IOException e) {
                e.printStackTrace();
            }
           
            BukkitTask task = new PluginMessageTask(this.plugin, player, bStream).runTaskLater(this.plugin, 1);
        }
     
    }
    Code (task (Unknown Language)):
    public class PluginMessageTask extends BukkitRunnable {
       
        private final ExamplePlugin plugin;
        private final Player player;
        private ByteArrayOutputStream bytes;
       
        public PluginMessageTask(ExamplePlugin plugin, Player player, ByteArrayOutputStream bytes) {
            this.plugin = plugin;
            this.player = player;
            this.bytes = bytes;
        }

        public void run() {
            player.sendPluginMessage(this.plugin, "ExamplePlugin", this.bytes.toByteArray());
        }

    }
     
  2. Wow didnt even see this post. Here was the example I tried and didnt get time to test. I may change the getter part to something similar to yours though
    Code (Text):
        @EventHandler
        public void login(PlayerJoinEvent event) {
            Player player = event.getPlayer();
            String prefix = "";
            String suffix = "";
            if(plugin.groupFixes){
            String group = plugin.chat.getPrimaryGroup(player);
            prefix = plugin.chat.getGroupPrefix(player.getWorld(), group);
            suffix = plugin.chat.getGroupSuffix(player.getWorld(), group);
            }else{
                prefix = plugin.chat.getPlayerPrefix(player);
                suffix = plugin.chat.getPlayerSuffix(player);
            }
            ByteArrayOutputStream b = new ByteArrayOutputStream();
            DataOutputStream out = new DataOutputStream(b);
            try {
                out.writeUTF("PrefixSuffix");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                out.writeUTF(player.getName());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                out.writeUTF(prefix);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                out.writeUTF(suffix);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            player.sendPluginMessage(plugin, "BungeeSuite", b.toByteArray());
        }
     
  3. Favorlock

    Sponsor

    Sorry, but that won't work without a delayed task. I ran into the problem and md_5 explained that you couldn't send a plugin message on a PlayerJoinEvent without using a delayed task. Just set the delay to 1 tick.
     
  4. Ah thanks I had done the delayed task previously with something else to fix the same issue just forgot about it. Thanks lol