From 4010e88237cfe3964c5c4f2e5527bb5fc66a5969 Mon Sep 17 00:00:00 2001 From: YouHaveTrouble Date: Thu, 17 Oct 2024 19:19:25 +0200 Subject: [PATCH] playing around with enchants --- .gitignore | 113 ++++++++++++++++++ pom.xml | 73 +++++++++++ .../youhavetrouble/enchantio/Enchantio.java | 19 +++ .../enchantio/EnchantioBootstrap.java | 43 +++++++ .../enchantio/EnchantioConfig.java | 16 +++ .../enchantio/enchants/EnchantioEnchant.java | 96 +++++++++++++++ .../enchantio/enchants/SoulboundEnchant.java | 36 ++++++ .../listeners/SoulboundListener.java | 31 +++++ src/main/resources/config.yml | 0 src/main/resources/paper-plugin.yml | 8 ++ 10 files changed, 435 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/me/youhavetrouble/enchantio/Enchantio.java create mode 100644 src/main/java/me/youhavetrouble/enchantio/EnchantioBootstrap.java create mode 100644 src/main/java/me/youhavetrouble/enchantio/EnchantioConfig.java create mode 100644 src/main/java/me/youhavetrouble/enchantio/enchants/EnchantioEnchant.java create mode 100644 src/main/java/me/youhavetrouble/enchantio/enchants/SoulboundEnchant.java create mode 100644 src/main/java/me/youhavetrouble/enchantio/listeners/SoulboundListener.java create mode 100644 src/main/resources/config.yml create mode 100644 src/main/resources/paper-plugin.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4788b4b --- /dev/null +++ b/.gitignore @@ -0,0 +1,113 @@ +# User-specific stuff +.idea/ + +*.iml +*.ipr +*.iws + +# IntelliJ +out/ + +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# Windows thumbnail cache files +Thumbs.db +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +target/ + +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next + +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +.mvn/wrapper/maven-wrapper.jar +.flattened-pom.xml + +# Common working directory +run/ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..4af2c7c --- /dev/null +++ b/pom.xml @@ -0,0 +1,73 @@ + + + 4.0.0 + + me.youhavetrouble + Enchantio + 0.1 + jar + + Enchantio + + Custom enchants for paper servers + + 21 + UTF-8 + + https://youhavetrouble.me + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + ${java.version} + ${java.version} + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + + + + + + src/main/resources + true + + + + + + + papermc-repo + https://repo.papermc.io/repository/maven-public/ + + + sonatype + https://oss.sonatype.org/content/groups/public/ + + + + + + io.papermc.paper + paper-api + 1.21.1-R0.1-SNAPSHOT + provided + + + diff --git a/src/main/java/me/youhavetrouble/enchantio/Enchantio.java b/src/main/java/me/youhavetrouble/enchantio/Enchantio.java new file mode 100644 index 0000000..d4cf301 --- /dev/null +++ b/src/main/java/me/youhavetrouble/enchantio/Enchantio.java @@ -0,0 +1,19 @@ +package me.youhavetrouble.enchantio; + + +import me.youhavetrouble.enchantio.listeners.SoulboundListener; +import org.bukkit.plugin.java.JavaPlugin; + +public final class Enchantio extends JavaPlugin { + + @Override + public void onEnable() { + getServer().getPluginManager().registerEvents(new SoulboundListener(), this); + + } + + @Override + public void onDisable() { + // Plugin shutdown logic + } +} diff --git a/src/main/java/me/youhavetrouble/enchantio/EnchantioBootstrap.java b/src/main/java/me/youhavetrouble/enchantio/EnchantioBootstrap.java new file mode 100644 index 0000000..06e3bb7 --- /dev/null +++ b/src/main/java/me/youhavetrouble/enchantio/EnchantioBootstrap.java @@ -0,0 +1,43 @@ +package me.youhavetrouble.enchantio; + +import io.papermc.paper.plugin.bootstrap.BootstrapContext; +import io.papermc.paper.plugin.bootstrap.PluginBootstrap; +import io.papermc.paper.plugin.bootstrap.PluginProviderContext; +import io.papermc.paper.registry.RegistryKey; +import io.papermc.paper.registry.TypedKey; +import io.papermc.paper.registry.event.RegistryEvents; +import io.papermc.paper.registry.keys.tags.ItemTypeTagKeys; +import me.youhavetrouble.enchantio.enchants.EnchantioEnchant; +import me.youhavetrouble.enchantio.enchants.SoulboundEnchant; +import org.bukkit.plugin.java.JavaPlugin; +import org.jetbrains.annotations.NotNull; + +@SuppressWarnings("all") +public class EnchantioBootstrap implements PluginBootstrap { + @Override + public void bootstrap(@NotNull BootstrapContext context) { + + EnchantioEnchant soulbound = new SoulboundEnchant(); + + context.getLifecycleManager().registerEventHandler(RegistryEvents.ENCHANTMENT.freeze().newHandler(event -> { + for (EnchantioEnchant enchant : EnchantioEnchant.getEnchants().values()) { + event.registry().register(TypedKey.create(RegistryKey.ENCHANTMENT, enchant.getKey()), enchantment -> { + enchantment.description(enchant.getDescription()); + enchantment.anvilCost(enchant.getAnvilCost()); + enchantment.maxLevel(enchant.getMaxLevel()); + enchantment.weight(enchant.getWeight()); + enchantment.minimumCost(enchant.getMinimumCost()); + enchantment.maximumCost(enchant.getMaximumCost()); + enchantment.activeSlots(enchant.getActiveSlots()); + enchantment.supportedItems(event.getOrCreateTag(ItemTypeTagKeys.ENCHANTABLE_ARMOR)); + }); + } + })); + + } + + @Override + public @NotNull JavaPlugin createPlugin(@NotNull PluginProviderContext context) { + return PluginBootstrap.super.createPlugin(context); + } +} diff --git a/src/main/java/me/youhavetrouble/enchantio/EnchantioConfig.java b/src/main/java/me/youhavetrouble/enchantio/EnchantioConfig.java new file mode 100644 index 0000000..e289d33 --- /dev/null +++ b/src/main/java/me/youhavetrouble/enchantio/EnchantioConfig.java @@ -0,0 +1,16 @@ +package me.youhavetrouble.enchantio; + +public class EnchantioConfig { + + private final Enchantio plugin; + + + + protected EnchantioConfig(Enchantio plugin) { + this.plugin = plugin; + plugin.saveDefaultConfig(); + plugin.reloadConfig(); + + } + +} diff --git a/src/main/java/me/youhavetrouble/enchantio/enchants/EnchantioEnchant.java b/src/main/java/me/youhavetrouble/enchantio/enchants/EnchantioEnchant.java new file mode 100644 index 0000000..ea622b0 --- /dev/null +++ b/src/main/java/me/youhavetrouble/enchantio/enchants/EnchantioEnchant.java @@ -0,0 +1,96 @@ +package me.youhavetrouble.enchantio.enchants; + +import io.papermc.paper.registry.data.EnchantmentRegistryEntry; +import io.papermc.paper.registry.tag.TagKey; +import net.kyori.adventure.key.Key; +import net.kyori.adventure.text.Component; +import org.bukkit.inventory.EquipmentSlotGroup; +import org.bukkit.inventory.ItemType; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +@SuppressWarnings("all") +public abstract class EnchantioEnchant { + + private static final Map enchantioEnchants = new HashMap(); + + private final Key key; + private final int anvilCost; + private final int maxLevel; + private final int weight; + private final Component description; + private final EnchantmentRegistryEntry.EnchantmentCost minimumCost; + private final EnchantmentRegistryEntry.EnchantmentCost maximumCost; + private final Set> supportedItems; + private final Set activeSlots; + + public EnchantioEnchant( + Key key, + Component description, + int anvilCost, + int maxLevel, + int weight, + EnchantmentRegistryEntry.EnchantmentCost minimumCost, + EnchantmentRegistryEntry.EnchantmentCost maximumCost, + Set> supportedItems, + Set activeSlots + ) { + this.key = key; + this.description = description; + this.anvilCost = anvilCost; + this.maxLevel = maxLevel; + this.weight = weight; + this.minimumCost = minimumCost; + this.maximumCost = maximumCost; + this.supportedItems = supportedItems; + this.activeSlots = activeSlots; + } + + public Key getKey() { + return key; + } + + public Component getDescription() { + return description; + } + + public int getAnvilCost() { + return anvilCost; + } + + public int getMaxLevel() { + return maxLevel; + } + + public int getWeight() { + return weight; + } + + public EnchantmentRegistryEntry.EnchantmentCost getMinimumCost() { + return minimumCost; + } + + public EnchantmentRegistryEntry.EnchantmentCost getMaximumCost() { + return maximumCost; + } + + public Set> getSupportedItems() { + return supportedItems; + } + + public Iterable getActiveSlots() { + return activeSlots; + } + + protected static void registerEnchant(EnchantioEnchant enchant) { + enchantioEnchants.put(enchant.getKey().asString(), enchant); + } + + public static Map getEnchants() { + return Collections.unmodifiableMap(enchantioEnchants); + } + +} diff --git a/src/main/java/me/youhavetrouble/enchantio/enchants/SoulboundEnchant.java b/src/main/java/me/youhavetrouble/enchantio/enchants/SoulboundEnchant.java new file mode 100644 index 0000000..f68d74a --- /dev/null +++ b/src/main/java/me/youhavetrouble/enchantio/enchants/SoulboundEnchant.java @@ -0,0 +1,36 @@ +package me.youhavetrouble.enchantio.enchants; + +import io.papermc.paper.registry.data.EnchantmentRegistryEntry; +import io.papermc.paper.registry.keys.tags.ItemTypeTagKeys; +import net.kyori.adventure.key.Key; +import net.kyori.adventure.text.Component; +import org.bukkit.inventory.EquipmentSlotGroup; +import java.util.Set; + +public class SoulboundEnchant extends EnchantioEnchant { + public SoulboundEnchant() { + super( + Key.key("enchantio:soulbound"), + Component.translatable("enchantio.enchant.soulbound","Soulbound"), + 1, + 1, + 10, + EnchantmentRegistryEntry.EnchantmentCost.of(1, 1), + EnchantmentRegistryEntry.EnchantmentCost.of(3, 1), + Set.of( + ItemTypeTagKeys.AXES, + ItemTypeTagKeys.PICKAXES, + ItemTypeTagKeys.SWORDS, + ItemTypeTagKeys.HOES, + ItemTypeTagKeys.SHOVELS, + ItemTypeTagKeys.ENCHANTABLE_BOW, + ItemTypeTagKeys.ENCHANTABLE_CROSSBOW, + ItemTypeTagKeys.ENCHANTABLE_MACE, + ItemTypeTagKeys.ENCHANTABLE_WEAPON, + ItemTypeTagKeys.ENCHANTABLE_ARMOR + ), + Set.of(EquipmentSlotGroup.ANY) + ); + registerEnchant(this); + } +} diff --git a/src/main/java/me/youhavetrouble/enchantio/listeners/SoulboundListener.java b/src/main/java/me/youhavetrouble/enchantio/listeners/SoulboundListener.java new file mode 100644 index 0000000..31d1e81 --- /dev/null +++ b/src/main/java/me/youhavetrouble/enchantio/listeners/SoulboundListener.java @@ -0,0 +1,31 @@ +package me.youhavetrouble.enchantio.listeners; + +import io.papermc.paper.registry.RegistryAccess; +import io.papermc.paper.registry.RegistryKey; +import me.youhavetrouble.enchantio.Enchantio; +import net.kyori.adventure.key.Key; +import org.bukkit.Registry; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.PlayerDeathEvent; + +public class SoulboundListener implements Listener { + + @EventHandler(ignoreCancelled = true, priority = EventPriority.NORMAL) + public void onSoulboundEnchantDeath(PlayerDeathEvent event) { + Registry registry = RegistryAccess.registryAccess().getRegistry(RegistryKey.ENCHANTMENT); + Enchantment soulbound = registry.get(Key.key("enchantio:soulbound")); + Enchantio.getPlugin(Enchantio.class).getLogger().info("Soulbound enchantment: " + soulbound); + + event.getPlayer().getInventory().forEach(itemStack -> { + if (itemStack != null && itemStack.getEnchantments().containsKey(soulbound)) { + event.getItemsToKeep().add(itemStack); + event.getDrops().remove(itemStack); + } + }); + + } + +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/paper-plugin.yml b/src/main/resources/paper-plugin.yml new file mode 100644 index 0000000..a30fd11 --- /dev/null +++ b/src/main/resources/paper-plugin.yml @@ -0,0 +1,8 @@ +name: Enchantio +version: '${project.version}' +main: me.youhavetrouble.enchantio.Enchantio +bootstrapper: me.youhavetrouble.enchantio.EnchantioBootstrap +api-version: '1.21' +authors: [YouHaveTrouble] +description: Custom enchants for paper servers +website: https://youhavetrouble.me