diff --git a/core/src/main/java/com/songoda/epicenchants/effect/EffectExecutor.java b/core/src/main/java/com/songoda/epicenchants/effect/EffectExecutor.java index 94a97e2..a87e35b 100644 --- a/core/src/main/java/com/songoda/epicenchants/effect/EffectExecutor.java +++ b/core/src/main/java/com/songoda/epicenchants/effect/EffectExecutor.java @@ -13,6 +13,7 @@ import org.bukkit.event.Event; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.Set; import java.util.function.Consumer; import static com.songoda.epicenchants.effect.EffectExecutor.Who.OPPONENT; @@ -20,21 +21,21 @@ import static com.songoda.epicenchants.effect.EffectExecutor.Who.USER; public abstract class EffectExecutor { @Getter private final ConfigurationSection section; - @Getter private final TriggerType triggerType; + @Getter private final Set triggerTypes; private final Condition condition; public EffectExecutor(ConfigurationSection section) { this.section = section; - this.triggerType = TriggerType.valueOf(section.getString("trigger")); + this.triggerTypes = GeneralUtils.parseTrigger(section.getString("trigger")); this.condition = Condition.of(section.getString("condition")); } public void testAndRun(@NotNull Player user, @Nullable LivingEntity opponent, int level, TriggerType type, Event event, EventType eventType) { - if (type != triggerType) { + if (triggerTypes.contains(type)) { return; } - if (section.isString("chance") && !GeneralUtils.chance(LeveledModifier.of(section.getString("chance")).get(level, 100))) { + if (section.isString("chance") && !GeneralUtils.chance(LeveledModifier.of(section.getString("chance")).get(level, 100, user, opponent))) { return; } @@ -65,7 +66,7 @@ public abstract class EffectExecutor { } public void consume(Consumer playerConsumer, Player user, @Nullable LivingEntity opponent) { - if (triggerType == TriggerType.HELD_ITEM || triggerType == TriggerType.STATIC_EFFECT) { + if (triggerTypes.contains(TriggerType.HELD_ITEM) || triggerTypes.contains(TriggerType.STATIC_EFFECT)) { playerConsumer.accept(user); return; } diff --git a/core/src/main/java/com/songoda/epicenchants/effect/effects/Burn.java b/core/src/main/java/com/songoda/epicenchants/effect/effects/Burn.java index 5c4f985..2aa3184 100644 --- a/core/src/main/java/com/songoda/epicenchants/effect/effects/Burn.java +++ b/core/src/main/java/com/songoda/epicenchants/effect/effects/Burn.java @@ -16,6 +16,6 @@ public class Burn extends EffectExecutor { @Override public void execute(@NotNull Player user, @Nullable LivingEntity opponent, int level, EventType eventType) { - consume(entity -> entity.setFireTicks((int) LeveledModifier.of(getSection().getString("time")).get(level, 20)), user, opponent); + consume(entity -> entity.setFireTicks((int) LeveledModifier.of(getSection().getString("time")).get(level, 20, user, opponent)), user, opponent); } } diff --git a/core/src/main/java/com/songoda/epicenchants/effect/effects/ConsoleCommand.java b/core/src/main/java/com/songoda/epicenchants/effect/effects/ConsoleCommand.java index 5b6875d..b995d50 100644 --- a/core/src/main/java/com/songoda/epicenchants/effect/effects/ConsoleCommand.java +++ b/core/src/main/java/com/songoda/epicenchants/effect/effects/ConsoleCommand.java @@ -2,6 +2,7 @@ package com.songoda.epicenchants.effect.effects; import com.songoda.epicenchants.effect.EffectExecutor; import com.songoda.epicenchants.enums.EventType; +import com.songoda.epicenchants.managers.HookManager; import org.bukkit.Bukkit; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.LivingEntity; @@ -15,9 +16,7 @@ public class ConsoleCommand extends EffectExecutor { @Override public void execute(@NotNull Player user, LivingEntity opponent, int level, EventType eventType) { - Bukkit.dispatchCommand(Bukkit.getConsoleSender(), getSection().getString("command") - .replace("{level}", "" + level) - .replace("{user}", user.getName()) - .replace("{opponent}", opponent == null ? "" : opponent.getName())); + Bukkit.dispatchCommand(Bukkit.getConsoleSender(), + HookManager.setPAPIPlaceholders(getSection().getString("command"), user, opponent, level)); } } diff --git a/core/src/main/java/com/songoda/epicenchants/effect/effects/Message.java b/core/src/main/java/com/songoda/epicenchants/effect/effects/Message.java index 9498e08..a611650 100644 --- a/core/src/main/java/com/songoda/epicenchants/effect/effects/Message.java +++ b/core/src/main/java/com/songoda/epicenchants/effect/effects/Message.java @@ -2,6 +2,7 @@ package com.songoda.epicenchants.effect.effects; import com.songoda.epicenchants.effect.EffectExecutor; import com.songoda.epicenchants.enums.EventType; +import com.songoda.epicenchants.managers.HookManager; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; @@ -17,9 +18,6 @@ public class Message extends EffectExecutor { @Override public void execute(@NotNull Player user, LivingEntity opponent, int level, EventType eventType) { if (eventType == EventType.ON || eventType == EventType.NONE) - consume(entity -> entity.sendMessage(color(getSection().getString("message")) - .replace("{level}", "" + level) - .replace("{user}", user.getName()) - .replace("{opponent}", opponent == null ? "" : opponent.getName())), user, opponent); + consume(entity -> entity.sendMessage(color(HookManager.setPAPIPlaceholders(getSection().getString("message"), user, opponent, level))), user, opponent); } } diff --git a/core/src/main/java/com/songoda/epicenchants/effect/effects/ModifyDamage.java b/core/src/main/java/com/songoda/epicenchants/effect/effects/ModifyDamage.java index 0a1e8d2..bc3523a 100644 --- a/core/src/main/java/com/songoda/epicenchants/effect/effects/ModifyDamage.java +++ b/core/src/main/java/com/songoda/epicenchants/effect/effects/ModifyDamage.java @@ -20,6 +20,6 @@ public class ModifyDamage extends EffectEventExecutor { return; } - ((EntityDamageEvent) event).setDamage(LeveledModifier.of(getSection().getString("modifier")).get(level, 1)); + ((EntityDamageEvent) event).setDamage(LeveledModifier.of(getSection().getString("modifier")).get(level, 1, user, opponent)); } } diff --git a/core/src/main/java/com/songoda/epicenchants/effect/effects/ModifyExp.java b/core/src/main/java/com/songoda/epicenchants/effect/effects/ModifyExp.java index a132dad..2aae644 100644 --- a/core/src/main/java/com/songoda/epicenchants/effect/effects/ModifyExp.java +++ b/core/src/main/java/com/songoda/epicenchants/effect/effects/ModifyExp.java @@ -21,9 +21,9 @@ public class ModifyExp extends EffectExecutor { if (entity instanceof Player) { Player player = (Player) entity; if (getSection().getString("amount").endsWith("L")) { - player.setLevel((int) (player.getLevel() + LeveledModifier.of(getSection().getString("amount").replace("L", "")).get(level, 0))); + player.setLevel((int) (player.getLevel() + LeveledModifier.of(getSection().getString("amount").replace("L", "")).get(level, 0, user, opponent))); } else { - Experience.changeExp(player, (int) getAmount().get(level, 0)); + Experience.changeExp(player, (int) getAmount().get(level, 0, user, opponent)); } } }, user, opponent); diff --git a/core/src/main/java/com/songoda/epicenchants/effect/effects/ModifyFood.java b/core/src/main/java/com/songoda/epicenchants/effect/effects/ModifyFood.java index 445d838..b01a990 100644 --- a/core/src/main/java/com/songoda/epicenchants/effect/effects/ModifyFood.java +++ b/core/src/main/java/com/songoda/epicenchants/effect/effects/ModifyFood.java @@ -17,7 +17,7 @@ public class ModifyFood extends EffectExecutor { public void execute(@NotNull Player user, @Nullable LivingEntity opponent, int level, EventType eventType) { consume(entity -> { if (entity instanceof Player) { - ((Player) entity).setFoodLevel((int) (((Player) entity).getFoodLevel() + getAmount().get(level, 0))); + ((Player) entity).setFoodLevel((int) (((Player) entity).getFoodLevel() + getAmount().get(level, 0, user, opponent))); } }, user, opponent); } diff --git a/core/src/main/java/com/songoda/epicenchants/effect/effects/ModifyHealth.java b/core/src/main/java/com/songoda/epicenchants/effect/effects/ModifyHealth.java index 09881a5..21040a8 100644 --- a/core/src/main/java/com/songoda/epicenchants/effect/effects/ModifyHealth.java +++ b/core/src/main/java/com/songoda/epicenchants/effect/effects/ModifyHealth.java @@ -16,7 +16,7 @@ public class ModifyHealth extends EffectExecutor { @Override public void execute(@NotNull Player user, LivingEntity opponent, int level, EventType eventType) { consume(entity -> { - double amount = getAmount().get(level, 0); + double amount = getAmount().get(level, 0, user, opponent); if (entity.getHealth() + amount > entity.getMaxHealth()) { entity.setHealth(entity.getMaxHealth()); } else if (entity.getHealth() + amount < 0) { diff --git a/core/src/main/java/com/songoda/epicenchants/effect/effects/ModifyOxygen.java b/core/src/main/java/com/songoda/epicenchants/effect/effects/ModifyOxygen.java new file mode 100644 index 0000000..6f410ad --- /dev/null +++ b/core/src/main/java/com/songoda/epicenchants/effect/effects/ModifyOxygen.java @@ -0,0 +1,28 @@ +package com.songoda.epicenchants.effect.effects; + +import com.songoda.epicenchants.effect.EffectEventExecutor; +import com.songoda.epicenchants.enums.EventType; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; +import org.bukkit.event.Event; + +public class ModifyOxygen extends EffectEventExecutor { + public ModifyOxygen(ConfigurationSection section) { + super(section); + } + + @Override + public void execute(Player user, LivingEntity opponent, int level, Event event, EventType eventType) { + consume(entity -> { + double amount = getAmount().get(level, 0, user, opponent); + if (entity.getRemainingAir() + amount > entity.getMaximumAir()) { + entity.setRemainingAir(entity.getMaximumAir()); + } else if (entity.getRemainingAir() + amount < 0) { + entity.setRemainingAir(0); + } else { + entity.setRemainingAir(entity.getRemainingAir() + (int) amount); + } + }, user, opponent); + } +} diff --git a/core/src/main/java/com/songoda/epicenchants/effect/effects/MoreDrops.java b/core/src/main/java/com/songoda/epicenchants/effect/effects/MoreDrops.java index 2f4af1b..f8e6a00 100644 --- a/core/src/main/java/com/songoda/epicenchants/effect/effects/MoreDrops.java +++ b/core/src/main/java/com/songoda/epicenchants/effect/effects/MoreDrops.java @@ -19,7 +19,7 @@ public class MoreDrops extends EffectEventExecutor { } @Override - public void execute(Player player, LivingEntity opponent, int level, Event event, EventType type) { + public void execute(Player user, LivingEntity opponent, int level, Event event, EventType type) { if (!(event instanceof EntityDeathEvent)) { return; } @@ -27,7 +27,7 @@ public class MoreDrops extends EffectEventExecutor { EntityDeathEvent deathEvent = (EntityDeathEvent) event; LeveledModifier modifier = getAmount(); List newDrops = deathEvent.getDrops().stream() - .peek(itemStack -> itemStack.setAmount(((int) (itemStack.getAmount() * modifier.get(level, 1))))) + .peek(itemStack -> itemStack.setAmount(((int) (itemStack.getAmount() * modifier.get(level, 1, user, opponent))))) .collect(Collectors.toList()); deathEvent.getDrops().clear(); diff --git a/core/src/main/java/com/songoda/epicenchants/effect/effects/PlayerCommand.java b/core/src/main/java/com/songoda/epicenchants/effect/effects/PlayerCommand.java index 7ef7fb0..d0cfc9d 100644 --- a/core/src/main/java/com/songoda/epicenchants/effect/effects/PlayerCommand.java +++ b/core/src/main/java/com/songoda/epicenchants/effect/effects/PlayerCommand.java @@ -2,6 +2,7 @@ package com.songoda.epicenchants.effect.effects; import com.songoda.epicenchants.effect.EffectExecutor; import com.songoda.epicenchants.enums.EventType; +import com.songoda.epicenchants.managers.HookManager; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; @@ -18,16 +19,15 @@ public class PlayerCommand extends EffectExecutor { @Override public void execute(@NotNull Player user, LivingEntity opponent, int level, EventType eventType) { - if (eventType == ON || eventType == NONE) { - if (who() == OPPONENT && !(opponent instanceof Player)) { - return; - } - - consume(entity -> ((Player) entity).performCommand(getSection().getString("command") - .replace("{level}", "" + level) - .replace("{user}", user.getName()) - .replace("{opponent}", opponent.getName())), user, opponent); + if (eventType != ON && eventType != NONE) { + return; } + if (who() == OPPONENT && !(opponent instanceof Player)) { + return; + } + + consume(entity -> ((Player) entity).performCommand(HookManager.setPAPIPlaceholders(getSection().getString("command"), user, opponent, level)), user, opponent); + } } diff --git a/core/src/main/java/com/songoda/epicenchants/effect/effects/Potion.java b/core/src/main/java/com/songoda/epicenchants/effect/effects/Potion.java index 0358666..5dc48ee 100644 --- a/core/src/main/java/com/songoda/epicenchants/effect/effects/Potion.java +++ b/core/src/main/java/com/songoda/epicenchants/effect/effects/Potion.java @@ -29,9 +29,9 @@ public class Potion extends EffectExecutor { return; } - if (this.getTriggerType() == TriggerType.STATIC_EFFECT || this.getTriggerType() == TriggerType.HELD_ITEM) { + if (this.getTriggerTypes() == TriggerType.STATIC_EFFECT || this.getTriggerTypes() == TriggerType.HELD_ITEM) { if (eventType == EventType.ON) { - consume(entity -> entity.addPotionEffect(new PotionEffect(effectType, Integer.MAX_VALUE, ((int) amplifier.get(level, 0)), + consume(entity -> entity.addPotionEffect(new PotionEffect(effectType, Integer.MAX_VALUE, (int) amplifier.get(level - 1, 0, user, opponent), false, false)), user, opponent); } else if (eventType == EventType.OFF) { consume(entity -> entity.removePotionEffect(effectType), user, opponent); @@ -41,8 +41,8 @@ public class Potion extends EffectExecutor { LeveledModifier duration = LeveledModifier.of(getSection().getString("duration")); - consume(entity -> entity.addPotionEffect(new PotionEffect(effectType, ((int) duration.get(level, 60)), - ((int) amplifier.get(level, 0)), false, false)), user, opponent); + consume(entity -> entity.addPotionEffect(new PotionEffect(effectType, ((int) duration.get(level, 60, user, opponent)), + ((int) amplifier.get(level - 1, 0, user, opponent)), false, false)), user, opponent); } } diff --git a/core/src/main/java/com/songoda/epicenchants/effect/effects/StealExp.java b/core/src/main/java/com/songoda/epicenchants/effect/effects/StealExp.java index f8ee739..05da780 100644 --- a/core/src/main/java/com/songoda/epicenchants/effect/effects/StealExp.java +++ b/core/src/main/java/com/songoda/epicenchants/effect/effects/StealExp.java @@ -17,20 +17,20 @@ public class StealExp extends EffectExecutor { } @Override - public void execute(@NotNull Player user, @Nullable LivingEntity entity, int level, EventType eventType) { - double amount = getAmount().get(level, 0); + public void execute(@NotNull Player user, @Nullable LivingEntity opponent, int level, EventType eventType) { + double amount = getAmount().get(level, 0, user, opponent); - if (!(entity instanceof Player)) { + if (!(opponent instanceof Player)) { return; } - Player opponent = (Player) entity; + Player player = (Player) opponent; - if (getExp(opponent) - amount <= 0) { - changeExp(opponent, 0); + if (getExp(player) - amount <= 0) { + changeExp(player, 0); } else { - changeExp(opponent, (int) -amount); + changeExp(player, (int) -amount); } if (getExp(user) + amount <= 0) { diff --git a/core/src/main/java/com/songoda/epicenchants/effect/effects/StealHealth.java b/core/src/main/java/com/songoda/epicenchants/effect/effects/StealHealth.java index 63ba68f..ad41e34 100644 --- a/core/src/main/java/com/songoda/epicenchants/effect/effects/StealHealth.java +++ b/core/src/main/java/com/songoda/epicenchants/effect/effects/StealHealth.java @@ -14,7 +14,7 @@ public class StealHealth extends EffectExecutor { @Override public void execute(@NotNull Player user, LivingEntity opponent, int level, EventType eventType) { - double amount = getAmount().get(level, 0); + double amount = getAmount().get(level, 0, user, opponent); if (opponent == null) { return; diff --git a/core/src/main/java/com/songoda/epicenchants/effect/effects/Throw.java b/core/src/main/java/com/songoda/epicenchants/effect/effects/Throw.java index 10afb2b..3799d1d 100644 --- a/core/src/main/java/com/songoda/epicenchants/effect/effects/Throw.java +++ b/core/src/main/java/com/songoda/epicenchants/effect/effects/Throw.java @@ -27,7 +27,7 @@ public class Throw extends EffectExecutor { } Vector vector; - double magnitude = LeveledModifier.of(getSection().getString("magnitude")).get(level, 0.1); + double magnitude = LeveledModifier.of(getSection().getString("magnitude")).get(level, 0.1, user, opponent); LivingEntity livingEntity = who() == USER ? user : opponent; LivingEntity relative = getSection().getString("relative-to").equalsIgnoreCase("opponent") ? opponent : user; diff --git a/core/src/main/java/com/songoda/epicenchants/effect/effects/Tnt.java b/core/src/main/java/com/songoda/epicenchants/effect/effects/Tnt.java index bc7aee7..9bef7b7 100644 --- a/core/src/main/java/com/songoda/epicenchants/effect/effects/Tnt.java +++ b/core/src/main/java/com/songoda/epicenchants/effect/effects/Tnt.java @@ -18,9 +18,9 @@ public class Tnt extends EffectExecutor { @Override public void execute(@NotNull Player user, LivingEntity opponent, int level, EventType eventType) { consume(player -> { - for (int i = 0; i < LeveledModifier.of(getSection().getString("amount")).get(level, 1); i++) { + for (int i = 0; i < LeveledModifier.of(getSection().getString("amount")).get(level, 1, user, opponent); i++) { TNTPrimed tntPrimed = (TNTPrimed) player.getWorld().spawnEntity(player.getLocation(), EntityType.PRIMED_TNT); - tntPrimed.setFuseTicks((int) LeveledModifier.of(getSection().getString("fuse")).get(level, 60)); + tntPrimed.setFuseTicks((int) LeveledModifier.of(getSection().getString("fuse")).get(level, 60, user, opponent)); } }, user, opponent); } diff --git a/core/src/main/java/com/songoda/epicenchants/managers/HookManager.java b/core/src/main/java/com/songoda/epicenchants/managers/HookManager.java index 2757061..0a119d4 100644 --- a/core/src/main/java/com/songoda/epicenchants/managers/HookManager.java +++ b/core/src/main/java/com/songoda/epicenchants/managers/HookManager.java @@ -1,9 +1,14 @@ package com.songoda.epicenchants.managers; +import com.songoda.epicenchants.objects.Placeholder; import com.songoda.ultimatebottles.UltimateBottles; import lombok.Getter; +import me.clip.placeholderapi.PlaceholderAPI; import org.bukkit.Bukkit; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; +import java.util.Arrays; import java.util.Optional; @Getter @@ -17,4 +22,32 @@ public class HookManager { public Optional getUltimateBottles() { return Optional.ofNullable(ultimateBottles); } + + public static String setPAPIPlaceholders(String input, Player user, LivingEntity opponent, int level) { + for (Placeholder pair : Arrays.asList( + Placeholder.of("level", level), + Placeholder.of("user_health", user.getHealth()), + Placeholder.of("attacker_health", opponent == null ? -1 : opponent.getHealth()), + Placeholder.of("user_food", user.getFoodLevel()), + Placeholder.of("attacker_food", opponent instanceof Player ? ((Player) opponent).getFoodLevel() : 0), + Placeholder.of("user_is_sneaking", user.isSneaking()), + Placeholder.of("attacker_is_sneaking", opponent instanceof Player && ((Player) opponent).isSneaking()), + Placeholder.of("world", user.getWorld().getName()), + Placeholder.of("players_near", user.getNearbyEntities(4, 4, 4).size()), + Placeholder.of("user_on_fire", user.getFireTicks() != 0), + Placeholder.of("attacker_on_fire", opponent != null && opponent.getFireTicks() != 0) + )) { + input = input.replace(pair.getPlaceholder(), pair.getToReplace().toString()); + } + + if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) { + input = PlaceholderAPI.setPlaceholders(user, input); + + if (opponent instanceof Player) { + input = PlaceholderAPI.setRelationalPlaceholders(user, (Player) opponent, input); + } + } + + return input; + } } diff --git a/core/src/main/java/com/songoda/epicenchants/objects/Condition.java b/core/src/main/java/com/songoda/epicenchants/objects/Condition.java index f1576d3..8cbf5b5 100644 --- a/core/src/main/java/com/songoda/epicenchants/objects/Condition.java +++ b/core/src/main/java/com/songoda/epicenchants/objects/Condition.java @@ -1,6 +1,6 @@ package com.songoda.epicenchants.objects; -import me.clip.placeholderapi.PlaceholderAPI; +import com.songoda.epicenchants.managers.HookManager; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.entity.LivingEntity; @@ -10,7 +10,6 @@ import org.jetbrains.annotations.Nullable; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; -import java.util.Arrays; public class Condition { private final String string; @@ -29,33 +28,7 @@ public class Condition { } ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("JavaScript"); - String toValidate = string; - - for (Placeholder pair : Arrays.asList( - Placeholder.of("level", level), - Placeholder.of("user_health", user.getHealth()), - Placeholder.of("attacker_health", attacker == null ? -1 : attacker.getHealth()), - Placeholder.of("user_food", user.getFoodLevel()), - Placeholder.of("attacker_food", attacker instanceof Player ? ((Player) attacker).getFoodLevel() : 0), - Placeholder.of("user_is_sneaking", user.isSneaking()), - Placeholder.of("attacker_is_sneaking", attacker instanceof Player && ((Player) attacker).isSneaking()), - Placeholder.of("world", user.getWorld().getName()), - Placeholder.of("players_near", user.getNearbyEntities(4, 4, 4).size()), - Placeholder.of("user_on_fire", user.getFireTicks() != 0), - Placeholder.of("attacker_on_fire", attacker != null && attacker.getFireTicks() != 0) - )) { - toValidate = toValidate.replace(pair.getPlaceholder(), pair.getToReplace().toString()); - } - - if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) { - toValidate = PlaceholderAPI.setPlaceholders(user, toValidate); - - if (attacker instanceof Player) { - toValidate = PlaceholderAPI.setRelationalPlaceholders(user, (Player) attacker, toValidate); - } - } - - toValidate = ChatColor.stripColor(toValidate); + String toValidate = ChatColor.stripColor(HookManager.setPAPIPlaceholders(string, user, attacker, level)); try { return Boolean.parseBoolean(scriptEngine.eval(toValidate).toString()); diff --git a/core/src/main/java/com/songoda/epicenchants/objects/LeveledModifier.java b/core/src/main/java/com/songoda/epicenchants/objects/LeveledModifier.java index 0fef6aa..2f5cda3 100644 --- a/core/src/main/java/com/songoda/epicenchants/objects/LeveledModifier.java +++ b/core/src/main/java/com/songoda/epicenchants/objects/LeveledModifier.java @@ -1,6 +1,9 @@ package com.songoda.epicenchants.objects; +import com.songoda.epicenchants.managers.HookManager; import org.bukkit.Bukkit; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; @@ -17,7 +20,7 @@ public class LeveledModifier { return new LeveledModifier(string); } - public double get(int level, double def) { + public double get(int level, double def, Player user, LivingEntity opponent) { if (string == null || string.isEmpty()) { return def; } @@ -27,8 +30,7 @@ public class LeveledModifier { } ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("JavaScript"); - - String toTest = string.replace("{level}", "" + level); + String toTest = HookManager.setPAPIPlaceholders(string, user, opponent, level); try { return Double.parseDouble(scriptEngine.eval(toTest).toString()); diff --git a/core/src/main/java/com/songoda/epicenchants/utils/single/GeneralUtils.java b/core/src/main/java/com/songoda/epicenchants/utils/single/GeneralUtils.java index a76404b..7a2d14e 100644 --- a/core/src/main/java/com/songoda/epicenchants/utils/single/GeneralUtils.java +++ b/core/src/main/java/com/songoda/epicenchants/utils/single/GeneralUtils.java @@ -1,6 +1,7 @@ package com.songoda.epicenchants.utils.single; import com.songoda.epicenchants.enums.EnchantResult; +import com.songoda.epicenchants.enums.TriggerType; import org.apache.commons.lang.StringUtils; import org.bukkit.ChatColor; @@ -49,4 +50,8 @@ public class GeneralUtils { public static List getSlotsList(String string) { return Arrays.stream(string.split(",")).filter(StringUtils::isNumeric).mapToInt(Integer::parseInt).boxed().collect(Collectors.toList()); } + + public static Set parseTrigger(String triggers) { + return Arrays.stream(triggers.replaceAll("\\s+", "").split(",")).map(TriggerType::valueOf).collect(Collectors.toSet()); + } } diff --git a/core/src/main/java/com/songoda/epicenchants/wrappers/EnchantmentWrapper.java b/core/src/main/java/com/songoda/epicenchants/wrappers/EnchantmentWrapper.java index d1c2e3b..be8f4e1 100644 --- a/core/src/main/java/com/songoda/epicenchants/wrappers/EnchantmentWrapper.java +++ b/core/src/main/java/com/songoda/epicenchants/wrappers/EnchantmentWrapper.java @@ -10,7 +10,7 @@ public class EnchantmentWrapper { private Enchantment enchantment; public int getAmplifier(int level) { - return (int) amplifier.get(level, 0); + return (int) amplifier.get(level, 0, user, opponent); } public Enchantment getEnchantment() { diff --git a/core/src/main/java/com/songoda/epicenchants/wrappers/MobWrapper.java b/core/src/main/java/com/songoda/epicenchants/wrappers/MobWrapper.java index 2310ddd..0f35fd0 100644 --- a/core/src/main/java/com/songoda/epicenchants/wrappers/MobWrapper.java +++ b/core/src/main/java/com/songoda/epicenchants/wrappers/MobWrapper.java @@ -4,7 +4,10 @@ import com.songoda.epicenchants.enums.TriggerType; import com.songoda.epicenchants.objects.LeveledModifier; import com.songoda.epicenchants.utils.objects.ItemBuilder; import com.songoda.epicenchants.utils.single.GeneralUtils; -import de.tr7zw.itemnbtapi.*; +import de.tr7zw.itemnbtapi.NBTEntity; +import de.tr7zw.itemnbtapi.NBTList; +import de.tr7zw.itemnbtapi.NBTListCompound; +import de.tr7zw.itemnbtapi.NBTType; import lombok.Builder; import org.bukkit.Location; import org.bukkit.entity.*; @@ -31,13 +34,13 @@ public class MobWrapper { return; } - if (!GeneralUtils.chance(spawnPercentage.get(level, 100))) { + if (!GeneralUtils.chance(spawnPercentage.get(level, 100, user, opponent))) { return; } Location location = player.getLocation(); - for (int i = 0; i < current().nextInt((int) (maxAmount.get(level, 1) + 1)); i++) { + for (int i = 0; i < current().nextInt((int) (maxAmount.get(level, 1, user, opponent) + 1)); i++) { Location spawnLocation = location.clone().add(current().nextInt(-3, 3), 0, current().nextInt(-3, 3)); int y = location.getWorld().getHighestBlockAt(spawnLocation).getY(); @@ -52,7 +55,7 @@ public class MobWrapper { if (entity instanceof LivingEntity) { LivingEntity livingEntity = (LivingEntity) entity; - int dropChance = (int) equipmentDropChance.get(level, 0); + int dropChance = (int) equipmentDropChance.get(level, 0, user, opponent); if (helmet != null) livingEntity.getEquipment().setHelmet(helmet.buildWithWrappers(level)); if (chestPlate != null) livingEntity.getEquipment().setChestplate(chestPlate.buildWithWrappers(level)); @@ -80,12 +83,12 @@ public class MobWrapper { for (int j = 0; j < list.size(); j++) { NBTListCompound lc = list.getCompound(j); if (lc.getString("Name").equals("generic.attackDamage")) { - lc.setDouble("Base", attackDamage.get(level, (int) lc.getDouble("Base"))); + lc.setDouble("Base", attackDamage.get(level, (int) lc.getDouble("Base"), user, opponent)); continue; } if (lc.getString("Name").equals("generic.maxHealth")) { - lc.setDouble("Base", health.get(level, (int) lc.getDouble("Base"))); + lc.setDouble("Base", health.get(level, (int) lc.getDouble("Base"), user, opponent)); } } }