playing around with enchants

This commit is contained in:
2024-10-17 19:19:25 +02:00
commit 4010e88237
10 changed files with 435 additions and 0 deletions
@@ -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
}
}
@@ -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);
}
}
@@ -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();
}
}
@@ -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<String, EnchantioEnchant> 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<TagKey<ItemType>> supportedItems;
private final Set<EquipmentSlotGroup> activeSlots;
public EnchantioEnchant(
Key key,
Component description,
int anvilCost,
int maxLevel,
int weight,
EnchantmentRegistryEntry.EnchantmentCost minimumCost,
EnchantmentRegistryEntry.EnchantmentCost maximumCost,
Set<TagKey<ItemType>> supportedItems,
Set<EquipmentSlotGroup> 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<TagKey<ItemType>> getSupportedItems() {
return supportedItems;
}
public Iterable<EquipmentSlotGroup> getActiveSlots() {
return activeSlots;
}
protected static void registerEnchant(EnchantioEnchant enchant) {
enchantioEnchants.put(enchant.getKey().asString(), enchant);
}
public static Map<String, EnchantioEnchant> getEnchants() {
return Collections.unmodifiableMap(enchantioEnchants);
}
}
@@ -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);
}
}
@@ -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<Enchantment> 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);
}
});
}
}
View File
+8
View File
@@ -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