1.17.1 Create plugin broadcast

Discussion in 'Spigot Plugin Development' started by Karliah, Aug 15, 2021.

  1. Hi, i'm trying to create a custom broadcast command but doesn't work.
    Everytime i launch the server i got an error.

    The code :
    Code (Java):
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class CommandBcboutique implements CommandExecutor {

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String msg, String[] args) {
           
            if(sender instanceof Player) {
                Player player = (Player)sender;
               
                if(cmd.getName().equalsIgnoreCase("bcboutique")){
                   
                    if (args.length == 0) {
                        player.sendMessage("La commande est : /bcboutique <message>");
                        }
                   
                    if (args.length >= 1) {
                       
                        StringBuilder bc = new StringBuilder();
                        for(String part : args) {
                            bc.append(part + " ");
                           
                        }
                       
                        Bukkit.broadcastMessage("[" + player.getName() + "] " + bc.toString());
                       
                    }
                   
                    return true;
                }
               
            }
           
            return false;
        }

    }

    The error :
    I'm a novice in the creation of plugin, i try to do my best but i can't find a way by myself so if someone can help me plz :)
    Have a nice day.
     
  2. Hello / Salut l'ami,

    You are trying to get a command that does not exists: you have forgot to add your command name into the plugin.yml file.
    Check the Add to plugin.yml part from the tutorial here: https://www.spigotmc.org/wiki/create-a-simple-command/

    Also, please remove the [Resource] tag, as this is not a resource.
     
    • Like Like x 1
  3. Hi sorry for the prefix, so this is my plugin.yml :

    Code (Text):
      bcboutique:
        description: Commande de broadcast de la boutique
    I think it's ok no ?
     
  4. Please show me the whole plugin.yml.
    Also, please check that into your fr.karliah.dpp.Dpp class, when you register your command, you correctly use getCommand("bcboutique");.
     
  5. Dpp file
    Code (Java):
    package fr.karliah.dpp;

    import org.bukkit.plugin.java.JavaPlugin;

    import fr.karliah.dpp.commands.CommandBcboutique;
    import fr.karliah.dpp.commands.CommandEnd;
    import fr.karliah.dpp.commands.CommandMondes;
    import fr.karliah.dpp.commands.CommandNether;
    import fr.karliah.dpp.commands.CommandRessource;

    public class Dpp extends JavaPlugin {

        @Override
        public void onEnable() {
            System.out.println("Le Plugin vient de s'allumer !");
            getCommand("mondes").setExecutor(new CommandMondes());
            getCommand("end").setExecutor(new CommandEnd());
            getCommand("nether").setExecutor(new CommandNether());
            getCommand("ressource").setExecutor(new CommandRessource());
            getCommand("bcboutique").setExecutor(new CommandBcboutique());
         
        }

        @Override
        public void onDisable() {
            System.out.println("Le plugin vient de s'├ęteindre ! ");
        }  
     
     
     
    }
     

    plugin.yml file
    Code (Text):
    name: DPP
    version: 1.0
    author: Karliah
    main: fr.karliah.claim.Claim
    commands:
      mondes:
        description: Ceci est une commande pour se teleporter aux mondes
      end:
        description: Ceci est une commande pour se teleporter au monde end
      nether:
        description: Ceci est une commande pour se teleporter au monde nether
      bcboutique:
        description: Commande de broadcast de la boutique

    I think i see the problem now ...
     
  6. Ok so it was a mistake made by me.
    But now i have a little problem, i have made this command only launchable by a player but i need to launch this command with the command panel, don't know the name in english.
     
  7. Just remove the check to see if the sender is a player and replace "player" with "sender" for the rest of the command.
     
  8. Then if you do not need a player object, don't use if(sender instanceof Player) {.
    This is a very optimized version of your code:
    Code (Java):
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String msg, String[] args) {
        if (args.length == 0) {
            player.sendMessage("La commande est : /bcboutique <message>");
            return true;
        }
     
        final String message = String.join(" ", args)
        Bukkit.broadcastMessage("[" + sender.getName() + "] " + message);
        return true;
    }
    By the way, you have to note that Bukkit#broadcastMessage method will work only for players which has the bukkit.broadcast.user permission.
    If you do not want this, you'll have to iterate over all players to send the message:
    Code (Java):
    Bukkit.getOnlinePlayers().forEach(p -> p.sendMessage(message);
     
  9. Thx so much for your help, all working good.
     
  10. I would like to ask you one more thing plz, how to add a cooldown for this command with a message for the player to tell him how much time left to do the command again ?
     
    #10 Karliah, Aug 16, 2021
    Last edited: Aug 17, 2021
  11. md_5

    Administrator Developer

    Or use the broadcastMessage which also has a string argument
     
    • Like Like x 1