英文:
How do I create a manager class to hold certain variables without abusing static?
问题
以下是翻译好的部分:
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() {
}
}
每个 HashSet 应该包含玩家的团队。其他类将访问布尔值来确定插件是否启用:
public class BwListener implements Listener {
private final BwMgr bwMgr = new BwMgr();
@EventHandler
public void onPlayerJoinEvent(PlayerJoinEvent e) {
if (bwMgr.enabled) {
// 做一些操作
}
}
}
enabled
应该在这里更新:
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;
}
}
当然,由于每个类都在使用不同的 BwMgr
实例,它们使用的是不同版本的布尔值 enabled
。我可以在 BwMgr
的所有内容前面加上 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.
答案1
得分: 2
你需要通过BwCmd和BwListener的构造函数传递相同的BwMgr
实例。
所以在BwCmd中应该如下所示:
public class BwCmd implements CommandExecutor {
private BwMgr bwMgr;
// 通过构造函数传递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类:
public class BwListener implements Listener {
private final BwMgr bwMgr;
// 通过构造函数传递BwMgr实例。
public BwListener(BwMgr bwMgr) {
this.bwMgr = bwMgr;
}
@EventHandler
public void onPlayerJoinEvent(PlayerJoinEvent e) {
if (bwMgr.enabled) {
// 做一些操作
}
}
}
在你的主类中:
// 创建一个bwMgr实例。
BwMgr bwMgr = new BwMgr();
// 使用相同的BwMgr初始化BwListener和BwCmd。
BwListener bwListener = new BwListener(bwMgr);
BwCmd bwCmd = new BwCmd(bwMgr);
英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论