how to double items?

Discussion in 'Plugin Development' started by Mr_maderator_UY, Mar 11, 2022.

  1. Offline

    Mr_maderator_UY

    Hello! I want to create item which can double block after click
    For example I clicked on grass and after that this block is removed and spawned two block of grass
    I don't know how to create this. I read documentation in hub.spigotmc.org about
    PlayerInteractEvent or BlockBreakEvent, but I didn't find any helpful infomation.
    This is my "logic" of plugin, but of course that is false. May you ask me in what side I need go?
    here is my code
     
  2. Offline

    Strahan

    I don't follow your intent. You said click grass and after block is removed and spawned two blocks of grass. So you mean if they break grass, it drops two instead of one? If so, you want to listen to BlockBreakEvent. Check if the block matches your filter (assuming you don't want every single block to drop 2x, otherwise this is unnecessary) and adjust the drops. There is a BlockBreakEvent#setDrops() you can use for that.
     
  3. Offline

    Mr_maderator_UY

    Here we are!
    I find how to fix it. here is code. But! in 38 line I have error
    How can i fix it?
    EDIT: I changed blockbroken into event, but it didn't help
     
  4. Online

    CraftCreeper6

    @Mr_maderator_UY
    The error is very self explanatory, blockBroken is null.

    You don't need to turn it into an event, that doesn't make a shred of sense.

    The issue is that if the player clicks somewhere that isn't a block, then the block will be null.

    Add a check to make sure that blockBroken is not null, also, why not use BlockBreakEvent?
     
  5. Offline

    Mr_maderator_UY

    I don't use BreakBlockEvent, cuz in 41 you can see event.getItem. We can't use this method in BreakBlockEvent
    @CraftCreeper6
     
  6. Online

    CraftCreeper6

    You can use BlockBreakEvent#getPlayer()#getInventory()#getHeldItemSlot() alongside a Player#getInventory()#getItem(slot) call.

    In any case, did you fix your original issue?
     
  7. Offline

    Mr_maderator_UY

    here is new code
    Well, what about getItem.
    If player have one item in slot #1 and I'll add this item in 1 slot. And there are will change. Does getItem have a const for free slot?
     
  8. Online

    CraftCreeper6

    @Mr_maderator_UY
    getItem takes a slot number, so just use getPlayer()#getInventory()#getHeldItemSlot() to get the slot of the currently held item.
     
  9. Offline

    Mr_maderator_UY

    It doesn`t work. after getHeldItemSlot() does not work equals
    @CraftCreeper6
     
  10. Online

    CraftCreeper6

    @Mr_maderator_UY
    Actually you can probably just use player#getItemInMainHand()
     
  11. Offline

    Mr_maderator_UY

  12. Online

    CraftCreeper6

  13. Offline

    Mr_maderator_UY

    it doesn`t work
    Can you check a code? Maybe I have error?
    @CraftCreeper6
     
  14. Online

    CraftCreeper6

    @Mr_maderator_UY
    I don't understand your trouble, this should work just fine.
    Code:
        ItemStack itemStack = e#getPlayer()#getInventory()#getItem(e#getPlayer()#getInventory()#getHeldItemSlot());
        if (itemStack#hasItemMeta())
        {
            if (itemStack#getItemMeta()#hasDisplayName())
            {
                String itemName = itemStack#getItemMeta()#getDisplayName();
                System#out#println("The user just clicked with a " + itemName + "!");
            }
        }
    
     
  15. Offline

    Mr_maderator_UY

    Well, I need that after clicking or breaking block, this block dropped two items of this block
    For example: I broke a block of groung and this block dropped two blocks of ground
    @CraftCreeper6
     
  16. Online

    CraftCreeper6

    @Mr_maderator_UY
    This isn't a difficult thing to do, when you post your code please use the CODE tags provided, so I don't have to keep following links to screenshots.

    The code I provided will print to the console if the item has a display name, simply check if the display name is the same as your base item (and some other item meta details too) then drop the block like you're doing.

    I don't know if you can directly compare ItemMeta's, I don't know that they contain the same information across instances, which could be the cause of your problem.
     
  17. Offline

    Mr_maderator_UY

    can i give u source code in github?
     
  18. Online

    CraftCreeper6

  19. Offline

    Mr_maderator_UY

  20. Online

    CraftCreeper6

    @Mr_maderator_UY
    Line 27 doesn't make any sense at all.
    Code:
    if (targetplayer.getInventory().equals(manager.pickAxe.getItemMeta())) {
    You are comparing the players inventory to some ItemMeta? This will never return true.

    You need to compare the players item in hand to the ItemMeta.
     
  21. Offline

    Mr_maderator_UY

  22. Online

    CraftCreeper6

    @Mr_maderator_UY
    Read this post again
     
  23. Offline

    Mr_maderator_UY

    I don't understand))
    I wanna to explain my logic
    ItemStack using with block and materials, so we need to use getItemMeta to copy name of a drop
    @CraftCreeper6
     
  24. Online

    CraftCreeper6

    @Mr_maderator_UY
    Is this what you want?

    1. BlockBreakEvent
    2. Get the player that broke the block
    3. Get the item that the player is holding
    4. If the item is the same as manager.pickAxe, then drop a second item
     
  25. Offline

    Mr_maderator_UY

  26. Online

    CraftCreeper6

    @Mr_maderator_UY
    Well then re-read the post I sent.

    Check the display name and some other ItemMeta related information, if the information matches manager.pickAxe, then drop an extra item.
     
  27. Offline

    Mr_maderator_UY

    I understand you, I will check later. Thanks.
     
  28. Offline

    Mr_maderator_UY

    I tested your code. I added a name of pickaxe in has DisplayName and getDisplayName and there was an error. I didn't understand where I can check display name. I saw that you told me thent i need to check, but error tell me that I can not add to ItemMeta String name

    Code:
     ItemStack itemStack = event.getPlayer().getInventory().getItem(event.getPlayer().getInventory().getHeldItemSlot());
            if (itemStack.hasItemMeta())
            {
                if (itemStack.getItemMeta().hasDisplayName("pickaxe of doubling"))
                {
                    String pickaxe_of_doubling = itemStack.getItemMeta().getDisplayName("pickaxe of doubling");
    
                }
            }
        }
     
  29. Offline

    timtower Administrator Administrator Moderator

    @Mr_maderator_UY You never check if itemStack is null
    hasDisplayName does not take an argument I believe.
     
  30. Offline

    Mr_maderator_UY

    Code:
    ItemStack itemStack = event.getPlayer().getInventory().getItem(event.getPlayer().getInventory().getHeldItemSlot());
            if (itemStack.hasItemMeta() && itemStack != null)
            {
                if (itemStack.getItemMeta().hasDisplayName("pickaxe of doubling"))
                {
                    String pickaxe_of_doubling = itemStack.getItemMeta().getDisplayName("pickaxe of doubling");
    }}}
    So, I need add !=null
     

Share This Page