From ea11cd1bb7d7932fbe59ef352b7050be23d8526e Mon Sep 17 00:00:00 2001 From: YouHaveTrouble Date: Tue, 13 May 2025 21:04:55 +0200 Subject: [PATCH] volley enchant --- .../youhavetrouble/enchantio/Enchantio.java | 3 + .../enchantio/EnchantioConfig.java | 3 + .../enchantio/enchants/VolleyEnchant.java | 151 ++++++++++++++++++ .../enchantio/listeners/VolleyListener.java | 90 +++++++++++ 4 files changed, 247 insertions(+) create mode 100644 src/main/java/me/youhavetrouble/enchantio/enchants/VolleyEnchant.java create mode 100644 src/main/java/me/youhavetrouble/enchantio/listeners/VolleyListener.java diff --git a/src/main/java/me/youhavetrouble/enchantio/Enchantio.java b/src/main/java/me/youhavetrouble/enchantio/Enchantio.java index 6c84449..1cf4bbc 100644 --- a/src/main/java/me/youhavetrouble/enchantio/Enchantio.java +++ b/src/main/java/me/youhavetrouble/enchantio/Enchantio.java @@ -43,6 +43,9 @@ public final class Enchantio extends JavaPlugin { if (EnchantioConfig.ENCHANTS.containsKey(CloakingEnchant.KEY)) { getServer().getPluginManager().registerEvents(new CloakingListener(), this); } + if (EnchantioConfig.ENCHANTS.containsKey(VolleyEnchant.KEY)) { + getServer().getPluginManager().registerEvents(new VolleyListener(), this); + } if (EnchantioConfig.ENCHANTS.containsKey(PanicEnchant.KEY)) { getServer().getPluginManager().registerEvents(new PanicListener(), this); diff --git a/src/main/java/me/youhavetrouble/enchantio/EnchantioConfig.java b/src/main/java/me/youhavetrouble/enchantio/EnchantioConfig.java index ee89e3d..f4c6502 100644 --- a/src/main/java/me/youhavetrouble/enchantio/EnchantioConfig.java +++ b/src/main/java/me/youhavetrouble/enchantio/EnchantioConfig.java @@ -69,6 +69,9 @@ public class EnchantioConfig { ConfigurationSection cloakingSection = getConfigSection(enchantsSection, "cloaking"); CloakingEnchant.create(cloakingSection); + ConfigurationSection volleySection = getConfigSection(enchantsSection, "volley"); + VolleyEnchant.create(volleySection); + ConfigurationSection cursesSection = getConfigSection(configuration, "curses"); ConfigurationSection panicSection = getConfigSection(cursesSection, "panic"); diff --git a/src/main/java/me/youhavetrouble/enchantio/enchants/VolleyEnchant.java b/src/main/java/me/youhavetrouble/enchantio/enchants/VolleyEnchant.java new file mode 100644 index 0000000..3f9584c --- /dev/null +++ b/src/main/java/me/youhavetrouble/enchantio/enchants/VolleyEnchant.java @@ -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> supportedItemTags; + private final Set> enchantTagKeys = new HashSet<>(); + private final Set activeSlots = new HashSet<>(); + + public VolleyEnchant( + int anvilCost, + int weight, + EnchantmentRegistryEntry.EnchantmentCost minimumCost, + EnchantmentRegistryEntry.EnchantmentCost maximumCost, + boolean canGetFromEnchantingTable, + Set> supportedItemTags, + Set 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 getActiveSlots() { + return activeSlots; + } + + @Override + public @NotNull Set> getSupportedItems() { + return supportedItemTags; + } + + @Override + public @NotNull Set> 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; + } + +} diff --git a/src/main/java/me/youhavetrouble/enchantio/listeners/VolleyListener.java b/src/main/java/me/youhavetrouble/enchantio/listeners/VolleyListener.java new file mode 100644 index 0000000..e6cafae --- /dev/null +++ b/src/main/java/me/youhavetrouble/enchantio/listeners/VolleyListener.java @@ -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; + } + + } + +}