How to write a consumable plugin

Discussion in 'Spigot Plugin Development' started by xiao_wen, Aug 12, 2019.

  1. Code (Java):
    @SuppressWarnings("deprecation")
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e)
    {
        Player p = e.getPlayer();
        if((e.getAction() == Action.RIGHT_CLICK_AIR) || ((e.getAction() == Action.RIGHT_CLICK_BLOCK)) && p.getItemInHand().equals(paper10))
        {
            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "eco give " + p.getName() + " 10");
            }
    }
    Paper10 is the item I made in the plugin.
    I want the player to use the right mouse button and consume a paper10.

    Eclipse IDE
    Version: 1.14.3
    API: spigot-api-1.14.3-R0.1-SNAPSHOT-shaded
     
  2. It looks like you haven't been coding for that long.

    Firstly you want to check if player has anything in hand with p.getInventory().getItemInMainHand();
    Then you can get the items display name or whatever you want to check it with and do the checks.

    But I suggest you watch some basic tutorials on spigot plugin coding.
     
  3. p.getInventory().getItemInMainHand();
    How to write the next line?
     
  4. How to put
    Code (Java):
    Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "eco give " + p.getName() + " 10");
    Modified to use vault to give money
    Instead of the console eco command
     
  5. Using ItemStack#equals is not the right way to do it. ItemStack is merely a wrapper class. As a beginner I suggest you should check for a lore line. NBT is a bit too advanced for newbies. Regarding vault, I would read up on how to setup a maven project (IntelliJ with Minecraft Development is useful for it.) Afterwards you can just add the VaultAPI as a dependency.
     
  6. How to modify?
     
  7. I would suggest you also use #isSimilar to check for the item since it does not consider stack size.
     
  8. I also highly recommend not suppressing problems, figure out what to do to get around the suppression.
     
    • Agree Agree x 1
  9. Such other items can also receive money.
    And or automatically shut down the server.
     
    #9 xiao_wen, Aug 13, 2019
    Last edited: Aug 13, 2019
  10. #isSimilar is the exact same with #equals. The only difference is, #equals also compares the amount of both itemstacks.
     
    • Like Like x 1
  11. How to make more than 2 items can also become consumables.

    The problem now is that it can only be used when there is one item left..
     
  12. Been posted a few times. You use #isSimilar not #equals for the itemstack.