diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/EcoArmorPlugin.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/EcoArmorPlugin.java index 9c95d75..cacc17d 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/EcoArmorPlugin.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/EcoArmorPlugin.java @@ -5,6 +5,7 @@ import com.willfp.eco.core.command.impl.PluginCommand; import com.willfp.eco.core.display.DisplayModule; import com.willfp.ecoarmor.commands.CommandEcoarmor; import com.willfp.ecoarmor.conditions.Conditions; +import com.willfp.ecoarmor.conditions.util.MovementConditionListener; import com.willfp.ecoarmor.config.EcoArmorJson; import com.willfp.ecoarmor.display.ArmorDisplay; import com.willfp.ecoarmor.effects.Effect; @@ -78,6 +79,7 @@ public class EcoArmorPlugin extends EcoPlugin { new EffectiveDurabilityListener(this), new DiscoverRecipeListener(this), new EffectWatcher(this), + new MovementConditionListener(this), new PreventSkullPlaceListener() ); } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/Condition.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/Condition.java index 47a722d..cf105fa 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/Condition.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/Condition.java @@ -59,9 +59,9 @@ public abstract class Condition implements Listener { protected abstract boolean isConditionMet(@NotNull Player player, @NotNull T value); - protected final void evaluateEffects(@NotNull final Player player, - @NotNull final T value, - @NotNull final ArmorSet set) { + public final void evaluateEffects(@NotNull final Player player, + @NotNull final Object value, + @NotNull final ArmorSet set) { this.getPlugin().getScheduler().runLater(() -> { if (isMet(player, value)) { for (Effect effect : set.getEffects().keySet()) { diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionAboveY.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionAboveY.java index cb08efb..56e7998 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionAboveY.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionAboveY.java @@ -14,28 +14,6 @@ public class ConditionAboveY extends Condition { super("above-y", Double.class); } - @EventHandler( - priority = EventPriority.MONITOR, - ignoreCancelled = true - ) - public void listener(@NotNull final PlayerMoveEvent event) { - Player player = event.getPlayer(); - - ArmorSet set = ArmorUtils.getSetOnPlayer(player); - - if (set == null) { - return; - } - - Double value = set.getConditionValue(this); - - if (value == null) { - return; - } - - evaluateEffects(player, value, set); - } - @Override public boolean isConditionMet(@NotNull final Player player, @NotNull final Double value) { diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionBelowY.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionBelowY.java index afd9c5f..e39d88a 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionBelowY.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionBelowY.java @@ -14,28 +14,6 @@ public class ConditionBelowY extends Condition { super("below-y", Double.class); } - @EventHandler( - priority = EventPriority.MONITOR, - ignoreCancelled = true - ) - public void listener(@NotNull final PlayerMoveEvent event) { - Player player = event.getPlayer(); - - ArmorSet set = ArmorUtils.getSetOnPlayer(player); - - if (set == null) { - return; - } - - Double value = set.getConditionValue(this); - - if (value == null) { - return; - } - - evaluateEffects(player, value, set); - } - @Override public boolean isConditionMet(@NotNull final Player player, @NotNull final Double value) { diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionInBiome.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionInBiome.java index cabae90..c673d45 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionInBiome.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionInBiome.java @@ -18,28 +18,6 @@ public class ConditionInBiome extends Condition { super("in-biome", String.class); } - @EventHandler( - priority = EventPriority.MONITOR, - ignoreCancelled = true - ) - public void listener(@NotNull final PlayerMoveEvent event) { - Player player = event.getPlayer(); - - ArmorSet set = ArmorUtils.getSetOnPlayer(player); - - if (set == null) { - return; - } - - String value = set.getConditionValue(this); - - if (value == null) { - return; - } - - evaluateEffects(player, value, set); - } - @Override public boolean isConditionMet(@NotNull final Player player, @NotNull final String value) { diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionInWater.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionInWater.java index b6fe023..9dc8c17 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionInWater.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionInWater.java @@ -15,28 +15,6 @@ public class ConditionInWater extends Condition { super("in-water", Boolean.class); } - @EventHandler( - priority = EventPriority.MONITOR, - ignoreCancelled = true - ) - public void listener(@NotNull final PlayerMoveEvent event) { - Player player = event.getPlayer(); - - ArmorSet set = ArmorUtils.getSetOnPlayer(player); - - if (set == null) { - return; - } - - Boolean value = set.getConditionValue(this); - - if (value == null) { - return; - } - - evaluateEffects(player, value, set); - } - @Override public boolean isConditionMet(@NotNull final Player player, @NotNull final Boolean value) { diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionInWorld.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionInWorld.java index c41ca3e..78e86e9 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionInWorld.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionInWorld.java @@ -18,28 +18,6 @@ public class ConditionInWorld extends Condition { super("in-world", String.class); } - @EventHandler( - priority = EventPriority.MONITOR, - ignoreCancelled = true - ) - public void listener(@NotNull final PlayerMoveEvent event) { - Player player = event.getPlayer(); - - ArmorSet set = ArmorUtils.getSetOnPlayer(player); - - if (set == null) { - return; - } - - String value = set.getConditionValue(this); - - if (value == null) { - return; - } - - evaluateEffects(player, value, set); - } - @Override public boolean isConditionMet(@NotNull final Player player, @NotNull final String value) { diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/util/MovementConditionListener.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/util/MovementConditionListener.java new file mode 100644 index 0000000..8ae438b --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/util/MovementConditionListener.java @@ -0,0 +1,62 @@ +package com.willfp.ecoarmor.conditions.util; + +import com.google.common.collect.ImmutableBiMap; +import com.google.common.collect.ImmutableSet; +import com.willfp.eco.core.PluginDependent; +import com.willfp.ecoarmor.EcoArmorPlugin; +import com.willfp.ecoarmor.conditions.Condition; +import com.willfp.ecoarmor.conditions.Conditions; +import com.willfp.ecoarmor.sets.ArmorSet; +import com.willfp.ecoarmor.sets.util.ArmorUtils; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerMoveEvent; +import org.jetbrains.annotations.NotNull; + +import java.net.http.WebSocket; +import java.util.Set; + +public class MovementConditionListener extends PluginDependent implements Listener { + /** + * Pass an {@link EcoArmorPlugin} in order to interface with it. + * + * @param plugin The plugin to manage. + */ + public MovementConditionListener(@NotNull EcoArmorPlugin plugin) { + super(plugin); + } + + private static final Set> CONDITIONS = ImmutableSet.of( + Conditions.ABOVE_Y, + Conditions.IN_BIOME, + Conditions.BELOW_Y, + Conditions.IN_WATER, + Conditions.IN_WORLD + ); + + @EventHandler( + priority = EventPriority.MONITOR, + ignoreCancelled = true + ) + public void listener(@NotNull final PlayerMoveEvent event) { + Player player = event.getPlayer(); + + ArmorSet set = ArmorUtils.getSetOnPlayer(player); + + if (set == null) { + return; + } + + for (Condition condition : CONDITIONS) { + Object value = set.getConditionValue(condition); + + if (value == null) { + continue; + } + + condition.evaluateEffects(player, value, set); + } + } +}