英文:
Problem with integers when logging to minecraft server
问题
int i = 1;
void waitForLogin(String message, Player p) {
Bukkit.getScheduler().runTaskLater(plugin, () -> justDoIt(message, p), 60);
Bukkit.getScheduler().runTaskLater(plugin, () -> justDoIt(message, p), 120);
Bukkit.getScheduler().runTaskLater(plugin, () -> justDoIt(message, p), 180);
Bukkit.getScheduler().runTaskLater(plugin, () -> justDoIt(message, p), 240);
Bukkit.getScheduler().runTaskLater(plugin, () -> justDoIt(message, p), 300);
Bukkit.getScheduler().runTaskLater(plugin, () -> justDoIt(message, p), 360);
}
void justDoIt(String message, Player p) {
if (may_loggin.contains(p)) {
if (i == 6) {
p.kickPlayer(ChatColor.DARK_RED + "Cas vyprsel!");
} else {
p.sendMessage(message);
i++;
}
}
}
英文:
I am making a plugin for logging (authme isn't working for me), which will send you 5 messages to warn you about logging and then it will kick you. Everything works perfectly, but int i will be after this 6. I know, I can set i to 1, but what can I do, if there are logging more players?
int i = 1;
void waitForLogin(String message, Player p) {
Bukkit.getScheduler().runTaskLater(plugin, () -> justDoIt(message, p), 60);
Bukkit.getScheduler().runTaskLater(plugin, () -> justDoIt(message, p), 120);
Bukkit.getScheduler().runTaskLater(plugin, () -> justDoIt(message, p), 180);
Bukkit.getScheduler().runTaskLater(plugin, () -> justDoIt(message, p), 240);
Bukkit.getScheduler().runTaskLater(plugin, () -> justDoIt(message, p), 300);
Bukkit.getScheduler().runTaskLater(plugin, () -> justDoIt(message, p), 360);
}
void justDoIt(String message, Player p) {
if (may_loggin.contains(p)) {
if (i == 6) {
p.kickPlayer(ChatColor.DARK_RED + "Cas vyprsel!");
} else {
p.sendMessage(message);
i++;
}
}
}
答案1
得分: 1
你可以使用HashMap
,以玩家姓名或某种类型的ID作为键。
Map<String, Integer> playerCounter = new HashMap<>();
void waitForLogin(String message, Player p) {
Bukkit.getScheduler().runTaskLater(plugin, () -> justDoIt(message, p), 60);
Bukkit.getScheduler().runTaskLater(plugin, () -> justDoIt(message, p), 120);
Bukkit.getScheduler().runTaskLater(plugin, () -> justDoIt(message, p), 180);
Bukkit.getScheduler().runTaskLater(plugin, () -> justDoIt(message, p), 240);
Bukkit.getScheduler().runTaskLater(plugin, () -> justDoIt(message, p), 300);
Bukkit.getScheduler().runTaskLater(plugin, () -> justDoIt(message, p), 360);
}
void justDoIt(String message, Player p) {
if (may_loggin.contains(p)) {
if (i == 6) {
p.kickPlayer(ChatColor.DARK_RED + "Cas vyprsel!");
playerCounter.remove(p.getName());
} else {
p.sendMessage(message);
playerCounter.put(p.getName(), playerCounter.getOrDefault(p.getName(), 0) + 1);
}
}
}
用玩家类所用于识别自身的字段替换.getName()
。
英文:
You can use a HashMap
with the player name or ID of some sort as the key.
Map<String. Integer> playerCounter = new HashMap<>():
void waitForLogin(String message, Player p) {
Bukkit.getScheduler().runTaskLater(plugin, () -> justDoIt(message, p), 60);
Bukkit.getScheduler().runTaskLater(plugin, () -> justDoIt(message, p), 120);
Bukkit.getScheduler().runTaskLater(plugin, () -> justDoIt(message, p), 180);
Bukkit.getScheduler().runTaskLater(plugin, () -> justDoIt(message, p), 240);
Bukkit.getScheduler().runTaskLater(plugin, () -> justDoIt(message, p), 300);
Bukkit.getScheduler().runTaskLater(plugin, () -> justDoIt(message, p), 360);
}
void justDoIt(String message, Player p) {
if (may_loggin.contains(p)) {
if (i == 6) {
p.kickPlayer(ChatColor.DARK_RED + "Cas vyprsel!");
playerCounter.remove(p.getName());
} else {
p.sendMessage(message);
playerCounter.put(p.getName(), playerCounter.getOrDefault(p.getName(), 0) + 1);
}
}
}
Replace..getName()
with whatever fields player class uses to identify itself.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论