Solved Using a HashMap with multiple class

Discussion in 'Plugin Development' started by EmeraldWither, May 14, 2021.

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

    EmeraldWither

    So I need to store a list of players which can be accessed and be modified by multiple classes and have changes applied to all of the classes. I know this is possible with static, but I am not using it because, yea. How should I store this and be able to pull this list?
     
  2. Offline

    davidclue

    Create an abstract class and have all child classes extend to it. Use abstract class in hashmap and now you can attach any class that extends to it in the hashmap along with the player.
     
  3. Offline

    KarimAKL

    That or dependency injection.
     
    Strahan likes this.
  4. Offline

    EmeraldWither

    I have tried that, but it throws a null-pointer exception.
     
  5. Offline

    Strahan

    This. A much simpler and straightforward solution.

    I'm just happy to not see "make it static!" in here, lol
     
  6. Offline

    davidclue

    @EmeraldWither Then just do what I said if dependency injection doesn't work for you...
     
  7. Offline

    EmeraldWither

    That is what I did for all of my abilites which needs cooldowns. (Thanks :D)
     
  8. Offline

    Kars

    No don't do that as you will be violating Liskovs principle, which is never a good idea.
     
  9. Offline

    EmeraldWither

    Sorry, I forgot to include my classes.

    There seems to be a NPE with line 32 in my Launch Ability class. (The line where it defines the speedrunner list)

    Code:
    public class ManhuntGameManager {
    
        GravityListener gravityListener = new GravityListener(this);
        LaunchAbility launchAbility = new LaunchAbility(this);
    
    
        private List<String> hunter = new ArrayList<>();
        private List<String> speedrunner = new ArrayList<>();
    
        private boolean hasGameStarted = false;
    
        public List<String> getTeam(Team team){
            if(team.equals(Team.HUNTER)){
                return hunter;
            }
            if(team.equals(Team.SPEEDRUNNER)){
                return speedrunner;
            }
            return null;
    
        }
        public boolean getGameStatus(){
            return hasGameStarted;
        }
    Code:
    public class LaunchAbility implements Listener {
    
        private ManhuntGameManager manhuntGameManager;
        public LaunchAbility(ManhuntGameManager manhuntGameManager) {
            this.manhuntGameManager = manhuntGameManager;
        }
    
        private Main main;
        public LaunchAbility(Main main){
            this.main = main;
        }
    
        List<String> speedrunner = manhuntGameManager.getTeam(Team.SPEEDRUNNER);
    
       @EventHandler
       Event Listener here
    
     
  10. Offline

    KarimAKL

    @EmeraldWither You get a NullPointerException because speedrunner is assigned before manhuntGameManager. Initialize speedrunner in your constructor to fix this.
     
  11. Offline

    davidclue

    No, actually it follows Liskovs principle to the definition, I don't know what you're talking about, and as he said himself it works.
     
  12. Offline

    EmeraldWither

    How? Every time I have tried to initialize, I have gotten a NPE (even in the ManhuntGameManager class)
     
  13. Offline

    KarimAKL

    @EmeraldWither Instead of this:
    Code:Java
    1. private ManhuntGameManager manhuntGameManager;
    2. List<String> speedrunner = manhuntGameManager.getTeam(Team.SPEEDRUNNER);
    3.  
    4. public LaunchAbility(ManhuntGameManager manhuntGameManager) {
    5. this.manhuntGameManager = manhuntGameManager;
    6. }

    Do this:
    Code:Java
    1. private ManhuntGameManager manhuntGameManager;
    2. List<String> speedrunner;
    3.  
    4. public LaunchAbility(ManhuntGameManager manhuntGameManager) {
    5. this.manhuntGameManager = manhuntGameManager;
    6. this.speedrunner = manhuntGameManager.getTeam(Team.SPEEDRUNNER);
    7. }
     
    davidclue likes this.
  14. Offline

    EmeraldWither

    Still produces a NPE whenever I try to access the speedrunner list.
     
    Last edited: May 16, 2021
  15. Offline

    KarimAKL

    What I posted just fixed your NPE on initialization. The problem you have now is that ManhuntGameManager#getTeam(Team.SPEEDRUNNER) is returning null, so you need to fix that.
     
  16. Offline

    davidclue

    @EmeraldWither You could also just not use teams for this. I created a similar plugin to what you are trying to do which I believe is make a hunters vs speedrunners game mode. You can achieve this with just two array lists that store the players UUID's, with no teams involved.
     
  17. Offline

    Kars

    I thought you guys were talking about extending a class because you needed access to a property of that class. I misread.
     
  18. Offline

    EmeraldWither

    I fixed it!
    Here is how I did it:

    In my class what I did was only have 1 constructor. Instead of having 2 differnent constructors, I changed it to only 1 by making the constructor,
    Code:
    public ClearInv(ManhuntGameManager manhuntGameManager, Main main) {
    
    , and in my main class, change it from
    Code:
    getServer().getPluginManager().registerEvents(new ClearInv(this),this)
    
    to
    Code:
    getServer().getPluginManager().registerEvents(new ClearInv(manhuntGameManager, this),this)
    
    .
    Big thank you to everyone who helped me!
     
Thread Status:
Not open for further replies.

Share This Page