【问题标题】:Any other way to optimize this?还有其他优化方法吗?
【发布时间】:2016-07-19 19:31:30
【问题描述】:

所以我的代码是:

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 和 spigot 中询问过,但没有人真正给我任何提示,所以我来到了这里。 此外,在它重新应用效果之前,它会让药水效果先用完,而不是每半秒重新应用一次。

【问题讨论】:

  • 关于优化已经有效的代码的问题,您可能想在codereview.se 上提问。不过,我鼓励您先阅读this

标签: java bukkit repeat


【解决方案1】:

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

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

我真的不认为这会产生巨大的性能差异,但它会使代码更具可读性。

【讨论】:

    猜你喜欢
    • 2023-02-20
    • 2022-01-04
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多