简体   繁体   中英

How do I create a manager class to hold certain variables without abusing static?

Java n00b here. I'm making a plugin for a Minecraft server, and I would like to use one class to hold all of the variables for the plugin:

public class BwMgr {

    Boolean enabled = false;

    final HashSet<Player> red = new HashSet<>();
    final HashSet<Player> green = new HashSet<>();
    final HashSet<Player> blue = new HashSet<>();
    final HashSet<Player> orange = new HashSet<>();
    final HashSet<Player> purple = new HashSet<>();
    final HashSet<Player> yellow = new HashSet<>();
    final HashSet<Player> black = new HashSet<>();
    final HashSet<Player> white = new HashSet<>();

    public void assignTeams() {

    }

}

Each HashSet is supposed to contain teams of players. The boolean will be accessed by other classes to determine whether the plugin is enabled or not:

public class BwListener implements Listener {

    private final BwMgr bwMgr = new BwMgr();

    @EventHandler
    public void onPlayerJoinEvent(PlayerJoinEvent e) {
        if (bwMgr.enabled) {
            // do stuff
        }
    }
}

enabled should be updated here:

public class BwCmd implements CommandExecutor {

    private BwMgr bwMgr = new BwMgr();

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (cmd.getName().equalsIgnoreCase("bw")) {
            if (args[0].equalsIgnoreCase("enable")) {
                bwMgr.enabled = true;             
            } else if (args[0].equalsIgnoreCase("disable")) {
                bwMgr.enabled = false;
            }
        }
        return false;
    }
}

Of course, since each class is using a different instance of BwMgr , they are using different versions of the Boolean enabled . I could just put static in front of everything in BwMgr but I feel like that would be abuse. Any ideas would be much appreciated.

You need to pass the same instance of BwMgr through the constructor of BwCmd and BwListener.

so in BwCmd should look like:

    public class BwCmd implements CommandExecutor {

    private BwMgr bwMgr;

    // Pass through the constructor an instance of BwMgr.
    public BwCmd(BwMgr bwMgr) {
        this.bwMgr = bwMgr;
    }
  
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (cmd.getName().equalsIgnoreCase("bw")) {
            if (args[0].equalsIgnoreCase("enable")) {
                bwMgr.enabled = true;             
            } else if (args[0].equalsIgnoreCase("disable")) {
                bwMgr.enabled = false;
            }
        }
        return false;
    }
}

BwListener class:

    public class BwListener implements Listener {

    private final BwMgr bwMgr;
    
    // Pass through the constructor an instance of BwMgr.
    public BwListener(BwMgr bwMgr) {
        this.bwMgr = bwMgr;
    }

    @EventHandler
    public void onPlayerJoinEvent(PlayerJoinEvent e) {
        if (bwMgr.enabled) {
            // do stuff
        }
    }
}

And in your main class:

// Create one instance of bwMgr.
BwMgr bwMgr = new BwMgr();

// Initialize BwListener and BwCmd with the same BwMgr.
BwListener bwListener = new BwListener(bwMgr);
BwCmd bwCmd = new BwCmd(bwMgr);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM