forked from Upstream/ExcellentEnchants-spigot
88 lines
3.3 KiB
Java
88 lines
3.3 KiB
Java
package su.nightexpress.excellentenchants.enchantment.impl.armor;
|
|
|
|
import org.bukkit.enchantments.EnchantmentTarget;
|
|
import org.bukkit.entity.LivingEntity;
|
|
import org.bukkit.event.EventPriority;
|
|
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
|
import org.bukkit.inventory.ItemStack;
|
|
import org.jetbrains.annotations.NotNull;
|
|
import su.nightexpress.excellentenchants.ExcellentEnchantsPlugin;
|
|
import su.nightexpress.excellentenchants.api.Modifier;
|
|
import su.nightexpress.excellentenchants.api.enchantment.Rarity;
|
|
import su.nightexpress.excellentenchants.api.enchantment.data.ChanceData;
|
|
import su.nightexpress.excellentenchants.api.enchantment.data.ChanceSettings;
|
|
import su.nightexpress.excellentenchants.api.enchantment.type.CombatEnchant;
|
|
import su.nightexpress.excellentenchants.enchantment.data.AbstractEnchantmentData;
|
|
import su.nightexpress.excellentenchants.enchantment.data.ChanceSettingsImpl;
|
|
import su.nightexpress.nightcore.config.FileConfig;
|
|
import su.nightexpress.nightcore.util.NumberUtil;
|
|
|
|
import java.io.File;
|
|
|
|
import static su.nightexpress.excellentenchants.Placeholders.*;
|
|
|
|
public class StoppingForceEnchant extends AbstractEnchantmentData implements ChanceData, CombatEnchant {
|
|
|
|
public static final String ID = "stopping_force";
|
|
|
|
private ChanceSettingsImpl chanceSettings;
|
|
private Modifier knockbackModifier;
|
|
|
|
public StoppingForceEnchant(@NotNull ExcellentEnchantsPlugin plugin, @NotNull File file) {
|
|
super(plugin, file);
|
|
this.setDescription(ENCHANTMENT_CHANCE + "% chance to resist knockback in combat by " + GENERIC_AMOUNT + "%.");
|
|
this.setMaxLevel(3);
|
|
this.setRarity(Rarity.UNCOMMON);
|
|
}
|
|
|
|
@Override
|
|
protected void loadAdditional(@NotNull FileConfig config) {
|
|
this.chanceSettings = ChanceSettingsImpl.create(config, Modifier.add(100, 0, 0, 100));
|
|
|
|
this.knockbackModifier = Modifier.read(config, "Settings.Knockback_Modifier",
|
|
Modifier.add(0.9, -0.2, 1),
|
|
"Sets the knockback multiplier when taking damage.", "Lower value = less knockback.");
|
|
|
|
this.addPlaceholder(GENERIC_AMOUNT, level -> NumberUtil.format(this.getKnockbackModifier(level) * 100));
|
|
}
|
|
|
|
public double getKnockbackModifier(int level) {
|
|
return this.knockbackModifier.getValue(level);
|
|
}
|
|
|
|
@NotNull
|
|
@Override
|
|
public EnchantmentTarget getCategory() {
|
|
return EnchantmentTarget.ARMOR_LEGS;
|
|
}
|
|
|
|
@NotNull
|
|
@Override
|
|
public EventPriority getProtectPriority() {
|
|
return EventPriority.HIGHEST;
|
|
}
|
|
|
|
@NotNull
|
|
@Override
|
|
public ChanceSettings getChanceSettings() {
|
|
return this.chanceSettings;
|
|
}
|
|
|
|
@Override
|
|
public boolean onAttack(@NotNull EntityDamageByEntityEvent event, @NotNull LivingEntity damager, @NotNull LivingEntity victim, @NotNull ItemStack weapon, int level) {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean onProtect(@NotNull EntityDamageByEntityEvent event, @NotNull LivingEntity damager, @NotNull LivingEntity victim, @NotNull ItemStack weapon, int level) {
|
|
if (!this.checkTriggerChance(level)) return false;
|
|
|
|
double mod = Math.max(0, this.getKnockbackModifier(level));
|
|
|
|
this.plugin.runTask(task -> {
|
|
victim.setVelocity(victim.getVelocity().multiply(mod));
|
|
});
|
|
return true;
|
|
}
|
|
}
|