Solved 2 commands, similar command, interferes with eachother

Discussion in 'Plugin Development' started by RandomHashTags, Oct 10, 2015.

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

    RandomHashTags

    I want to run the command "/ah sell", but then my other command, "auctionhouse", or "ah" or an aliases, interferes with it. Any fix?
    Main:
    Code:
    package auctionHouse;
    
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
      
        public void onEnable() {
            registerEvents();
            getCommand("auctionhouse").setExecutor(new auctionHouse());
            getCommand("auctionhouse sell").setExecutor(new auctionHouseSell());
          
        }
        public void onDisable() {
          
        }
        public void registerEvents() {
            PluginManager pm = getServer().getPluginManager();
          
            pm.registerEvents(new auctionHouseClose(), this);
            pm.registerEvents(new auctionHouseRefresh(), this);
            pm.registerEvents(new auctionHouseSell(), this);
        }
    
    }
    
    auctionHouse:
    Code:
    package auctionHouse;
    
    import org.bukkit.Bukkit;
    import org.bukkit.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.event.Listener;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class auctionHouse implements Listener, CommandExecutor {
    
        public Inventory auctionHouse;
      
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
          
            if(!(sender instanceof Player)) {
                sender.sendMessage("You must be a player to execute this command!");
                return true;
            }
          
            if(sender instanceof Player) {
                Player player = (Player) sender;
              
                ItemStack refresh = new ItemStack(Material.CHEST, 1);
                ItemMeta refreshMeta = refresh.getItemMeta();
                refreshMeta.setDisplayName(ChatColor.GOLD + "" + ChatColor.BOLD + "Refresh Auctions");
                refresh.setItemMeta(refreshMeta);
              
                ItemStack close = new ItemStack(Material.STAINED_GLASS_PANE, 1, ((byte) 14));
                ItemMeta closeMeta = close.getItemMeta();
                closeMeta.setDisplayName(ChatColor.RED + "Close");
                close.setItemMeta(closeMeta);
              
                auctionHouse = Bukkit.createInventory(null, 54, "Auction House");
                auctionHouse.setItem(49, refresh);
                auctionHouse.setItem(53, close);
                player.openInventory(auctionHouse);
            }
            return true;
        }
    }
    
    auctionHouseSell:
    Code:
    package auctionHouse;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    //import org.bukkit.inventory.ItemStack;
    //import org.bukkit.inventory.PlayerInventory;
    
    public class auctionHouseSell implements Listener, CommandExecutor { // Player did /ah sell
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
          
            if(!(sender instanceof Player)) {
                sender.sendMessage("You must be a player to execute this command!");
            }
            if(sender instanceof Player) {
                sender.sendMessage("testing the ah sell"); // Doesn't get executed / sent to player
              
            }
            return true;
        }
    
    }
    
    Plugin YML:
    HTML:
    name: RandomAuctionHouse
    author: RandomHashTags
    main: auctionHouse.Main
    version: 1.0
    description: Best auction house plugin here!
    commands:
      auctionhouse:
        description: Opens the AuctionHouse
        usage: /ah
        aliases: [ah]
      auctionhouse sell:
        description: Puts the item in your hand in the ah
        usage: /ah sell <price>
        aliases: [ah_sell]
     
  2. Offline

    SuperSniper

    @RandomHashTags For arguments "sell" as in "auctionhouse sell", you need to check the argument length and the argument string inside of the "auctionhouse" command.

    Ill give you an example using your code:

    Code:
    package auctionHouse;
    
    import org.bukkit.Bukkit;
    import org.bukkit.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.event.Listener;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class auctionHouse implements Listener, CommandExecutor {
    
       public Inventory auctionHouse;
     
       @Override
       public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
         
           if(!(sender instanceof Player)) {
                sender.sendMessage("You must be a player to execute this command!");
               return true;
           }
         
           if(sender instanceof Player) {
                Player player = (Player) sender;
             
                ItemStack refresh = new ItemStack(Material.CHEST, 1);
                ItemMeta refreshMeta = refresh.getItemMeta();
                refreshMeta.setDisplayName(ChatColor.GOLD + "" + ChatColor.BOLD + "Refresh Auctions");
                refresh.setItemMeta(refreshMeta);
             
                ItemStack close = new ItemStack(Material.STAINED_GLASS_PANE, 1, ((byte) 14));
                ItemMeta closeMeta = close.getItemMeta();
                closeMeta.setDisplayName(ChatColor.RED + "Close");
               close.setItemMeta(closeMeta);
             
                auctionHouse = Bukkit.createInventory(null, 54, "Auction House");
                auctionHouse.setItem(49, refresh);
                auctionHouse.setItem(53, close);
                player.openInventory(auctionHouse);
           }else if(args.length == 1) {
              if(args[0].equalsIgnoreCase("sell")) {
                        player.sendMessage("testing the ah sell");
    }
    }
           return true;
       }
    }
    
    
    All you need to do is register the "auctionhouse" command, not the sell command, since it's part of the auctionhouse command.

    Code:
    getCommand("auctionhouse").setExecutor(new auctionHouse());
    
     
    Zombie_Striker likes this.
  3. Offline

    Zombie_Striker

    JavaNamingConventioins, don't call a class main and atleast have two files ( such as "Something.Something") in your package name
    Code:
            getCommand("auctionhouse sell").
    Nope, cannot register an arg with the command.

    I don't see any events.
     
  4. Offline

    RandomHashTags

    @Zombie_Striker My bad. Some of the implements don't even need to be their.
    @SuperSniper Seems to not be working. Added the code into mine, but still opens the GUI.
     
  5. Offline

    SuperSniper

  6. Offline

    RandomHashTags

    Main:
    Code:
    package auctionHouse;
    
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
       
        public void onEnable() {
            registerEvents();
            getCommand("auctionhouse").setExecutor(new auctionHouse());
           
        }
        public void onDisable() {
           
        }
        public void registerEvents() {
            PluginManager pm = getServer().getPluginManager();
           
            pm.registerEvents(new auctionHouseClose(), this);
            pm.registerEvents(new auctionHouseRefresh(), this);
            pm.registerEvents(new auctionHouseSell(), this);
        }
    
    }
    
    auctionHouse:
    Code:
    package auctionHouse;
    
    import org.bukkit.Bukkit;
    import org.bukkit.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.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class auctionHouse implements CommandExecutor {
    
        public Inventory auctionHouse;
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
           
            if(!(sender instanceof Player)) {
                sender.sendMessage("You must be a player to execute this command!");
                return true;
            }
           
            if(sender instanceof Player) {
                Player player = (Player) sender;
               
                ItemStack refresh = new ItemStack(Material.CHEST, 1);
                ItemMeta refreshMeta = refresh.getItemMeta();
                refreshMeta.setDisplayName(ChatColor.GOLD + "" + ChatColor.BOLD + "Refresh Auctions");
                refresh.setItemMeta(refreshMeta);
               
                ItemStack close = new ItemStack(Material.STAINED_GLASS_PANE, 1, ((byte) 14));
                ItemMeta closeMeta = close.getItemMeta();
                closeMeta.setDisplayName(ChatColor.RED + "Close");
                close.setItemMeta(closeMeta);
               
                auctionHouse = Bukkit.createInventory(null, 54, "Auction House");
                auctionHouse.setItem(49, refresh);
                auctionHouse.setItem(53, close);
                player.openInventory(auctionHouse);
            } else if (args.length == 1) {
                if(args[0].equalsIgnoreCase("sell")) {
                    sender.sendMessage("testing the ah sell");
                }
               
            }
            return true;
        }
    }
    
    Plugin YML:
    HTML:
    name: RandomAuctionHouse
    author: RandomHashTags
    main: auctionHouse.Main
    version: 1.0
    description: Best auction house plugin here!
    commands:
      auctionhouse:
        description: Opens the AuctionHouse
        usage: /ah
        aliases: [ah]
      auctionhouse sell:
        description: test
        usage: /ah sell <price>
        aliases: [ah_sell]
     
  7. Offline

    SuperSniper

    @RandomHashTags

    remove

    Code:
      auctionhouse sell:
        description: test
        usage: /ah sell <price>
        aliases: [ah_sell]
    
    from your plugin.yml
     
  8. Offline

    RandomHashTags

  9. Offline

    SuperSniper

  10. Offline

    RandomHashTags

    @SuperSniper I want it so when I do "/ah sell", It will remove the item from the player hand, which I have not added yet. But, when I do "/ah sell", It opens the AuctionHouse, where all the item will be when a player does "/ah"
     
  11. Offline

    RoboticPlayer

    A) Don't put an onDisable method if you aren't using it
    B) onEnable can be overridden
    C) As @Zombie_Striker said, Java Naming Conventions. Class names should not be "Main", use something that actually identifies the class. Also, change your package name to something unique to this plugin and your player
    D) Remove the "auctionhouse sell" command from your plugin.yml
    E) Let's take a look at what happens in your command:
    1) First, you check if the sender isn't a player. If it's not a player, you cancel it and send them a message. This is good.
    2) Then you check if the sender is a player. This is useless, as you already checked if it wasn't
    3) You then cast sender to player. This is safe because you already know it is a player
    4) You do all of your inventory stuff
    5) If the sender isn't a player (look at the brackets on your else if statement) and there is 1 argument, check it
    6) If the argument is "sell", run a test.
    So, that means you need to change it a bit.
    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    3. // Check if sender isn't a player
    4. // Cast sender to player
    5. // Check if args.length == 0
    6. // If it is, do your inventory stuff
    7. // If it isn't, check if args.length == 1
    8. // If it is, check if the name of the argument is "sell"
    9. // If args[0] is not "sell", add a check
    10. // If args.length isn't 0 and isn't 1, do something
    11. return true;
    12. }


    Edit: Ninja'd by @SuperSniper for part D :p
     
  12. Offline

    SuperSniper

    @RandomHashTags Ah, okay. You need to check the command name and the args.length:

    Code:
    package auctionHouse;
    
    import org.bukkit.Bukkit;
    import org.bukkit.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.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class auctionHouse implements CommandExecutor {
    
       public Inventory auctionHouse;
      
       @Override
       public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
          
           if(!(sender instanceof Player)) {
                sender.sendMessage("You must be a player to execute this command!");
               return true;
           }
     
           if(sender instanceof Player) {
                Player player = (Player) sender;
                   if(cmd.getName().equalsIgnoreCase("auctionhouse") && args.length == 0) {
                ItemStack refresh = new ItemStack(Material.CHEST, 1);
                ItemMeta refreshMeta = refresh.getItemMeta();
                refreshMeta.setDisplayName(ChatColor.GOLD + "" + ChatColor.BOLD + "Refresh Auctions");
                refresh.setItemMeta(refreshMeta);
              
                ItemStack close = new ItemStack(Material.STAINED_GLASS_PANE, 1, ((byte) 14));
                ItemMeta closeMeta = close.getItemMeta();
                closeMeta.setDisplayName(ChatColor.RED + "Close");
               close.setItemMeta(closeMeta);
              
                auctionHouse = Bukkit.createInventory(null, 54, "Auction House");
                auctionHouse.setItem(49, refresh);
                auctionHouse.setItem(53, close);
                player.openInventory(auctionHouse);
           } else if (args.length == 1) {
               if(args[0].equalsIgnoreCase("sell")) {
                    sender.sendMessage("testing the ah sell");
               }
            }  
           }
           return true;
       }
    }
    
     
  13. Offline

    RandomHashTags

    Okay, so I did both of @henderry2019 and @SuperSniper things, and it worked! The only thing that sent an error was the else if after the check if the sender was a player.
    Final Code:
    Code:
    package auctionHouse;
    
    import org.bukkit.Bukkit;
    import org.bukkit.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.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class auctionHouse implements CommandExecutor {
    
        public Inventory auctionHouse;
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
           
            if(!(sender instanceof Player)) {
                sender.sendMessage("You must be a player to execute this command!");
                return true;
            }
            if(cmd.getName().equalsIgnoreCase("auctionhouse") && args.length == 0) {
                Player player = (Player) sender;
               
                ItemStack refresh = new ItemStack(Material.CHEST, 1);
                ItemMeta refreshMeta = refresh.getItemMeta();
                refreshMeta.setDisplayName(ChatColor.GOLD + "" + ChatColor.BOLD + "Refresh Auctions");
                refresh.setItemMeta(refreshMeta);
               
                ItemStack close = new ItemStack(Material.STAINED_GLASS_PANE, 1, ((byte) 14));
                ItemMeta closeMeta = close.getItemMeta();
                closeMeta.setDisplayName(ChatColor.RED + "Close");
                close.setItemMeta(closeMeta);
               
                auctionHouse = Bukkit.createInventory(null, 54, "Auction House");
                auctionHouse.setItem(49, refresh);
                auctionHouse.setItem(53, close);
                player.openInventory(auctionHouse);
                return true;
            }
               
                if(args[0].equalsIgnoreCase("sell")) {
                    sender.sendMessage("testing the ah sell");
                }
            return true;
        }
    }
    
     
  14. Offline

    RoboticPlayer

    First part is wrong. If an onCommand method is only handling one command, you don't need to check the command. It's good practice, but there is no requirement to do so
    Second part the OP is already doing.
    Also, @RandomHashTags the Bukkit API uses the Java language, not JavaScript. Your [ code ] //code [ /code ] brackets are set to JavaScript.

    Not quite. If you choose to check the command, do that first (you can check the sender before that), make sure that the only thing within the check is checking the command
    Code:
    if(cmd.getName().equalsIgnoreCase("auction")/* Nothing else here! */) {
        if (args.length == 0) {
            // Inventory stuff
        } else if(args.length == 1) {
            if (args[0].equalsIgnoreCase("sell") {
                // First argument is "sell", handle it
            } else {
                // There is 1 argument, but it isn't "sell"
            }
        } else {
            // There are more than 1 arguments
        }
    }
     
    Last edited by a moderator: Oct 10, 2015
  15. Offline

    RandomHashTags

  16. Offline

    RoboticPlayer

  17. Offline

    RandomHashTags

Thread Status:
Not open for further replies.

Share This Page