Create a command with one and only item can execute.

Discussion in 'Spigot Plugin Development' started by _X3V, Jun 14, 2019 at 9:51 PM.

  1. Sorry for my bad english. but, I want to

    Code (Java):
    if(executor.getItemInHand().getType() == Material.getMaterial(<itemidwithmetaonlyforspecificcommand>)){
    if u know what i mean, if not,
    I want to make an item that can be only used for the commands example is a smite stick. I tried
    Code (Java):
    Material.getMaterial(smiteitem)
    smiteitem is a stick but commands can be executed using normal sticks and that should not happen.
    i want to execute the /smite command using only a stick but not normal sticks. omg this is hard to explain.
    someone please help.
     
  2. You can check the item's name and lore, but that may be unsafe if some players have renaming capabilities.
    If that's the case, I recommend using NBT tags, which is the safest option but more advanced.
     
  3. Check what the item name is, if it equals to whatever you want to call the smitestick, then execute the command.

    Verstuurd vanaf mijn SM-A530F met Tapatalk
     
  4. Thx for ur reply. I'm a fan of ur plugin RankGrant+
    Anyways, I'm not using any api rn and doing all things in one class. any recommendations on how can I improve and suggestions on NBT APIs?
     
  5. add a display name and lore to the item and set this in a yaml file
    Code (YAML):
    Items:
        DIAMOND
    :
            Command
    : "help"
            name
    : "Command Item"
            Lore
    :
           - "Command Item dose /help"
     
  6. Code (Java):
       @EventHandler
        public void TestClick(PlayerInteractEvent event)
        {
            Player player = event.getPlayer();





            if(event.getAction().equals(Action.RIGHT_CLICK_AIR))
            {
                if(player.getItemInHand().getType().equals(Material.matchMaterial("DIAMOND")))
                {

                    ItemStack itemStack = player.getItemInHand();

                    ItemMeta itemMeta = itemStack.getItemMeta();

                    if(itemStack.hasItemMeta())
                    {

                        if(itemMeta.getDisplayName().equalsIgnoreCase("YOUR ITEM DISPLAY NAME"))
                        {
                            player.performCommand("help");
                        }

                    }
                }
            }
        }
    Something like this but add your config to it tho
     
  7. I do not recommend doing everything one class. This just makes it harder to read and go through. There are 0 benefits. Also using an API for something that can be coded simply and quickly is pretty dumb. Why use somebodies else code when you can easily make your own? I would search for a tutorial on NBT and NMS




    I am quite sure that this is NOT the best way to do this.

    Code (Java):

    if(event.getAction().equals(Action.RIGHT_CLICK_AIR)) {
        if (player.getItemInHand().equals(itemStack) && player.getItemInHand().getItemMeta().getDisplayName().equals("Display name")) {
                  // EXECUTE CODE
        }
    }
     
     
  8. I just giving him/she an idea of how to do it.
     
  9. Ah I see sorry.
     
  10. It's cool, I love making pluigins
     

Share This Page