Solved Clear item in player hand. Bukkit bug???

Discussion in 'Plugin Development' started by eezjzbzbzb, Apr 19, 2014.

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

    eezjzbzbzb

    Hello.

    In my plugin I made it so to skip the respawn menu. I used "EntityDamageEvent" to see when the player health would get below or equal to zero, they would then be teleported to random location.

    The problem is, if that player dies for example with an iron sword, after they respawn their damage(without any damage changing item in hand) will stay the same as if they have an iron sword in their hand untill they actually use any other damage changing item(like word, shovel or axe). How can I fix this?

    I use bukkit-1.7.2-R0.3

    This is the method I use to clear the player.
    Code:Java
    1. private void clearPlayer(Player p){
    2. p.setHealth(20);
    3. p.getInventory().clear();
    4. p.getEquipment().clear();
    5. p.setItemInHand(null);
    6. p.setItemOnCursor(null);
    7. p.getInventory().setHeldItemSlot(0);
    8. p.getInventory().setItemInHand(null);
    9. for(PotionEffect pet: p.getActivePotionEffects()){
    10. p.removePotionEffect(pet.getType());
    11. }
    12. p.setExp(0);
    13. p.setLevel(0);
    14. p.setFoodLevel(20);
    15. p.setExhaustion(0);
    16. p.setFallDistance(0);
    17. p.setFireTicks(0);
    18. p.setFlying(false);
    19. p.setGameMode(GameMode.SURVIVAL);
    20. p.setSaturation(20);
    21. p.setSneaking(false);
    22. p.setSprinting(false);
    23. }


    When the player dies, they get banned for 30 seconds. After the 30 seconds, they should come back as if they have been respawned(thats what I want). But somehow, their damage is equal to the last item-damage they had before they died.
    This is the where I call this method.

    Code:Java
    1. @EventHandler
    2. public void onPlayerDamage(EntityDamageEvent event){
    3. if(event.getEntityType() == EntityType.PLAYER){
    4.  
    5. double dmg;
    6. double curHp;
    7. Player p = (Player)event.getEntity();
    8. dmg = event.getDamage();
    9. curHp = ((Player)event.getEntity()).getHealth();
    10.  
    11. if (curHp - dmg <= 0){ //<------- Here I check if they would die by this hit
    12. event.setCancelled(true); //<------ Here I cancel the EntityDamageEvent
    13.  
    14. p.setMaximumNoDamageTicks(200);
    15. p.setNoDamageTicks(200);
    16. clearPlayer(p); //<--------- Here I use my clearPlayer(Player) method(player is still online here)
    17.  
    18. //The rest is not important, its a custom player data file which I am checking for things.
    19. ZNHardcore.pList.get(p.getName()).setStillAlive(false);
    20. ZNHardcore.pList.get(p.getPlayer().getName()).setKickDur(deathPenalty);
    21. ZNHardcore.pList.get(p.getPlayer().getName()).setDead();
    22. ZNHardcore.pList.get(p.getPlayer().getName()).setKick();
    23. p.kickPlayer(MSG_DEATH_PENALTY); //<----Here I kick the player.
    24.  
    25. if (ZNHardcore.TempPList.get(p.getName()).isInBattle()){
    26. ZNHardcore.TempPList.get(p.getName()).showDamageScoreboard();
    27.  
    28. Player mostDamager = plugin.getServer().getPlayer(ZNHardcore.TempPList.get(p.getName()).getMostDamager());
    29. if(mostDamager.isOnline()){
    30. ZNHardcore.pList.get(mostDamager.getName()).addCredits(100);
    31. ZNHardcore.pList.get(mostDamager.getName()).setpKill();
    32. }else{
    33. ZNPlayerData tempLoadMostDamager = new ZNPlayerData(plugin);
    34. tempLoadMostDamager.LoadProfileData(p);
    35. tempLoadMostDamager.addCredits(100);
    36. tempLoadMostDamager.setpKill();
    37. tempLoadMostDamager.SaveProfile();
    38. }
    39. }
    40. @SuppressWarnings("unused")
    41. BukkitTask task = new ZNThreader(plugin, ZNHardcore.pList.get(p.getName())).runTaskTimer(this.plugin, 2, 2);
    42. ZNHardcore.pList.remove(p.getName());
    43. }
    44. }
    45. }
     
  2. I dont understand.
     
  3. Offline

    Garris0n

    Post the full event.
     
  4. Offline

    Wruczek

    try
    Code:java
    1. p.setItemInHand(Material.AIR);
     
  5. Offline

    __Sour

    eezjzbzbzb Use this.
    Code:java
    1. player.getInventory().setItemInHand(null);
     
  6. Offline

    Garris0n

    Wat?

    Just wat?
     
    Th3Br1x likes this.
  7. Offline

    Th3Br1x

    eezjzbzbzb Even though it's a deprecated method, try:
    Code:java
    1. player.updateInventory();


    I don't exactly understand what your problem is, but maybe this helps :)
     
  8. Offline

    Garris0n

    What he's doing is known to cause weird bugs.
     
    Th3Br1x likes this.
  9. Offline

    Th3Br1x

    Garris0n Oh, okay :) I wasn't quite sure what he meant, but it worth a try ;)
     
  10. Offline

    eezjzbzbzb

    I update the post, I will try what you guys suggested now. This is really a critical point for my plugin :(

    EDIT: I actually thought of this idea today... I wanna try if I make the player drop their items before they get kicked and then removed those items from the map, but I couldn't figure out how to drop the player items...
    So if anyone could let me know how to drop player items, I will try that aswell..


    EDIT2: Some didn't understand whats the problem. To put it simply:
    Plugin kicks players right before they die and resets their profile, clearing their inventory is one of the processes the profile-clearing takes. But when they come back, their damage is the same as the last weapon they had in their hand before they got kicked and profile-cleared.
    So they don't have diamond sword, but do same damage as diamond sword because they had a diamond sword in their hand before they died.
     
  11. Offline

    Garris0n

    Why don't you clear their inventory on login instead?
     
  12. Offline

    eezjzbzbzb

    Garris0n hmmm, thats a good idea... This sounds like something I would depend on if there is really nothing else I can do.

    Wruczek and Th3Br1x I just tried what you suggested, it didn't change anything...
    __Sour I have already try that before, thats the first thing I tried.

    I tried so many things just now, also clear inv on login, I found out that clearing the inventory somehow doesnt clear items that do damage, it removes them, but if they are in hand, the damage stays in hand while the item is gone.

    Well I found a way around which is sufficient for my plugin, but the problem is still not fixed.
    I made my plugin remove the playerdata file from the world its running on, so it's as if the player is new to that world.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
Thread Status:
Not open for further replies.

Share This Page