还有其他优化方法吗?
Any other way to optimize this?
所以我的代码是:
public void checkArmor() {
Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
public void run() {
for (Player p : Bukkit.getOnlinePlayers()) {
if (p.getInventory().getBoots().getType() == Material.CHAINMAIL_BOOTS
&& p.getInventory().getLeggings().getType() == Material.CHAINMAIL_LEGGINGS
&& p.getInventory().getChestplate().getType() == Material.CHAINMAIL_CHESTPLATE
&& p.getInventory().getHelmet().getType() == Material.CHAINMAIL_HELMET) {
p.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 20, 0));
p.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 20, 1));
p.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 20, 0));
}
if (p.getInventory().getBoots().getType() == Material.LEATHER_BOOTS
&& p.getInventory().getLeggings().getType() == Material.LEATHER_LEGGINGS
&& p.getInventory().getChestplate().getType() == Material.LEATHER_CHESTPLATE
&& p.getInventory().getHelmet().getType() == Material.LEATHER_HELMET) {
p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 20, 1));
p.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 20, 1));
p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 20, 1));
}
if (p.getInventory().getBoots().getType() == Material.IRON_BOOTS
&& p.getInventory().getLeggings().getType() == Material.IRON_LEGGINGS
&& p.getInventory().getChestplate().getType() == Material.IRON_CHESTPLATE
&& p.getInventory().getHelmet().getType() == Material.IRON_HELMET) {
p.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 20, 0));
p.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 20, 1));
p.addPotionEffect(new PotionEffect(PotionEffectType.HEALTH_BOOST, 20, 1));
}
if (p.getInventory().getBoots().getType() == Material.GOLD_BOOTS
&& p.getInventory().getLeggings().getType() == Material.GOLD_LEGGINGS
&& p.getInventory().getChestplate().getType() == Material.GOLD_CHESTPLATE
&& p.getInventory().getHelmet().getType() == Material.GOLD_HELMET) {
p.addPotionEffect(new PotionEffect(PotionEffectType.SATURATION, 20, 2));
p.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 140, 0));
p.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 20, 1));
p.addPotionEffect(new PotionEffect(PotionEffectType.GLOWING, 20, 1));
}
}
}
}, 0, 10);
}
请问还有什么优化方法吗?我问过 bukkit 和水龙头,但没有人真正给我任何提示,所以我来到这里。
此外,在重新应用效果之前,它会先让药水效果 运行 退出,而不是每半秒重新应用一次。
您一遍又一遍地调用相同的方法。为什么不只调用一次并将结果分配给某个变量,比如
leggingsType = p.getInventory().getLeggings().getType();
我真的不认为这会产生巨大的性能差异,但它会使代码更具可读性。
所以我的代码是:
public void checkArmor() {
Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
public void run() {
for (Player p : Bukkit.getOnlinePlayers()) {
if (p.getInventory().getBoots().getType() == Material.CHAINMAIL_BOOTS
&& p.getInventory().getLeggings().getType() == Material.CHAINMAIL_LEGGINGS
&& p.getInventory().getChestplate().getType() == Material.CHAINMAIL_CHESTPLATE
&& p.getInventory().getHelmet().getType() == Material.CHAINMAIL_HELMET) {
p.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 20, 0));
p.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 20, 1));
p.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 20, 0));
}
if (p.getInventory().getBoots().getType() == Material.LEATHER_BOOTS
&& p.getInventory().getLeggings().getType() == Material.LEATHER_LEGGINGS
&& p.getInventory().getChestplate().getType() == Material.LEATHER_CHESTPLATE
&& p.getInventory().getHelmet().getType() == Material.LEATHER_HELMET) {
p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 20, 1));
p.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 20, 1));
p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 20, 1));
}
if (p.getInventory().getBoots().getType() == Material.IRON_BOOTS
&& p.getInventory().getLeggings().getType() == Material.IRON_LEGGINGS
&& p.getInventory().getChestplate().getType() == Material.IRON_CHESTPLATE
&& p.getInventory().getHelmet().getType() == Material.IRON_HELMET) {
p.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 20, 0));
p.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 20, 1));
p.addPotionEffect(new PotionEffect(PotionEffectType.HEALTH_BOOST, 20, 1));
}
if (p.getInventory().getBoots().getType() == Material.GOLD_BOOTS
&& p.getInventory().getLeggings().getType() == Material.GOLD_LEGGINGS
&& p.getInventory().getChestplate().getType() == Material.GOLD_CHESTPLATE
&& p.getInventory().getHelmet().getType() == Material.GOLD_HELMET) {
p.addPotionEffect(new PotionEffect(PotionEffectType.SATURATION, 20, 2));
p.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 140, 0));
p.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 20, 1));
p.addPotionEffect(new PotionEffect(PotionEffectType.GLOWING, 20, 1));
}
}
}
}, 0, 10);
}
请问还有什么优化方法吗?我问过 bukkit 和水龙头,但没有人真正给我任何提示,所以我来到这里。 此外,在重新应用效果之前,它会先让药水效果 运行 退出,而不是每半秒重新应用一次。
您一遍又一遍地调用相同的方法。为什么不只调用一次并将结果分配给某个变量,比如
leggingsType = p.getInventory().getLeggings().getType();
我真的不认为这会产生巨大的性能差异,但它会使代码更具可读性。