How do I make an argument with multiple words?

Discussion in 'Plugin Development' started by coopjc, Oct 9, 2016.

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

    coopjc

    I'm trying to create a plugin to copy Mineplex for my amusement where if a player types /a (arguments), it sends a staff member the message. The message does send to online staff members, however, only the first word of the sentence I typed after /a is shown up. I put my code down below.
    Code:
    package me.coopjc;
    
    import com.sun.org.apache.xpath.internal.Arg;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import java.util.ArrayList;
    
    /**
    * Created by coopjc on 10/9/2016.
    */
    public class main extends JavaPlugin {
    
        public void onEnable() {
            staff.add("coopjc");
    
        }
    
        public void onDisable() {
    
        }
    
        ArrayList staff = new ArrayList();
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.equals("a" + args[0])) {
                Player p = (Player) sender;
                Player r = (Player) staff;
                p.sendMessage(ChatColor.WHITE + sender.getName() + " " + ChatColor.LIGHT_PURPLE + args[0]);
            }
            if (args == null) {
                sender.sendMessage(ChatColor.BLUE + "Message> " + ChatColor.RED + "Err...something went wrong?");
            }
                for(Player p : Bukkit.getOnlinePlayers()) {
                    if (staff.contains(p.getName())) {
                        p.sendMessage(ChatColor.WHITE + sender.getName() + " " + ChatColor.LIGHT_PURPLE + args[0]);
                    }
                }
            return true;
        }
    }

     
  2. Offline

    Zombie_Striker

    @coopjc
    1. cmd will never be equal to "a+args[0]", because args[0] is not a part of the command, it is an arg.
    2. An arraylist cannot be casted to a player. How is an arraylist the same thing as a player?
    3. If your onDisable does nothing, remove it.
    4. You have not added any storage modifiers for the arraytlist. The arraylist can hold anything, even things that are not players.
    5. You are storing names to the list, but expecting them to be a Player variable. Pick one or the other.
    6. Args will never be null. At the very least, it will be empty.
    7. Don't access arrays if you do not know if it contains anything. Right now, the command will throw an AOOB if a player does not provide an arg
    8. Main problem: you are forgetting to use a string builder. For loop through all the args and build the string
    It is extremely clear you have not learned Java yet. You can not work on bukkit without knowledge in Java, or else you will be making these mistakes. Please, pick a tutorial from the following list, learn Java, and then come back to bukkit. HERE
     
Thread Status:
Not open for further replies.

Share This Page