Development Assistance Can mods communicate with plugins?

Discussion in 'Plugin Help/Development/Requests' started by micrlink, Apr 4, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    micrlink

    Is it possible to have a mod that can receive information from a server plugin. Like by using packets. If so could I see an example or explanation.
     
  2. Offline

    NoSpanMan

    I think not.
     
  3. Invisible

    nverdier

    @micrlink Wait what exactly is it you want to accomplish?
     
  4. Offline

    teej107

  5. Offline

    mythbusterma

    @teej107 @micrlink

    That being said, without a plugin specifically supporting this sort of thing, no it is not.
     
  6. Offline

    An0nym8us

    Yes, they can.

    Bukkit has API for exactly what you mean (however, plugin<->mod communication was main goal). I don't know how does it work at mod side (look into MC Forge API or sth like that if you're mod dev too). This API is used for Bukkit<->BungeeCord communication as Plugin Messaging API.

    To send data from server to mod you have to register outgoing channel:

    Code:
    Bukkit.getServer().getMessenger().registerOutgoingPluginChannel(plugin, channel);
    Where:
    - plugin is your plugin's instance;
    - channel is string of your channel's name.

    And send method:
    Code:
    player.sendPluginMessage(plugin, channel, data);
    Where:
    - player is Player variable;
    - plugin is your plugin's instance;
    - channel is string of your channel's name;
    - data is byte array of your data (any data, it's recommended by me to use ByteArrayDataOutput to pack data).



    To receive data from mod, you have to register plugin messaging listener. You have to create class that implements PluginMessageListener interface, and in constructor (or elsewhere, but it's required) register incoming channel (incoming from mod):

    Code:
    Bukkit.getServer().getMessenger().registerIncomingPluginChannel(plugin, channel, pluginMessagingListener);
    Where:
    - plugin is your plugin's instance;
    - channel is string of your channel's name;
    - pluginMessagingListener is specified object which inherits to PluginMessageListener.



    Receiving is based on handling receiving data like handling Bukkit events, what you can do by this:

    Code:
    @Override
    public void onPluginMessageReceived(String channel, Player player, byte[] message)
    {
     
    }
    Where:
    - channel is string your channel's name;
    - player is instance of Player which sent message to server;
    - message is byte array of received data.

    This method will be called when Bukkit will receive any data, from any player, but only from channels which were registered to your listener.




    Some of practical code:

    Listener
    Code:
    public class ModListener implements PluginMessageListener
    {
        public static final String CHANNEL = "MyExtraModChannel";
     
        public ModListener(JavaPlugin plugin)
        {
            Bukkit.getServer().getMessenger().registerIncomingPluginChannel(plugin, CHANNEL, this);
         
            if(!Bukkit.getServer().getMessenger().isOutgoingChannelRegistered(plugin, CHANNEL))
            {
                Bukkit.getServer().getMessenger().registerOutgoingPluginChannel(plugin, CHANNEL);
            }
        }
    
        @Override
        public void onPluginMessageReceived(String channel, Player player, byte[] message)
        {
            if (channel.equals(CHANNEL)) // This part of code is not required, if you register only one channel, if you register more, then use if... else or switch... case to manage that :).
            {
                ByteArrayDataInput in = ByteStreams.newDataInput(message);
             
                // Reading you data
            }
        }
    }



    Sending data (I use this way, it's simple :) ):
    Code:
    void sendMessage(JavaPlugin plugin, Player player, String channel)
        {
            ByteArrayDataOutput output  = ByteStreams.newDataOutput();
         
            // Here write some data to stream, you can also read received data in related way (using ByteArrayDataInput stream)
            output.writeInt(7);
            output.writeUTF("Hello Mod!");
            output.writeLong(1234567890);
         
            player.sendPluginMessage(plugin, channel, output.toByteArray());
        }




    As I said, BungeeCord is using this API to Bukkit<->BungeeCord communication, Bungee team also wrote mini tutorial - it's for their way, don't look at subchannels: http://www.spigotmc.org/wiki/bukkit-bungee-plugin-messaging-channel/#what-is-a-plugin-message


    I hope I helped :). If you have more questions, ask here ;).
     
    Last edited: Apr 5, 2015
    Pokechu22, Konato_K and 1Rogue like this.
  7. Offline

    timtower Administrator Administrator Moderator

    Moved to Bukkit alternatives
     
  8. Offline

    Konato_K

Thread Status:
Not open for further replies.

Share This Page