基本上,我有一个minecraft插口插件,正在做一个经济系统。我想每5分钟给所有球员75美元。为此,我使用sleep()方法,但我不希望for循环在每次完成repeater函数之前暂停。我只想让它等一次函数。这是我的代码,我会在最后解释一切。并且在主类上调用init函数。
public class Economy extends Thread {
private static final int moneyToAdd = 75;
public static void init(Server server){
addeach5min(moneyToAdd, server);
server.getConsoleSender().sendMessage("MoneyChanged");
}
public static void addeach5min(int value, Server server){
List<Player> players = new ArrayList<>(Bukkit.getOnlinePlayers());
for(Player ply : players){
if(ply.hasMetadata("cash")){
ply.setMetadata("cash", new FixedMetadataValue(Main.instance, value + ply.getMetadata("cash").get(0).asInt()));
System.out.print(ply.getMetadata("cash").get(0).asString());
repeater(moneyToAdd, server);
}
if(!ply.hasMetadata("cash")) {
ply.setMetadata("cash", new FixedMetadataValue(Main.instance, value));
System.out.print(ply.getMetadata("cash").get(0).asString());
repeater(moneyToAdd, server);
}
}
}
public static void repeater(int adder, Server server) {
try {
Thread.sleep(300000);
} catch (InterruptedException e) {
System.out.println(e.toString());
}
addeach5min(adder, server);
}
这里我检查播放器是否已经有了元数据,最后我调用一个函数来重复for循环
if(ply.hasMetadata("cash")){
ply.setMetadata("cash", new FixedMetadataValue(Main.instance, value + ply.getMetadata("cash").get(0).asInt()));
System.out.print(ply.getMetadata("cash").get(0).asString());
repeater(moneyToAdd, server);
}
如果玩家已经有了元数据,我就给他们钱,然后重复for循环
if(!ply.hasMetadata("cash")) {
ply.setMetadata("cash", new FixedMetadataValue(Main.instance, value));
System.out.print(ply.getMetadata("cash").get(0).asString());
repeater(moneyToAdd, server);
}
下面是重复for循环的函数
public static void repeater(int adder, Server server) {
try {
Thread.sleep(300000);
} catch (InterruptedException e) {
System.out.println(e.toString());
}
addeach5min(adder, server);
}
如果你需要更多的信息,请发表评论,我会更新这篇文章
感谢您的帮助:)
暂无答案!
目前还没有任何答案,快来回答吧!