HashMap

Discussion in 'Plugin Development' started by ProStriker123, Jan 5, 2015.

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

    ProStriker123

    How can i create each hashmap for each player with this
    HashMap<String,Entity> antiaura = new HashMap<String, Entity>();

    Every help will be appreciated
     
  2. Offline

    ColaCraft

    Try <String, Player>
     
  3. Offline

    ProStriker123

    @ColaCraft, its must be like this <String, Entity> that i add a mob in the hashmap and the player and i want to have this for each player in the server
     
  4. Offline

    1Rogue Retired Staff

    You mean make a separate map for each player? Or a separate value for each player that you put into a map?

    If it's the first, just create a new class that contains a map and hold a backreference to the player's uuid.
     
  5. Offline

    ProStriker123

    separate map for each player
    @1Rogue
     
  6. Offline

    mythbusterma

    @ProStriker123

    You can nest the Maps, i.e. Map<String, Map<Object, Entity>>
     
  7. Offline

    ProStriker123

    like this?
    HashMap<String, HashMap<Object, Entity>> Lol = new HashMap<String, HashMap<Object, Entity>>();

    @mythbusterma,
    like this?
    Map<String, Map<Object, Entity>> Lol = new HashMap<String, Map<Object, Entity>>();
    cause its errors me
     
    Last edited by a moderator: Jan 5, 2015
  8. Offline

    ChipDev

    That doesn't help.
    What are the errors?
     
  9. Offline

    ProStriker123

    @ChipDev, cannot cast to java.util.Map

    can you explain me more cause i didnt understand verywell? explain more

    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Jan 5, 2015
  10. Offline

    1Rogue Retired Staff

    Code:java
    1. public class PlayerInfo {
    2.  
    3. private final Map<Object, Entity> map = new HashMap<>();
    4.  
    5. public PlayerInfo(Player p) {
    6. //get any info you want from the p variable
    7. }
    8.  
    9. //add methods
    10.  
    11. }


    Code:java
    1. Player p;
    2. Map<UUID, PlayerInfo> playerInfos = new HashMap<>(); //wow
    3. PlayerInfo info = playerInfos.get(p.getUniqueId());//much info
    4. info.someMethod();//very oop


    Edit: I see your last reply now. You essentially create a class to hold your information for you, and then keep a map of that class. Then in the class do whatever you want to the map. You can store whatever info you want as well if you need to get a reference to the player back (e.g. their UUID to get the object via Bukkit#getPlayer(UUID)).
     
  11. Offline

    ProStriker123

    @1Rogue, its will create separate HashMaps that if the player is in the hashmap of spectators(example) and if he hits a villager its will say him a message and if the others will hit hes villager from the Hashmap its will send him message?

    sorry for my bad grammar :(

    is this is ok?
    its will create a separate HashMap each player and each entityfor each player?
    Code:
    public class PlayerInfo {
        
        private final Map<Object, Entity> map = new HashMap<>();
       
        public void checkVillager(Player p, Entity e)  {
            if(!map.containsKey(p)) {
                map.put(p, e);
            }
        }
    }
     
    Last edited by a moderator: Jan 5, 2015
  12. Offline

    ChipDev

    I don't get errors.
     
  13. Offline

    nverdier

  14. Offline

    ProStriker123

    uh its not works me i want to do that i addidng a player and villager to a separate HashMap and if the player hits the villager its will send him message and if the others will hit that villager its wont send them message no meter what :(

    @ChipDev @nverdier @1Rogue @mythbusterma
     
  15. Offline

    nverdier

    @ProStriker123 Wait.. you want a separate HashMap for every player? Or you just want a different entry for each player?
     
  16. Offline

    ProStriker123

    i want separate each player with a villager in the hashmap and when he hits the villager will send him a message that hes kool :(
     
  17. Offline

    nverdier

    @ProStriker123 What exactly are you trying to accomplish? How do you decide the villager for each player? Can it be any player? Any villager?
     
  18. Offline

    ProStriker123

    well i want to make each HashMap thats will contain a player and villager(Example method addNewHashMap(player, villager)) when you spawn him of course and when you hit your villager in your HashMap its will send you message and if the others will hit him its wont send him message no metter what if you created him a each HashMap
    sorry for my grammar surrrrrrrrrrr
     
  19. Offline

    ChipDev

    HashMap<Player, Villager>
     
  20. Offline

    ProStriker123

    @ChipDev, i give up, idk how to make the methode :(
     
  21. Offline

    ChipDev

    Map<Player,Villager> PlayersMap = new HashMap<Player,Villager>();
     
  22. Offline

    1Rogue Retired Staff

    You'd be looking to map an identifier of a player to a respective villager. Which to start leaves us with:

    Map<UUID, Entity>

    However, if you wanted to have a list of entities you'd need a list instead of Entity or a separate class that holds all of your entities for you. Additionally, if you want to make sure that if one person tags a villager that is already tagged, you're going to have to separately track which players have tagged which villager.
     
Thread Status:
Not open for further replies.

Share This Page