Player HashMap

Discussion in 'Plugin Development' started by xX1yeahXx, Dec 14, 2014.

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

    xX1yeahXx

    Hey,
    I have a problem...
    I want after joining the server a greace period for the player, who joins.
    But this doesn't work:

    http://pastebin.com/cDn9QMc6

    Thanks for your help" :)
     
  2. Offline

    Skionz

    Cancel the EntityDamageEntityEvent <- not sure of the name check the javadocs.
     
  3. Offline

    mine-care

    @Skionz gave you the solution, and it is EntityDamageByEntityEvent
    Also in your code there is this: " tasks.remove(p.getName());" but the tasks map contains Player object as key not String object... soo that wouldnt work, Please have different names for variables instead of adding a number next to it like variabe1 and variable, this way you will avoid possible errors. And why are there 2 hashmaps?
    And last but not least, dont forget to handle player ojects apropriately to avoid ram leaks on world unloads.
     
  4. Offline

    adam753

    Don't put Players in lists or hashmaps. Use their UUID instead (player.getUniqueId() ).
     
  5. Offline

    Unica

    @adam753

    If you remove the player object on reloads / quit it doesn't really matter.
     
  6. Offline

    adam753

    @Unica
    But it's still very bad practice.
     
    ChipDev likes this.
  7. Offline

    mythbusterma

  8. Offline

    adam753

    @mythbusterma
    Storing Players has the potential for memory leaks. Storing UUIDs doesn't have the potential for memory leaks. It's all very well saying you'll be careful, but it's not like there's a downside to doing it the safe way.
     
  9. Offline

    mythbusterma

    @adam753

    But there is, looking a Player up by UUID is an O(n) operation, looking up a Player by using the Player object reference? An O(1) operation.
     
  10. Offline

    adam753

    @mythbusterma
    Now you're clutching at technicalities. Having your code run a nanosecond faster is no excuse for giving it to people who could end up with memory leaks because they don't understand the difference.
     
  11. Offline

    mythbusterma

    @adam753

    It's not a nanosecond difference, and it's not a technicality, in an expensive operation that can mean many milliseconds.
     
  12. Offline

    teej107

    @adam753 How can removing reference to an object cause memory leaks? You'll be going through the same amount of effort spreading awareness about using UUIDs and that of actually telling them to remove the Player when they leave.
     
  13. Offline

    Totom3

    Or you could just use a WeakHashMap and then you won't even need to think about removing the Players when they log out.

    Storing the Player's name or UUID instead of the Player object itself is actually the bad practice (if running CraftBukkit). CraftServer#getPlayer(UUID), CraftServer#getPlayer(String) and CraftServer#getPlayerExact(String) loop through ALL of the online Players, even if the said Player is not online. Storing OfflinePlayers is either not a valid choice (against Player) because sadly CraftOfflinePlayer#getPlayer() suffers from the same problem..
     
  14. Offline

    mythbusterma

    @Totom3

    That's what I said, good call on the WeakHashMap though, I forgot to mention that.
     
Thread Status:
Not open for further replies.

Share This Page