如何仅对从数组列表中删除的内容执行操作?
How do I perform an action only on the even something is removed from an array list?
这是我正在使用的示例...
Player p = (Player) sender;
ArrayList<Player> nofalldmg = new ArrayList<Player>();
if (p.hasPermission("custome.fly")) {
if (p.isFlying()) {
p.setAllowFlight(false);
p.setFlying(false);
nofalldmg.add(p);
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
public void run() {
nofalldmg.remove(p);
}
}, 60);
所以你可以看到我有两行代码 1 "nofalldmg.add(p);"将播放器添加到数组列表,以及 2 "nofalldmg.remove(p);"从数组列表中删除播放器
当它们被添加到数组列表时,它们的跌落伤害被取消,我想知道的是一旦它们从该数组列表中删除,我该如何重新启用它们的跌落伤害?
这是可以帮助您的完整 class(没有导入,带有解释其工作原理的注释):
private final ArrayList<Player> nofalldmg = new ArrayList<Player>(); // declare list
@EventHandler
public void onDamageDisabler(EntityDamageEvent e) { // method to disable fall damage
if (e.getCause() == EntityDamageEvent.DamageCause.FALL) { // only if it's fall
if(e.getEntity() instanceof Player && nofalldmg.contains((Player) e.getEntity()) { // only if it's in list
e.setCancelled(true); // cancel
}
}
}
public void yourMethod(CommandSender sender) {
Player p = (Player) sender;
if (p.hasPermission("custome.fly")) { // your conditions
if (p.isFlying()) {
p.setAllowFlight(false);
p.setFlying(false);
nofalldmg.add(p); // here we add it to the list
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> {
nofalldmg.remove(p);
// here you can resetting everything that you want
}, 60); // no we remove it, so can take damage
}
}
}
这是我正在使用的示例...
Player p = (Player) sender;
ArrayList<Player> nofalldmg = new ArrayList<Player>();
if (p.hasPermission("custome.fly")) {
if (p.isFlying()) {
p.setAllowFlight(false);
p.setFlying(false);
nofalldmg.add(p);
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
public void run() {
nofalldmg.remove(p);
}
}, 60);
所以你可以看到我有两行代码 1 "nofalldmg.add(p);"将播放器添加到数组列表,以及 2 "nofalldmg.remove(p);"从数组列表中删除播放器
当它们被添加到数组列表时,它们的跌落伤害被取消,我想知道的是一旦它们从该数组列表中删除,我该如何重新启用它们的跌落伤害?
这是可以帮助您的完整 class(没有导入,带有解释其工作原理的注释):
private final ArrayList<Player> nofalldmg = new ArrayList<Player>(); // declare list
@EventHandler
public void onDamageDisabler(EntityDamageEvent e) { // method to disable fall damage
if (e.getCause() == EntityDamageEvent.DamageCause.FALL) { // only if it's fall
if(e.getEntity() instanceof Player && nofalldmg.contains((Player) e.getEntity()) { // only if it's in list
e.setCancelled(true); // cancel
}
}
}
public void yourMethod(CommandSender sender) {
Player p = (Player) sender;
if (p.hasPermission("custome.fly")) { // your conditions
if (p.isFlying()) {
p.setAllowFlight(false);
p.setFlying(false);
nofalldmg.add(p); // here we add it to the list
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> {
nofalldmg.remove(p);
// here you can resetting everything that you want
}, 60); // no we remove it, so can take damage
}
}
}