add ward enchantment

This commit is contained in:
2025-05-13 23:45:45 +02:00
parent 9a9bef2068
commit eb3f7888ea
5 changed files with 267 additions and 0 deletions
@@ -8,9 +8,12 @@ import io.papermc.paper.tag.TagEntry;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.EquipmentSlotGroup;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ItemType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Set;
@@ -54,4 +57,36 @@ public interface EnchantioEnchant {
return TagEntry.valueEntry(TypedKey.create(RegistryKey.ENCHANTMENT, getKey()));
}
static @Nullable ItemStack findFirstWithEnchant(
@NotNull EntityEquipment equipment,
@NotNull Enchantment enchantment) {
Set<EquipmentSlotGroup> equipmentSlotGroups = enchantment.getActiveSlotGroups();
if (equipmentSlotGroups.contains(EquipmentSlotGroup.ANY) || equipmentSlotGroups.contains(EquipmentSlotGroup.HAND) || equipmentSlotGroups.contains(EquipmentSlotGroup.MAINHAND)) {
if (equipment.getItemInMainHand().getEnchantmentLevel(enchantment) > 0) return equipment.getItemInMainHand();
}
if (equipmentSlotGroups.contains(EquipmentSlotGroup.ANY) || equipmentSlotGroups.contains(EquipmentSlotGroup.HAND) || equipmentSlotGroups.contains(EquipmentSlotGroup.OFFHAND)) {
if (equipment.getItemInOffHand().getEnchantmentLevel(enchantment) > 0) return equipment.getItemInOffHand();
}
if (equipmentSlotGroups.contains(EquipmentSlotGroup.ANY) || equipmentSlotGroups.contains(EquipmentSlotGroup.ARMOR) || equipmentSlotGroups.contains(EquipmentSlotGroup.HEAD)) {
ItemStack helmet = equipment.getHelmet();
if (helmet != null && helmet.getEnchantmentLevel(enchantment) > 0) return helmet;
}
if (equipmentSlotGroups.contains(EquipmentSlotGroup.ANY) || equipmentSlotGroups.contains(EquipmentSlotGroup.ARMOR) || equipmentSlotGroups.contains(EquipmentSlotGroup.CHEST)) {
ItemStack chestplate = equipment.getChestplate();
if (chestplate != null && chestplate.getEnchantmentLevel(enchantment) > 0) return chestplate;
}
if (equipmentSlotGroups.contains(EquipmentSlotGroup.ANY) || equipmentSlotGroups.contains(EquipmentSlotGroup.ARMOR) || equipmentSlotGroups.contains(EquipmentSlotGroup.LEGS)) {
ItemStack leggings = equipment.getLeggings();
if (leggings != null && leggings.getEnchantmentLevel(enchantment) > 0) return leggings;
}
if (equipmentSlotGroups.contains(EquipmentSlotGroup.ANY) || equipmentSlotGroups.contains(EquipmentSlotGroup.ARMOR) || equipmentSlotGroups.contains(EquipmentSlotGroup.FEET)) {
ItemStack boots = equipment.getBoots();
if (boots != null && boots.getEnchantmentLevel(enchantment) > 0) return boots;
}
return null;
}
}
@@ -0,0 +1,156 @@
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 WardEnchant implements EnchantioEnchant {
public static final Key KEY = Key.key("enchantio:ward");
private final int anvilCost, weight, cooldownTicks;
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<>();
private final String blockSound;
public WardEnchant(
int anvilCost,
int weight,
EnchantmentRegistryEntry.EnchantmentCost minimumCost,
EnchantmentRegistryEntry.EnchantmentCost maximumCost,
boolean canGetFromEnchantingTable,
Set<TagEntry<ItemType>> supportedItemTags,
Set<EquipmentSlotGroup> activeSlots,
int cooldownTicks,
String blockSound
) {
this.anvilCost = anvilCost;
this.weight = weight;
this.minimumCost = minimumCost;
this.maximumCost = maximumCost;
this.supportedItemTags = supportedItemTags;
this.activeSlots.addAll(activeSlots);
this.cooldownTicks = cooldownTicks;
this.blockSound = blockSound;
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.ward", "Ward");
}
@Override
public int getAnvilCost() {
return anvilCost;
}
@Override
public int getMaxLevel() {
return 1;
}
@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;
}
public int getCooldownTicks() {
return cooldownTicks;
}
public @NotNull String getBlockSound() {
return blockSound;
}
@Override
public @NotNull Set<TagKey<Enchantment>> getEnchantTagKeys() {
return Collections.unmodifiableSet(enchantTagKeys);
}
public static WardEnchant create(ConfigurationSection configurationSection) {
WardEnchant wardEnchant = new WardEnchant(
EnchantioConfig.getInt(configurationSection, "anvilCost", 12),
EnchantioConfig.getInt(configurationSection, "weight", 2),
EnchantmentRegistryEntry.EnchantmentCost.of(
EnchantioConfig.getInt(configurationSection, "minimumCost.base", 35),
EnchantioConfig.getInt(configurationSection, "minimumCost.additionalPerLevel", 1)
),
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:shield"
)
)),
EnchantioConfig.getEquipmentSlotGroups(EnchantioConfig.getStringList(
configurationSection,
"activeSlots",
List.of(
"OFF_HAND"
)
)),
EnchantioConfig.getInt(configurationSection, "cooldownTicks", 40),
EnchantioConfig.getString(configurationSection, "blockSound", "minecraft:item.shield.block")
);
if (EnchantioConfig.getBoolean(configurationSection, "enabled", true)) {
ENCHANTS.put(WardEnchant.KEY, wardEnchant);
}
return wardEnchant;
}
}