[SOLVED]item despawn faster then normally

Discussion in 'Plugin Development' started by ceoepts, Jul 10, 2012.

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

    ceoepts

    Is there any way to make a item despawn faster then normally?
    I found ItemDespawnEvent but i dont know how i can use it to despawn items faster :S
    I started coding java yesterday so im a bit of a newbie.
    Sorry for my english.
     
  2. you can use your own loop when an item is dropped, and despawn if the despawn ttime is over
     
  3. Offline

    ceoepts

    Im sorry im a bit of a newbie in java and bukkit modding could you give me a exemple code and explain what i does :)
     
  4. Offline

    Father Of Time

    There should be, if you set the ticks lived it in theory should speed up the decay time; sadly just like the last post I made I am already aware that this has a bug in it currently and is not functional; you can view the bug report I made by going to bukkits bug tracker and searching for itemDespawn.

    In short the problem is that for some reason the items despawning is using a local variable in craftbukkit and not the age variable inside the CraftEntity class. Because of this even if you keep rolling the age back (or in your case forward) it simple does nothing and will still decay at 5000 ticks because that it is using a variable that we can't access server side.

    This is doable once this bug is fixed, but sadly it is not possible in the current 1.2.5 RB 4... :(

    That is without reflections and native code calls (which is what we did to get around this for the time being); however, even this has side effects... the server understands the despawn change, but the client doesn't; so the item will disappear server side when your code decides, but the client continues to use it's internal timer and decays at a different time... So in my instance the item would decay "vanish" even though in reality I had stopped the despawn, so I would have to log in and back out in order to see the item again...

    This whole section of craftbukkit is buggy as sh*t, I advise putting this on the backburner and following the bug report until it's resolved, otherwise you will give yourself alot of heartache for no reason. :(
     
  5. Sorry, I am not the guy who likes the write big code event handles for the comunuty, because I dont think users learn from this, I mostly only point out the direction, or some 1 liners
     
  6. Offline

    ceoepts

    Thx again! You own. But i think that Uberminecraft.com is using the item despawn or they did for like 2 weeks ago. I lost much diamonds on that :p Derp. And i think mcctf is or was using this too :p

    Wait now! I think i dont need this im not sure if i can make that only swords is dropped from a player :S i think i found it out from my other post!

    No now when i think at it this dont work :( If anyone knows how to make items despawn faster then normally post how here :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  7. What exacly is your goal ? Do you want to allow items to drop but despawn faster or do you want to prevent dropping of certain items ? The 2nd would be alot easier I belive...
     
  8. Offline

    ceoepts

    Ok if you can make this you are the best. I want to prevent dropping for every item except Swords :)
    Not block dropping for everyitem and add swords i want the enchant and the durability to stay :p
     
  9. Offline

    Father Of Time

    It's my pleasure, I am happy to assist. Just a word to the wise, using other large scale servers as a model of what can and can't be done isn't the best idea; many larger servers have private developers who manage the servers (like my server, we code everything internally), so often they have modified their craftbukkit in some way or another to introduce the necessary functionality, because they have the knowledge and manpower to keep modifying it as craftbukkit changes.

    I can't speak for the server you referenced above, but I know our server has an entire "common" library that does nothing but correct bugs in craftbukkit, and our other plugins are dependant on that project to perform their task that would otherwise be impossible with just bukkit. In the senario I am painting :
    Bukkit = Craftbukkit
    Common library = bukkit

    We are using our commons library as the stable api, and commons is what is constantly being modified to meet the changes of the craftbukkit native code.

    Simply put, just because you see other servers do it doesn't mean it's part of the vanilla server or bukkit, because often people modify small things here or there that are never publicly released.

    Oh snap, thats simple; use the PlayerDropItem event:

    http://jd.bukkit.org/apidocs/org/bukkit/event/player/PlayerDropItemEvent.html

    check to see if the item being dropped is a sword, if so allow it to drop, otherwise cancel the event.

    I should note that this will only keep the person from using Q to throw it to the world, I think they will still lose the item:
    1) at the time of death
    2) if they drag it out of their inventory window

    Is this enough, or do you need to stop a different type of dropping?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  10. Offline

    ceoepts

    Oh sorry i mean on death :p
     
  11. Offline

    Father Of Time

    in the PlayerDeathEvent there is a function that can be called, something like "getItemsDropped()" (I'm going from memory, don't have the time to research atm) What you would do is iterate the collection and look at each item, if the item is not a sword then remove it from the list (remember, use iterators and not loops, you can't remove an object during iteration while using loops), and if it is a sword keep it... What ever is left in the list is what drops to the ground once the event finishes, what ever isn't in the list will remain on your person.

    Does that work?

    I feel like I am swinging at softballs, yet making contact with none of them. *laughs*

    edit: ps, here is that despawning event bug report I was referring to:
    https://bukkit.atlassian.net/browse/BUKKIT-1674
     
  12. Offline

    ceoepts

    I could use the code you helped me with on the pickup event and change it :)
     
  13. Offline

    Father Of Time

    You got the idea. ;) Just make sure to verify all the variable names, thats often the first mistake of re-using code.

    If you take the collection (the one you get from getDroppedItems() ) you can do the something similar tot he following:

    Code:
     Iterator<ItemStack> it = event.getDroppedItems().iterator<ItemStack>();
     while( it.hasNext() )
     {
      ItemStack stack = it.next();
      if( stack != sword ) // << this isn't a real check, you know what goes here
       it.remove();
     }
    
    and bingo, the collection of dropped items will have nothing but swords in it, which will be what is dropped when you die, everything else will remain in your belongings.

    Again, I wrote the above freehand in notepad, so it will likely need revisions; but it gives you the idea.

    Good luck putting this all together!

    *leaves work finally*
     
  14. Offline

    ceoepts

    Code:
        @EventHandler
        public void OnlyDropSwords(PlayerDeathEvent event)
        {
            Iterator<ItemStack> it = event.getDroppedItems().iterator<ItemStack>();
            while( it.hasNext() )
            {
              ItemStack stack = it.next();
              if( stack != sword ) // << i know what goes here
              it.remove();
            }
        }
    I am getting a error that it wants expression after this token. I have tryed to fix this but i dont know the expression if it is a. Please help me as i said i am a java and bukkit plugin noob so please help me :)
    Iterator<ItemStack> it = event.getDroppedItems().iterator<ItemStack>();

    I fixed the error by myself :) I was deleting <ItemStack> and change getDroppedItems to getDrops and then added a variable Material item = stack.getType()
    and then i added that swords and bam! It worked! Thx so much again you are awesome :)
    BTW: Heres my final Event Code
    Code:
        @EventHandler
        public void OnlyDropSwords(PlayerDeathEvent event)
        {
            Iterator<ItemStack> it = event.getDrops().iterator();
            while( it.hasNext() )
            {
              ItemStack stack = it.next();
              Material Item = stack.getType();
              if( Item != Material.WOOD_SWORD ||
                      Item != Material.STONE_SWORD ||
                      Item != Material.GOLD_SWORD ||
                      Item != Material.DIAMOND_SWORD )
              it.remove();
            }
        }
    Hmmm it worked with 1 sword but when i was adding more it breaked why?
    :confused:

    Replay this the fastest you can :)
    I have the pickup script btw but i dont know why this broke when i was adding:
    ||
    Item != Material.STONE_SWORD ||
    Item != Material.GOLD_SWORD ||
    Item != Material.DIAMOND_SWORD
    but ok :)

    Anyone :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  15. Offline

    mike0631

    Code:
        @EventHandler
        public void OnlyDropSwords(PlayerDeathEvent event){
            Iterator<ItemStack> it = event.getDrops().iterator();
            while( it.hasNext() ){
    ItemStack stack = it.next();
    Material Item = stack.getType();
    if( Item != Material.WOOD_SWORD){
    it.remove();
    }
    else if( Item != Material.WOOD_SWORD){
    it.remove();
    }
    else if( Item != Material.GOLD_SWORD){
    it.remove();
    }
    else if( Item != Material.DIAMOND_SWORD ){
    it.remove();
    }
            }
        }
                       
    
    This is untested, but try if it works.
     
  16. Offline

    ceoepts

    Do i dont need i else statement i have'nt tested it but ok :) i will
     
  17. Offline

    mike0631

    of yes, wait a sec i will edit.

    edit:
    Code:
        @EventHandler
        public void OnlyDropSwords(PlayerDeathEvent event){
            Iterator<ItemStack> it = event.getDrops().iterator();
            while( it.hasNext() ){
    ItemStack stack = it.next();
    Material Item = stack.getType();
    if( Item != Material.WOOD_SWORD){
    it.remove();
    }
    else if( Item != Material.WOOD_SWORD){
    it.remove();
    }
    else if( Item != Material.GOLD_SWORD){
    it.remove();
    }
    else if( Item != Material.DIAMOND_SWORD ){
    it.remove();
    }
            }
        }
    
     
  18. Offline

    ceoepts

    Oh ok i will test that
     
  19. Offline

    mike0631

    Tell me what you get =p
     
  20. Offline

    ceoepts

    No i tryed that before and a moment ago it do not drop anything
     
  21. Offline

    mike0631

    weird. :oops:
     
  22. Offline

    ceoepts

    mmm but i change the code from
    Code:
    Iterator<ItemStack> it = event.getDroppedItems().iterator<ItemStack>();
    while( it.hasNext() )
    {
      ItemStack stack = it.next();
      if( stack != sword ) // << this isn't a real check, you know what goes here
      it.remove();
    }
    but i got a error so i changed it see if you can get the error(if you want)
    the error is on
    Iterator<ItemStack> it = event.getDroppedItems().iterator<ItemStack>();
    ......................................................................................................................^
    and it say that i need a "expression" after that token

    Anyone knows whats going on? =?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  23. Offline

    desht

    You have a logic error there - you're using '||' (logical OR) instead of '&&' (logical AND).

    Think about it for a minute: what you're saying is "if the item isn't a wood sword OR the item isn't a stone sword OR ...". Well, if it's a wood sword, then it's obviously not a stone/iron/diamond sword so your condition always evaluates to true. Use '&&', or alternatively use a switch (more readable IMHO, but a question of style):

    PHP:
    while ( it.hasNext() ) {
      
    ItemStack stack it.next();
      switch (
    stack.getType()) {
        case 
    WOOD_SWORD:
        case 
    IRON_SWORD:
        case 
    STONE_SWORD:
        case 
    DIAMOND_SWORD:
          break; 
    // these items don't get removed
        
    default:
          
    it.remove(); // everything else does get removed
      
    }
    }
     
    biel likes this.
Thread Status:
Not open for further replies.

Share This Page