Weird bug / problem with scheduled item dropping...

Discussion in 'Plugin Development' started by Regenwurm97, Mar 20, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Regenwurm97

    Hey there!

    While coding on one of my plugins, I registered a bug by which players could clone itemstacks etc. and of course I wanted to remove it...
    The problem is, i don't even know what's causing the bug.

    Let me give you a simple Test EventHandler you can implement into one of your listener classes to test it out yourselves:

    Code:
    @EventHandler
        private void onPlayerDropTestItem(PlayerDropItemEvent e) {
           
            final int dropStackSize = e.getItemDrop().getItemStack().getAmount();
            final Item item = e.getItemDrop();
           
            Player player = e.getPlayer();
            Material dropType = item.getItemStack().getType();
           
            if(dropType == Material.STONE || dropType == Material.GRASS || dropType == Material.DIRT || dropType == Material.COBBLESTONE) {
               
                Bukkit.getScheduler().scheduleSyncDelayedTask(WurmBarbeque.getPlugin(), new Runnable() {
     
                    @Override
                    public void run() {
                       
                        Item product = item.getWorld().dropItem(item.getLocation(), item.getItemStack().clone()); //Das fertige Item wird gedroppt
                        product.setVelocity(new Vector(0, 0.3, 0));
                       
                        item.remove();
                       
                    }
                   
                }, 20L);
               
            }
           
        }
    So as you can see, there are simply 4 block which, when you drop them, will be removed and replaced by the same one, after 20 seconds.

    Although, after dropping several items in a row (or simply spam your "q" button), you will notice, that the item stacks are actually increasing in their amount.

    I actually don't know what is wrong with this code...I already double checked the amount of exisiting threads and all threads, that are once opened, are also closed. I know that threads can cause some problems...but is this still normal?

    Hopefully one of you guys knows an awnser to this :)
     
  2. Offline

    BeefyPlays

    Question.
    Why do you need this?
    Anyways you can just make a int and make it countdown in seconds. Then check for the item. Then clear the item if it is the item your looking for. Then spawn a item at the place it was dropped.
     
  3. Offline

    Regenwurm97

    I need it for a plugin where items are replaced by other items when you drop them onto a specific block.
    You can e.g. drop 9 goldnuggets onto a plate and they will be removed after like 1 second and then be replaced by a gold bar

    Sry, but I dont really know what you mean by your solution...
    I might have forgotten about to tell that in my plugin, the item is given a pickup cooldown for 30 ticks, so you can't pick them up again...

    Maybe I should put the pickup delay some lines further up...

    Could anyone maybe test weather the same bug appears at his own server or not? :eek:

    @EDIT 2: Ok, now I understand nothing anymore...
    I think I'm pretty close to the bug...but I just don't get it

    Here's an EventHandler which works just fine. It simply drops apples instead of the original items, but still with the original drop size!

    Code:
    @EventHandler
        private void onPlayerDropTestItem(PlayerDropItemEvent e) {
           
            final int dropStackSize = e.getItemDrop().getItemStack().getAmount();
            final Item item = e.getItemDrop();
            final ItemStack stack = new ItemStack(Material.APPLE, dropStackSize);
           
            item.setPickupDelay(20 * 3);
           
            Material dropType = item.getItemStack().getType();
           
            if(dropType == Material.STONE || dropType == Material.GRASS || dropType == Material.DIRT || dropType == Material.COBBLESTONE || dropType == Material.BEDROCK) {
               
                Bukkit.getScheduler().scheduleSyncDelayedTask(WurmBarbeque.getPlugin(), new Runnable() {
     
                    @Override
                    public void run() {
                       
                        Item product = item.getWorld().dropItem(item.getLocation(), stack);
                        product.setVelocity(new Vector(0, 0.3, 0));
                       
                        item.remove();
                       
                    }
                   
                }, 20L);
               
            }
           
        }
    Now here's the exact same code, except that I changed the line

    to

    Code:
        @EventHandler
        private void onPlayerDropTestItem(PlayerDropItemEvent e) {
           
            final int dropStackSize = e.getItemDrop().getItemStack().getAmount();
            final Item item = e.getItemDrop();
            final ItemStack stack = new ItemStack(item.getItemStack().getType(), dropStackSize);
           
            item.setPickupDelay(20 * 3);
           
            Material dropType = item.getItemStack().getType();
           
            if(dropType == Material.STONE || dropType == Material.GRASS || dropType == Material.DIRT || dropType == Material.COBBLESTONE || dropType == Material.BEDROCK) {
               
                Bukkit.getScheduler().scheduleSyncDelayedTask(WurmBarbeque.getPlugin(), new Runnable() {
     
                    @Override
                    public void run() {
                       
                        Item product = item.getWorld().dropItem(item.getLocation(), stack);
                        product.setVelocity(new Vector(0, 0.3, 0));
                       
                        item.remove();
                       
                    }
                   
                }, 20L);
               
            }
           
        }
    The thing is, that I double checked the dropStackSize variable and it's always right. Although, when dropping some items and picking them up, they have multiplied for whatever reason!

    It's definately this line of code, especially the thing I insert into the required Material for an ItemStack.

    The last thing I can think off is that the JVM is just a little bit too slow and swaps the Materials when spamming the items, thus dropping more of the one type or so... :l
     
  4. Offline

    Regenwurm97

    bump - nobody got an idea? D:
     
Thread Status:
Not open for further replies.

Share This Page