diff --git a/config/checkstyle/suppression.xml b/config/checkstyle/suppression.xml index c460957..5d0bdaa 100644 --- a/config/checkstyle/suppression.xml +++ b/config/checkstyle/suppression.xml @@ -8,7 +8,10 @@ + + + \ No newline at end of file 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 3d8006b..934a6ca 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 @@ -8,15 +8,17 @@ import com.willfp.eco.util.protocollib.AbstractPacketAdapter; import com.willfp.ecoarmor.commands.CommandEagive; import com.willfp.ecoarmor.commands.CommandEareload; import com.willfp.ecoarmor.commands.TabcompleterEagive; +import com.willfp.ecoarmor.conditions.Conditions; import com.willfp.ecoarmor.display.ArmorDisplay; import com.willfp.ecoarmor.effects.Effect; import com.willfp.ecoarmor.effects.Effects; +import com.willfp.ecoarmor.effects.util.EffectWatcher; import com.willfp.ecoarmor.sets.ArmorSets; import com.willfp.ecoarmor.sets.util.EffectiveDurabilityListener; import com.willfp.ecoarmor.sets.util.PotionEffectListener; +import com.willfp.ecoarmor.upgrades.Tiers; import com.willfp.ecoarmor.upgrades.listeners.AdvancementShardListener; import com.willfp.ecoarmor.upgrades.listeners.CrystalListener; -import com.willfp.ecoarmor.upgrades.Tiers; import com.willfp.ecoarmor.util.DiscoverRecipeListener; import lombok.Getter; import org.bukkit.event.Listener; @@ -48,6 +50,7 @@ public class EcoArmorPlugin extends AbstractEcoPlugin { @Override public void enable() { Effects.values().stream().filter(Effect::isEnabled).forEach(effect -> this.getEventManager().registerListener(effect)); + Conditions.values().forEach(condition -> this.getEventManager().registerListener(condition)); this.getExtensionLoader().loadExtensions(); @@ -139,7 +142,8 @@ public class EcoArmorPlugin extends AbstractEcoPlugin { new AdvancementShardListener(this), new PotionEffectListener(this), new EffectiveDurabilityListener(this), - new DiscoverRecipeListener(this) + new DiscoverRecipeListener(this), + new EffectWatcher(this) ); } 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 new file mode 100644 index 0000000..c3e8f07 --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/Condition.java @@ -0,0 +1,85 @@ +package com.willfp.ecoarmor.conditions; + +import com.willfp.ecoarmor.EcoArmorPlugin; +import com.willfp.ecoarmor.effects.Effect; +import com.willfp.ecoarmor.sets.ArmorSet; +import com.willfp.ecoarmor.sets.util.ArmorUtils; +import lombok.AccessLevel; +import lombok.Getter; +import org.bukkit.entity.Player; +import org.bukkit.event.Listener; +import org.jetbrains.annotations.NotNull; + +public abstract class Condition implements Listener { + /** + * Instance of EcoArmor. + */ + @Getter(AccessLevel.PROTECTED) + private final EcoArmorPlugin plugin = EcoArmorPlugin.getInstance(); + + /** + * The name of the effect. + */ + @Getter + private final String name; + + /** + * The class of the config getter type. + */ + @Getter + private final Class typeClass; + + /** + * Create a new condition. + * + * @param name The condition name. + * @param typeClass The class of the config type. + */ + protected Condition(@NotNull final String name, + @NotNull final Class typeClass) { + this.name = name; + this.typeClass = typeClass; + + Conditions.addNewCondition(this); + } + + /** + * Get if condition is met for a player. + * + * @param player The player. + * @param value The value of the condition. + * @return If met. + */ + public final boolean isMet(@NotNull final Player player, + @NotNull final Object value) { + return isConditionMet(player, typeClass.cast(value)); + } + + 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) { + this.getPlugin().getScheduler().runLater(() -> { + if (isMet(player, value)) { + for (Effect effect : set.getEffects().keySet()) { + Object strength = set.getEffectStrength(effect); + + if (ArmorUtils.isWearingAdvanced(player)) { + Object advancedStrength = set.getAdvancedEffectStrength(effect); + if (advancedStrength != null) { + strength = advancedStrength; + } + } + + if (strength != null) { + effect.enable(player, strength); + } + } + } else { + set.getEffects().keySet().forEach(effect -> effect.disable(player)); + } + }, 1); + } +} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/Conditions.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/Conditions.java new file mode 100644 index 0000000..6080636 --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/Conditions.java @@ -0,0 +1,64 @@ +package com.willfp.ecoarmor.conditions; + +import com.google.common.collect.BiMap; +import com.google.common.collect.HashBiMap; +import com.google.common.collect.ImmutableList; +import com.willfp.ecoarmor.conditions.conditions.ConditionAboveHealthPercent; +import com.willfp.ecoarmor.conditions.conditions.ConditionAboveXPLevel; +import com.willfp.ecoarmor.conditions.conditions.ConditionAboveY; +import com.willfp.ecoarmor.conditions.conditions.ConditionBelowHealthPercent; +import com.willfp.ecoarmor.conditions.conditions.ConditionBelowXPLevel; +import com.willfp.ecoarmor.conditions.conditions.ConditionBelowY; +import com.willfp.ecoarmor.conditions.conditions.ConditionInWater; +import com.willfp.ecoarmor.conditions.conditions.ConditionInWorld; +import lombok.experimental.UtilityClass; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +@UtilityClass +@SuppressWarnings("unused") +public class Conditions { + /** + * All registered effects. + */ + private static final BiMap> BY_NAME = HashBiMap.create(); + + public static final Condition BELOW_Y = new ConditionBelowY(); + public static final Condition ABOVE_Y = new ConditionAboveY(); + public static final Condition ABOVE_HEALTH_PERCENT = new ConditionAboveHealthPercent(); + public static final Condition BELOW_HEALTH_PERCENT = new ConditionBelowHealthPercent(); + public static final Condition IN_WATER = new ConditionInWater(); + public static final Condition IN_WORLD = new ConditionInWorld(); + public static final Condition ABOVE_XP_LEVEL = new ConditionAboveXPLevel(); + public static final Condition BELOW_XP_LEVEL = new ConditionBelowXPLevel(); + + /** + * Get condition matching name.s + * + * @param name The name to query. + * @return The matching condition, or null if not found. + */ + public static Condition getByName(@NotNull final String name) { + return BY_NAME.get(name); + } + + /** + * List of all registered conditions. + * + * @return The conditions. + */ + public static List> values() { + return ImmutableList.copyOf(BY_NAME.values()); + } + + /** + * Add new condition to EcoArmor. + * + * @param condition The condition to add. + */ + public static void addNewCondition(@NotNull final Condition condition) { + BY_NAME.remove(condition.getName()); + BY_NAME.put(condition.getName(), condition); + } +} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionAboveHealthPercent.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionAboveHealthPercent.java new file mode 100644 index 0000000..290c577 --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionAboveHealthPercent.java @@ -0,0 +1,79 @@ +package com.willfp.ecoarmor.conditions.conditions; + +import com.willfp.ecoarmor.conditions.Condition; +import com.willfp.ecoarmor.sets.ArmorSet; +import com.willfp.ecoarmor.sets.util.ArmorUtils; +import org.bukkit.attribute.Attribute; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.event.entity.EntityRegainHealthEvent; +import org.jetbrains.annotations.NotNull; + +public class ConditionAboveHealthPercent extends Condition { + public ConditionAboveHealthPercent() { + super("above-health-percent", Double.class); + } + + @EventHandler( + priority = EventPriority.MONITOR, + ignoreCancelled = true + ) + public void listener(@NotNull final EntityRegainHealthEvent event) { + if (!(event.getEntity() instanceof Player)) { + return; + } + + Player player = (Player) event.getEntity(); + + ArmorSet set = ArmorUtils.getSetOnPlayer(player); + + if (set == null) { + return; + } + + Double value = set.getConditionValue(this); + + if (value == null) { + return; + } + + evaluateEffects(player, value, set); + } + + @EventHandler( + priority = EventPriority.MONITOR, + ignoreCancelled = true + ) + public void listener(@NotNull final EntityDamageEvent event) { + if (!(event.getEntity() instanceof Player)) { + return; + } + + Player player = (Player) event.getEntity(); + + 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) { + double maxHealth = player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue(); + double health = player.getHealth(); + + return (health / maxHealth) * 100 >= value; + } +} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionAboveXPLevel.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionAboveXPLevel.java new file mode 100644 index 0000000..2dbe30e --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionAboveXPLevel.java @@ -0,0 +1,44 @@ +package com.willfp.ecoarmor.conditions.conditions; + +import com.willfp.ecoarmor.conditions.Condition; +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.player.PlayerExpChangeEvent; +import org.jetbrains.annotations.NotNull; + +public class ConditionAboveXPLevel extends Condition { + public ConditionAboveXPLevel() { + super("above-xp-level", Integer.class); + } + + @EventHandler( + priority = EventPriority.MONITOR, + ignoreCancelled = true + ) + public void listener(@NotNull final PlayerExpChangeEvent event) { + Player player = event.getPlayer(); + + ArmorSet set = ArmorUtils.getSetOnPlayer(player); + + if (set == null) { + return; + } + + Integer value = set.getConditionValue(this); + + if (value == null) { + return; + } + + evaluateEffects(player, value, set); + } + + @Override + public boolean isConditionMet(@NotNull final Player player, + @NotNull final Integer value) { + return player.getLevel() >= value; + } +} 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 new file mode 100644 index 0000000..cb08efb --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionAboveY.java @@ -0,0 +1,44 @@ +package com.willfp.ecoarmor.conditions.conditions; + +import com.willfp.ecoarmor.conditions.Condition; +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.player.PlayerMoveEvent; +import org.jetbrains.annotations.NotNull; + +public class ConditionAboveY extends Condition { + public ConditionAboveY() { + 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) { + return player.getLocation().getY() >= value; + } +} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionBelowHealthPercent.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionBelowHealthPercent.java new file mode 100644 index 0000000..3b22e9a --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionBelowHealthPercent.java @@ -0,0 +1,79 @@ +package com.willfp.ecoarmor.conditions.conditions; + +import com.willfp.ecoarmor.conditions.Condition; +import com.willfp.ecoarmor.sets.ArmorSet; +import com.willfp.ecoarmor.sets.util.ArmorUtils; +import org.bukkit.attribute.Attribute; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.event.entity.EntityRegainHealthEvent; +import org.jetbrains.annotations.NotNull; + +public class ConditionBelowHealthPercent extends Condition { + public ConditionBelowHealthPercent() { + super("below-health-percent", Double.class); + } + + @EventHandler( + priority = EventPriority.MONITOR, + ignoreCancelled = true + ) + public void listener(@NotNull final EntityRegainHealthEvent event) { + if (!(event.getEntity() instanceof Player)) { + return; + } + + Player player = (Player) event.getEntity(); + + ArmorSet set = ArmorUtils.getSetOnPlayer(player); + + if (set == null) { + return; + } + + Double value = set.getConditionValue(this); + + if (value == null) { + return; + } + + evaluateEffects(player, value, set); + } + + @EventHandler( + priority = EventPriority.MONITOR, + ignoreCancelled = true + ) + public void listener(@NotNull final EntityDamageEvent event) { + if (!(event.getEntity() instanceof Player)) { + return; + } + + Player player = (Player) event.getEntity(); + + 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) { + double maxHealth = player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue(); + double health = player.getHealth(); + + return (health / maxHealth) * 100 < value; + } +} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionBelowXPLevel.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionBelowXPLevel.java new file mode 100644 index 0000000..eb0eaf2 --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionBelowXPLevel.java @@ -0,0 +1,44 @@ +package com.willfp.ecoarmor.conditions.conditions; + +import com.willfp.ecoarmor.conditions.Condition; +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.player.PlayerExpChangeEvent; +import org.jetbrains.annotations.NotNull; + +public class ConditionBelowXPLevel extends Condition { + public ConditionBelowXPLevel() { + super("below-xp-level", Integer.class); + } + + @EventHandler( + priority = EventPriority.MONITOR, + ignoreCancelled = true + ) + public void listener(@NotNull final PlayerExpChangeEvent event) { + Player player = event.getPlayer(); + + ArmorSet set = ArmorUtils.getSetOnPlayer(player); + + if (set == null) { + return; + } + + Integer value = set.getConditionValue(this); + + if (value == null) { + return; + } + + evaluateEffects(player, value, set); + } + + @Override + public boolean isConditionMet(@NotNull final Player player, + @NotNull final Integer value) { + return player.getLevel() < 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 new file mode 100644 index 0000000..afd9c5f --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionBelowY.java @@ -0,0 +1,44 @@ +package com.willfp.ecoarmor.conditions.conditions; + +import com.willfp.ecoarmor.conditions.Condition; +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.player.PlayerMoveEvent; +import org.jetbrains.annotations.NotNull; + +public class ConditionBelowY extends Condition { + public ConditionBelowY() { + 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) { + return player.getLocation().getY() < 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 new file mode 100644 index 0000000..b6fe023 --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionInWater.java @@ -0,0 +1,45 @@ +package com.willfp.ecoarmor.conditions.conditions; + +import com.willfp.ecoarmor.conditions.Condition; +import com.willfp.ecoarmor.sets.ArmorSet; +import com.willfp.ecoarmor.sets.util.ArmorUtils; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.player.PlayerMoveEvent; +import org.jetbrains.annotations.NotNull; + +public class ConditionInWater extends Condition { + public ConditionInWater() { + 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) { + return (player.getLocation().getBlock().getType() == Material.WATER) == 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 new file mode 100644 index 0000000..c41ca3e --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/conditions/conditions/ConditionInWorld.java @@ -0,0 +1,54 @@ +package com.willfp.ecoarmor.conditions.conditions; + +import com.willfp.ecoarmor.conditions.Condition; +import com.willfp.ecoarmor.sets.ArmorSet; +import com.willfp.ecoarmor.sets.util.ArmorUtils; +import org.bukkit.World; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.player.PlayerMoveEvent; +import org.jetbrains.annotations.NotNull; + +import java.util.Arrays; +import java.util.List; + +public class ConditionInWorld extends Condition { + public ConditionInWorld() { + 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) { + List worldNames = Arrays.asList(value.toLowerCase().split(" ")); + World world = player.getLocation().getWorld(); + if (world == null) { + return false; + } + + return worldNames.contains(world.getName().toLowerCase()); + } +} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/Effect.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/Effect.java index ff88a3c..72863c4 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/Effect.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/Effect.java @@ -3,9 +3,13 @@ package com.willfp.ecoarmor.effects; import com.willfp.ecoarmor.EcoArmorPlugin; import lombok.AccessLevel; import lombok.Getter; +import org.bukkit.entity.Player; import org.bukkit.event.Listener; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import java.util.HashMap; +import java.util.Map; import java.util.UUID; public abstract class Effect implements Listener { @@ -39,6 +43,11 @@ public abstract class Effect implements Listener { @Getter private final Class typeClass; + /** + * Players that the effect is currently enabled for. + */ + private final Map enabledPlayers = new HashMap<>(); + /** * Create a new effect. * @@ -55,6 +64,57 @@ public abstract class Effect implements Listener { Effects.addNewEffect(this); } + /** + * Get the effect strength for a player. + * + * @param player The player. + * @return The strength. + */ + @Nullable + public final T getStrengthForPlayer(@NotNull final Player player) { + return enabledPlayers.get(player.getUniqueId()); + } + + /** + * Enable the effect for a player. + * + * @param player The player. + * @param value The strength. + */ + public final void enable(@NotNull final Player player, + @NotNull final Object value) { + if (!this.isEnabled()) { + return; + } + + if (enabledPlayers.containsKey(player.getUniqueId())) { + return; + } + + enabledPlayers.put(player.getUniqueId(), typeClass.cast(value)); + + this.onEnable(player); + } + + /** + * Disable the effect for a player. + * + * @param player The player. + */ + public final void disable(@NotNull final Player player) { + enabledPlayers.remove(player.getUniqueId()); + + this.onDisable(player); + } + + protected void onEnable(@NotNull final Player player) { + // Empty by default + } + + protected void onDisable(@NotNull final Player player) { + // Empty by default + } + /** * Update if the effect is enabled. */ diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/Effects.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/Effects.java index f5dc636..606c28b 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/Effects.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/Effects.java @@ -15,7 +15,7 @@ import com.willfp.ecoarmor.effects.effects.Flight; import com.willfp.ecoarmor.effects.effects.HungerLossMultiplier; import com.willfp.ecoarmor.effects.effects.MeleeDamageMultiplier; import com.willfp.ecoarmor.effects.effects.RegenerationMultiplier; -import com.willfp.ecoarmor.effects.effects.SpeedMutiplier; +import com.willfp.ecoarmor.effects.effects.SpeedMultiplier; import com.willfp.ecoarmor.effects.effects.TridentDamageMultiplier; import com.willfp.ecoarmor.effects.effects.WarpChance; import lombok.experimental.UtilityClass; @@ -39,7 +39,7 @@ public class Effects { public static final Effect MELEE_DAMAGE_MULTIPLIER = new MeleeDamageMultiplier(); public static final Effect TRIDENT_DAMAGE_MULTIPLIER = new TridentDamageMultiplier(); public static final Effect BONUS_HEARTS = new BonusHearts(); - public static final Effect SPEED_MULTIPLIER = new SpeedMutiplier(); + public static final Effect SPEED_MULTIPLIER = new SpeedMultiplier(); public static final Effect EXPERIENCE_MULTIPLIER = new ExperienceMultiplier(); public static final Effect REGENERATION_MULTIPLIER = new RegenerationMultiplier(); public static final Effect WARP_CHANCE = new WarpChance(); diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/AttackSpeedMultiplier.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/AttackSpeedMultiplier.java index 38c0087..9011f67 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/AttackSpeedMultiplier.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/AttackSpeedMultiplier.java @@ -1,13 +1,10 @@ package com.willfp.ecoarmor.effects.effects; -import com.willfp.eco.util.events.armorequip.ArmorEquipEvent; import com.willfp.ecoarmor.effects.Effect; -import com.willfp.ecoarmor.sets.util.ArmorUtils; import org.bukkit.attribute.Attribute; import org.bukkit.attribute.AttributeInstance; import org.bukkit.attribute.AttributeModifier; import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; import org.jetbrains.annotations.NotNull; public class AttackSpeedMultiplier extends Effect { @@ -15,23 +12,28 @@ public class AttackSpeedMultiplier extends Effect { super("attack-speed-multiplier", Double.class); } - @EventHandler - public void listener(@NotNull final ArmorEquipEvent event) { - Player player = event.getPlayer(); + @Override + protected void onEnable(@NotNull final Player player) { + AttributeInstance attackSpeed = player.getAttribute(Attribute.GENERIC_ATTACK_SPEED); + assert attackSpeed != null; - AttributeInstance movementSpeed = player.getAttribute(Attribute.GENERIC_ATTACK_SPEED); - assert movementSpeed != null; + Double multiplier = this.getStrengthForPlayer(player); - this.getPlugin().getScheduler().runLater(() -> { - Double multiplier = ArmorUtils.getEffectStrength(player, this); - if (multiplier == null) { - movementSpeed.removeModifier(new AttributeModifier(this.getUuid(), "attack-speed-multiplier", 0, AttributeModifier.Operation.MULTIPLY_SCALAR_1)); - } else { - AttributeModifier modifier = new AttributeModifier(this.getUuid(), "attack-speed-multiplier", multiplier - 1, AttributeModifier.Operation.MULTIPLY_SCALAR_1); - if (!movementSpeed.getModifiers().contains(modifier)) { - movementSpeed.addModifier(modifier); - } - } - }, 1); + if (multiplier == null) { + return; + } + + AttributeModifier modifier = new AttributeModifier(this.getUuid(), "attack-speed-multiplier", 1 - multiplier, AttributeModifier.Operation.MULTIPLY_SCALAR_1); + if (attackSpeed.getModifiers().stream().noneMatch(attributeModifier -> attributeModifier.getUniqueId().equals(modifier.getUniqueId()))) { + attackSpeed.addModifier(modifier); + } + } + + @Override + protected void onDisable(@NotNull final Player player) { + AttributeInstance attackSpeed = player.getAttribute(Attribute.GENERIC_ATTACK_SPEED); + assert attackSpeed != null; + + attackSpeed.removeModifier(new AttributeModifier(this.getUuid(), "attack-speed-multiplier", 0, AttributeModifier.Operation.MULTIPLY_SCALAR_1)); } } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/BonusHearts.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/BonusHearts.java index fb83586..b73b269 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/BonusHearts.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/BonusHearts.java @@ -1,13 +1,10 @@ package com.willfp.ecoarmor.effects.effects; -import com.willfp.eco.util.events.armorequip.ArmorEquipEvent; import com.willfp.ecoarmor.effects.Effect; -import com.willfp.ecoarmor.sets.util.ArmorUtils; import org.bukkit.attribute.Attribute; import org.bukkit.attribute.AttributeInstance; import org.bukkit.attribute.AttributeModifier; import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; import org.jetbrains.annotations.NotNull; public class BonusHearts extends Effect { @@ -15,23 +12,28 @@ public class BonusHearts extends Effect { super("bonus-hearts", Integer.class); } - @EventHandler - public void listener(@NotNull final ArmorEquipEvent event) { - Player player = event.getPlayer(); - + @Override + protected void onEnable(@NotNull final Player player) { AttributeInstance maxHealth = player.getAttribute(Attribute.GENERIC_MAX_HEALTH); assert maxHealth != null; - this.getPlugin().getScheduler().runLater(() -> { - Integer bonus = ArmorUtils.getEffectStrength(player, this); - if (bonus == null) { - maxHealth.removeModifier(new AttributeModifier(this.getUuid(), "bonus-hearts", 0, AttributeModifier.Operation.ADD_NUMBER)); - } else { - AttributeModifier modifier = new AttributeModifier(this.getUuid(), "bonus-hearts", bonus * 2, AttributeModifier.Operation.ADD_NUMBER); - if (!maxHealth.getModifiers().contains(modifier)) { - maxHealth.addModifier(modifier); - } - } - }, 1); + Integer bonus = this.getStrengthForPlayer(player); + + if (bonus == null) { + return; + } + + AttributeModifier modifier = new AttributeModifier(this.getUuid(), "bonus-hearts", bonus, AttributeModifier.Operation.ADD_NUMBER); + if (maxHealth.getModifiers().stream().noneMatch(attributeModifier -> attributeModifier.getUniqueId().equals(modifier.getUniqueId()))) { + maxHealth.addModifier(modifier); + } + } + + @Override + protected void onDisable(@NotNull final Player player) { + AttributeInstance maxHealth = player.getAttribute(Attribute.GENERIC_MAX_HEALTH); + assert maxHealth != null; + + maxHealth.removeModifier(new AttributeModifier(this.getUuid(), "bonus-hearts", 0, AttributeModifier.Operation.ADD_NUMBER)); } } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/BowDamageMultiplier.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/BowDamageMultiplier.java index bf09977..e96bc07 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/BowDamageMultiplier.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/BowDamageMultiplier.java @@ -1,7 +1,6 @@ package com.willfp.ecoarmor.effects.effects; import com.willfp.ecoarmor.effects.Effect; -import com.willfp.ecoarmor.sets.util.ArmorUtils; import org.bukkit.entity.Arrow; import org.bukkit.entity.Player; import org.bukkit.entity.Projectile; @@ -33,7 +32,7 @@ public class BowDamageMultiplier extends Effect { Player player = (Player) shooter; - Double multiplier = ArmorUtils.getEffectStrength(player, this); + Double multiplier = this.getStrengthForPlayer(player); if (multiplier == null) { return; } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/DamageMultiplier.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/DamageMultiplier.java index 726e79d..c63dece 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/DamageMultiplier.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/DamageMultiplier.java @@ -1,7 +1,6 @@ package com.willfp.ecoarmor.effects.effects; import com.willfp.ecoarmor.effects.Effect; -import com.willfp.ecoarmor.sets.util.ArmorUtils; import org.bukkit.entity.Player; import org.bukkit.entity.Projectile; import org.bukkit.event.EventHandler; @@ -38,7 +37,7 @@ public class DamageMultiplier extends Effect { return; } - Double multiplier = ArmorUtils.getEffectStrength(attacker, this); + Double multiplier = this.getStrengthForPlayer(attacker); if (multiplier == null) { return; } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/DamageTakenMultiplier.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/DamageTakenMultiplier.java index fbbb941..c01380f 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/DamageTakenMultiplier.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/DamageTakenMultiplier.java @@ -1,7 +1,6 @@ package com.willfp.ecoarmor.effects.effects; import com.willfp.ecoarmor.effects.Effect; -import com.willfp.ecoarmor.sets.util.ArmorUtils; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.entity.EntityDamageEvent; @@ -24,7 +23,7 @@ public class DamageTakenMultiplier extends Effect { Player player = (Player) event.getEntity(); - Double multiplier = ArmorUtils.getEffectStrength(player, this); + Double multiplier = this.getStrengthForPlayer(player); if (multiplier == null) { return; } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/DurabilityMultiplier.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/DurabilityMultiplier.java index b2f7071..946399a 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/DurabilityMultiplier.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/DurabilityMultiplier.java @@ -2,7 +2,6 @@ package com.willfp.ecoarmor.effects.effects; import com.willfp.eco.util.NumberUtils; import com.willfp.ecoarmor.effects.Effect; -import com.willfp.ecoarmor.sets.util.ArmorUtils; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.player.PlayerItemDamageEvent; @@ -21,7 +20,7 @@ public class DurabilityMultiplier extends Effect { Player player = event.getPlayer(); - Double multiplier = ArmorUtils.getEffectStrength(player, this); + Double multiplier = this.getStrengthForPlayer(player); if (multiplier == null) { return; diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/EvadeChance.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/EvadeChance.java index 968b5ef..2968771 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/EvadeChance.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/EvadeChance.java @@ -2,7 +2,6 @@ package com.willfp.ecoarmor.effects.effects; import com.willfp.eco.util.NumberUtils; import com.willfp.ecoarmor.effects.Effect; -import com.willfp.ecoarmor.sets.util.ArmorUtils; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.entity.EntityDamageEvent; @@ -25,7 +24,7 @@ public class EvadeChance extends Effect { Player player = (Player) event.getEntity(); - Double chance = ArmorUtils.getEffectStrength(player, this); + Double chance = this.getStrengthForPlayer(player); if (chance == null) { return; diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/ExperienceMultiplier.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/ExperienceMultiplier.java index d0c03a3..06bbe4e 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/ExperienceMultiplier.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/ExperienceMultiplier.java @@ -2,7 +2,6 @@ package com.willfp.ecoarmor.effects.effects; import com.willfp.eco.util.events.naturalexpgainevent.NaturalExpGainEvent; import com.willfp.ecoarmor.effects.Effect; -import com.willfp.ecoarmor.sets.util.ArmorUtils; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.jetbrains.annotations.NotNull; @@ -20,7 +19,7 @@ public class ExperienceMultiplier extends Effect { return; } - Double multiplier = ArmorUtils.getEffectStrength(player, this); + Double multiplier = this.getStrengthForPlayer(player); if (multiplier == null) { return; diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/FallDamageMultiplier.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/FallDamageMultiplier.java index 17d820d..d940f7b 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/FallDamageMultiplier.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/FallDamageMultiplier.java @@ -1,7 +1,6 @@ package com.willfp.ecoarmor.effects.effects; import com.willfp.ecoarmor.effects.Effect; -import com.willfp.ecoarmor.sets.util.ArmorUtils; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.entity.EntityDamageEvent; @@ -28,7 +27,7 @@ public class FallDamageMultiplier extends Effect { Player player = (Player) event.getEntity(); - Double multiplier = ArmorUtils.getEffectStrength(player, this); + Double multiplier = this.getStrengthForPlayer(player); if (multiplier == null) { return; } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/Flight.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/Flight.java index 3f1a2ca..ddb8acc 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/Flight.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/Flight.java @@ -1,11 +1,8 @@ package com.willfp.ecoarmor.effects.effects; -import com.willfp.eco.util.events.armorequip.ArmorEquipEvent; import com.willfp.ecoarmor.effects.Effect; -import com.willfp.ecoarmor.sets.util.ArmorUtils; import org.bukkit.GameMode; import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; import org.jetbrains.annotations.NotNull; public class Flight extends Effect { @@ -13,21 +10,15 @@ public class Flight extends Effect { super("flight", Boolean.class); } - @EventHandler - public void onArmorEquip(@NotNull final ArmorEquipEvent event) { - Player player = event.getPlayer(); + @Override + protected void onEnable(@NotNull final Player player) { + player.setAllowFlight(true); + } - this.getPlugin().getScheduler().runLater(() -> { - Boolean flight = ArmorUtils.getEffectStrength(player, this); - if (flight == null) { - if (player.getGameMode() == GameMode.SURVIVAL || player.getGameMode() == GameMode.ADVENTURE) { - player.setAllowFlight(false); - } - } else { - if (flight) { - player.setAllowFlight(true); - } - } - }, 1); + @Override + protected void onDisable(@NotNull final Player player) { + if (player.getGameMode() == GameMode.SURVIVAL || player.getGameMode() == GameMode.ADVENTURE) { + player.setAllowFlight(false); + } } } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/HungerLossMultiplier.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/HungerLossMultiplier.java index 7b8174c..bd71fb1 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/HungerLossMultiplier.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/HungerLossMultiplier.java @@ -2,7 +2,6 @@ package com.willfp.ecoarmor.effects.effects; import com.willfp.eco.util.NumberUtils; import com.willfp.ecoarmor.effects.Effect; -import com.willfp.ecoarmor.sets.util.ArmorUtils; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.entity.FoodLevelChangeEvent; @@ -21,7 +20,7 @@ public class HungerLossMultiplier extends Effect { Player player = (Player) event.getEntity(); - Double multiplier = ArmorUtils.getEffectStrength(player, this); + Double multiplier = this.getStrengthForPlayer(player); if (multiplier == null) { return; diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/MeleeDamageMultiplier.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/MeleeDamageMultiplier.java index 910d9dd..f4fc350 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/MeleeDamageMultiplier.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/MeleeDamageMultiplier.java @@ -1,7 +1,6 @@ package com.willfp.ecoarmor.effects.effects; import com.willfp.ecoarmor.effects.Effect; -import com.willfp.ecoarmor.sets.util.ArmorUtils; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.entity.EntityDamageByEntityEvent; @@ -24,7 +23,8 @@ public class MeleeDamageMultiplier extends Effect { Player attacker = (Player) event.getDamager(); - Double multiplier = ArmorUtils.getEffectStrength(attacker, this); + Double multiplier = this.getStrengthForPlayer(attacker); + if (multiplier == null) { return; } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/RegenerationMultiplier.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/RegenerationMultiplier.java index 87b6d00..1c63969 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/RegenerationMultiplier.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/RegenerationMultiplier.java @@ -1,7 +1,6 @@ package com.willfp.ecoarmor.effects.effects; import com.willfp.ecoarmor.effects.Effect; -import com.willfp.ecoarmor.sets.util.ArmorUtils; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.entity.EntityRegainHealthEvent; @@ -24,7 +23,7 @@ public class RegenerationMultiplier extends Effect { Player player = (Player) event.getEntity(); - Double multiplier = ArmorUtils.getEffectStrength(player, this); + Double multiplier = this.getStrengthForPlayer(player); if (multiplier == null) { return; diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/SpeedMultiplier.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/SpeedMultiplier.java new file mode 100644 index 0000000..d8b3b90 --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/SpeedMultiplier.java @@ -0,0 +1,39 @@ +package com.willfp.ecoarmor.effects.effects; + +import com.willfp.ecoarmor.effects.Effect; +import org.bukkit.attribute.Attribute; +import org.bukkit.attribute.AttributeInstance; +import org.bukkit.attribute.AttributeModifier; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +public class SpeedMultiplier extends Effect { + public SpeedMultiplier() { + super("speed-multiplier", Double.class); + } + + @Override + protected void onEnable(@NotNull final Player player) { + AttributeInstance movementSpeed = player.getAttribute(Attribute.GENERIC_MOVEMENT_SPEED); + assert movementSpeed != null; + + Double strength = this.getStrengthForPlayer(player); + + if (strength == null) { + return; + } + + AttributeModifier modifier = new AttributeModifier(this.getUuid(), "speed-multiplier", strength - 1, AttributeModifier.Operation.MULTIPLY_SCALAR_1); + if (movementSpeed.getModifiers().stream().noneMatch(attributeModifier -> attributeModifier.getUniqueId().equals(modifier.getUniqueId()))) { + movementSpeed.addModifier(modifier); + } + } + + @Override + protected void onDisable(@NotNull final Player player) { + AttributeInstance movementSpeed = player.getAttribute(Attribute.GENERIC_MOVEMENT_SPEED); + assert movementSpeed != null; + + movementSpeed.removeModifier(new AttributeModifier(this.getUuid(), "speed-multiplier", 0, AttributeModifier.Operation.MULTIPLY_SCALAR_1)); + } +} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/SpeedMutiplier.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/SpeedMutiplier.java deleted file mode 100644 index 1a3a162..0000000 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/SpeedMutiplier.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.willfp.ecoarmor.effects.effects; - -import com.willfp.eco.util.events.armorequip.ArmorEquipEvent; -import com.willfp.ecoarmor.effects.Effect; -import com.willfp.ecoarmor.sets.util.ArmorUtils; -import org.bukkit.attribute.Attribute; -import org.bukkit.attribute.AttributeInstance; -import org.bukkit.attribute.AttributeModifier; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.jetbrains.annotations.NotNull; - -public class SpeedMutiplier extends Effect { - public SpeedMutiplier() { - super("speed-multiplier", Double.class); - } - - @EventHandler - public void listener(@NotNull final ArmorEquipEvent event) { - Player player = event.getPlayer(); - - AttributeInstance movementSpeed = player.getAttribute(Attribute.GENERIC_MOVEMENT_SPEED); - assert movementSpeed != null; - - this.getPlugin().getScheduler().runLater(() -> { - Double multiplier = ArmorUtils.getEffectStrength(player, this); - if (multiplier == null) { - movementSpeed.removeModifier(new AttributeModifier(this.getUuid(), "speed-multiplier", 0, AttributeModifier.Operation.MULTIPLY_SCALAR_1)); - } else { - AttributeModifier modifier = new AttributeModifier(this.getUuid(), "speed-multiplier", multiplier - 1, AttributeModifier.Operation.MULTIPLY_SCALAR_1); - if (!movementSpeed.getModifiers().contains(modifier)) { - movementSpeed.addModifier(modifier); - } - } - }, 1); - } -} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/TridentDamageMultiplier.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/TridentDamageMultiplier.java index 4703c4b..5a272a7 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/TridentDamageMultiplier.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/TridentDamageMultiplier.java @@ -1,7 +1,6 @@ package com.willfp.ecoarmor.effects.effects; import com.willfp.ecoarmor.effects.Effect; -import com.willfp.ecoarmor.sets.util.ArmorUtils; import org.bukkit.entity.Player; import org.bukkit.entity.Projectile; import org.bukkit.entity.Trident; @@ -33,7 +32,8 @@ public class TridentDamageMultiplier extends Effect { Player player = (Player) shooter; - Double multiplier = ArmorUtils.getEffectStrength(player, this); + Double multiplier = this.getStrengthForPlayer(player); + if (multiplier == null) { return; } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/WarpChance.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/WarpChance.java index e11bd2b..bf06ecb 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/WarpChance.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/effects/WarpChance.java @@ -2,7 +2,6 @@ package com.willfp.ecoarmor.effects.effects; import com.willfp.eco.util.NumberUtils; import com.willfp.ecoarmor.effects.Effect; -import com.willfp.ecoarmor.sets.util.ArmorUtils; import org.bukkit.Location; import org.bukkit.block.Block; import org.bukkit.entity.LivingEntity; @@ -34,11 +33,7 @@ public class WarpChance extends Effect { Player player = (Player) event.getDamager(); LivingEntity victim = (LivingEntity) event.getEntity(); - if (!ArmorUtils.hasEffect(player, this)) { - return; - } - - Double chance = ArmorUtils.getEffectStrength(player, this); + Double chance = this.getStrengthForPlayer(player); if (chance == null) { return; diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/util/EffectWatcher.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/util/EffectWatcher.java new file mode 100644 index 0000000..b578425 --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/util/EffectWatcher.java @@ -0,0 +1,70 @@ +package com.willfp.ecoarmor.effects.util; + +import com.willfp.eco.util.events.armorequip.ArmorEquipEvent; +import com.willfp.eco.util.internal.PluginDependent; +import com.willfp.eco.util.plugin.AbstractEcoPlugin; +import com.willfp.ecoarmor.effects.Effect; +import com.willfp.ecoarmor.effects.Effects; +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.Listener; +import org.jetbrains.annotations.NotNull; + +public class EffectWatcher extends PluginDependent implements Listener { + /** + * Pass an {@link AbstractEcoPlugin} in order to interface with it. + * + * @param plugin The plugin to manage. + */ + public EffectWatcher(@NotNull final AbstractEcoPlugin plugin) { + super(plugin); + } + + /** + * Listener for armor equipping. + * + * @param event The event to listen for. + */ + @EventHandler + public void armorEquipListener(@NotNull final ArmorEquipEvent event) { + Player player = event.getPlayer(); + + this.getPlugin().getScheduler().runLater(() -> { + ArmorSet set = ArmorUtils.getSetOnPlayer(player); + + for (Effect effect : Effects.values()) { + boolean enabled = true; + + if (set == null) { + effect.disable(player); + continue; + } + + Object strength = set.getEffectStrength(effect); + + if (ArmorUtils.isWearingAdvanced(player)) { + Object advancedStrength = set.getAdvancedEffectStrength(effect); + if (advancedStrength != null) { + strength = advancedStrength; + } + } + + if (strength == null) { + enabled = false; + } + + if (!ArmorUtils.areConditionsMet(player)) { + enabled = false; + } + + if (enabled) { + effect.enable(player, strength); + } else { + effect.disable(player); + } + } + }, 1); + } +} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/sets/ArmorSet.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/sets/ArmorSet.java index 652d717..ffc0519 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/sets/ArmorSet.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/sets/ArmorSet.java @@ -8,6 +8,8 @@ import com.willfp.eco.util.recipe.RecipeParts; import com.willfp.eco.util.recipe.parts.ComplexRecipePart; import com.willfp.eco.util.recipe.recipes.EcoShapedRecipe; import com.willfp.ecoarmor.EcoArmorPlugin; +import com.willfp.ecoarmor.conditions.Condition; +import com.willfp.ecoarmor.conditions.Conditions; import com.willfp.ecoarmor.effects.Effect; import com.willfp.ecoarmor.effects.Effects; import com.willfp.ecoarmor.sets.meta.ArmorSlot; @@ -55,6 +57,12 @@ public class ArmorSet { @Getter(AccessLevel.PRIVATE) private final AbstractUndefinedConfig config; + /** + * Conditions and their values. + */ + @Getter + private final Map, Object> conditions = new HashMap<>(); + /** * Effects and their strengths. */ @@ -115,6 +123,14 @@ public class ArmorSet { this.config = config; this.name = name; + for (String definedKey : this.getConfig().getStrings("conditions")) { + String[] split = definedKey.split(":"); + String key = split[0].trim(); + String value = split[1].trim(); + Condition condition = Conditions.getByName(key); + conditions.put(condition, ArmorUtils.getConditionValue(value, condition)); + } + for (String definedKey : this.getConfig().getStrings("set-bonus")) { String[] split = definedKey.split(":"); String key = split[0].trim(); @@ -324,6 +340,18 @@ public class ArmorSet { return advancedItems.get(slot); } + /** + * Get condition value of effect. + * + * @param condition The condition to query. + * @param The type of the condition value. + * @return The value. + */ + @Nullable + public T getConditionValue(@NotNull final Condition condition) { + return (T) conditions.get(condition); + } + /** * Get effect strength of effect. * diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/sets/util/ArmorUtils.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/sets/util/ArmorUtils.java index a802f56..06029f2 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/sets/util/ArmorUtils.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/sets/util/ArmorUtils.java @@ -1,6 +1,7 @@ package com.willfp.ecoarmor.sets.util; import com.willfp.ecoarmor.EcoArmorPlugin; +import com.willfp.ecoarmor.conditions.Condition; import com.willfp.ecoarmor.effects.Effect; import com.willfp.ecoarmor.sets.ArmorSet; import com.willfp.ecoarmor.sets.ArmorSets; @@ -20,6 +21,7 @@ import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.UUID; @UtilityClass @@ -97,40 +99,24 @@ public class ArmorUtils { } /** - * Get the strength of an effect on a player's set. + * Get if all conditions are met for a player. * - * @param player The player to test. - * @param effect The effect to test. - * @param Effect type. - * @return The strength, or null if not found. + * @param player The player. + * @return If conditions are men. */ - @Nullable - public T getEffectStrength(@NotNull final Player player, - @NotNull final Effect effect) { + public boolean areConditionsMet(@NotNull final Player player) { ArmorSet set = getSetOnPlayer(player); if (set == null) { - return null; + return true; } - T strength = set.getEffectStrength(effect); - - if (isWearingAdvanced(player)) { - strength = set.getAdvancedEffectStrength(effect); + for (Map.Entry, Object> entry : set.getConditions().entrySet()) { + if (!entry.getKey().isMet(player, entry.getValue())) { + return false; + } } - return strength; - } - - /** - * If a player has an active effect. - * - * @param player The player to test. - * @param effect The effect to test. - * @return If a player has an active effect. - */ - public boolean hasEffect(@NotNull final Player player, - @NotNull final Effect effect) { - return getEffectStrength(player, effect) != null; + return true; } /** @@ -184,7 +170,7 @@ public class ArmorUtils { * Set tier on item. * * @param itemStack The item to check. - * @param tier The tier to set. + * @param tier The tier to set. */ public static void setTier(@NotNull final ItemStack itemStack, @NotNull final Tier tier) { @@ -382,4 +368,30 @@ public class ArmorUtils { return string; } + + /** + * Get value of condition. + * + * @param string Value as string. + * @param condition Condition. + * @param The type of the condition. + * @return Value. + */ + @NotNull + public static Object getConditionValue(@NotNull final String string, + @NotNull final Condition condition) { + if (condition.getTypeClass().equals(Boolean.class)) { + return Boolean.parseBoolean(string); + } + + if (condition.getTypeClass().equals(Integer.class)) { + return Integer.parseInt(string); + } + + if (condition.getTypeClass().equals(Double.class)) { + return Double.parseDouble(string); + } + + return string; + } } diff --git a/eco-core/core-plugin/src/main/resources/sets/ender.yml b/eco-core/core-plugin/src/main/resources/sets/ender.yml index 8ef9a22..cac7f15 100644 --- a/eco-core/core-plugin/src/main/resources/sets/ender.yml +++ b/eco-core/core-plugin/src/main/resources/sets/ender.yml @@ -1,4 +1,5 @@ enabled: true +conditions: [] set-bonus: - "warp-chance: 20" - "evade-chance: 10" diff --git a/eco-core/core-plugin/src/main/resources/sets/miner.yml b/eco-core/core-plugin/src/main/resources/sets/miner.yml index 62c5436..ca661b1 100644 --- a/eco-core/core-plugin/src/main/resources/sets/miner.yml +++ b/eco-core/core-plugin/src/main/resources/sets/miner.yml @@ -1,4 +1,5 @@ enabled: true +conditions: [] set-bonus: - "experience-multiplier: 1.25" advanced-set-bonus: diff --git a/eco-core/core-plugin/src/main/resources/sets/reaper.yml b/eco-core/core-plugin/src/main/resources/sets/reaper.yml index 651a937..a293644 100644 --- a/eco-core/core-plugin/src/main/resources/sets/reaper.yml +++ b/eco-core/core-plugin/src/main/resources/sets/reaper.yml @@ -1,4 +1,5 @@ enabled: true +conditions: [] set-bonus: - "damage-multiplier: 1.25" advanced-set-bonus: diff --git a/eco-core/core-plugin/src/main/resources/sets/young.yml b/eco-core/core-plugin/src/main/resources/sets/young.yml index 16d7c26..56b123e 100644 --- a/eco-core/core-plugin/src/main/resources/sets/young.yml +++ b/eco-core/core-plugin/src/main/resources/sets/young.yml @@ -1,4 +1,6 @@ enabled: true +conditions: + - "above-health-percent: 50" set-bonus: - "speed-multiplier: 1.25" advanced-set-bonus: @@ -10,7 +12,7 @@ advanced-lore: - "&lADVANCED BONUS" - "&8» &bPermanent Jump Boost II" - "&8» &bMove 50% faster" - - "&8&oRequires full set to be worn" + - "&8&oRequires full set to be worn above 50% health" advancement-shard-name: "Advancement Shard: &bYoung" advancement-shard-lore: - "&8Drop this onto &bYoung Armor" @@ -43,7 +45,7 @@ helmet: lore: - "&b&lYOUNG SET BONUS" - "&8» &bMove 25% faster" - - "&8&oRequires full set to be worn" + - "&8&oRequires full set to be worn above 50% health" - "" - "&fTier: %tier%" - "&8&oUpgrade with an Upgrade Crystal" @@ -74,7 +76,7 @@ chestplate: lore: - "&b&lYOUNG SET BONUS" - "&8» &bMove 25% faster" - - "&8&oRequires full set to be worn" + - "&8&oRequires full set to be worn above 50% health" - "" - "&fTier: %tier%" - "&8&oUpgrade with an Upgrade Crystal" @@ -103,7 +105,7 @@ elytra: lore: - "&b&lYOUNG SET BONUS" - "&8» &bMove 25% faster" - - "&8&oRequires full set to be worn" + - "&8&oRequires full set to be worn above 50% health" - "" - "&fTier: %tier%" - "&8&oUpgrade with an Upgrade Crystal" @@ -134,7 +136,7 @@ leggings: lore: - "&b&lYOUNG SET BONUS" - "&8» &bMove 25% faster" - - "&8&oRequires full set to be worn" + - "&8&oRequires full set to be worn above 50% health" - "" - "&fTier: %tier%" - "&8&oUpgrade with an Upgrade Crystal" @@ -166,7 +168,7 @@ boots: lore: - "&b&lYOUNG SET BONUS" - "&8» &bMove 25% faster" - - "&8&oRequires full set to be worn" + - "&8&oRequires full set to be worn above 50% health" - "" - "&fTier: %tier%" - "&8&oUpgrade with an Upgrade Crystal"