From fe1c6b19d3546ca26b1c3b1f8786c438436f4fb1 Mon Sep 17 00:00:00 2001 From: Dmitry Luk Date: Sat, 29 Jun 2024 15:49:22 +0500 Subject: [PATCH] refactor: effect get level --- .../api/entity/effect/EffectInstance.java | 22 +++++++++++-------- .../effect/type/EffectAbsorptionType.java | 6 +---- .../effect/type/EffectHealthBoostType.java | 8 +++---- .../effect/type/EffectSlownessType.java | 8 +++---- .../entity/effect/type/EffectSpeedType.java | 8 +++---- .../player/EntityPlayerBaseComponentImpl.java | 1 + 6 files changed, 27 insertions(+), 26 deletions(-) diff --git a/Allay-API/src/main/java/org/allaymc/api/entity/effect/EffectInstance.java b/Allay-API/src/main/java/org/allaymc/api/entity/effect/EffectInstance.java index 67fc46657..5c6c84912 100644 --- a/Allay-API/src/main/java/org/allaymc/api/entity/effect/EffectInstance.java +++ b/Allay-API/src/main/java/org/allaymc/api/entity/effect/EffectInstance.java @@ -10,12 +10,25 @@ */ public interface EffectInstance { + static EffectInstance fromNBT(NbtMap nbt) { + var effectType = EffectRegistry.getRegistry().getByK1((int) nbt.getByte("Id")); + Preconditions.checkNotNull(effectType, "Effect type not found for id: " + nbt.getByte("Id") + "!"); + int amplifier = nbt.getByte("Amplifier"); + int duration = nbt.getInt("Duration"); + boolean visible = nbt.getBoolean("ShowParticles"); + return new SimpleEffectInstance(effectType, amplifier, duration, visible); + } + EffectType getType(); int getAmplifier(); void setAmplifier(int amplifier); + default int getLevel() { + return getAmplifier() + 1; + } + boolean isVisible(); void setVisible(boolean visible); @@ -25,13 +38,4 @@ public interface EffectInstance { void setDuration(int duration); NbtMap saveNBT(); - - static EffectInstance fromNBT(NbtMap nbt) { - var effectType = EffectRegistry.getRegistry().getByK1((int) nbt.getByte("Id")); - Preconditions.checkNotNull(effectType, "Effect type not found for id: " + nbt.getByte("Id") + "!"); - int amplifier = nbt.getByte("Amplifier"); - int duration = nbt.getInt("Duration"); - boolean visible = nbt.getBoolean("ShowParticles"); - return new SimpleEffectInstance(effectType, amplifier, duration, visible); - } } diff --git a/Allay-API/src/main/java/org/allaymc/api/entity/effect/type/EffectAbsorptionType.java b/Allay-API/src/main/java/org/allaymc/api/entity/effect/type/EffectAbsorptionType.java index 386a7f1bd..b230d575a 100644 --- a/Allay-API/src/main/java/org/allaymc/api/entity/effect/type/EffectAbsorptionType.java +++ b/Allay-API/src/main/java/org/allaymc/api/entity/effect/type/EffectAbsorptionType.java @@ -21,11 +21,7 @@ private EffectAbsorptionType() { @Override public void onAdd(Entity entity, EffectInstance effectInstance) { - var amplifier = effectInstance.getAmplifier() + 1; - var newValue = amplifier * 4; - if (newValue > entity.getAbsorption()) { - entity.setAbsorption(newValue); - } + entity.setAbsorption(effectInstance.getLevel() * 4); } @Override diff --git a/Allay-API/src/main/java/org/allaymc/api/entity/effect/type/EffectHealthBoostType.java b/Allay-API/src/main/java/org/allaymc/api/entity/effect/type/EffectHealthBoostType.java index 89fa29198..0b527a2d1 100644 --- a/Allay-API/src/main/java/org/allaymc/api/entity/effect/type/EffectHealthBoostType.java +++ b/Allay-API/src/main/java/org/allaymc/api/entity/effect/type/EffectHealthBoostType.java @@ -22,16 +22,16 @@ private EffectHealthBoostType() { @Override public void onAdd(Entity entity, EffectInstance effectInstance) { - var amplifier = effectInstance.getAmplifier() + 1; if (!(entity instanceof EntityAttributeComponent attributeComponent)) return; - attributeComponent.setMaxHealth(attributeComponent.getMaxHealth() + (amplifier * 4)); + var level = effectInstance.getLevel(); + attributeComponent.setMaxHealth(attributeComponent.getMaxHealth() + (level * 4)); } @Override public void onRemove(Entity entity, EffectInstance effectInstance) { - var amplifier = effectInstance.getAmplifier() + 1; if (!(entity instanceof EntityAttributeComponent attributeComponent)) return; - attributeComponent.setMaxHealth(attributeComponent.getMaxHealth() - (amplifier * 4)); + var level = effectInstance.getLevel(); + attributeComponent.setMaxHealth(attributeComponent.getMaxHealth() - (level * 4)); if (attributeComponent.getHealth() > attributeComponent.getMaxHealth()) { attributeComponent.setHealth(attributeComponent.getMaxHealth()); } diff --git a/Allay-API/src/main/java/org/allaymc/api/entity/effect/type/EffectSlownessType.java b/Allay-API/src/main/java/org/allaymc/api/entity/effect/type/EffectSlownessType.java index 886dafcfe..088daf308 100644 --- a/Allay-API/src/main/java/org/allaymc/api/entity/effect/type/EffectSlownessType.java +++ b/Allay-API/src/main/java/org/allaymc/api/entity/effect/type/EffectSlownessType.java @@ -23,8 +23,8 @@ private EffectSlownessType() { @Override public void onAdd(Entity entity, EffectInstance effectInstance) { if (!(entity instanceof EntityPlayer player)) return; - var amplifier = effectInstance.getAmplifier() + 1; - var slowness = 1 - amplifier * 0.15f; + var level = effectInstance.getLevel(); + var slowness = 1 - level * 0.15f; if (slowness <= 0) slowness = 0.00001f; player.setMovementSpeed(player.getMovementSpeed() * slowness); } @@ -32,8 +32,8 @@ public void onAdd(Entity entity, EffectInstance effectInstance) { @Override public void onRemove(Entity entity, EffectInstance effectInstance) { if (!(entity instanceof EntityPlayer player)) return; - var amplifier = effectInstance.getAmplifier() + 1; - var slowness = 1 - amplifier * 0.15f; + var level = effectInstance.getLevel(); + var slowness = 1 - level * 0.15f; if (slowness <= 0) slowness = 0.00001f; player.setMovementSpeed(player.getMovementSpeed() / slowness); } diff --git a/Allay-API/src/main/java/org/allaymc/api/entity/effect/type/EffectSpeedType.java b/Allay-API/src/main/java/org/allaymc/api/entity/effect/type/EffectSpeedType.java index c49b1b99c..4d930d235 100644 --- a/Allay-API/src/main/java/org/allaymc/api/entity/effect/type/EffectSpeedType.java +++ b/Allay-API/src/main/java/org/allaymc/api/entity/effect/type/EffectSpeedType.java @@ -23,16 +23,16 @@ private EffectSpeedType() { @Override public void onAdd(Entity entity, EffectInstance effectInstance) { if (!(entity instanceof EntityPlayer player)) return; - var amplifier = effectInstance.getAmplifier() + 1; - var speed = 1 + amplifier * 0.2f; + var level = effectInstance.getLevel(); + var speed = 1 + level * 0.2f; player.setMovementSpeed(player.getMovementSpeed() * speed); } @Override public void onRemove(Entity entity, EffectInstance effectInstance) { if (!(entity instanceof EntityPlayer player)) return; - var amplifier = effectInstance.getAmplifier() + 1; - var speed = 1 + amplifier * 0.2f; + var level = effectInstance.getLevel(); + var speed = 1 + level * 0.2f; player.setMovementSpeed(player.getMovementSpeed() / speed); } } diff --git a/Allay-Server/src/main/java/org/allaymc/server/entity/component/player/EntityPlayerBaseComponentImpl.java b/Allay-Server/src/main/java/org/allaymc/server/entity/component/player/EntityPlayerBaseComponentImpl.java index 00e3fed84..ed8776fb8 100644 --- a/Allay-Server/src/main/java/org/allaymc/server/entity/component/player/EntityPlayerBaseComponentImpl.java +++ b/Allay-Server/src/main/java/org/allaymc/server/entity/component/player/EntityPlayerBaseComponentImpl.java @@ -755,6 +755,7 @@ protected void sendMobEffectPacket(MobEffectPacket packet) { @Override public void setAbsorption(float absorption) { + if (this.absorption == absorption) return; super.setAbsorption(absorption); attributeComponent.setAttribute(AttributeType.ABSORPTION, absorption); }