与整数有关的问题在登录到Minecraft服务器时发生

huangapple go评论76阅读模式
英文:

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++;
			}
		}
	}

My code

答案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&lt;String. Integer&gt; playerCounter = new HashMap&lt;&gt;():
    
    void waitForLogin(String message, Player p) {
        Bukkit.getScheduler().runTaskLater(plugin, () -&gt; justDoIt(message, p), 60);
        Bukkit.getScheduler().runTaskLater(plugin, () -&gt; justDoIt(message, p), 120);
        Bukkit.getScheduler().runTaskLater(plugin, () -&gt; justDoIt(message, p), 180);
        Bukkit.getScheduler().runTaskLater(plugin, () -&gt; justDoIt(message, p), 240);
        Bukkit.getScheduler().runTaskLater(plugin, () -&gt; justDoIt(message, p), 300);
        Bukkit.getScheduler().runTaskLater(plugin, () -&gt; justDoIt(message, p), 360);
    }

    void justDoIt(String message, Player p) {
        if (may_loggin.contains(p)) {
            if (i == 6) {
                p.kickPlayer(ChatColor.DARK_RED + &quot;Cas vyprsel!&quot;);
                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.

huangapple
  • 本文由 发表于 2020年8月16日 16:53:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63434946.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定