This commit is contained in:
BuildTools 2023-07-29 18:56:44 +05:00
parent 4648f48aa5
commit e57e2ab26e
19 changed files with 189 additions and 66 deletions

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>ExcellentEnchants</artifactId>
<groupId>su.nightexpress.excellentenchants</groupId>
<version>3.5.1</version>
<version>3.5.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@ -81,27 +81,27 @@
<dependency>
<groupId>su.nightexpress.excellentenchants</groupId>
<artifactId>NMS</artifactId>
<version>3.5.1</version>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>su.nightexpress.excellentenchants</groupId>
<artifactId>V1_17_R1</artifactId>
<version>3.5.1</version>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>su.nightexpress.excellentenchants</groupId>
<artifactId>V1_18_R2</artifactId>
<version>3.5.1</version>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>su.nightexpress.excellentenchants</groupId>
<artifactId>V1_19_R3</artifactId>
<version>3.5.1</version>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>su.nightexpress.excellentenchants</groupId>
<artifactId>V1_20_R1</artifactId>
<version>3.5.1</version>
<version>3.5.2</version>
</dependency>
</dependencies>

View File

@ -76,5 +76,7 @@ public interface IEnchantment {
boolean isOutOfCharges(@NotNull ItemStack item);
void consumeCharges(@NotNull ItemStack item);
void consumeCharges(@NotNull ItemStack item, int level);
void consumeChargesNoUpdate(@NotNull ItemStack item, int level);
}

View File

@ -119,6 +119,7 @@ public class EnchantRegistry {
this.register(FlareEnchant.ID, () -> new FlareEnchant(plugin));
this.register(EnchantGhast.ID, () -> new EnchantGhast(plugin));
this.register(EnchantHover.ID, () -> new EnchantHover(plugin));
this.register(SniperEnchant.ID, () -> new SniperEnchant(plugin));
this.register(EnchantPoisonedArrows.ID, () -> new EnchantPoisonedArrows(plugin));
this.register(EnchantWitheredArrows.ID, () -> new EnchantWitheredArrows(plugin));

View File

@ -164,7 +164,11 @@ public abstract class ExcellentEnchant extends Enchantment implements IEnchantme
@NotNull
public String getNameFormatted(int level) {
return this.getTier().getColor() + this.getDisplayName() + " " + NumberUtil.toRoman(level);
String name = this.getTier().getColor() + this.getDisplayName();
if (level > 1 || this.getMaxLevel() > 1) {
name += " " + NumberUtil.toRoman(level);
}
return name;
}
@NotNull
@ -353,8 +357,13 @@ public abstract class ExcellentEnchant extends Enchantment implements IEnchantme
}
@Override
public void consumeCharges(@NotNull ItemStack item) {
EnchantUtils.consumeCharges(item, this);
public void consumeChargesNoUpdate(@NotNull ItemStack item, int level) {
EnchantUtils.consumeCharges(item, this, level);
}
@Override
public void consumeCharges(@NotNull ItemStack item, int level) {
this.consumeChargesNoUpdate(item, level);
EnchantUtils.updateDisplay(item);
}
}

View File

@ -87,7 +87,7 @@ public class EnchantElementalProtection extends ExcellentEnchant {
if (level <= 0) continue;
protectionAmount += this.getProtectionAmount(level);
this.consumeCharges(armor);
this.consumeCharges(armor, level);
}
if (protectionAmount <= 0D) return;

View File

@ -141,7 +141,7 @@ public class EnchantRegrowth extends ExcellentEnchant implements Chanced, Passiv
enchants.forEach((enchant, level) -> {
if (enchant.isOutOfCharges(item)) return;
if (enchant.onTrigger(entity, item, level)) {
enchant.consumeCharges(item);
enchant.consumeCharges(item, level);
}
});
});

View File

@ -111,7 +111,7 @@ public class EnchantSaturation extends ExcellentEnchant implements PassiveEnchan
enchants.forEach((enchant, level) -> {
if (enchant.isOutOfCharges(item)) return;
if (enchant.onTrigger(entity, item, level)) {
enchant.consumeCharges(item);
enchant.consumeCharges(item, level);
}
});
});

View File

@ -0,0 +1,90 @@
package su.nightexpress.excellentenchants.enchantment.impl.bow;
import org.bukkit.enchantments.EnchantmentTarget;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Projectile;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityShootBowEvent;
import org.bukkit.event.entity.ProjectileHitEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;
import su.nexmedia.engine.utils.NumberUtil;
import su.nightexpress.excellentenchants.ExcellentEnchants;
import su.nightexpress.excellentenchants.Placeholders;
import su.nightexpress.excellentenchants.api.enchantment.meta.Chanced;
import su.nightexpress.excellentenchants.api.enchantment.type.BowEnchant;
import su.nightexpress.excellentenchants.enchantment.config.EnchantScaler;
import su.nightexpress.excellentenchants.enchantment.impl.ExcellentEnchant;
import su.nightexpress.excellentenchants.enchantment.impl.meta.ChanceImplementation;
import su.nightexpress.excellentenchants.enchantment.util.EnchantPriority;
public class SniperEnchant extends ExcellentEnchant implements BowEnchant, Chanced {
public static final String ID = "sniper";
private static final String PLACEHOLDER_PROJECTILE_SPEED = "%enchantment_projectile_speed%";
private ChanceImplementation chanceImplementation;
private EnchantScaler speedModifier;
public SniperEnchant(@NotNull ExcellentEnchants plugin) {
super(plugin, ID, EnchantPriority.LOWEST);
this.getDefaults().setDescription("Increases projectile speed by " + PLACEHOLDER_PROJECTILE_SPEED + "%");
this.getDefaults().setLevelMax(5);
this.getDefaults().setTier(0.3);
}
@Override
public void loadSettings() {
super.loadSettings();
this.chanceImplementation = ChanceImplementation.create(this, "100.0");
this.speedModifier = EnchantScaler.read(this, "Settings.Speed_Modifier",
"1.0 + " + Placeholders.ENCHANTMENT_LEVEL + " / 5.0", "Sets projectile's speed modifier.");
this.addPlaceholder(PLACEHOLDER_PROJECTILE_SPEED, level -> NumberUtil.format(this.getSpeedModifier(level) * 100D));
}
public double getSpeedModifier(int level) {
return this.speedModifier.getValue(level);
}
@NotNull
@Override
public EnchantmentTarget getItemTarget() {
return EnchantmentTarget.BOW;
}
@NotNull
@Override
public Chanced getChanceImplementation() {
return this.chanceImplementation;
}
@Override
public boolean onShoot(@NotNull EntityShootBowEvent e, @NotNull LivingEntity shooter, @NotNull ItemStack bow, int level) {
if (!this.isAvailableToUse(shooter)) return false;
if (!this.checkTriggerChance(level)) return false;
double modifier = this.getSpeedModifier(level);
Entity entity = e.getProjectile();
Vector vector = entity.getVelocity();
entity.setVelocity(vector.multiply(modifier));
return true;
}
@Override
public boolean onHit(@NotNull ProjectileHitEvent e, @NotNull Projectile projectile, @NotNull ItemStack bow, int level) {
return false;
}
@Override
public boolean onDamage(@NotNull EntityDamageByEntityEvent e, @NotNull Projectile projectile, @NotNull LivingEntity shooter, @NotNull LivingEntity victim, @NotNull ItemStack weapon, int level) {
return false;
}
}

View File

@ -63,6 +63,6 @@ public class RiverMasterEnchant extends ExcellentEnchant {
hook.setVelocity(hook.getVelocity().multiply(this.getDistanceMod(level)));
this.consumeCharges(rod);
this.consumeCharges(rod, level);
}
}

View File

@ -21,7 +21,6 @@ import su.nightexpress.excellentenchants.enchantment.util.EnchantUtils;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
@ -73,23 +72,26 @@ public class EnchantAnvilListener extends AbstractListener<ExcellentEnchants> {
@NotNull ItemStack first, @NotNull ItemStack second, @NotNull ItemStack result) {
if (second.getType().isAir()) return false;
Set<ExcellentEnchant> chargeables = EnchantUtils.getExcellents(first).keySet().stream()
.filter(en -> en.isChargesEnabled() && en.isChargesFuel(second) && !en.isFullOfCharges(first))
.collect(Collectors.toSet());
if (chargeables.isEmpty()) return false;
Map<ExcellentEnchant, Integer> chargable = new HashMap<>();
EnchantUtils.getExcellents(first).forEach((enchant, level) -> {
if (enchant.isChargesEnabled() && enchant.isChargesFuel(second) && !enchant.isFullOfCharges(first)) {
chargable.put(enchant, level);
}
});
if (chargable.isEmpty()) return false;
ItemStack result2 = new ItemStack(first);
int count = 0;
while (count < second.getAmount() && !chargeables.stream().allMatch(en -> en.isFullOfCharges(result2))) {
chargeables.forEach(enchant -> EnchantUtils.rechargeCharges(result2, enchant));
while (count < second.getAmount() && !chargable.keySet().stream().allMatch(en -> en.isFullOfCharges(result2))) {
chargable.forEach((enchant, level) -> EnchantUtils.rechargeCharges(result2, enchant, level));
count++;
}
PDCUtil.set(result2, RECHARGED, count);
EnchantUtils.updateDisplay(result2);
e.setResult(result2);
this.plugin.runTask(task -> e.getInventory().setRepairCost(chargeables.size()));
this.plugin.runTask(task -> e.getInventory().setRepairCost(chargable.size()));
return true;
}
@ -116,7 +118,7 @@ public class EnchantAnvilListener extends AbstractListener<ExcellentEnchants> {
enchantments.forEach((enchant, level) -> {
if (EnchantUtils.add(result2, enchant, level, false)) {
repairCost.addAndGet(enchant.getAnvilMergeCost(level));
EnchantUtils.setCharges(result2, enchant, charges.getOrDefault(enchant, 0));
EnchantUtils.setCharges(result2, enchant, level, charges.getOrDefault(enchant, 0));
}
});

View File

@ -119,7 +119,7 @@ public class EnchantGenericListener extends AbstractListener<ExcellentEnchants>
e.getEnchantsToAdd().forEach((enchantment, level) -> {
if (enchantment instanceof ExcellentEnchant enchant && enchant.isChargesEnabled()) {
EnchantUtils.restoreCharges(result, enchant);
EnchantUtils.restoreCharges(result, enchant, level);
}
});
EnchantUtils.updateDisplay(result);
@ -198,7 +198,7 @@ public class EnchantGenericListener extends AbstractListener<ExcellentEnchants>
ItemStack item = equipment.getItem(slot);
if (EnchantUtils.isEnchantable(item)) {
if (doPopulation) EnchantUtils.populate(item, ObtainType.MOB_SPAWNING);
EnchantUtils.getExcellents(item).keySet().forEach(enchant -> EnchantUtils.restoreCharges(item, enchant));
EnchantUtils.getExcellents(item).forEach((enchant, level) -> EnchantUtils.restoreCharges(item, enchant, level));
equipment.setItem(slot, item);
}
}

View File

@ -94,9 +94,10 @@ public class EnchantHandlerListener extends AbstractListener<ExcellentEnchants>
EnchantUtils.getExcellents(weapon, CombatEnchant.class).forEach((combatEnchant, level) -> {
if (combatEnchant.isOutOfCharges(weapon)) return;
if (combatEnchant.onAttack(e, damager, victim, weapon, level)) {
combatEnchant.consumeCharges(weapon);
combatEnchant.consumeChargesNoUpdate(weapon, level);
}
});
EnchantUtils.updateDisplay(weapon);
}
private void handleCombatArmorEnchants(@NotNull EntityDamageByEntityEvent e,
@ -112,9 +113,10 @@ public class EnchantHandlerListener extends AbstractListener<ExcellentEnchants>
EnchantUtils.getExcellents(armor, CombatEnchant.class).forEach((combatEnchant, level) -> {
if (combatEnchant.isOutOfCharges(armor)) return;
if (combatEnchant.onProtect(e, damager, victim, weaponDamager, level)) {
combatEnchant.consumeCharges(armor);
combatEnchant.consumeChargesNoUpdate(armor, level);
}
});
EnchantUtils.updateDisplay(armor);
}
}
@ -123,9 +125,10 @@ public class EnchantHandlerListener extends AbstractListener<ExcellentEnchants>
enchants.forEach((enchant, level) -> {
if (enchant.isOutOfCharges(item)) return;
if (enchant.onDamage(e, entity, item, level)) {
enchant.consumeCharges(item);
enchant.consumeChargesNoUpdate(item, level);
}
});
EnchantUtils.updateDisplay(item);
});
}
@ -159,9 +162,10 @@ public class EnchantHandlerListener extends AbstractListener<ExcellentEnchants>
arrowed.addData(projectile);
arrowed.addTrail(projectile);
}
bowEnchant.consumeCharges(bow);
bowEnchant.consumeChargesNoUpdate(bow, level);
}
});
EnchantUtils.updateDisplay(bow);
if (e.getProjectile() instanceof Projectile projectile) {
this.setSourceWeapon(projectile, bow);
@ -201,9 +205,10 @@ public class EnchantHandlerListener extends AbstractListener<ExcellentEnchants>
EnchantUtils.getExcellents(item, InteractEnchant.class).forEach((interEnchant, level) -> {
if (interEnchant.isOutOfCharges(item)) return;
if (interEnchant.onInteract(e, player, item, level)) {
interEnchant.consumeCharges(item);
interEnchant.consumeChargesNoUpdate(item, level);
}
});
EnchantUtils.updateDisplay(item);
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
@ -217,9 +222,10 @@ public class EnchantHandlerListener extends AbstractListener<ExcellentEnchants>
if (event.isCancelled()) return; // Check if event was cancelled by some enchantment.
if (enchant.isOutOfCharges(item)) return;
if (enchant.onFishing(event, item, level)) {
enchant.consumeCharges(item);
enchant.consumeChargesNoUpdate(item, level);
}
});
EnchantUtils.updateDisplay(item);
}
// ---------------------------------------------------------------
@ -233,9 +239,10 @@ public class EnchantHandlerListener extends AbstractListener<ExcellentEnchants>
enchants.forEach(((deathEnchant, level) -> {
if (deathEnchant.isOutOfCharges(item)) return;
if (deathEnchant.onDeath(e, entity, level)) {
deathEnchant.consumeCharges(item);
deathEnchant.consumeChargesNoUpdate(item, level);
}
}));
EnchantUtils.updateDisplay(item);
});
Player killer = entity.getKiller();
@ -247,9 +254,10 @@ public class EnchantHandlerListener extends AbstractListener<ExcellentEnchants>
EnchantUtils.getExcellents(weapon, DeathEnchant.class).forEach((deathEnchant, level) -> {
if (deathEnchant.isOutOfCharges(weapon)) return;
if (deathEnchant.onKill(e, entity, killer, level)) {
deathEnchant.consumeCharges(weapon);
deathEnchant.consumeChargesNoUpdate(weapon, level);
}
});
EnchantUtils.updateDisplay(weapon);
}
// Handle BlockBreak enchantments.
@ -264,9 +272,10 @@ public class EnchantHandlerListener extends AbstractListener<ExcellentEnchants>
EnchantUtils.getExcellents(tool, BlockBreakEnchant.class).forEach((blockEnchant, level) -> {
if (blockEnchant.isOutOfCharges(tool)) return;
if (blockEnchant.onBreak(e, player, tool, level)) {
blockEnchant.consumeCharges(tool);
blockEnchant.consumeChargesNoUpdate(tool, level);
}
});
EnchantUtils.updateDisplay(tool);
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
@ -281,9 +290,10 @@ public class EnchantHandlerListener extends AbstractListener<ExcellentEnchants>
EnchantUtils.getExcellents(tool, BlockDropEnchant.class).forEach((enchant, level) -> {
if (enchant.isOutOfCharges(tool)) return;
if (enchant.onDrop(e, dropContainer, player, tool, level)) {
enchant.consumeCharges(tool);
enchant.consumeChargesNoUpdate(tool, level);
}
});
EnchantUtils.updateDisplay(tool);
BlockState state = e.getBlockState();
World world = state.getWorld();

View File

@ -178,6 +178,9 @@ public class EnchantUtils {
public static boolean updateDisplay(@NotNull ItemStack item) {
if (Config.ENCHANTMENTS_DISPLAY_MODE.get() != 1) return false;
ItemMeta meta = item.getItemMeta();
if (meta == null) return false;
if (!isEnchantable(item)) {
PDCUtil.remove(item, KEY_LORE_SIZE);
return false;
@ -185,14 +188,12 @@ public class EnchantUtils {
Map<ExcellentEnchant, Integer> enchants = getExcellents(item);
int sizeHas = PDCUtil.getInt(item, KEY_LORE_SIZE).orElse(0);
int sizeCached = PDCUtil.getInt(item, KEY_LORE_SIZE).orElse(0);
int sizeReal = enchants.size();
ItemMeta meta = item.getItemMeta();
if (meta == null) return false;
if (sizeCached == 0 && sizeReal == 0) return false;
List<String> lore = meta.getLore() == null ? new ArrayList<>() : meta.getLore();
for (int index = 0; index < sizeHas && !lore.isEmpty(); index++) {
for (int index = 0; index < sizeCached && !lore.isEmpty(); index++) {
lore.remove(0);
}
//lore.removeIf(str -> enchants.keySet().stream().anyMatch(enchant -> str.contains(enchant.getDisplayName())));
@ -280,38 +281,45 @@ public class EnchantUtils {
return getCharges(item, enchant) == max;
}
public static void consumeCharges(@NotNull ItemStack item, @NotNull ExcellentEnchant enchant) {
public static void consumeCharges(@NotNull ItemStack item, @NotNull ExcellentEnchant enchant, int level) {
if (!enchant.isChargesEnabled()) return;
int level = getLevel(item, enchant);
//int level = getLevel(item, enchant);
int has = getCharges(item, enchant);
int use = enchant.getChargesConsumeAmount(level);
setCharges(item, enchant, has - use);
setCharges(item, enchant, level,has - use);
}
public static void restoreCharges(@NotNull ItemStack item, @NotNull ExcellentEnchant enchant) {
if (!enchant.isChargesEnabled()) return;
/*public static void restoreCharges(@NotNull ItemStack item, @NotNull ExcellentEnchant enchant) {
int level = getLevel(item, enchant);
int max = enchant.getChargesMax(level);
setCharges(item, enchant, max);
restoreCharges(item, enchant, level);
}*/
public static void restoreCharges(@NotNull ItemStack item, @NotNull ExcellentEnchant enchant, int level) {
setCharges(item, enchant, level, Integer.MAX_VALUE);
}
public static void rechargeCharges(@NotNull ItemStack item, @NotNull ExcellentEnchant enchant) {
if (!enchant.isChargesEnabled()) return;
public static void rechargeCharges(@NotNull ItemStack item, @NotNull ExcellentEnchant enchant, int level) {
//if (!enchant.isChargesEnabled()) return;
int level = getLevel(item, enchant);
//int level = getLevel(item, enchant);
int recharge = enchant.getChargesRechargeAmount(level);
int has = getCharges(item, enchant);
setCharges(item, enchant, has + recharge);
int set = has + recharge;
setCharges(item, enchant, level, set);
}
public static void setCharges(@NotNull ItemStack item, @NotNull ExcellentEnchant enchant, int charges) {
/*public static void setCharges(@NotNull ItemStack item, @NotNull ExcellentEnchant enchant, int charges) {
int level = getLevel(item, enchant);
setCharges(item, enchant, level, charges);
}*/
public static void setCharges(@NotNull ItemStack item, @NotNull ExcellentEnchant enchant, int level, int charges) {
if (!enchant.isChargesEnabled()) return;
int level = getLevel(item, enchant);
int max = enchant.getChargesMax(level);
PDCUtil.set(item, enchant.getChargesKey(), Math.max(0, Math.min(charges, max)));
int set = Math.min(Math.abs(charges), max);
PDCUtil.set(item, enchant.getChargesKey(), set);
}
public static int getExcellentAmount(@NotNull ItemStack item) {
@ -390,10 +398,11 @@ public class EnchantUtils {
if (enchant instanceof Potioned potioned) {
if (enchant.isOutOfCharges(item)) return;
if (enchant.onTrigger(entity, item, level)) {
enchant.consumeCharges(item);
enchant.consumeChargesNoUpdate(item, level);
}
}
});
EnchantUtils.updateDisplay(item);
});
}
}

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>ExcellentEnchants</artifactId>
<groupId>su.nightexpress.excellentenchants</groupId>
<version>3.5.1</version>
<version>3.5.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>ExcellentEnchants</artifactId>
<groupId>su.nightexpress.excellentenchants</groupId>
<version>3.5.1</version>
<version>3.5.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@ -26,7 +26,7 @@
<dependency>
<groupId>su.nightexpress.excellentenchants</groupId>
<artifactId>NMS</artifactId>
<version>3.5.1</version>
<version>3.5.2</version>
</dependency>
</dependencies>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>ExcellentEnchants</artifactId>
<groupId>su.nightexpress.excellentenchants</groupId>
<version>3.5.1</version>
<version>3.5.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@ -26,7 +26,7 @@
<dependency>
<groupId>su.nightexpress.excellentenchants</groupId>
<artifactId>NMS</artifactId>
<version>3.5.1</version>
<version>3.5.2</version>
</dependency>
</dependencies>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>ExcellentEnchants</artifactId>
<groupId>su.nightexpress.excellentenchants</groupId>
<version>3.5.1</version>
<version>3.5.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@ -26,7 +26,7 @@
<dependency>
<groupId>su.nightexpress.excellentenchants</groupId>
<artifactId>NMS</artifactId>
<version>3.5.1</version>
<version>3.5.2</version>
</dependency>
</dependencies>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>ExcellentEnchants</artifactId>
<groupId>su.nightexpress.excellentenchants</groupId>
<version>3.5.1</version>
<version>3.5.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@ -26,7 +26,7 @@
<dependency>
<groupId>su.nightexpress.excellentenchants</groupId>
<artifactId>NMS</artifactId>
<version>3.5.1</version>
<version>3.5.2</version>
</dependency>
</dependencies>

View File

@ -7,7 +7,7 @@
<groupId>su.nightexpress.excellentenchants</groupId>
<artifactId>ExcellentEnchants</artifactId>
<packaging>pom</packaging>
<version>3.5.1</version>
<version>3.5.2</version>
<modules>
<module>Core</module>
<module>NMS</module>