refactor to allow adding enchants to arbitrary tags

This commit is contained in:
2024-10-28 16:01:51 +01:00
parent deb4d0d67a
commit 3752a9e31a
8 changed files with 93 additions and 89 deletions
@@ -6,16 +6,12 @@ import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
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.EnchantmentTagKeys;
import io.papermc.paper.registry.keys.tags.ItemTypeTagKeys;
import io.papermc.paper.tag.TagEntry;
import me.youhavetrouble.enchantio.enchants.EnchantioEnchant;
import org.bukkit.enchantments.Enchantment;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;
@@ -26,19 +22,18 @@ public class EnchantioBootstrap implements PluginBootstrap {
@Override
public void bootstrap(@NotNull BootstrapContext context) {
EnchantioConfig config;
try {
config = new EnchantioConfig(context.getDataDirectory(), logger);
new EnchantioConfig(context.getDataDirectory(), logger);
} catch (IOException e) {
throw new RuntimeException(e);
}
Collection<EnchantioEnchant> enchantioEnchants = EnchantioConfig.ENCHANTS.values();
logger.info("Registering supported item tags");
logger.fine("Registering supported item tags");
context.getLifecycleManager().registerEventHandler(LifecycleEvents.TAGS.preFlatten(RegistryKey.ITEM).newHandler((event) -> {
for (EnchantioEnchant enchant : enchantioEnchants) {
logger.info("Registering item tag " + enchant.getTagForSupportedItems().key());
logger.fine("Registering item tag " + enchant.getTagForSupportedItems().key());
event.registrar().addToTag(
ItemTypeTagKeys.create(enchant.getTagForSupportedItems().key()),
enchant.getSupportedItems()
@@ -48,7 +43,7 @@ public class EnchantioBootstrap implements PluginBootstrap {
context.getLifecycleManager().registerEventHandler(RegistryEvents.ENCHANTMENT.freeze().newHandler(event -> {
for (EnchantioEnchant enchant : enchantioEnchants) {
logger.info("Registering enchantment " + enchant.getKey());
logger.fine("Registering enchantment " + enchant.getKey());
event.registry().register(TypedKey.create(RegistryKey.ENCHANTMENT, enchant.getKey()), enchantment -> {
enchantment.description(enchant.getDescription());
enchantment.anvilCost(enchant.getAnvilCost());
@@ -63,13 +58,12 @@ public class EnchantioBootstrap implements PluginBootstrap {
}));
context.getLifecycleManager().registerEventHandler(LifecycleEvents.TAGS.preFlatten(RegistryKey.ENCHANTMENT).newHandler((event) -> {
Set<TagEntry<Enchantment>> enchantTags = new HashSet<>(enchantioEnchants.size());
for (EnchantioEnchant enchant : enchantioEnchants) {
if (!enchant.canGetFromEnchantingTable()) continue;
logger.info("Registering enchantment " + enchant.getKey() + " to enchanting table possibilities");
enchantTags.add(enchant.getTagEntry());
enchant.getEnchantTagKeys().forEach(enchantmentTagKey -> {
logger.fine("Registering enchantment tag " + enchantmentTagKey.key());
event.registrar().addToTag(enchantmentTagKey, Set.of(enchant.getTagEntry()));
});
}
event.registrar().addToTag(EnchantmentTagKeys.IN_ENCHANTING_TABLE, enchantTags);
}));
}
@@ -2,13 +2,17 @@ package me.youhavetrouble.enchantio.enchants;
import io.papermc.paper.registry.RegistryKey;
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 net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.EquipmentSlotGroup;
import org.bukkit.inventory.ItemType;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
@SuppressWarnings("UnstableApiUsage")
@@ -19,9 +23,9 @@ public class BeheadingEnchant implements EnchantioEnchant {
private final int anvilCost, weight, maxLevel;
private final EnchantmentRegistryEntry.EnchantmentCost minimumCost;
private final EnchantmentRegistryEntry.EnchantmentCost maximumCost;
private final boolean canGetFromEnchantingTable;
private final Set<TagEntry<ItemType>> supportedItemTags;
private final double chanceToDropHeadPerLevel;
private final Set<TagKey<Enchantment>> enchantTagKeys = new HashSet<>();
public BeheadingEnchant(
int anvilCost,
@@ -37,10 +41,13 @@ public class BeheadingEnchant implements EnchantioEnchant {
this.weight = weight;
this.minimumCost = minimumCost;
this.maximumCost = maximumCost;
this.canGetFromEnchantingTable = canGetFromEnchantingTable;
this.supportedItemTags = supportedItemTags;
this.maxLevel = maxLevel;
this.chanceToDropHeadPerLevel = chanceToDropHeadPerLevel;
if (canGetFromEnchantingTable) {
enchantTagKeys.add(EnchantmentTagKeys.IN_ENCHANTING_TABLE);
}
}
@Override
@@ -87,11 +94,6 @@ public class BeheadingEnchant implements EnchantioEnchant {
return Set.of(EquipmentSlotGroup.ANY);
}
@Override
public boolean canGetFromEnchantingTable() {
return canGetFromEnchantingTable;
}
@Override
public TagKey<ItemType> getTagForSupportedItems() {
return TagKey.create(RegistryKey.ITEM, Key.key("enchantio:beheading_enchantable"));
@@ -102,4 +104,9 @@ public class BeheadingEnchant implements EnchantioEnchant {
return supportedItemTags;
}
@Override
public Set<TagKey<Enchantment>> getEnchantTagKeys() {
return Collections.unmodifiableSet(enchantTagKeys);
}
}
@@ -32,12 +32,14 @@ public interface EnchantioEnchant {
Iterable<EquipmentSlotGroup> getActiveSlots();
boolean canGetFromEnchantingTable();
TagKey<ItemType> getTagForSupportedItems();
Set<TagEntry<ItemType>> getSupportedItems();
Set<TagKey<Enchantment>> getEnchantTagKeys();
default TagKey<ItemType> getTagForSupportedItems() {
return TagKey.create(RegistryKey.ITEM, Key.key( getKey().asString() + "_enchantable"));
}
default TagEntry<Enchantment> getTagEntry() {
return TagEntry.valueEntry(TypedKey.create(RegistryKey.ENCHANTMENT, getKey()));
}
@@ -2,13 +2,17 @@ package me.youhavetrouble.enchantio.enchants;
import io.papermc.paper.registry.RegistryKey;
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 net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.EquipmentSlotGroup;
import org.bukkit.inventory.ItemType;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
@SuppressWarnings("UnstableApiUsage")
@@ -19,9 +23,9 @@ public class ExecutionerEnchant implements EnchantioEnchant {
private final int anvilCost, weight, maxLevel;
private final EnchantmentRegistryEntry.EnchantmentCost minimumCost;
private final EnchantmentRegistryEntry.EnchantmentCost maximumCost;
private final boolean canGetFromEnchantingTable;
private final Set<TagEntry<ItemType>> supportedItemTags;
private final double damageMultiplierPerLevel, maxDamageHpThreshold;
private final Set<TagKey<Enchantment>> enchantTagKeys = new HashSet<>();
public ExecutionerEnchant(
@@ -39,11 +43,13 @@ public class ExecutionerEnchant implements EnchantioEnchant {
this.weight = weight;
this.minimumCost = minimumCost;
this.maximumCost = maximumCost;
this.canGetFromEnchantingTable = canGetFromEnchantingTable;
this.supportedItemTags = supportedItemTags;
this.maxLevel = maxLevel;
this.damageMultiplierPerLevel = damageMultiplierPerLevel;
this.maxDamageHpThreshold = maxDamageHpThreshold;
if (canGetFromEnchantingTable) {
enchantTagKeys.add(EnchantmentTagKeys.IN_ENCHANTING_TABLE);
}
}
@Override
@@ -94,19 +100,14 @@ public class ExecutionerEnchant implements EnchantioEnchant {
return Set.of(EquipmentSlotGroup.HAND);
}
@Override
public boolean canGetFromEnchantingTable() {
return canGetFromEnchantingTable;
}
@Override
public TagKey<ItemType> getTagForSupportedItems() {
return TagKey.create(RegistryKey.ITEM, Key.key("enchantio:executioner_enchantable"));
}
@Override
public Set<TagEntry<ItemType>> getSupportedItems() {
return supportedItemTags;
}
@Override
public Set<TagKey<Enchantment>> getEnchantTagKeys() {
return Collections.unmodifiableSet(enchantTagKeys);
}
}
@@ -1,14 +1,17 @@
package me.youhavetrouble.enchantio.enchants;
import io.papermc.paper.registry.RegistryKey;
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 net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.EquipmentSlotGroup;
import org.bukkit.inventory.ItemType;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
@SuppressWarnings("UnstableApiUsage")
@@ -19,8 +22,8 @@ public class ReplantingEnchant implements EnchantioEnchant {
private final int anvilCost, weight;
private final EnchantmentRegistryEntry.EnchantmentCost minimumCost;
private final EnchantmentRegistryEntry.EnchantmentCost maximumCost;
private final boolean canGetFromEnchantingTable;
private final Set<TagEntry<ItemType>> supportedItemTags;
private final Set<TagKey<Enchantment>> enchantTagKeys = new HashSet<>();
public ReplantingEnchant(
int anvilCost,
@@ -34,8 +37,10 @@ public class ReplantingEnchant implements EnchantioEnchant {
this.weight = weight;
this.minimumCost = minimumCost;
this.maximumCost = maximumCost;
this.canGetFromEnchantingTable = canGetFromEnchantingTable;
this.supportedItemTags = supportedItemTags;
if (canGetFromEnchantingTable) {
enchantTagKeys.add(EnchantmentTagKeys.IN_ENCHANTING_TABLE);
}
}
@Override
@@ -78,19 +83,14 @@ public class ReplantingEnchant implements EnchantioEnchant {
return Set.of(EquipmentSlotGroup.HAND);
}
@Override
public boolean canGetFromEnchantingTable() {
return canGetFromEnchantingTable;
}
@Override
public TagKey<ItemType> getTagForSupportedItems() {
return TagKey.create(RegistryKey.ITEM, Key.key("enchantio:replanting_enchantable"));
}
@Override
public Set<TagEntry<ItemType>> getSupportedItems() {
return supportedItemTags;
}
@Override
public Set<TagKey<Enchantment>> getEnchantTagKeys() {
return Collections.unmodifiableSet(enchantTagKeys);
}
}
@@ -1,14 +1,17 @@
package me.youhavetrouble.enchantio.enchants;
import io.papermc.paper.registry.RegistryKey;
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 net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.EquipmentSlotGroup;
import org.bukkit.inventory.ItemType;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
@SuppressWarnings("UnstableApiUsage")
@@ -19,8 +22,8 @@ public class SmeltingEnchant implements EnchantioEnchant {
private final int anvilCost, weight;
private final EnchantmentRegistryEntry.EnchantmentCost minimumCost;
private final EnchantmentRegistryEntry.EnchantmentCost maximumCost;
private final boolean canGetFromEnchantingTable;
private final Set<TagEntry<ItemType>> supportedItemTags;
private final Set<TagKey<Enchantment>> enchantTagKeys = new HashSet<>();
public SmeltingEnchant(
int anvilCost,
@@ -34,8 +37,10 @@ public class SmeltingEnchant implements EnchantioEnchant {
this.weight = weight;
this.minimumCost = minimumCost;
this.maximumCost = maximumCost;
this.canGetFromEnchantingTable = canGetFromEnchantingTable;
this.supportedItemTags = supportedItemTags;
if (canGetFromEnchantingTable) {
enchantTagKeys.add(EnchantmentTagKeys.IN_ENCHANTING_TABLE);
}
}
@Override
@@ -78,19 +83,14 @@ public class SmeltingEnchant implements EnchantioEnchant {
return Set.of(EquipmentSlotGroup.ANY);
}
@Override
public boolean canGetFromEnchantingTable() {
return canGetFromEnchantingTable;
}
@Override
public TagKey<ItemType> getTagForSupportedItems() {
return TagKey.create(RegistryKey.ITEM, Key.key("enchantio:smelting_enchantable"));
}
@Override
public Set<TagEntry<ItemType>> getSupportedItems() {
return supportedItemTags;
}
@Override
public Set<TagKey<Enchantment>> getEnchantTagKeys() {
return Collections.unmodifiableSet(enchantTagKeys);
}
}
@@ -1,14 +1,17 @@
package me.youhavetrouble.enchantio.enchants;
import io.papermc.paper.registry.RegistryKey;
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 net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.EquipmentSlotGroup;
import org.bukkit.inventory.ItemType;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
@SuppressWarnings("UnstableApiUsage")
@@ -19,8 +22,8 @@ public class SoulboundEnchant implements EnchantioEnchant {
private final int anvilCost, weight;
private final EnchantmentRegistryEntry.EnchantmentCost minimumCost;
private final EnchantmentRegistryEntry.EnchantmentCost maximumCost;
private final boolean canGetFromEnchantingTable;
private final Set<TagEntry<ItemType>> supportedItemTags;
private final Set<TagKey<Enchantment>> enchantTagKeys = new HashSet<>();
public SoulboundEnchant(
int anvilCost,
@@ -34,8 +37,10 @@ public class SoulboundEnchant implements EnchantioEnchant {
this.weight = weight;
this.minimumCost = minimumCost;
this.maximumCost = maximumCost;
this.canGetFromEnchantingTable = canGetFromEnchantingTable;
this.supportedItemTags = supportedItemTags;
if (canGetFromEnchantingTable) {
enchantTagKeys.add(EnchantmentTagKeys.IN_ENCHANTING_TABLE);
}
}
@Override
@@ -78,19 +83,14 @@ public class SoulboundEnchant implements EnchantioEnchant {
return Set.of(EquipmentSlotGroup.ANY);
}
@Override
public boolean canGetFromEnchantingTable() {
return canGetFromEnchantingTable;
}
@Override
public TagKey<ItemType> getTagForSupportedItems() {
return TagKey.create(RegistryKey.ITEM, Key.key("enchantio:soulbound_enchantable"));
}
@Override
public Set<TagEntry<ItemType>> getSupportedItems() {
return supportedItemTags;
}
@Override
public Set<TagKey<Enchantment>> getEnchantTagKeys() {
return Collections.unmodifiableSet(enchantTagKeys);
}
}
@@ -1,14 +1,17 @@
package me.youhavetrouble.enchantio.enchants;
import io.papermc.paper.registry.RegistryKey;
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 net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.EquipmentSlotGroup;
import org.bukkit.inventory.ItemType;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
@SuppressWarnings("UnstableApiUsage")
@@ -19,8 +22,8 @@ public class TelepathyEnchant implements EnchantioEnchant {
private final int anvilCost, weight;
private final EnchantmentRegistryEntry.EnchantmentCost minimumCost;
private final EnchantmentRegistryEntry.EnchantmentCost maximumCost;
private final boolean canGetFromEnchantingTable;
private final Set<TagEntry<ItemType>> supportedItemTags;
private final Set<TagKey<Enchantment>> enchantTagKeys = new HashSet<>();
public TelepathyEnchant(
int anvilCost,
@@ -34,8 +37,10 @@ public class TelepathyEnchant implements EnchantioEnchant {
this.weight = weight;
this.minimumCost = minimumCost;
this.maximumCost = maximumCost;
this.canGetFromEnchantingTable = canGetFromEnchantingTable;
this.supportedItemTags = supportedItemTags;
if (canGetFromEnchantingTable) {
enchantTagKeys.add(EnchantmentTagKeys.IN_ENCHANTING_TABLE);
}
}
@Override
@@ -78,19 +83,14 @@ public class TelepathyEnchant implements EnchantioEnchant {
return Set.of(EquipmentSlotGroup.HAND);
}
@Override
public boolean canGetFromEnchantingTable() {
return canGetFromEnchantingTable;
}
@Override
public TagKey<ItemType> getTagForSupportedItems() {
return TagKey.create(RegistryKey.ITEM, Key.key("enchantio:telepathy_enchantable"));
}
@Override
public Set<TagEntry<ItemType>> getSupportedItems() {
return supportedItemTags;
}
@Override
public Set<TagKey<Enchantment>> getEnchantTagKeys() {
return Collections.unmodifiableSet(enchantTagKeys);
}
}