Having problems with commands not executing

Discussion in 'Plugin Development' started by Rockinredross867, Aug 7, 2015.

Thread Status:
Not open for further replies.
  1. Hello, I have been scanning through my code to check to see if I made an error and I have not found out why my commands aren't executing so I wanted to see if one of you guys know what is going on.

    Here is my Commands class.

    Code:
    package com.CustomEditor.commands;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.UUID;
    
    import net.md_5.bungee.api.ChatColor;
    
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class Commands implements CommandExecutor{
       
        public static Map<UUID, Boolean> addTerrain = new HashMap<UUID, Boolean>();
        public static Map<UUID, Boolean> subtractTerrain = new HashMap<UUID, Boolean>();
        public static Map<UUID, Integer> sphereRadius = new HashMap<UUID, Integer>();
        public static Map<UUID, Boolean> sphereHollow = new HashMap<UUID, Boolean>();
        public static Map<UUID, Material> sphereBlock = new HashMap<UUID, Material>();
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            //THIS COMMAND IS EXECUTING
            if(args[0].equalsIgnoreCase("tool") && sender instanceof Player) {
                Player player = (Player) sender;
                List<String> lore = new ArrayList<String>();
                lore.add("This tool edits your terrain with ease.");
                ItemStack magicTool = new ItemStack(Material.GOLD_AXE);
                ItemMeta magicToolItemMeta = magicTool.getItemMeta();
                magicToolItemMeta.setDisplayName(ChatColor.DARK_PURPLE.toString() + ChatColor.BOLD + "World Editor Tool");
                magicToolItemMeta.setLore(lore);
                magicTool.setItemMeta(magicToolItemMeta);
                player.getInventory().addItem(magicTool);
                player.sendMessage(ChatColor.DARK_GREEN + "Here you go!");
                return true;
            }
            //THIS ONE ISN'T
            if(args[0].equalsIgnoreCase("mode") && sender instanceof Player) {
                Player player = (Player) sender;
                if(args[1].equalsIgnoreCase("AddTerrain")) {
                    if(!addTerrain.get(player.getUniqueId())) {
                        addTerrain.put(player.getUniqueId(), true);
                        sphereHollow.put(player.getUniqueId(), false);
                        player.sendMessage(ChatColor.AQUA + "The " + ChatColor.YELLOW + "AddTerrain " + ChatColor.AQUA + "mode has just been toggled on");
                        return true;
                    } else {
                        addTerrain.put(player.getUniqueId(), false);
                        player.sendMessage(ChatColor.AQUA + "The " + ChatColor.YELLOW + "AddTerrain " + ChatColor.AQUA + "mode has just been toggled off");
                        return true;
                    }
                //THIS ONE ISN'T
                } else if(args[1].equalsIgnoreCase("SubtractTerrain")) {
                    if(!subtractTerrain.get(player.getUniqueId())) {
                        subtractTerrain.put(player.getUniqueId(), true);
                        sphereHollow.put(player.getUniqueId(), false);
                        sphereBlock.put(player.getUniqueId(), Material.AIR);
                        player.sendMessage(ChatColor.AQUA + "The " + ChatColor.YELLOW + "SubtractTerrain " + ChatColor.AQUA + "mode has just been toggled on");
                        return true;
                    } else {
                        subtractTerrain.put(player.getUniqueId(), false);
                        sphereBlock.put(player.getUniqueId(), Material.GRASS);
                        player.sendMessage(ChatColor.AQUA + "The " + ChatColor.YELLOW + "SubtractTerrain " + ChatColor.AQUA + "mode has just been toggled off");
                        return true;
                    }
                    }
            }
            //THIS ONE ISN'T
            if(args[0].equalsIgnoreCase("properties") && sender instanceof Player) {
                Player player = (Player) sender;
                if(args.length != 1)
                    return false;
                int radius = Integer.valueOf(args[1]);
                if(radius < 1) {
                    player.sendMessage(ChatColor.RED + "Radius is set too low!");
                    return false;
                } else if(radius > 90) {
                    player.sendMessage(ChatColor.RED + "Radius is set too high!");
                    return false;
                }
                try {
                    sphereRadius.put(player.getUniqueId(), radius);
                    player.sendMessage(ChatColor.AQUA + "Sphere radius has been set to " + ChatColor.YELLOW + args[1]);
                    return true;
                } catch(Exception e) {
                    player.sendMessage(ChatColor.RED + "Invalid properties!");
                    return false;
                }
            }
            //THIS ONE ISN'T
            if(args[0].equalsIgnoreCase("setBlock") && sender instanceof Player) {
                Player player = (Player) sender;
                if(player.getItemInHand().getType().isBlock()) {
                    sphereBlock.put(player.getUniqueId(), player.getItemInHand().getType());
                    player.sendMessage(ChatColor.AQUA + "The block of the sphere has been set to " + ChatColor.YELLOW + args[1]);
                    return true;
                } else {
                    player.sendMessage(ChatColor.RED + "The item that is being held is not a block!");
                }
            }
            return false;
        }
       
    }
    
     
  2. Offline

    Tecno_Wizard

    @Rockinredross867, you"re going to have to do some of your own debugging here. It isn't hard. Use some console output and figure out where things go wrong.
     
  3. @Tecno_Wizard Here is the thing there is no console output from the command. It is just the default console output Rockinredross867 has issued <command>.
     
  4. Offline

    SuperOriginal

    Oh dear god.... The static ;-;

    You don't even check for a command name, and are using args that might not even exist.
     
  5. Offline

    SantaClawz69

    You don't need to check the command name when you're using CommandExecutor. You register the command name in the main class.

    @Rockinredross867 make a constructor in your command class that uses your main class. Then go into your main class and register the command and the constructor so it knows where to look for your commands and then it'll execute.

    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Aug 8, 2015
  6. Offline

    SuperOriginal

    @SantaClawz69
    Really? Huh. That's interesting, that's not mentioned in the documentation.
     
  7. Offline

    SantaClawz69

    @SuperOriginal the documentation is for guiding you and your code, it's not for total usage. Sometimes it will just expect that you know some things. Don't worry when I started making plugins I didn't know about it either lel.
     
  8. Offline

    SuperOriginal

    @SantaClawz69 Yeah, I'm used to my own command registration system so I don't really bother with that stuff :p
     
Thread Status:
Not open for further replies.

Share This Page