volley enchant

This commit is contained in:
2025-05-13 21:04:55 +02:00
parent 2923c8ae1c
commit ea11cd1bb7
4 changed files with 247 additions and 0 deletions
@@ -43,6 +43,9 @@ public final class Enchantio extends JavaPlugin {
if (EnchantioConfig.ENCHANTS.containsKey(CloakingEnchant.KEY)) { if (EnchantioConfig.ENCHANTS.containsKey(CloakingEnchant.KEY)) {
getServer().getPluginManager().registerEvents(new CloakingListener(), this); getServer().getPluginManager().registerEvents(new CloakingListener(), this);
} }
if (EnchantioConfig.ENCHANTS.containsKey(VolleyEnchant.KEY)) {
getServer().getPluginManager().registerEvents(new VolleyListener(), this);
}
if (EnchantioConfig.ENCHANTS.containsKey(PanicEnchant.KEY)) { if (EnchantioConfig.ENCHANTS.containsKey(PanicEnchant.KEY)) {
getServer().getPluginManager().registerEvents(new PanicListener(), this); getServer().getPluginManager().registerEvents(new PanicListener(), this);
@@ -69,6 +69,9 @@ public class EnchantioConfig {
ConfigurationSection cloakingSection = getConfigSection(enchantsSection, "cloaking"); ConfigurationSection cloakingSection = getConfigSection(enchantsSection, "cloaking");
CloakingEnchant.create(cloakingSection); CloakingEnchant.create(cloakingSection);
ConfigurationSection volleySection = getConfigSection(enchantsSection, "volley");
VolleyEnchant.create(volleySection);
ConfigurationSection cursesSection = getConfigSection(configuration, "curses"); ConfigurationSection cursesSection = getConfigSection(configuration, "curses");
ConfigurationSection panicSection = getConfigSection(cursesSection, "panic"); ConfigurationSection panicSection = getConfigSection(cursesSection, "panic");
@@ -0,0 +1,151 @@
package me.youhavetrouble.enchantio.enchants;
import io.papermc.paper.registry.data.EnchantmentRegistryEntry;
import io.papermc.paper.registry.keys.tags.EnchantmentTagKeys;
import io.papermc.paper.registry.tag.TagKey;
import io.papermc.paper.tag.TagEntry;
import me.youhavetrouble.enchantio.EnchantioConfig;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.EquipmentSlotGroup;
import org.bukkit.inventory.ItemType;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static me.youhavetrouble.enchantio.EnchantioConfig.ENCHANTS;
@SuppressWarnings("UnstableApiUsage")
public class VolleyEnchant implements EnchantioEnchant {
public static final Key KEY = Key.key("enchantio:volley");
private final int anvilCost, weight, maxLevel, additionalArrowsPerLevel;
private final EnchantmentRegistryEntry.EnchantmentCost minimumCost;
private final EnchantmentRegistryEntry.EnchantmentCost maximumCost;
private final Set<TagEntry<ItemType>> supportedItemTags;
private final Set<TagKey<Enchantment>> enchantTagKeys = new HashSet<>();
private final Set<EquipmentSlotGroup> activeSlots = new HashSet<>();
public VolleyEnchant(
int anvilCost,
int weight,
EnchantmentRegistryEntry.EnchantmentCost minimumCost,
EnchantmentRegistryEntry.EnchantmentCost maximumCost,
boolean canGetFromEnchantingTable,
Set<TagEntry<ItemType>> supportedItemTags,
Set<EquipmentSlotGroup> activeSlots,
int maxLevel,
int additionalArrowsPerLevel
) {
this.anvilCost = anvilCost;
this.weight = weight;
this.minimumCost = minimumCost;
this.maximumCost = maximumCost;
this.supportedItemTags = supportedItemTags;
this.maxLevel = maxLevel;
this.activeSlots.addAll(activeSlots);
this.additionalArrowsPerLevel = additionalArrowsPerLevel;
if (canGetFromEnchantingTable) {
enchantTagKeys.add(EnchantmentTagKeys.IN_ENCHANTING_TABLE);
}
}
@Override
public @NotNull Key getKey() {
return KEY;
}
@Override
public @NotNull Component getDescription() {
return Component.translatable("enchantio.enchant.volley","Volley");
}
@Override
public int getAnvilCost() {
return anvilCost;
}
@Override
public int getMaxLevel() {
return maxLevel;
}
public int getAdditionalArrowsPerLevel() {
return additionalArrowsPerLevel;
}
@Override
public int getWeight() {
return weight;
}
@Override
public EnchantmentRegistryEntry.@NotNull EnchantmentCost getMinimumCost() {
return minimumCost;
}
@Override
public EnchantmentRegistryEntry.@NotNull EnchantmentCost getMaximumCost() {
return maximumCost;
}
@Override
public @NotNull Iterable<EquipmentSlotGroup> getActiveSlots() {
return activeSlots;
}
@Override
public @NotNull Set<TagEntry<ItemType>> getSupportedItems() {
return supportedItemTags;
}
@Override
public @NotNull Set<TagKey<Enchantment>> getEnchantTagKeys() {
return Collections.unmodifiableSet(enchantTagKeys);
}
public static VolleyEnchant create(ConfigurationSection configurationSection) {
VolleyEnchant executionerEnchant = new VolleyEnchant(
EnchantioConfig.getInt(configurationSection, "anvilCost", 1),
EnchantioConfig.getInt(configurationSection, "weight", 10),
EnchantmentRegistryEntry.EnchantmentCost.of(
EnchantioConfig.getInt(configurationSection, "minimumCost.base", 40),
EnchantioConfig.getInt(configurationSection, "minimumCost.additionalPerLevel", 3)
),
EnchantmentRegistryEntry.EnchantmentCost.of(
EnchantioConfig.getInt(configurationSection, "maximumCost.base", 65),
EnchantioConfig.getInt(configurationSection, "maximumCost.additionalPerLevel", 1)
),
EnchantioConfig.getBoolean(configurationSection, "canGetFromEnchantingTable", true),
EnchantioConfig.getTagsFromList(EnchantioConfig.getStringList(
configurationSection,
"supportedItemTags",
List.of(
"minecraft:bow"
)
)),
EnchantioConfig.getEquipmentSlotGroups(EnchantioConfig.getStringList(
configurationSection,
"activeSlots",
List.of(
"MAINHAND"
)
)),
EnchantioConfig.getInt(configurationSection, "maxLevel", 3),
EnchantioConfig.getInt(configurationSection, "additionalArrowsPerLevel", 1)
);
if (EnchantioConfig.getBoolean(configurationSection, "enabled", true)) {
ENCHANTS.put(VolleyEnchant.KEY, executionerEnchant);
}
return executionerEnchant;
}
}
@@ -0,0 +1,90 @@
package me.youhavetrouble.enchantio.listeners;
import io.papermc.paper.registry.RegistryAccess;
import io.papermc.paper.registry.RegistryKey;
import me.youhavetrouble.enchantio.EnchantioConfig;
import me.youhavetrouble.enchantio.enchants.EnchantioEnchant;
import me.youhavetrouble.enchantio.enchants.ExecutionerEnchant;
import me.youhavetrouble.enchantio.enchants.VolleyEnchant;
import org.bukkit.Registry;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.*;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.ProjectileLaunchEvent;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
public class VolleyListener implements Listener {
private final Registry<@NotNull Enchantment> registry = RegistryAccess.registryAccess().getRegistry(RegistryKey.ENCHANTMENT);
private final Enchantment volley = registry.get(VolleyEnchant.KEY);
private final Random random = ThreadLocalRandom.current();
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onVolley(ProjectileLaunchEvent event) {
if (volley == null) return;
Entity projectileEntity = event.getEntity();
if (CreatureSpawnEvent.SpawnReason.ENCHANTMENT.equals(projectileEntity.getEntitySpawnReason())) return;
if (!(event.getEntity().getShooter() instanceof LivingEntity shooter)) return;
EntityEquipment equipment = shooter.getEquipment();
if (equipment == null) return;
ItemStack bow = equipment.getItemInMainHand();
int level = bow.getEnchantmentLevel(volley);
if (level <= 0) return;
if (!(EnchantioConfig.ENCHANTS.get(ExecutionerEnchant.KEY) instanceof VolleyEnchant volleyEnchant)) return;
if (projectileEntity instanceof Arrow arrow) {
for (int i = 0; i < level * volleyEnchant.getAdditionalArrowsPerLevel(); i++) {
Vector velocity = arrow.getVelocity();
double spread = 0.5;
double spreadX = (random.nextDouble() - 0.5) * spread;
double spreadY = (random.nextDouble() - 0.5) * spread;
double spreadZ = (random.nextDouble() - 0.5) * spread;
Vector newVelocity = velocity.clone().add(new Vector(spreadX, spreadY, spreadZ));
shooter.getWorld().spawn(arrow.getLocation(), Arrow.class, spawnedArrow -> {
spawnedArrow.setVelocity(newVelocity);
spawnedArrow.getPersistentDataContainer().set(volley.getKey(), PersistentDataType.BOOLEAN, true);
spawnedArrow.setCritical(arrow.isCritical());
spawnedArrow.setShooter(arrow.getShooter());
spawnedArrow.setHasLeftShooter(arrow.hasLeftShooter());
spawnedArrow.setBasePotionType(arrow.getBasePotionType());
spawnedArrow.setPickupStatus(AbstractArrow.PickupStatus.CREATIVE_ONLY);
arrow.getCustomEffects().forEach(effect -> spawnedArrow.addCustomEffect(effect, false));
}, CreatureSpawnEvent.SpawnReason.ENCHANTMENT);
}
return;
}
if (projectileEntity instanceof SpectralArrow arrow) {
arrow.setPickupStatus(AbstractArrow.PickupStatus.CREATIVE_ONLY);
for (int i = 0; i < level; i++) {
Vector velocity = arrow.getVelocity();
double spread = 0.5; // Adjust the spread value as needed
double spreadX = (random.nextDouble() - 0.5) * spread;
double spreadY = (random.nextDouble() - 0.5) * spread;
double spreadZ = (random.nextDouble() - 0.5) * spread;
Vector newVelocity = velocity.clone().add(new Vector(spreadX, spreadY, spreadZ));
shooter.getWorld().spawn(arrow.getLocation(), SpectralArrow.class, spawnedArrow -> {
spawnedArrow.setVelocity(newVelocity);
spawnedArrow.getPersistentDataContainer().set(volley.getKey(), PersistentDataType.BOOLEAN, true);
spawnedArrow.setCritical(arrow.isCritical());
spawnedArrow.setShooter(arrow.getShooter());
spawnedArrow.setHasLeftShooter(arrow.hasLeftShooter());
spawnedArrow.setPickupStatus(AbstractArrow.PickupStatus.CREATIVE_ONLY);
}, CreatureSpawnEvent.SpawnReason.ENCHANTMENT);
}
return;
}
}
}