Create Plugin Message Channel

Discussion in 'Plugin Development' started by LCastr0, Jun 21, 2014.

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

    LCastr0

    How would I create a Plugin Message Channel for my plugin? Like the Bungee has a plugin message channel, I need one for my plugin.
     
  2. Offline

    Laxer21117

    Code:java
    1. public class MyPlugin extends JavaPlugin implements PluginMessageListener {
    2. @Override
    3. public void onEnable() {
    4. this.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
    5. this.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", this);
    6. }
    7.  
    8. public static String serverName; // Example: using the GetServer
    9. // subchannel
    10.  
    11. @Override
    12. public void onPluginMessageReceived(String channel, Player player, byte[] message) {
    13. if (!channel.equals("BungeeCord")) {
    14. return;
    15. }
    16.  
    17.  
    18. try {
    19. String subchannel = in.readUTF();
    20. if (subchannel.equals("SomeSubChannel")) {
    21. // Use the code sample in the 'Response' sections below to read
    22. // the data.
    23. } else if (subchannel.equals("SomeOtherSubChannel")) {
    24. // Read the data for a different subchannel.
    25. } else if (subchannel.equals("GetServer")) {
    26. // Example: GetServer subchannel
    27. serverName = in.readUTF();
    28. }
    29. } catch (IOException e) {
    30. // There was an issue in creating the subchannel string
    31. }
    32. }
    33.  
    34. }
    35.  


    Found this on spigot, See if this works...
     
  3. Offline

    LCastr0

    I don't want to use Bungee's channel, I want to create my own channel...
     
    Laxer21117 likes this.
  4. Offline

    xTigerRebornx

  5. Offline

    LCastr0

    It does nothing :/
    Code:java
    1. package me.LCastr0.PMC;
    2.  
    3. import java.io.ByteArrayInputStream;
    4. import java.io.ByteArrayOutputStream;
    5. import java.io.DataInputStream;
    6. import java.io.DataOutputStream;
    7.  
    8. import org.bukkit.Bukkit;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.player.PlayerJoinEvent;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14. import org.bukkit.plugin.messaging.PluginMessageListener;
    15.  
    16. public class Load extends JavaPlugin implements PluginMessageListener, Listener{
    17.  
    18. public void onEnable(){
    19.  
    20. Bukkit.getMessenger().registerOutgoingPluginChannel(this, "GalaxyApi");
    21. Bukkit.getMessenger().registerIncomingPluginChannel(this, "GalaxyApi", this);
    22. getServer().getPluginManager().registerEvents(this, this);
    23.  
    24. }
    25.  
    26. @EventHandler
    27. public void onPlayerJoin(PlayerJoinEvent event){
    28. Player player = event.getPlayer();
    29. try{
    30. out.writeUTF("subchannel");
    31. player.sendMessage("sending...");
    32. player.sendPluginMessage(this, "GalaxyApi", b.toByteArray());
    33. player.sendMessage("sent!");
    34. } catch(Exception e){
    35. e.printStackTrace();
    36. }
    37. }
    38.  
    39. @Override
    40. public void onPluginMessageReceived(String channel, Player player, byte[] message) {
    41.  
    42. player.sendMessage("received");
    43. try{
    44. String msg = in.readUTF();
    45. Bukkit.broadcastMessage(msg);
    46. } catch(Exception e){
    47. e.printStackTrace();
    48. }
    49.  
    50. }
    51.  
    52. }
    53.  
     
  6. Offline

    ferrybig

    Do you have some code at client catching your message?
     
  7. Offline

    LCastr0

    Not sure '-' What is it? How would I do that? Lol. Probaby not
     
  8. Offline

    Zupsub

    What are you trying to do?
    I think you don't know, what this channels are.
     
  9. Offline

    mazentheamazin

    LCastr0
    You need to flush or close (as it flushes anyways) the output stream, streams are not 'a stream of bits and bytes'.
     
  10. Offline

    LCastr0

    I want to create my plugin channel
     
  11. Offline

    LCastr0

    Bump? D:
     
Thread Status:
Not open for further replies.

Share This Page