mirror of
https://github.com/Auxilor/EcoArmor.git
synced 2025-12-29 11:59:13 +00:00
Optimised movement-based conditions
This commit is contained in:
@@ -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()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -59,9 +59,9 @@ public abstract class Condition<T> 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()) {
|
||||
|
||||
@@ -14,28 +14,6 @@ public class ConditionAboveY extends Condition<Double> {
|
||||
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) {
|
||||
|
||||
@@ -14,28 +14,6 @@ public class ConditionBelowY extends Condition<Double> {
|
||||
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) {
|
||||
|
||||
@@ -18,28 +18,6 @@ public class ConditionInBiome extends Condition<String> {
|
||||
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) {
|
||||
|
||||
@@ -15,28 +15,6 @@ public class ConditionInWater extends Condition<Boolean> {
|
||||
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) {
|
||||
|
||||
@@ -18,28 +18,6 @@ public class ConditionInWorld extends Condition<String> {
|
||||
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) {
|
||||
|
||||
@@ -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<EcoArmorPlugin> 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<Condition<?>> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user