Solved How to send information to spigot form Bungee

Discussion in 'BungeeCord Plugin Development' started by AsssassinJianer, Jun 12, 2018.

  1. Hey, i'm working on a plugin that will make the spigot server send a different Scoreboard Title depending on the IP that they joined from in Bungee, all of that i have it working but i have no idea of how to send information from bungee to spigot, the other way around seem to be easy as explained here: https://www.spigotmc.org/wiki/bukkit-bungee-plugin-messaging-channel/
    I tried using this method:
    https://www.spigotmc.org/threads/send-message-to-bungeecord.3779/ But it will give me errors:

    Code (Text):


    WARNING: Error dispatching event PostLoginEvent(player=AssasinJianer25) to listener [email protected]
    java.lang.NullPointerException
        at infinity.me.Main.sendMessage(Main.java:43)
        at infinity.me.Main.onPostLogin(Main.java:74)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at net.md_5.bungee.event.EventHandlerMethod.invoke(EventHandlerMethod.java:19)
        at net.md_5.bungee.event.EventBus.post(EventBus.java:46)
        at net.md_5.bungee.api.plugin.PluginManager.callEvent(PluginManager.java:377)
        at net.md_5.bungee.connection.InitialHandler$6$1.run(InitialHandler.java:513)
        at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
        at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404)
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:463)
        at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:886)
        at java.lang.Thread.run(Unknown Source)

     
     
    • Informative Informative x 1

  2. I grabbed this from that post


    Code (Text):


    public void sendMessage(String message, Server server) {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            DataOutputStream out = new DataOutputStream(stream);
            try {
                out.writeUTF("Hello!");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            server.sendData("BungeeCord", stream.toByteArray()); // (Main.java:43)
            // Alright quick note here guys : ProxiedPlayer.sendData() [b]WILL NOT WORK[/b]. It will send it to the client, and not to the server the client is connected. See the difference ? You need to send to Server or ServerInfo.
        }

     
     
  3. Yup:

    Spigot:
    Code (Text):


    package me.infinity.main;

    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.plugin.messaging.PluginMessageListener;

    import com.google.common.io.ByteArrayDataInput;
    import com.google.common.io.ByteStreams;

    public class Main extends JavaPlugin implements PluginMessageListener {
        public void onEnable() {
            this.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
            this.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", this);
        }

       
        public void sendMessage(String message) {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            DataOutputStream out = new DataOutputStream(stream);
            try {
                out.writeUTF("Hello!");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            this.getServer().getPlayer("AssasinJianer25").sendPluginMessage(this, "BungeeCord", stream.toByteArray());
            //this.getServer().getOnlinePlayers()[0].sendPluginMessage(this, "YourPluginChannel", stream.toByteArray());
           
        }
       
        public void onPluginMessageReceived(String channel, Player player, byte[] bytes) {
            if (!channel.equals("BungeeCord")) {
                return;
            }
       
            ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
            DataInputStream in = new DataInputStream(stream);
            try {
                this.getLogger().info(in.readUTF());
                if(in.readUTF().toString().contains("plover")) {
                    Bukkit.broadcastMessage("Player joined thru plover");              
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
       
    }

     
    Bungee:
    Code (Text):


    package infinity.me;

    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;

    import net.md_5.bungee.api.ChatColor;
    import net.md_5.bungee.api.ProxyServer;
    import net.md_5.bungee.api.chat.TextComponent;
    import net.md_5.bungee.api.config.ServerInfo;
    import net.md_5.bungee.api.connection.ProxiedPlayer;
    import net.md_5.bungee.api.connection.Server;
    import net.md_5.bungee.api.event.PluginMessageEvent;
    import net.md_5.bungee.api.event.PostLoginEvent;
    import net.md_5.bungee.api.event.PreLoginEvent;
    import net.md_5.bungee.api.plugin.Listener;
    import net.md_5.bungee.api.plugin.Plugin;
    import net.md_5.bungee.event.EventHandler;

    public class Main extends Plugin implements Listener{

        @Override
        public void onEnable() {
            getLogger().info("Infinity injected.");
            getProxy().getPluginManager().registerListener(this, this);
            this.getProxy().registerChannel("BungeeCord");
           
            this.getProxy().getPluginManager().registerListener(this, this);

        }
       
        public void sendMessage(String message, Server server) {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            DataOutputStream out = new DataOutputStream(stream);
            try {
                out.writeUTF(message);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            server.sendData("BungeeCord", stream.toByteArray());
            // Alright quick note here guys : ProxiedPlayer.sendData() [b]WILL NOT WORK[/b]. It will send it to the client, and not to the server the client is connected. See the difference ? You need to send to Server or ServerInfo.
        }
       
        @EventHandler
        public void onPluginMessage(PluginMessageEvent ev) {
            if (!ev.getTag().equals("BungeeCord")) {
                return;
            }
       
            if (!(ev.getSender() instanceof Server)) {
                return;
            }

            ByteArrayInputStream stream = new ByteArrayInputStream(ev.getData());
            DataInputStream in = new DataInputStream(stream);
            try {
                this.getLogger().info(in.readUTF());
               
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        @EventHandler
        public void onPostLogin(PostLoginEvent e) {

            for (ProxiedPlayer player : ProxyServer.getInstance().getPlayers()) {
                if(e.getPlayer().getPendingConnection().getVirtualHost().getHostString().contains("noobsters")) {
                    player.sendMessage(e.getPlayer().getName() + " has joined Noobsters!");
                    sendMessage("SSome stuff !", null);
                }
                else if(e.getPlayer().getPendingConnection().getVirtualHost().getHostString().contains("plover")) {
                    player.sendMessage(e.getPlayer().getName() + " has joined Plover!");
                    sendMessage("plover", e.getPlayer().getServer());
                }
                else {
                    player.sendMessage(e.getPlayer().getName() + " has joined Localhost!");
                    sendMessage("local", e.getPlayer().getServer());
                }
            }

        }

        @EventHandler
        public void onPostLogin(PreLoginEvent e) {

            if(e.getConnection().getVirtualHost().getHostString().contains("noobsters")) {
                //
            }
            else if(e.getConnection().getVirtualHost().getHostString().contains("noobsters")) {
                //
            }
            else {
                //
            }

        }

    }


     
     
  4. I tried using a custom channel and same error
     
  5. Wym? How do i do that?
     
  6. What you said helped me out figure it out, thanks, the problem was that the e.getPlayer().getServer() server on the PostLogin event is null since the player has not connected just yet, so, i changed the event to ServerConnected event and it worked
     
  7. best way create new Socket connect, also you need open port for it
     
  8. Teach me how to create a Socket
     
  9. take it