1.15.2 Running a command with vanilla command (@p)

Discussion in 'Spigot Plugin Development' started by SkyyDaork, Feb 7, 2020.

Thread Status:
Not open for further replies.
  1. How can I setup this command to work with "/startrun @p" (used in command block)

    Code (Java):
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if (cmd.getName().equalsIgnoreCase("startrun")) {
                Player p = Bukkit.getPlayer(sender.getName());
                ItemStack bstand = new ItemStack(Material.BREWING_STAND, 1);
                ItemMeta itemMeta = bstand.getItemMeta();
                itemMeta.setDisplayName("Tanker-Ton");
                itemMeta.setLore(Arrays.asList(new String[] { "Big Boi" }));
                bstand.setItemMeta(itemMeta);
                PlayerInventory inventory = p.getInventory();
                inventory.addItem(new ItemStack(bstand));
            }
    return false;
    }
     
  2. Oke here we go,

    As first, if you execute this command in a commandblock. the sender will be a commandblock. So this.
    Code (Java):
    Player p = Bukkit.getPlayer(sender.getName());
    will always return null, As a command block is no player.

    Second, if you do /startrun @p in a commandblock it will execute the command with the name of the most nearby player at the spot of @p. This means that the command will be /startrun <player-name>. So if you want to get that player you have to check if the first argument is set and if thats a player.

    And as last, why do you want to use commandblocks?
     
  3. 1) I was originally using to test commands to see if work normally but blanked to check for @p cause instead being used in command block
    2) so how would I make it know to give the @p user the item?
    3) Minigame
     
Thread Status:
Not open for further replies.