Wrong MessageChannel

Discussion in 'BungeeCord Plugin Development' started by celcius, Jun 11, 2021 at 3:47 AM.

  1. Hi, i am new in bunge and i am triying to send a message from bungecord to spigot, but does not works.

    this is my code

    Code (Text):
    package com.celciusjj.superteleportbungee;

    import com.celciusjj.superteleportbungee.commands.CommandsUser;
    import net.md_5.bungee.api.ProxyServer;
    import net.md_5.bungee.api.plugin.Plugin;

    public class SuperTeleportBungee extends Plugin {
            @Override
            public void onEnable()
            {
                getProxy().registerChannel( "my:superteleport" );
                getLogger().info( "Plugin enabled!" );
                ProxyServer.getInstance().getPluginManager().registerCommand(this, new CommandsUser());
            }

            @Override
            public void onDisable()
            {
                getLogger().info( "Plugin disabled!" );
            }
    }
     

    Code (Text):
    package com.celciusjj.superteleportbungee;

    import com.google.common.io.ByteArrayDataInput;
    import com.google.common.io.ByteStreams;
    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.plugin.Listener;
    import net.md_5.bungee.event.EventHandler;

    public class PluginMessageReceiver implements Listener
    {

        @EventHandler
        public void on(PluginMessageEvent event)
        {
            if ( !event.getTag().equalsIgnoreCase( "my:superteleport" ) )
            {
                return;
            }
            ByteArrayDataInput in = ByteStreams.newDataInput( event.getData() );
            String subChannel = in.readUTF();
            if ( subChannel.equalsIgnoreCase( "OtherSubchannel" ) )
            {
                // the receiver is a ProxiedPlayer when a server talks to the proxy
                if ( event.getReceiver() instanceof ProxiedPlayer)
                {
                    ProxiedPlayer receiver = (ProxiedPlayer) event.getReceiver();
                    // do things
                }
                // the receiver is a server when the proxy talks to a server
                if ( event.getReceiver() instanceof Server)
                {
                    Server receiver = (Server) event.getReceiver();
                    // do things
                }
            }
        }
    }

    Code (Text):
    package com.celciusjj.superteleportbungee.commands;

    import com.celciusjj.superteleportbungee.SuperTeleportBungee;
    import com.google.common.io.ByteArrayDataOutput;
    import com.google.common.io.ByteStreams;
    import net.md_5.bungee.api.CommandSender;
    import net.md_5.bungee.api.ProxyServer;
    import net.md_5.bungee.api.connection.ProxiedPlayer;
    import net.md_5.bungee.api.plugin.Command;

    import java.util.Collection;

    public class CommandsUser extends Command {


        public CommandsUser() {
            super("teleportBungee");
        }

        public void execute(CommandSender sender, String[] strings) {
            if ((sender instanceof ProxiedPlayer)) {
                ProxiedPlayer p = (ProxiedPlayer)sender;
                if(strings[0].equals("mazmorras")) {
                    sendCustomData(p);
                    p.connect(ProxyServer.getInstance().getServerInfo("mazmorras"));
                }
            }
        }

        public void sendCustomData(ProxiedPlayer player)
        {
            Collection<ProxiedPlayer> networkPlayers = ProxyServer.getInstance().getPlayers();
            // perform a check to see if globally are no players
            if ( networkPlayers == null || networkPlayers.isEmpty() )
            {
                return;
            }
            ByteArrayDataOutput out = ByteStreams.newDataOutput();
            out.writeUTF( "my:superteleportspigot" ); // the channel could be whatever you want
            out.writeUTF(String.valueOf(player.getUniqueId()));
            out.writeUTF("eventos");
            out.writeFloat(1000);
            out.writeFloat(60);
            out.writeFloat(-1000);
            //ProxyServer().getInstance().getConsole().sendMessage("entra");
            // we send the data to the server
            // using ServerInfo the packet is being queued if there are no players in the server
            // using only the server to send data the packet will be lost if no players are in it
            player.getServer().getInfo().sendData( "my:superteleport", out.toByteArray() );

        }
    }
     

    SPIGOT PLUGIN



    Code (Text):
    ackage com.celciusjj.superteleport;

    import com.celciusjj.superteleport.events.PlayerEvent;
    import com.google.common.io.ByteArrayDataInput;
    import com.google.common.io.ByteStreams;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.plugin.messaging.PluginMessageListener;

    import java.util.HashMap;
    import java.util.Map;
    import java.util.UUID;

    public class SuperTeleport extends JavaPlugin implements PluginMessageListener {

        private Map<UUID, Data> data = new HashMap<>();
        @Override
        public void onEnable() {
            Bukkit.getConsoleSender().sendMessage("§5 The plugin has been enabled §b 1.0");
            Bukkit.getConsoleSender().sendMessage("§5 Developed by §b Celcius");

            checkIfBungee();
            if ( !getServer().getPluginManager().isPluginEnabled( this ) )
            {
                return;
            }
            getServer().getMessenger().registerIncomingPluginChannel( this, "my:superteleport", this ); // we register the incoming channel
            // you can register outgoing channel if you want to send messages to the proxy
            // getServer().getMessenger().registerOutgoingPluginChannel( this, "my:channel" );
            getLogger().info( "§5[SuperTeleport] * driver enabled successfully." );
            registerEvents();
        }

        @Override
        public void onDisable() {
            Bukkit.getConsoleSender()
                    .sendMessage("§5 The plugin has been disabled");
        }

        @Override
        public void onPluginMessageReceived(String channel, Player player, byte[] bytes)
        {
            if ( !channel.equalsIgnoreCase( "my:superteleport" ) )
                {
                return;
            }
            Bukkit.getConsoleSender().sendMessage("llego un mensaje");
            ByteArrayDataInput in = ByteStreams.newDataInput( bytes );
            String subChannel = in.readUTF();
            if ( subChannel.equalsIgnoreCase( "my:superteleportspigot" ) )
            {
                String UUID = in.readUTF();
                String world = in.readUTF();
                double x = in.readFloat();
                double y = in.readFloat();
                double z = in.readFloat();
                Data info = new Data(UUID, world, x,y,z);
                getData().put(java.util.UUID.fromString(UUID), info);
                Bukkit.getConsoleSender().sendMessage(UUID + " " + world + " " + x + " " + y +" " + z + " ");

                // do things with the data
            }
        }

        private void checkIfBungee()
        {

            if ( !getServer().spigot().getConfig().getConfigurationSection("settings").getBoolean( "settings.bungeecord" ) )
            {
                getLogger().severe( "This server is not BungeeCord." );
                getLogger().severe( "If the server is already hooked to BungeeCord, please enable it into your spigot.yml aswell." );
                getLogger().severe( "Plugin disabled!" );
                getServer().getPluginManager().disablePlugin( this );
            }
        }

        public void registerEvents() {
            PluginManager mg = getServer().getPluginManager();
            mg.registerEvents(new PlayerEvent(), this);
        }

        public Map<UUID, Data> getData() {
            return data;
        }

        public void setData(Map<UUID, Data> data) {
            this.data = data;
        }
    }
     


    if you can help me, I'll be very greatful
     
    #1 celcius, Jun 11, 2021 at 3:47 AM
    Last edited: Jun 11, 2021 at 6:15 PM