From 5cc92b0bb30b7afbdbd77cf64e81cdc718048235 Mon Sep 17 00:00:00 2001 From: GB6 Date: Thu, 17 Jan 2019 17:11:59 +0100 Subject: [PATCH] Improvement to commands. --- core/pom.xml | 3 ++ .../songoda/epicenchants/EpicEnchants.java | 4 +- .../epicenchants/commands/EnchantCommand.java | 4 +- .../epicenchants/objects/ActionClass.java | 26 ++++++++++-- .../epicenchants/objects/LeveledModifier.java | 26 ++++++++++++ .../epicenchants/utils/ConfigParser.java | 41 +++++++++++-------- .../epicenchants/utils/ItemBuilder.java | 6 +++ .../epicenchants/utils/ValidationUtils.java | 28 ------------- .../wrappers/EnchantmentWrapper.java | 7 ++-- .../epicenchants/wrappers/MobWrapper.java | 19 +++++---- .../wrappers/PotionChanceWrapper.java | 15 +++---- .../wrappers/PotionEffectWrapper.java | 14 +++---- core/src/main/resources/StrengthEnchant.yml | 9 +++- 13 files changed, 118 insertions(+), 84 deletions(-) create mode 100644 core/src/main/java/com/songoda/epicenchants/objects/LeveledModifier.java delete mode 100644 core/src/main/java/com/songoda/epicenchants/utils/ValidationUtils.java diff --git a/core/pom.xml b/core/pom.xml index 44859b1..72e73d7 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -36,6 +36,9 @@ 1.8 1.8 + + -parameters + diff --git a/core/src/main/java/com/songoda/epicenchants/EpicEnchants.java b/core/src/main/java/com/songoda/epicenchants/EpicEnchants.java index f29fea9..aacafd8 100644 --- a/core/src/main/java/com/songoda/epicenchants/EpicEnchants.java +++ b/core/src/main/java/com/songoda/epicenchants/EpicEnchants.java @@ -67,10 +67,8 @@ public class EpicEnchants extends JavaPlugin { this.commandManager = new BukkitCommandManager(this); commandManager.registerDependency(EpicEnchants.class, "instance", this); - commandManager.getCommandCompletions().registerCompletion("enchants", c -> enchantManager.getEnchants().stream().map(Enchant::getIdentifier).collect(Collectors.toList())); - - commandManager.getCommandContexts().registerContext(Enchant.class, c -> enchantManager.getEnchant(c.getFirstArg()).orElseThrow(() -> new InvalidCommandArgument("Unknown enchant: " + c.getFirstArg()))); + commandManager.getCommandContexts().registerContext(Enchant.class, c -> enchantManager.getEnchant(c.popFirstArg()).orElseThrow(() -> new InvalidCommandArgument("No echant exists by that name"))); commandManager.registerCommand(new EnchantCommand()); } diff --git a/core/src/main/java/com/songoda/epicenchants/commands/EnchantCommand.java b/core/src/main/java/com/songoda/epicenchants/commands/EnchantCommand.java index 7381094..8bf7029 100644 --- a/core/src/main/java/com/songoda/epicenchants/commands/EnchantCommand.java +++ b/core/src/main/java/com/songoda/epicenchants/commands/EnchantCommand.java @@ -14,10 +14,10 @@ public class EnchantCommand extends BaseCommand { //ee give {player} {enchant} {tier} @Subcommand("give") - @CommandCompletion("@players @enchants") + @CommandCompletion("@players @enchants @nothing @nothing @nothing") @Description("Give books to players") @CommandPermission("epicenchants.givebook") - public void onGiveBook(@Flags("other") Player target, Enchant enchant, @Default("1") Integer tier, @Optional Double successRate, @Optional Double destroyRate) { + public void onGiveBook(@Flags("other") Player target, Enchant enchant, @Default("1") int tier, @Optional Double successRate, @Optional Double destroyRate) { target.getInventory().addItem(enchant.getBookItem().get(enchant, tier, successRate, destroyRate)); } diff --git a/core/src/main/java/com/songoda/epicenchants/objects/ActionClass.java b/core/src/main/java/com/songoda/epicenchants/objects/ActionClass.java index 2d927d6..4915faf 100644 --- a/core/src/main/java/com/songoda/epicenchants/objects/ActionClass.java +++ b/core/src/main/java/com/songoda/epicenchants/objects/ActionClass.java @@ -2,13 +2,33 @@ package com.songoda.epicenchants.objects; import com.songoda.epicenchants.wrappers.PotionChanceWrapper; import lombok.Builder; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.Set; +import static com.songoda.epicenchants.objects.ActionClass.DamageType.TAKEN; + @Builder public class ActionClass { - private Set potionEffects; - private double modifyDamageTaken; - private double modifyDamageGiven; + private Set potionEffectsWearer; + private Set potionEffectOpponent; + private LeveledModifier modifyDamageTaken; + private LeveledModifier modifyDamageGiven; + public double run(@NotNull Player wearer, @Nullable Player opponent, int level, double damage, DamageType damageType) { + potionEffectsWearer.stream().filter(p -> p.test(level)).forEach(p -> p.perform(wearer, level)); + + if (opponent != null) { + potionEffectOpponent.stream().filter(p -> p.test(level)).forEach(p -> p.perform(opponent, level)); + } + + + return damageType == TAKEN ? modifyDamageTaken.get(level) : modifyDamageGiven.get(level); + } + + public enum DamageType { + TAKEN, GIVEN + } } diff --git a/core/src/main/java/com/songoda/epicenchants/objects/LeveledModifier.java b/core/src/main/java/com/songoda/epicenchants/objects/LeveledModifier.java new file mode 100644 index 0000000..881675f --- /dev/null +++ b/core/src/main/java/com/songoda/epicenchants/objects/LeveledModifier.java @@ -0,0 +1,26 @@ +package com.songoda.epicenchants.objects; + +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; + +public class LeveledModifier { + private String string; + private static ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("JavaScript"); + + private LeveledModifier(String string) { + this.string = string; + } + + public static LeveledModifier of(String string) { + return new LeveledModifier(string); + } + + public double get(int level) { + try { + return Double.parseDouble(scriptEngine.eval(string.replaceAll("\\{level}", "" + level)).toString()); + } catch (ScriptException | NumberFormatException e) { + return 0; + } + } +} diff --git a/core/src/main/java/com/songoda/epicenchants/utils/ConfigParser.java b/core/src/main/java/com/songoda/epicenchants/utils/ConfigParser.java index b0853aa..ed1d6f9 100644 --- a/core/src/main/java/com/songoda/epicenchants/utils/ConfigParser.java +++ b/core/src/main/java/com/songoda/epicenchants/utils/ConfigParser.java @@ -2,12 +2,14 @@ package com.songoda.epicenchants.utils; import com.songoda.epicenchants.objects.ActionClass; import com.songoda.epicenchants.objects.BookItem; +import com.songoda.epicenchants.objects.LeveledModifier; import com.songoda.epicenchants.wrappers.*; import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.enchantments.Enchantment; import org.bukkit.potion.PotionEffectType; +import java.util.Set; import java.util.stream.Collectors; import static com.songoda.epicenchants.utils.Chat.color; @@ -15,52 +17,55 @@ import static com.songoda.epicenchants.utils.Chat.color; public class ConfigParser { public static ActionClass parseActionClass(ConfigurationSection section) { return ActionClass.builder() - .modifyDamageGiven(section.getDouble("modify-damage-given")) - .modifyDamageTaken(section.getDouble("modify-damage-taken")) - .potionEffects(section.getConfigurationSection("potion-effects") - .getKeys(false).stream() - .map(s -> "potion-effects." + s) - .map(section::getConfigurationSection) - .map(ConfigParser::parsePotionChanceEffect) - .collect(Collectors.toSet())) + .modifyDamageGiven(LeveledModifier.of(section.getString("modify-damage-given"))) + .modifyDamageTaken(LeveledModifier.of(section.getString("modify-damage-taken"))) + .potionEffectsWearer(ConfigParser.getPotionChanceSet(section.getConfigurationSection("potion-effects-defendant"))) + .potionEffectOpponent(ConfigParser.getPotionChanceSet(section.getConfigurationSection("potion-effects-opponent"))) .build(); } + private static Set getPotionChanceSet(ConfigurationSection section) { + return section.getKeys(false).stream() + .map(section::getConfigurationSection) + .map(ConfigParser::parsePotionChanceEffect) + .collect(Collectors.toSet()); + } + public static PotionChanceWrapper parsePotionChanceEffect(ConfigurationSection section) { return PotionChanceWrapper.chanceBuilder() .type(PotionEffectType.getByName(section.getName())) - .amplifier(section.getString("amplifier")) - .duration(section.getString("duration")) - .chance(section.getDouble("chance")) + .amplifier(LeveledModifier.of(section.getString("amplifier"))) + .duration(LeveledModifier.of(section.getString("duration"))) + .chance(LeveledModifier.of(section.getString("chance"))) .build(); } public static PotionEffectWrapper parsePotionEffect(ConfigurationSection section) { return PotionEffectWrapper.builder() .type(PotionEffectType.getByName(section.getName())) - .amplifier(section.getString("amplifier")) - .duration(section.getString("duration")) + .amplifier(LeveledModifier.of(section.getString("amplifier"))) + .duration(LeveledModifier.of(section.getString("duration"))) .build(); } public static MobWrapper parseMobWrapper(ConfigurationSection section) { return MobWrapper.builder() .amount(section.getInt("amount")) - .spawnPercentage(section.getDouble("spawn-percentage")) + .spawnPercentage(LeveledModifier.of(section.getString("spawn-percentage"))) .health(section.getInt("health")) .attackDamage(section.getDouble("attack-damage")) .hostile(section.getBoolean("hostile")) .displayName(color(section.getString("display-name"))) - .helmet(new ItemBuilder(section.getConfigurationSection("armor.helmet")).build()) - .leggings(new ItemBuilder(section.getConfigurationSection("armor.chest-plate")).build()) - .chestPlate(new ItemBuilder(section.getConfigurationSection("armor.leggings")).build()) + .helmet(new ItemBuilder(section.getConfigurationSection("armor.helmet"))) + .leggings(new ItemBuilder(section.getConfigurationSection("armor.chest-plate"))) + .chestPlate(new ItemBuilder(section.getConfigurationSection("armor.leggings"))) .boots(new ItemBuilder(section.getConfigurationSection("armor.boots")).build()) .build(); } public static EnchantmentWrapper parseEnchantmentWrapper(String key) { return EnchantmentWrapper.builder() - .amplifier(key.contains(":") ? key.split(":")[1] : "") + .amplifier(LeveledModifier.of(key.contains(":") ? key.split(":")[1] : "")) .enchantment(Enchantment.getByName(key.split(":")[0])) .build(); } diff --git a/core/src/main/java/com/songoda/epicenchants/utils/ItemBuilder.java b/core/src/main/java/com/songoda/epicenchants/utils/ItemBuilder.java index 763e117..cc6a7a7 100644 --- a/core/src/main/java/com/songoda/epicenchants/utils/ItemBuilder.java +++ b/core/src/main/java/com/songoda/epicenchants/utils/ItemBuilder.java @@ -185,6 +185,12 @@ public class ItemBuilder { return item; } + public ItemStack buildWithWrappers(int level) { + item.setItemMeta(meta); + enchantmentWrappers.forEach(enchant -> item.addEnchantment(enchant.getEnchantment(), enchant.getAmplifier(level))); + return item; + } + public Map getEnchants() { return meta.getEnchants(); } diff --git a/core/src/main/java/com/songoda/epicenchants/utils/ValidationUtils.java b/core/src/main/java/com/songoda/epicenchants/utils/ValidationUtils.java deleted file mode 100644 index 092d0c7..0000000 --- a/core/src/main/java/com/songoda/epicenchants/utils/ValidationUtils.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.songoda.epicenchants.utils; - -import co.aikar.commands.*; -import org.jetbrains.annotations.NotNull; - -public class ValidationUtils { - @NotNull - public static Number parseAndValidateNumber(CommandExecutionContext c, Number minValue, Number maxValue) throws InvalidCommandArgument { - final Number val = ACFUtil.parseNumber(c.popFirstArg(), c.hasFlag("suffixes")); - validateMinMax(c, val, minValue, maxValue); - return val; - } - - private static void validateMinMax(CommandExecutionContext c, Number val) throws InvalidCommandArgument { - validateMinMax(c, val, null, null); - } - - private static void validateMinMax(CommandExecutionContext c, Number val, Number minValue, Number maxValue) throws InvalidCommandArgument { - minValue = c.getFlagValue("min", minValue); - maxValue = c.getFlagValue("max", maxValue); - if (maxValue != null && val.doubleValue() > maxValue.doubleValue()) { - throw new InvalidCommandArgument(MessageKeys.PLEASE_SPECIFY_AT_MOST, "{max}", String.valueOf(maxValue)); - } - if (minValue != null && val.doubleValue() < minValue.doubleValue()) { - throw new InvalidCommandArgument(MessageKeys.PLEASE_SPECIFY_AT_LEAST, "{min}", String.valueOf(minValue)); - } - } -} 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 98a52ee..74b83f4 100644 --- a/core/src/main/java/com/songoda/epicenchants/wrappers/EnchantmentWrapper.java +++ b/core/src/main/java/com/songoda/epicenchants/wrappers/EnchantmentWrapper.java @@ -1,15 +1,16 @@ package com.songoda.epicenchants.wrappers; +import com.songoda.epicenchants.objects.LeveledModifier; import lombok.Builder; import org.bukkit.enchantments.Enchantment; @Builder public class EnchantmentWrapper { - private String amplifier; + private LeveledModifier amplifier; private Enchantment enchantment; - public int getAmplifier(int tier) { - return amplifier.isEmpty() ? 0 : Integer.parseInt(amplifier.replaceAll("\\{tier}", "" + tier)); + public int getAmplifier(int level) { + return (int) amplifier.get(level); } 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 cd54ca4..07bfefc 100644 --- a/core/src/main/java/com/songoda/epicenchants/wrappers/MobWrapper.java +++ b/core/src/main/java/com/songoda/epicenchants/wrappers/MobWrapper.java @@ -1,11 +1,12 @@ package com.songoda.epicenchants.wrappers; +import com.songoda.epicenchants.objects.LeveledModifier; import com.songoda.epicenchants.utils.GeneralUtils; +import com.songoda.epicenchants.utils.ItemBuilder; import lombok.Builder; import org.bukkit.Location; import org.bukkit.entity.EntityType; import org.bukkit.entity.LivingEntity; -import org.bukkit.inventory.ItemStack; import static java.util.concurrent.ThreadLocalRandom.current; @@ -15,13 +16,13 @@ public class MobWrapper { private int amount; private int health; private EntityType entityType; - private double spawnPercentage; + private LeveledModifier spawnPercentage; private double attackDamage; private boolean hostile; - private ItemStack helmet, chestPlate, leggings, boots; + private ItemBuilder helmet, chestPlate, leggings, boots; - public boolean trySpawn(Location location) { - if (!GeneralUtils.chance(spawnPercentage)) { + public boolean trySpawn(Location location, int level) { + if (!GeneralUtils.chance(spawnPercentage.get(level))) { return false; } @@ -38,10 +39,10 @@ public class MobWrapper { entity.setCustomName(displayName); entity.setCustomNameVisible(true); entity.setHealth(health); - entity.getEquipment().setHelmet(helmet); - entity.getEquipment().setChestplate(chestPlate); - entity.getEquipment().setLeggings(leggings); - entity.getEquipment().setBoots(boots); + entity.getEquipment().setHelmet(helmet.buildWithWrappers(level)); + entity.getEquipment().setChestplate(chestPlate.buildWithWrappers(level)); + entity.getEquipment().setLeggings(leggings.buildWithWrappers(level)); + entity.getEquipment().setBoots(boots.buildWithWrappers(level)); } return true; diff --git a/core/src/main/java/com/songoda/epicenchants/wrappers/PotionChanceWrapper.java b/core/src/main/java/com/songoda/epicenchants/wrappers/PotionChanceWrapper.java index eea662c..63a5e25 100644 --- a/core/src/main/java/com/songoda/epicenchants/wrappers/PotionChanceWrapper.java +++ b/core/src/main/java/com/songoda/epicenchants/wrappers/PotionChanceWrapper.java @@ -1,6 +1,7 @@ package com.songoda.epicenchants.wrappers; -import io.netty.util.internal.ThreadLocalRandom; +import com.songoda.epicenchants.objects.LeveledModifier; +import com.songoda.epicenchants.utils.GeneralUtils; import lombok.Builder; import lombok.Getter; import org.bukkit.entity.Player; @@ -8,19 +9,19 @@ import org.bukkit.potion.PotionEffectType; @Getter public class PotionChanceWrapper extends PotionEffectWrapper { - private double chance; + private LeveledModifier chance; @Builder(builderMethodName = "chanceBuilder") - PotionChanceWrapper(PotionEffectType type, String amplifier, String duration, double chance) { + PotionChanceWrapper(PotionEffectType type, LeveledModifier amplifier, LeveledModifier duration, LeveledModifier chance ) { super(type, amplifier, duration); this.chance = chance; } - public boolean test() { - return ThreadLocalRandom.current().nextDouble(101) < chance; + public boolean test(int level) { + return GeneralUtils.chance(chance.get(level)); } - public void perform(Player player, int tier) { - player.addPotionEffect(get(tier)); + public void perform(Player player, int level) { + player.addPotionEffect(get(level)); } } diff --git a/core/src/main/java/com/songoda/epicenchants/wrappers/PotionEffectWrapper.java b/core/src/main/java/com/songoda/epicenchants/wrappers/PotionEffectWrapper.java index 4730939..bb6fd16 100644 --- a/core/src/main/java/com/songoda/epicenchants/wrappers/PotionEffectWrapper.java +++ b/core/src/main/java/com/songoda/epicenchants/wrappers/PotionEffectWrapper.java @@ -1,23 +1,19 @@ package com.songoda.epicenchants.wrappers; +import com.songoda.epicenchants.objects.LeveledModifier; import lombok.AllArgsConstructor; import lombok.Builder; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; -import static java.lang.Integer.parseInt; -import static java.lang.String.valueOf; - @Builder @AllArgsConstructor public class PotionEffectWrapper { private PotionEffectType type; - private String amplifier; - private String duration; + private LeveledModifier amplifier; + private LeveledModifier duration; - public PotionEffect get(int tier) { - int tempAmplifier = amplifier.isEmpty() ? 0 : parseInt(amplifier.replace("{tier}", valueOf(tier))); - int tempDuration = duration.isEmpty() ? 0 : parseInt(duration.replace("{tier}", valueOf(tier))); - return new PotionEffect(type, tempDuration, tempAmplifier); + public PotionEffect get(int level) { + return new PotionEffect(type, (int) amplifier.get(level), (int) duration.get(level)); } } diff --git a/core/src/main/resources/StrengthEnchant.yml b/core/src/main/resources/StrengthEnchant.yml index 1038c51..d203c67 100644 --- a/core/src/main/resources/StrengthEnchant.yml +++ b/core/src/main/resources/StrengthEnchant.yml @@ -65,8 +65,13 @@ mobs: action: modify-damage-taken: 40 modify-damage-given: -10 - potion-effects: + potion-effects-wearer: STRENGTH: duration: 3 - chance: 20 + chance: 20 * {level} + amplifier: 2 + potion-effects-opponent: + STRENGTH: + duration: 3 + chance: 20 * {level} amplifier: 2 \ No newline at end of file