1v1/Dueling

Discussion in 'Plugin Development' started by Irantwomiles, Mar 25, 2015.

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

    Irantwomiles

    I'm lost , I don't understand how to use hashmaps in this, could someone give me an example? It would be amazing.
     
  2. Offline

    nverdier

    @Irantwomiles Well what do you need to use HashMaps for? We won't write code for you, we don't spoonfeed here. If you want somebody else to write you code, then go the the plugin requests section. But what is your current issue? What problem to you need to fix?
     
  3. Offline

    Irantwomiles

    @nverdier I dont want you to write the code for me, the hashmaps would be for checking who dueled. I've looked at many threads and all of them say something different.
     
  4. Offline

    Signatured

    I believe you're looking for a way to store the two players dueling correct? To do so you'll have to declare a hashmap. Here's something to get you started:
    Code:
    HashMap<Player, Player> dueling = new HashMap<Player, Player>();
    This declares your hashmap. Simply add the player dueling to the HashMap to store them and clear the HashMap when they're done.
     
  5. Offline

    ResultStatic

    @Signatured gross dont do that, NEVER store the player object or any bukkit objects anywhere as it can cause memory leaks if not handled properly, why even risk it? instead store the UUID string(recommended) or store a WeakReference<Player> (not recommended).
     
  6. Offline

    teej107

    @ResultStatic What's not recommended about a WeakReference? You can also use WeakHashMaps or just remove the Player when they leave.
     
    nverdier likes this.
  7. Offline

    Irantwomiles

    Let's say I store the uuid in the hashmap when I /duel someone but doesn't that add both of them to the hashmap? I want a way to make it when I diel someone it adds the person who dueled to the hashmap then when the player does /accept it adds them to the hashmap to. Don't know if Possible or not
     
  8. Offline

    nverdier

    Chill bro. It's easy to make them be "handled properly". Just remove the entry when a player disconnects.
     
  9. Offline

    Irantwomiles

    I thought adding players to hashmaps and Arraylists could cause memory leaks? should I use Strings instead?
     
  10. Offline

    teej107

    Yes, putting Players in Maps and Lists can cause memory leaks if you don't handle them when they leave. If you remove the Player from the List/Map when they leave, you have properly handled it! Using A WeakHashMap and WeakReferences is an easy alternative than listening to a PlayerQuitEvent to remove the Player.
     
    nverdier likes this.
  11. Offline

    Irantwomiles

    @teej107 ive never heard of those before so I'll look into it, thank you.
     
  12. Offline

    ResultStatic

    @teej107 well they are good to use just a little bit more advanced than storing a uuid
     
  13. Offline

    TheDiamond06

    @Irantwomiles Declare a HashMap with Two Strings that can either be the player's UUID's or their name. Since you want to test if a player has dueled, then whenever in your code you want to test that, make a for loop going through the hashmap and check if the player name or UUID is inside of it.
     
Thread Status:
Not open for further replies.

Share This Page