how to double items?

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

  1. Offline

    CraftCreeper6

    @Mr_maderator_UY
    No that'll still break because you're checking it after you check if it has item meta.
    You need another if statement prior to checking if the itemstack has any ItemMeta.
    As tim said, hasDisplayName doesn't take any arguments, you need to use itemStack#getItemMeta()#getDisplayName() and compare the output with something else, in your case it looks to be "pickaxe of doubling". That does not mean remove the hasDisplayName check, just remove the argument.
     
  2. Offline

    Mr_maderator_UY

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

    @CraftCreeper6 , here is it?
     
  3. Offline

    CraftCreeper6

    @Mr_maderator_UY
    For my own sake, here's it formatted:
    Code:
        ItemStack itemStack = event#getPlayer()#getInventory()#getItem(event#getPlayer()#getInventory()#getHeldItemSlot());
        if (itemStack != null) // I swapped this with the line below because we need to check if the ItemStack exists before we try to check if it has ItemMeta#
        {
            if (itemStack#hasItemMeta()) // here we check if the ItemStack has any ItemMeta
            {
                if (itemStack#getItemMeta()#hasDisplayName()) // see here, I removed the "pickaxe of doubling" argument because it's not needed
                {
                    String pickaxe_of_doubling = itemStack#getItemMeta()#getDisplayName(); // we then fetch the name of the item
                    // do something with your string, check if it's "pickaxe of doubling" or whatever and then drop the block
                }
            }
        }
    I've moved some code around and added some comments so that you can better understand this. Obviously I've used # instead of . so at least take this chance to read through and understand what's happening in the code.
     
  4. Offline

    Mr_maderator_UY

    And can i check this item using this code?
    Code:
            if (stack.getType().name().contains("pickaxe of doubling")) return;
     
  5. Offline

    CraftCreeper6

    @Mr_maderator_UY
    No, that would get the name of the Material. You want the display name.
     
  6. Offline

    Mr_maderator_UY

    Well, instead name we need to put haveDisplayName and remove getType?
     
  7. Offline

    CraftCreeper6

  8. Offline

    Mr_maderator_UY

    Code:
    ItemStack itemStack = event#getPlayer()#getInventory()#getItem(event#getPlayer()#getInventory()#getHeldItemSlot());
        if (itemStack != null) // I swapped this with the line below because we need to check if the ItemStack exists before we try to check if it has ItemMeta#
        {
            if (itemStack#hasItemMeta()) // here we check if the ItemStack has any ItemMeta
            {
                if (itemStack#getItemMeta()#hasDisplayName()) // see here, I removed the "pickaxe of doubling" argument because it's not needed
                {
                    String pickaxe_of_doubling = itemStack#getItemMeta()#getDisplayName(); // we then fetch the name of the item
                    if(itemStack.getDisplayName("pickaxe of doubling"){
                    ItemStack item = new ItemStack(ititemStack, 2);
                    blockBroken.getWorld().dropItemNaturally(blockBroken.getLocation(), item);
                }
    That is true?
     
  9. Offline

    CraftCreeper6

    @Mr_maderator_UY
    No, getDisplayName doesn't take an argument, just like hasDisplayName. You need to use .equals() to compare itemStack#getItemMeta()#getDisplayName with "pickaxe of doubling"

    Also, please use the reply or tahg button so I get a notification when you reply.
     
  10. Offline

    Mr_maderator_UY

    itemStack#getItemMeta()#getDisplayName.equals("pickaxe of doubling")
    True?
     
  11. Offline

    CraftCreeper6

    @Mr_maderator_UY
    Without the hashtags, yes. Your previous code too, you need to replace all # with .
     
    Mr_maderator_UY likes this.
  12. Offline

    Mr_maderator_UY

    Why?
    IntelliJ idea thinks that is error with #
     
  13. Online

    timtower Administrator Administrator Moderator

    @Mr_maderator_UY Because they are indicators for types, it is not actual code.
     
    Mr_maderator_UY likes this.
  14. Offline

    Mr_maderator_UY

    Thanks for everything, I'll check later!

    I tested and i have the last error
    Code:
      ItemStack item = new ItemStack(itemStack, 2);
    
    Trouble in itemStack
    how i can fix it?

    @CraftCreeper6 , did you know?
     
    Last edited: Mar 15, 2022
  15. Offline

    CraftCreeper6

  16. Offline

    Mr_maderator_UY

    @CraftCreeper6, But i need all blocks. I need to create swich to check every block?
     
  17. Offline

    CraftCreeper6

  18. Offline

    Mr_maderator_UY

    @CraftCreeper6
    It doesn't work. In my mind i need add
    Code:
                    if (event.getItem().getItemMeta().equals(manager.wand.getItemMeta())){
    
    how in stickevent (link on github). In there code i used playerInteractEvent, but in code of pickaxe i used blockBreackEvent
    How i remember you told me that we check a name of pickaxe in this code
    Code:
    if(itemStack.getItemMeta().getDisplayName().equals("Pickaxe of doubling")){
    
    In manager file we set this name.
    In game this code didn't work
    EDIT: Will it work if i will check item lore?
     
    Last edited: Mar 24, 2022
  19. Offline

    CraftCreeper6

    @Mr_maderator_UY
    You're using a colour code in your manager file, you need to use the same colour code in your check too.

    Alternatively you can use
    manager#pickAxe#getItemMeta()#getDisplayName()

    The filename for your class 'events' doesn't match.

    For your stick you just need to use the same comparison, compare the display name and not the ItemMeta.
     
    Mr_maderator_UY likes this.
  20. Offline

    Mr_maderator_UY

    @CraftCreeper6 , when i added color code - pickaxe are doubingXD
    in next code, there is an error, how I saw, there is was about boolean
     
  21. Offline

    CraftCreeper6

  22. Offline

    Mr_maderator_UY

    @CraftCreeper6,
    Code:
    manager.pickAxe.getItemMeta().getDisplayName()
    
    here is screen
    also i checked name with color code and after breaking a block a block is droping and with it dropped two diamond pickaxe with no enchants. (Standart pickaxe)
     
  23. Offline

    CraftCreeper6

    @Mr_maderator_UY
    That's because you're using a String as a condition, which doesn't make any sense.

    I don't think you quite understand what you're trying to do here, so I'll make you an example. Do not copy this code since it has no bearing on what you want at all, it is simply a demonstration, and won't compile for many reasons.

    Firstly, check this link out because it will get your introduced to if statements. Which are a fundamental part of Java, so learn them.

    ItemMeta.java
    Code:
    // This is what a dumbed down ItemMeta class would look like
    public class ItemMeta
    {
     
        private String displayName;
     
        public String getDisplayName()
        {
           return displayName;
        }
     
    }
    
    This is a dumbed down version of the ItemMeta class, you can see that it contains a string for the displayName and a getter that returns the displayName.

    ItemStack.java
    Code:
    // This is what a dumbed down ItemStack class would look like
    public class ItemStack
    {
     
        private ItemMeta itemMeta;
     
        public boolean hasItemMeta()
        {
           return itemMeta != null;
        }
     
        public ItemMeta getItemMeta()
        {
           return itemMeta;
        }
     
    }
    This is a dumbed down version of the ItemStack class, you can see that it contains a reference to some ItemMeta, a getter which returns the itemMeta, and a check that returns true if the itemMeta is not null.

    YourEvent.java
    Code:
    // This is your event class
    public class YourEvent implements Listener
    {
        @EventHandler
        public void onYourEvent(YourEvent event)
        {
           ItemStack eventItem = event.getItemUsedToBreakBlock();
     
           if (eventItem.hasItemMeta())
           {
               if (eventItem.getItemMeta().getDisplayName().equals("My item name"))
               {
                  // do the doubling or whatever
               }
           }
        }
    }
    This is your event class, I'll go through it line by line.

    Code:
    public class YourEvent implements Listener
    Here we are declaring your class and implementing the Listener, it looks like you've already done this so not much else to say.

    Code:
    @EventHandler
    public void onYourEvent(YourEvent event)
    This is your event, in your case it will be BlockBreakEvent, it will be called whenever the event happens.

    Code:
    ItemStack eventItem = event.getItemUsedToBreakBlock();
    This is the ItemStack that was used to break the block, this is not the real methods you should be using, you already have the code for that anyway.

    Code:
    if (eventItem.hasItemMeta())
    This line checks to make sure that the ItemStack used to break the block has some ItemMeta, as mentioned before, this will only return true if the itemMeta variable within ItemStack is not null. If it is null then the if statement will not be fulfilled and the event is over.

    Code:
    if (eventItem.getItemMeta().getDisplayName().equals("My item name"))
    This line should be fairly self explanatory but lets break it down anyway.
    As we know from the link that I sent, an if statement takes a condition, if the condition is true then it will execute the code within.
    What's our condition?
    Code:
    eventItem.getItemMeta().getDisplayName().equals("My item name")
    That's a condition because we have a method that returns a boolean value, the method is 'equals' if it wasn't obvious.
    When will it return true?
    Well think about it, go through it in your head.

    Code:
    eventItem.getItemMeta()
    First we're getting the ItemMeta, which we already established before is stored inside of the ItemStack.

    Code:
    eventItem.getItemMeta().getDisplayName()
    Then we're getting it's displayName, which we already established is a variable within ItemMeta that stores the display name of the item.

    Then what? We have a display name now so what do we want with it?
    Well, we want to check that it is equal to something else of course.
    Code:
    eventItem.getItemMeta().getDisplayName().equals("My item name")
    So to recap, we want to get the ItemMeta from the ItemStack, we then need to get the display name from the ItemMeta, and then we need to check that the display name is equal to whatever we want.

    If it's equal, we can execute the code within the if, if it's not equal then don't bother with it.
     
  24. Offline

    Mr_maderator_UY

    Finaly!
    I found error...
    This is a code without errors!
    Code:
    public class eventpickaxe implements Listener {
    
    
        @EventHandler
        public static void onRightClicktwo(BlockBreakEvent event) {
    
                Block blockBroken = event.getBlock();
                ItemStack itemStack = event.getPlayer().getInventory().getItem(event.getPlayer().getInventory().getHeldItemSlot());
    
                    if (itemStack != null) 
                    {
                        if (itemStack.hasItemMeta()) 
                        {
                            if (itemStack.getItemMeta().hasDisplayName())
                            {
                               
                                if (itemStack.getItemMeta().getDisplayName().equals("§bPickaxe of doubling")) {
                                    ItemStack item = new ItemStack(blockBroken.getType(), 2);
                                    blockBroken.getWorld().dropItemNaturally(blockBroken.getLocation(), item);
    
                                }
    
                            }
                        }
                    }
                }
    Where we double items, we need to use blockBrocken and add getType of block, how in my code. We need to create a variable blockBrocken and add .getBlock that get a block.
    Also... after breaking a grass we get 3 blocks: 2 blocks of grass and a block of dirt. In my mind we need to check this special block using switch case.
    @CraftCreeper6 , @timtower - thanks a lot for helping!
     
  25. Offline

    CraftCreeper6

    The reason for this is that your code is effectively silktouch, if instead you wanted to only receive the dirt from the block you can use event.getBlock().getDrops().
     
  26. Offline

    Mr_maderator_UY

    @CraftCreeper6, am I can add in variable blockBroken .getDrops()?ED.
    Code:
            Block blockBroken = (Block) event.getBlock().getDrops();
    
     
    Last edited: Mar 29, 2022
  27. Offline

    CraftCreeper6

    @Mr_maderator_UY
    Not really, you'll have to change the datatype from Block to List<ItemStack> if you wanted to do that.

    I would just keep your code as it is, and instead of having:
    Code:
    ItemStack item = new ItemStack(blockBroken.getType(), 2);
    Have a for loop that loops getDrops()
    Code:
    for (ItemStack item : blockBroken.getDrops())
    {
        // make a new ItemStack like you did before but instead of 2 have item.amount * 2
        // drop the ItemStack like you already do with world.dropItemNaturally()
    }
    Keep in mind you can also pass the tool into getDrops() if you want that to count towards it. Like so:
    block.getDrops(myToolItemStack)
     
  28. Offline

    Mr_maderator_UY

    @CraftCreeper6, i think to create switch case like this
    Code:
     case grass:
                    if (manager.pickAxe)
                        return new ItemStack(Material.GRASS, 2);
                    else
                        return new ItemStack(Material.DIRT, 1);
     
  29. Offline

    CraftCreeper6

    @Mr_maderator_UY
    Yeah that works but you'll have to do it for every single block that is affected by silk touch. Try mining a block of diamond ore for example, you'll get 2 blocks of diamond ore, not the diamonds themselves.
     
  30. Offline

    Mr_maderator_UY

    @CraftCreeper6, how i know...
    There aren't a lot of blocks, so I think I'll use switch case, because I'm not a friend with for or while
    :)
     

Share This Page