Improvement to commands.

This commit is contained in:
GB6 2019-01-17 17:11:59 +01:00
parent b5756c434c
commit 5cc92b0bb3
13 changed files with 118 additions and 84 deletions

View File

@ -36,6 +36,9 @@
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>

View File

@ -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());
}

View File

@ -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));
}

View File

@ -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<PotionChanceWrapper> potionEffects;
private double modifyDamageTaken;
private double modifyDamageGiven;
private Set<PotionChanceWrapper> potionEffectsWearer;
private Set<PotionChanceWrapper> 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
}
}

View File

@ -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;
}
}
}

View File

@ -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<PotionChanceWrapper> 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();
}

View File

@ -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<Enchantment, Integer> getEnchants() {
return meta.getEnchants();
}

View File

@ -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));
}
}
}

View File

@ -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() {

View File

@ -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;

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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