Solved How Can I Send a command to a server easy.

Discussion in 'BungeeCord Plugin Development' started by TrillestPvP, Sep 28, 2018.

  1. How do I do this? I have 3 Spigot servers and a bungeecord server and how do I make it so I can execute a command like /sendcommand [server] [command] and the command executes on that server or do it like: /send[server] [command]
     
  2. The most simple thing you can probably use is the BungeeCord Plugin Messaging channels for this. Take a look at this for more information: https://www.spigotmc.org/wiki/bukkit-bungee-plugin-messaging-channel/
     
  3. When you said an command, it's an spigot command or bungeecord that you want to send ?
     
  4. You can do it with messaging channels, read about that and for send the action to an specified server you need to get your Server variable and use sendPluginMessage function
     
  5. A spigot command
     
  6. You could use BungeeSpigotBridge
     
  7. Wait does that allow me to send a command from a server to another server? If so how?
     
  8. Use plugin messaging channel. And create a channel just for that. Then interpret this with the spigot plugin and execute the commande with Bukkit.dispatchCommand();
     
  9. And... how do I do that? Also does that mean I can send any command through to a certain server?
     
  10. Of course you can but you have to one player on a server to execute a command. There are a lot a of tutorial on YouTube or the doc
     
  11. Can't I make console execute a command? Also, HOW do I do it?
     
  12. Please someone explain the code to do it.
     
  13. I got it but someone has to be online for it to send the command... wierd.
     
  14. Do you know how to fix that?
     
  15. No it’s impossible. You must have to have one player or use others.
     
  16. Is it possible for me to queue the command and send it once a player is online?

    Bungeecord

    BungeeSend class:
    Code (Text):
    package me.enchanted.bungeesend;

    import net.md_5.bungee.api.plugin.Plugin;

    public class BungeeSend extends Plugin {
       
        @Override
        public void onEnable(){
            getProxy().getPluginManager().registerCommand(this, new SendCommand());
        }
    }
     
    SendCommand Class
    Code (Text):
    package me.enchanted.bungeesend;

    import java.io.ByteArrayOutputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.util.Arrays;

    import net.md_5.bungee.BungeeCord;
    import net.md_5.bungee.api.CommandSender;
    import net.md_5.bungee.api.config.ServerInfo;
    import net.md_5.bungee.api.plugin.Command;

    public class SendCommand extends Command {

        public SendCommand() {
            super("sendcommand", "enchanted.send");
            // TODO Auto-generated constructor stub
        }

        @SuppressWarnings("deprecation")
        @Override
        public void execute(CommandSender sender, String[] args) {
            if(args.length > 2){
                String server = args[0];
                String[] newArgs = Arrays.copyOfRange(args, 1, args.length);
                String command = joinCommand(newArgs);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                DataOutputStream out = new DataOutputStream(baos);
                try {
                    out.writeUTF("commandrequest");
                    out.writeUTF(command);
                } catch (IOException ex) {

                }
                ServerInfo info = BungeeCord.getInstance().getServerInfo(server);
                if(info != null){
                    if(info.sendData("BungeeCord", baos.toByteArray() , false)){
                        //ok
                    }else{
                        //not ok
                    }
                }else{
                }
                sender.sendMessage("§Server doesn't exist!");
            }else{
                sender.sendMessage("§cUsage: /sendcommand <Server> <Command>");
            }

        }
       
        public static String joinCommand(String[] args){
            StringBuilder builder = new StringBuilder();
            builder.append(args[0]);
            for(int i = 1 ; i < args.length ; i++){
                builder.append(" ").append(args[i]);
            }
            return builder.toString();
        }

    }
     
    Spigot

    KitPvPRecieve Class
    Code (Text):
    package me.enchanted.kitpvprecieve;

    import org.bukkit.plugin.java.JavaPlugin;

    public class KitPvPRecieve extends JavaPlugin {
       
        @Override
        public void onEnable() {
            getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", new Recieve());
        }

    }
     
    Recieve Class
    Code (Text):
    package me.enchanted.kitpvprecieve;

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

    import org.bukkit.entity.Player;
    import org.bukkit.plugin.messaging.PluginMessageListener;

    public class Recieve implements PluginMessageListener {

        @Override
        public void onPluginMessageReceived(String channel, Player player, byte[] data) {
            ByteArrayInputStream bais = new ByteArrayInputStream(data);
            DataInputStream stream = new DataInputStream(bais);
            try {
                String subChannel = stream.readUTF();
                String command = stream.readUTF();
                if(subChannel.equals("commandrequest")){
                    KitPvPRecieve.getPlugin(KitPvPRecieve.class).getServer().dispatchCommand(KitPvPRecieve.getPlugin(KitPvPRecieve.class).getServer().getConsoleSender() , command);
                }
            } catch (IOException e) {
               
            }
        }

    }
     
    With my current code how can I do that?
     
  17. Spigot shutdown so I think I have to quote you again...
     

Share This Page