Solved how do I get variables?

Discussion in 'Plugin Development' started by cognate, Jun 5, 2018.

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

    cognate

    hi guys. I'm a new bukkit developer and have almost lost all self confidence in doing this as an actual hobby because it's so freaking confusing

    anyway

    how can I get /broadcast <message> to actually work?

    so /broadcast <message>

    it broadcasts the prefix from the config and <message>

    Code:
    package me.ibenqal.helloworld.commands;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import me.ibenqal.helloworld.Main;
    
    public class Broadcast implements CommandExecutor {
       
        private Main plugin;
       
        public Broadcast(Main plugin) {
            this.plugin = plugin;
            plugin.getCommand("broadcast").setExecutor(this);
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player) sender;
           
            if (p.hasPermission("broadcast.use")) {
                JavaPlugin plugin;
                Bukkit.broadcastMessage((plugin.getConfig().getString("bprefix")) + " ");
                return true;
            }
        }
    
    }
    sorry for double posting but I came up with a new code,

    Code:
    package me.ibenqal.helloworld.commands;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import me.ibenqal.helloworld.Main;
    
    public class Broadcast implements CommandExecutor {
      
        private Main plugin;
      
        public Broadcast(Main plugin) {
            this.plugin = plugin;
            plugin.getCommand("broadcast").setExecutor(this);
          
        }
      
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage("Only players can execute this command");
                return true;
            }
            Player p = (Player) sender;
          
            if (p.hasPermission("broadcast.use")) {
                Bukkit.broadcastMessage((plugin.getConfig().getString("prefix")) + " " + label);
                return true;
            } else{
                p.sendMessage("You do not have permission to execute this command");
            }
            return false;
        }
    
    }
    
    
    Nevermind, found a solution by myself.
    Code:
    package me.ibenqal.helloworld.commands;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import me.ibenqal.helloworld.Main;
    
    public class Broadcast implements CommandExecutor {
     
        private Main plugin;
     
        public Broadcast(Main plugin) {
            this.plugin = plugin;
            plugin.getCommand("broadcast").setExecutor(this);
         
        }
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage("Only players can execute this command");
                return true;
            }
           
            if (label.length() < 1) {
                return true;
            }
            Player p = (Player) sender;
         
            if (p.hasPermission("broadcast.use")) {
                Bukkit.broadcastMessage((plugin.getConfig().getString("prefix")) + " " + label);
                return true;
            } else{
                p.sendMessage("You do not have permission to execute this command");
            }
            return false;
        }
    
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2018
  2. Offline

    SavageAvocado

    Edit your last post instead of double posting. To turn the arguments into a String, append the arguments together with a StringBuilder.

    Kinda like this:
    Code:
    String message;
    StringBuilder stringBuilder = new StringBuilder();
    
    for (String arg : args) {
      stringBuilder.append(arg).append(" ");
    }
    
    message = stringBuilder.toString().trim();
     
Thread Status:
Not open for further replies.

Share This Page