How do I access hashmap from different class?

Discussion in 'Plugin Development' started by iZanax, Aug 31, 2012.

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

    iZanax

    How can i use the hashmap party? from the main class to the entitylistener.
    When I try it now it spams 2 lines of error. I did to "extends main".
    So how do i make this work properly?

    Class: Main
    Code:
        public final HashMap<Player, Integer> party = new HashMap<Player, Integer>();
    Class: EntityListener
    Code:
        @EventHandler
        public void onEntityDamage(EntityDamageByEntityEvent event) {
            if (event.getDamager() instanceof Player && event.getEntity() instanceof Player) {
                if(party.get((Player) event.getDamager()) == party.get((Player) event.getEntity())){
                    event.setCancelled(true);
                    return;
                }
            }
     
  2. Offline

    sternmin8or

    extends doesnt work like that. What you need to do is pass an instance of Main to your listener. Or, if you dont use the hashmap in your main class you can just create the hashmap in your listener class.
     
  3. Offline

    iZanax

    Code:
    public class EntityListener implements Listener, Main{
    it says something about it must be a interface.

    And I cant set the hashmap to the entitylistener. all the commands are connected to the hashmap in the main.
     
  4. Offline

    sternmin8or

    Dont implement main. It doenst work that way. The way to correctly pass the hashmap would be to have a constructor that includes it:

    public class EntityListener implements Listener{
    Main pl;
    public EntityListener(Main plugin){
    pl = plugin;
    }

    then to modify it:

    pl.party.get("ILIKETURTLES");

    or have a modifier method in main and call it:
    pl.setParty("ILIKETURTLES",999)

    Protip: dont use a hashmap with Player in it, String will suffice because you can use player.getName().
     
  5. Offline

    6SidedPentagon

Thread Status:
Not open for further replies.

Share This Page