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


    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
        public final HashMap<Player, Integer> party = new HashMap<Player, Integer>();
    Class: EntityListener
        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())){
  2. Offline


    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


    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


    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:"ILIKETURTLES");

    or have a modifier method in main and call it:

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


Thread Status:
Not open for further replies.

Share This Page