还有其他优化方法吗?

时间:2016-07-19 19:31:30

标签: java bukkit repeat

所以我的代码是:

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和插口,但没有人给我任何提示,所以我来到这里。 此外,在重新应用效果之前,它会让魔药效果首先耗尽,而不是每半秒重新应用一次。

1 个答案:

答案 0 :(得分:0)

你一遍又一遍地调用相同的方法。为什么不只调用它们一次并将结果分配给某个变量,比如

leggingsType = p.getInventory().getLeggings().getType();

我并不认为这会带来巨大的性能差异,但会让代码更具可读性。

相关问题