Solved Ignores a command

Discussion in 'Plugin Development' started by NimsTail, Jul 5, 2021.

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

    NimsTail

    Hello everyone!

    I have started learning Java just a few weeks ago and would love to try making something my own.
    I have an issue.

    There must be 2 commands but works only the /info one. But the code does not have any issues and the console does not give any error when sending the /sold command. Seems like the server entirely ignores it.

    I am trying to make something similar to an auction when you are able to provide items for a price.

    Will be grateful for any help!



    The code:
    Code:
    public class Info implements CommandExecutor {
    
    public static Inventory inventory = Bukkit.createInventory(null, 9, "Custom Inventory") ;
    // Selling
    public boolean onCommand(CommandSender sender, Command cmd, String label, Integer[] args) {
    
    if (sender instanceof Player) {
    Player player = (Player) sender; if (cmd.getName().equals("sold")) {
    int price = args[0];
    Player hand = (Player) player.getItemInHand();
    ItemStack item = new ItemStack((ItemStack) hand);
    ItemMeta meta = ((ItemStack) hand).getItemMeta();
    ArrayList<String> lore = new ArrayList<String>();
    lore.add(price + " diamonds");
    if (price == 0) {
    player.sendMessage(ChatColor.RED + "The price cannot equal to zero!");
    } else {
    inventory.setItem(1, new ItemStack((ItemStack) hand));
    }
    
    
    } else {
    System.out.println("You are not a player!");
    }
    
    
    } return false;
    }
    
    // GUI Command
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
    if (sender instanceof Player) {
    Player player = (Player) sender;
    if (cmd.getName().equals("info")) {
    player.openInventory(inventory);
    }
    
    } else {
    System.out.println("You are not a player!");
    }
    
    return false;
    }
    }
     
    Last edited by a moderator: Jul 5, 2021
  2. Online

    timtower Administrator Administrator Moderator

    @NimsTail First one is not a valid onCommand.
    Arguments are fixed to string
     
  3. Offline

    NimsTail

    @timtower
    Thank you for your reply. I was trying to create an Integer for the price. I also tried using
    int NewPrice = Integer.parseInt(price);
    But it`s not working.
    Then used regex to exclude everything except of numbers, but no response too. I think I made a mistake somewhere else.
    Code:
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (sender instanceof Player) {
                Player player = (Player) sender;
    
                if (cmd.getName().equals("info")) {
                    player.openInventory(inventory);}
    
                if (cmd.getName().equals("sold")) {
                    if (!args[0].matches("-?\\d+")) {
                        String price = args[0];
                        Player hand = (Player) player.getItemInHand();
                        ItemStack item = new ItemStack((ItemStack) hand);
                        ItemMeta meta = ((ItemStack) hand).getItemMeta();
    
                        ArrayList<String> lore = new ArrayList<String>();
    
                        lore.add(price + " diamonds");
    
    
                        if (price.equals("0")) {
                            player.sendMessage(ChatColor.RED + "The price cannot equal to zero!");
    
                        } else {
                            inventory.setItem(1, new ItemStack((ItemStack) hand));
                    }
                    } else {
                        player.sendMessage("Invalid argument!");
                    }
    
                    } else {
                        System.out.println("You are not a player!");
                    }
    
    
                }
    
    
            return false;
    
        }
     
  4. Online

    timtower Administrator Administrator Moderator

    @NimsTail Did you register the class as commandexecutor?
     
  5. Offline

    NimsTail

    @timtower I think I did.
    Do you mean this part?
    Code:
    public class Info implements CommandExecutor {
     
  6. Online

    timtower Administrator Administrator Moderator

    @NimsTail No, I am talking about the setExecutor part
     
  7. Offline

    NimsTail

    @timtower Oh, you mean in main. Yes, I did.
    Code:
        @Override
        public void onEnable() {
            System.out.println("Working");
            getCommand("info").setExecutor(new Info());
    
        }
     
  8. Online

    timtower Administrator Administrator Moderator

    @NimsTail Then how is it not working? Any errors?
     
  9. Offline

    NimsTail

    @timtower I do not know why is not it working. In the code, there are just a few yellow flags. But I do not understand the first and the last ones.
    In main:
    upload_2021-7-5_15-14-27.png

    And some in commandexecutor class:
    upload_2021-7-5_15-15-42.png
    upload_2021-7-5_15-15-57.png upload_2021-7-5_15-16-4.png
    upload_2021-7-5_15-16-17.png
     

    Attached Files:

  10. Online

    timtower Administrator Administrator Moderator

    @NimsTail Means that you are not using certain things.
    And then you add printlines in the onCommand
     
  11. Offline

    Shqep

    @NimsTail
    About the "yellow flags" or "warnings":
    Warnings (open)

    Method invocation "setExecutor" may produce NullPointerException.
    This is the IDE telling you that the method getCommand(name) returns a @Nullable value. You get null when you haven't specified that "name" value in plugin.yml

    getItemInHand() is deprecated because in 1.9, dual wield was added. If you're developing against a later version API, you are advised to use PlayerInventory#getItemInMainHand() and PlayerInventory#getItemInOffHand().

    Contents of collection 'lore' are updated, but never queried
    This means that you update the values of this collection named "lore". But you never use the "lore" variable after updating the values.

    Explicit type argument String can be replaced with <>
    I don't know when it was added, but from Java 1.8, generics can be inferred implicitly.
    As such:
    PHP:
    // You already specified it's a list of "String", so the ArrayList<> generic part is not necessary anymore.
    final List<String> list = new ArrayList<>();


    About your problems:
    ...
    Are you sure you registered the command "sold" yet?

    And I don't understand what you're trying to do with /sold, can you elaborate on this?
    As I'm seeing, you're matching a String against a regex?
    If you want the number value, Integer.parseInt() does you fine. Just wrap it with a try-catch, as it throws a NumberFormatException if the value was not a number.

    The spoilers are there to prevent this becoming a huge text wall.
     
  12. Offline

    NimsTail

    Thank you, I solved it.
     
Thread Status:
Not open for further replies.

Share This Page