如何将 hashmap 的小于 x 值的键放入数组列表中,以使其在每次键小于 x 值时重新生成 x 值
How to get the keys of the hashmap that have less than x value into an arraylist to make it regen to x value everytime the keys get less than x value
我正在制作一个 Bukkit 插件,玩家可以在其中获得法力和法术。每个法术都有自己的法力消耗,玩家从 20 法力开始。
我有一个 ArrayList
用于存储法力值低于 20 的玩家,还有一个 HashMap
用于存储玩家当前的法力值。我需要知道如何制作一种方法,每秒为法力值低于 20 的玩家增加 1 法力值。
每次有玩家进入 ArrayList
时我都需要这样做,然后在他们达到 20 法力值时将他们从列表中删除。
这是我当前的代码:
public HashMap<String, Integer> mana = new HashMap<String, Integer>();
public ArrayList<String> changedMana = new ArrayList<String>();
public void onManaChange(){
//code goes here
}
将其更改为 TreeMap
并使用 headMap()
method。
您可以每秒 运行 一个任务计时器(这应该放在您的 onEnable()
方法中):
Bukkit.getServer().getScheduler().runTaskTimer(plugin, new Runnable(){
public void run(){
}
},20L,20L); //run this task every 20 ticks (20 ticks = 1 second)
并为 ArrayList
:
中的玩家增加魔法值
List<String> toRemove = new ArrayList<String>();
for(String player : changedMana){ //loop through all of the players in the ArrayList
int current = mana.get(player); //get the player's current mana
if(current < 20){ //if the player's mana is less than 20
current++; //add to the player's mana
mana.put(player, current); //update the player's mana
}
else if(current == 20){ //if the player's mana is greater than or equal to 20
toRemove.add(player); //add the player to the toRemove list, in order to remove the player from the ArrayList later safely
}
else{
//do something if the current mana is > 20, if you don't want to, just remove this
}
}
changedMana.removeAll(toRemove); //remove players from the changed mana array
那么,如果代码在您的 Main
class(extends JavaPlugin
中),您的代码可能会是这样的:
public HashMap<String, Integer> mana = new HashMap<String, Integer>();
public ArrayList<String> changedMana = new ArrayList<String>();
@Override
public void onEnable(){
Bukkit.getServer().getScheduler().runTaskTimer(plugin, new Runnable(){
public void run(){
List<String> toRemove = new ArrayList<String>();
for(String player : changedMana){ //loop through all of the players in the ArrayList
int current = mana.get(player); //get the player's current mana
if(current < 20){ //if the player's mana is less than 20
current++; //add to the player's mana
mana.put(player, current); //update the player's mana
}
else if(current == 20){ //if the player's mana is greater than or equal to 20
toRemove.add(player); //add the player to the toRemove list, in order to remove the player from the ArrayList later safely
}
else{
//do something if the current mana is > 20, if you don't want to, just remove this
}
}
changedMana.removeAll(toRemove); //remove players from the changed mana array
}
},20L,20L);
}
我正在制作一个 Bukkit 插件,玩家可以在其中获得法力和法术。每个法术都有自己的法力消耗,玩家从 20 法力开始。
我有一个 ArrayList
用于存储法力值低于 20 的玩家,还有一个 HashMap
用于存储玩家当前的法力值。我需要知道如何制作一种方法,每秒为法力值低于 20 的玩家增加 1 法力值。
每次有玩家进入 ArrayList
时我都需要这样做,然后在他们达到 20 法力值时将他们从列表中删除。
这是我当前的代码:
public HashMap<String, Integer> mana = new HashMap<String, Integer>();
public ArrayList<String> changedMana = new ArrayList<String>();
public void onManaChange(){
//code goes here
}
将其更改为 TreeMap
并使用 headMap()
method。
您可以每秒 运行 一个任务计时器(这应该放在您的 onEnable()
方法中):
Bukkit.getServer().getScheduler().runTaskTimer(plugin, new Runnable(){
public void run(){
}
},20L,20L); //run this task every 20 ticks (20 ticks = 1 second)
并为 ArrayList
:
List<String> toRemove = new ArrayList<String>();
for(String player : changedMana){ //loop through all of the players in the ArrayList
int current = mana.get(player); //get the player's current mana
if(current < 20){ //if the player's mana is less than 20
current++; //add to the player's mana
mana.put(player, current); //update the player's mana
}
else if(current == 20){ //if the player's mana is greater than or equal to 20
toRemove.add(player); //add the player to the toRemove list, in order to remove the player from the ArrayList later safely
}
else{
//do something if the current mana is > 20, if you don't want to, just remove this
}
}
changedMana.removeAll(toRemove); //remove players from the changed mana array
那么,如果代码在您的 Main
class(extends JavaPlugin
中),您的代码可能会是这样的:
public HashMap<String, Integer> mana = new HashMap<String, Integer>();
public ArrayList<String> changedMana = new ArrayList<String>();
@Override
public void onEnable(){
Bukkit.getServer().getScheduler().runTaskTimer(plugin, new Runnable(){
public void run(){
List<String> toRemove = new ArrayList<String>();
for(String player : changedMana){ //loop through all of the players in the ArrayList
int current = mana.get(player); //get the player's current mana
if(current < 20){ //if the player's mana is less than 20
current++; //add to the player's mana
mana.put(player, current); //update the player's mana
}
else if(current == 20){ //if the player's mana is greater than or equal to 20
toRemove.add(player); //add the player to the toRemove list, in order to remove the player from the ArrayList later safely
}
else{
//do something if the current mana is > 20, if you don't want to, just remove this
}
}
changedMana.removeAll(toRemove); //remove players from the changed mana array
}
},20L,20L);
}