Make Player Drop Armour on Logout

Discussion in 'Plugin Development' started by alexander_q, Dec 5, 2011.

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

    alexander_q

    Using code from codename_B, I have made a plugin that drops the player inventory on logout that is compatible with the latest craftbukkit dev build for 1.0. The jar is available <Edit by Moderator: Redacted mediafire url>

    Be a dear and make it also drop player armour in the same way - the meta-gamers deserve it. For great justice?
     
    Last edited by a moderator: Nov 12, 2016
  2. Offline

    halley

    Er, this sounds like a request for people to implement the feature for you. On your plugin.

    The only difference between armor and the rest of the inventory is which inventory slot it's held. Sounds like you should be able to finish this change in about two minutes.

    If not, explain more what you're confused about?
     
  3. Offline

    alexander_q

    Ownership is of no concern to me. You're welcome to call it yours, as long as you credit myself and codename_B for what we have done. I'm only interested in the effect it will have on my server, and servers in general.

    Thanks for your comments in any case. Do you mean something like this:


    Code:
    ItemStack[] stacks = player.getArmorContents().getContents().clone();
    
            player.getArmorContents().setContents(new ItemStack[stacks.length]);
    
            for (ItemStack stack : stacks)
    
                if (stack != null)
    
                    player.getWorld()
                        .dropItemNaturally(player.getLocation(), stack);
    
            stacks = null;
     
  4. Offline

    halley

    Uh, no.
     
  5. Offline

    strupan

    Hmm, wow that is plenty rude, how about you explain what is wrong with his code or at least give some advice? Else why even bother posting that?

    But back to the topic, I actually stomped as well what getArmorContent actually does compare to the other armor piececontents, but I am assuming armorcontent means all. I still don't know how to help you know, pretty new myself, but I was wondering if someone would answer your question so I can learn a little from it as well.

    Best of luck
     
    ArcheCane likes this.
  6. Offline

    CptSausage

    Here you go:

    Code:java
    1. // First step: Go through his armor and drop it at his location
    2. ItemStack[] armorContents = player.getInventory().getArmorContents();
    3. for (ItemStack content : armorContents) {
    4. if (content.getAmount() != 0) {
    5. player.getWorld().dropItemNaturally(player.getLocation(), content);
    6. }
    7. }
    8. // Second step: delete his armor
    9. player.getInventory().setArmorContents(new ItemStack[4]);
    10. }


    edited because bukkit posted my answer before I was done
     
  7. Offline

    alexander_q

    Thanks very much CptSausage. Once I've tested it I'll publish it for the whole community to use. On my server it has been disabled for now, because we experienced two crashes after which our inventories were deleted but failed to drop (we were also relocated to the point at which we previously logged in, though the world was as it was supposed to be, so it wasn't a complete roll back). I suspect that when crashing the plugin as is does not behave properly - it seems to get through the inventory erasing part, but not the inventory drop part.

    Can anyone suggest a way to make this safer? Here is the item drop code:


    Code:
    ItemStack[] stacks = player.getInventory().getContents().clone();
    
            player.getInventory().setContents(new ItemStack[stacks.length]);
    
            for (ItemStack stack : stacks)
    
                if (stack != null)
    
                    player.getWorld()
                            .dropItemNaturally(player.getLocation(), stack);
    
            stacks = null;
     
  8. Offline

    CptSausage

    Yes. Use the code I posted.

    Let's have a look at your code.
    Code:
    if (stack != null)
    Your 'stack' will never be null. It would be right if you were looking for an empty slot in the inventory. But since you are looking for the armor contents, it will never be null.
    To be clear:
    An empty armor slot, does NOT return null.
    Instead it returns an ItemStack with...
    an amount of: 0
    a durability of: -1
    and a typ id of: 0

    That's why I am checking for (stack.getAmount() == 0).

    Code:
    stacks = null;
    And I don't know what this is supposed to be.
     
  9. Offline

    alexander_q

    The armour drop part works perfectly, but force-crashing the server consistently deletes the inventory without drop every time. The plugin needs to either:
    -Not activate in the event of a server crash
    -Make some sort of backup, which it loads from in the event of a server crash

    Any ideas?
     
Thread Status:
Not open for further replies.

Share This Page