refactor yet again to allow encahts to support multiple tags

This commit is contained in:
2024-10-19 20:15:13 +02:00
parent 8e7d95d7f8
commit 61ed32ccfa
4 changed files with 153 additions and 122 deletions
@@ -8,6 +8,7 @@ import io.papermc.paper.registry.RegistryKey;
import io.papermc.paper.registry.TypedKey; import io.papermc.paper.registry.TypedKey;
import io.papermc.paper.registry.event.RegistryEvents; import io.papermc.paper.registry.event.RegistryEvents;
import io.papermc.paper.registry.keys.tags.EnchantmentTagKeys; import io.papermc.paper.registry.keys.tags.EnchantmentTagKeys;
import io.papermc.paper.registry.keys.tags.ItemTypeTagKeys;
import io.papermc.paper.tag.TagEntry; import io.papermc.paper.tag.TagEntry;
import me.youhavetrouble.enchantio.enchants.EnchantioEnchant; import me.youhavetrouble.enchantio.enchants.EnchantioEnchant;
import me.youhavetrouble.enchantio.enchants.SoulboundEnchant; import me.youhavetrouble.enchantio.enchants.SoulboundEnchant;
@@ -24,11 +25,22 @@ public class EnchantioBootstrap implements PluginBootstrap {
@Override @Override
public void bootstrap(@NotNull BootstrapContext context) { public void bootstrap(@NotNull BootstrapContext context) {
EnchantioEnchant soulbound = new SoulboundEnchant(); Set<EnchantioEnchant> enchantioEnchants = Set.of(
EnchantioEnchant telepathy = new TelepathyEnchant(); new SoulboundEnchant(),
new TelepathyEnchant()
);
context.getLifecycleManager().registerEventHandler(LifecycleEvents.TAGS.preFlatten(RegistryKey.ITEM).newHandler((event) -> {
for (EnchantioEnchant enchant : enchantioEnchants) {
event.registrar().addToTag(
ItemTypeTagKeys.create(enchant.getTagForSupportedItems().key()),
enchant.getSupportedItems()
);
}
}));
context.getLifecycleManager().registerEventHandler(RegistryEvents.ENCHANTMENT.freeze().newHandler(event -> { context.getLifecycleManager().registerEventHandler(RegistryEvents.ENCHANTMENT.freeze().newHandler(event -> {
for (EnchantioEnchant enchant : EnchantioEnchant.getEnchants().values()) { for (EnchantioEnchant enchant : enchantioEnchants) {
event.registry().register(TypedKey.create(RegistryKey.ENCHANTMENT, enchant.getKey()), enchantment -> { event.registry().register(TypedKey.create(RegistryKey.ENCHANTMENT, enchant.getKey()), enchantment -> {
enchantment.description(enchant.getDescription()); enchantment.description(enchant.getDescription());
enchantment.anvilCost(enchant.getAnvilCost()); enchantment.anvilCost(enchant.getAnvilCost());
@@ -37,15 +49,14 @@ public class EnchantioBootstrap implements PluginBootstrap {
enchantment.minimumCost(enchant.getMinimumCost()); enchantment.minimumCost(enchant.getMinimumCost());
enchantment.maximumCost(enchant.getMaximumCost()); enchantment.maximumCost(enchant.getMaximumCost());
enchantment.activeSlots(enchant.getActiveSlots()); enchantment.activeSlots(enchant.getActiveSlots());
enchantment.supportedItems(event.getOrCreateTag(enchant.getSupportedItems())); enchantment.supportedItems(event.getOrCreateTag(enchant.getTagForSupportedItems()));
enchantment.primaryItems(event.getOrCreateTag(enchant.getPrimaryItems()));
}); });
} }
})); }));
context.getLifecycleManager().registerEventHandler(LifecycleEvents.TAGS.preFlatten(RegistryKey.ENCHANTMENT).newHandler((event) -> { context.getLifecycleManager().registerEventHandler(LifecycleEvents.TAGS.preFlatten(RegistryKey.ENCHANTMENT).newHandler((event) -> {
Set<TagEntry<Enchantment>> enchantTags = new HashSet<>(EnchantioEnchant.getEnchants().size()); Set<TagEntry<Enchantment>> enchantTags = new HashSet<>(enchantioEnchants.size());
for (EnchantioEnchant enchant : EnchantioEnchant.getEnchants().values()) { for (EnchantioEnchant enchant : enchantioEnchants) {
if (!enchant.canGetFromEnchantingTable()) continue; if (!enchant.canGetFromEnchantingTable()) continue;
enchantTags.add(enchant.getTagEntry()); enchantTags.add(enchant.getTagEntry());
} }
@@ -12,107 +12,36 @@ import org.bukkit.inventory.EquipmentSlotGroup;
import org.bukkit.inventory.ItemType; import org.bukkit.inventory.ItemType;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@SuppressWarnings("all") @SuppressWarnings("all")
public abstract class EnchantioEnchant { public interface EnchantioEnchant {
private static final Map<String, EnchantioEnchant> enchantioEnchants = new HashMap(); public Key getKey();
private final Key key; public Component getDescription();
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 TagKey<ItemType> supportedItems;
private final TagKey<ItemType> primaryItems;
private final Set<EquipmentSlotGroup> activeSlots;
private final boolean canGetFromEnchantingTable;
public EnchantioEnchant( public int getAnvilCost();
Key key,
Component description,
int anvilCost,
int maxLevel,
int weight,
EnchantmentRegistryEntry.EnchantmentCost minimumCost,
EnchantmentRegistryEntry.EnchantmentCost maximumCost,
TagKey<ItemType> primaryItems,
TagKey<ItemType> supportedItems,
Set<EquipmentSlotGroup> activeSlots,
boolean canGetFromEnchantingTable
) {
this.key = key;
this.description = description;
this.anvilCost = anvilCost;
this.maxLevel = maxLevel;
this.weight = weight;
this.minimumCost = minimumCost;
this.maximumCost = maximumCost;
this.primaryItems = primaryItems;
this.supportedItems = supportedItems;
this.activeSlots = activeSlots;
this.canGetFromEnchantingTable = canGetFromEnchantingTable;
}
public Key getKey() { public int getMaxLevel();
return key;
}
public Component getDescription() { public int getWeight();
return description;
}
public int getAnvilCost() { public EnchantmentRegistryEntry.EnchantmentCost getMinimumCost();
return anvilCost;
}
public int getMaxLevel() { public EnchantmentRegistryEntry.EnchantmentCost getMaximumCost();
return maxLevel;
}
public int getWeight() { public Iterable<EquipmentSlotGroup> getActiveSlots();
return weight;
}
public EnchantmentRegistryEntry.EnchantmentCost getMinimumCost() { public boolean canGetFromEnchantingTable();
return minimumCost;
}
public EnchantmentRegistryEntry.EnchantmentCost getMaximumCost() { public default TagEntry<Enchantment> getTagEntry() {
return maximumCost;
}
public TagKey<ItemType> getSupportedItems() {
return supportedItems;
}
public TagKey<ItemType> getPrimaryItems() {
return primaryItems;
}
public Iterable<EquipmentSlotGroup> getActiveSlots() {
return activeSlots;
}
public boolean canGetFromEnchantingTable() {
return canGetFromEnchantingTable;
}
public TagEntry<Enchantment> getTagEntry() {
return TagEntry.valueEntry(TypedKey.create(RegistryKey.ENCHANTMENT, getKey())); return TagEntry.valueEntry(TypedKey.create(RegistryKey.ENCHANTMENT, getKey()));
} }
protected static void registerEnchant(EnchantioEnchant enchant) { public TagKey<ItemType> getTagForSupportedItems();
enchantioEnchants.put(enchant.getKey().asString(), enchant);
}
public static Map<String, EnchantioEnchant> getEnchants() { public Set<TagEntry<ItemType>> getSupportedItems();
return Collections.unmodifiableMap(enchantioEnchants);
}
} }
@@ -1,30 +1,78 @@
package me.youhavetrouble.enchantio.enchants; package me.youhavetrouble.enchantio.enchants;
import io.papermc.paper.registry.RegistryKey;
import io.papermc.paper.registry.data.EnchantmentRegistryEntry; import io.papermc.paper.registry.data.EnchantmentRegistryEntry;
import io.papermc.paper.registry.keys.tags.ItemTypeTagKeys; import io.papermc.paper.registry.keys.tags.ItemTypeTagKeys;
import io.papermc.paper.registry.tag.TagKey;
import io.papermc.paper.tag.TagEntry;
import net.kyori.adventure.key.Key; import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import org.bukkit.inventory.EquipmentSlotGroup; import org.bukkit.inventory.EquipmentSlotGroup;
import org.bukkit.inventory.ItemType;
import java.util.Set; import java.util.Set;
public class SoulboundEnchant extends EnchantioEnchant { public class SoulboundEnchant implements EnchantioEnchant {
public static final Key KEY = Key.key("enchantio:soulbound"); public static final Key KEY = Key.key("enchantio:soulbound");
public SoulboundEnchant() { @Override
super( public Key getKey() {
KEY, return KEY;
Component.translatable("enchantio.enchant.soulbound","Soulbound"), }
1,
1, @Override
10, public Component getDescription() {
EnchantmentRegistryEntry.EnchantmentCost.of(10, 1), return Component.translatable("enchantio.enchant.soulbound","Soulbound");
EnchantmentRegistryEntry.EnchantmentCost.of(30, 1), }
ItemTypeTagKeys.ENCHANTABLE_ARMOR,
ItemTypeTagKeys.ENCHANTABLE_ARMOR, @Override
Set.of(EquipmentSlotGroup.ANY), public int getAnvilCost() {
true return 1;
}
@Override
public int getMaxLevel() {
return 1;
}
@Override
public int getWeight() {
return 10;
}
@Override
public EnchantmentRegistryEntry.EnchantmentCost getMinimumCost() {
return EnchantmentRegistryEntry.EnchantmentCost.of(10, 1);
}
@Override
public EnchantmentRegistryEntry.EnchantmentCost getMaximumCost() {
return EnchantmentRegistryEntry.EnchantmentCost.of(30, 1);
}
@Override
public Set<TagEntry<ItemType>> getSupportedItems() {
return Set.of(
TagEntry.tagEntry(ItemTypeTagKeys.ENCHANTABLE_ARMOR),
TagEntry.tagEntry(ItemTypeTagKeys.ENCHANTABLE_WEAPON),
TagEntry.tagEntry(ItemTypeTagKeys.ENCHANTABLE_MINING)
); );
registerEnchant(this); }
@Override
public Iterable<EquipmentSlotGroup> getActiveSlots() {
return Set.of(EquipmentSlotGroup.ANY);
}
@Override
public boolean canGetFromEnchantingTable() {
return false;
}
@Override
public TagKey<ItemType> getTagForSupportedItems() {
return TagKey.create(RegistryKey.ITEM, Key.key("enchantio:telepathy_enchantable"));
} }
} }
@@ -1,32 +1,75 @@
package me.youhavetrouble.enchantio.enchants; package me.youhavetrouble.enchantio.enchants;
import io.papermc.paper.registry.RegistryKey;
import io.papermc.paper.registry.data.EnchantmentRegistryEntry; import io.papermc.paper.registry.data.EnchantmentRegistryEntry;
import io.papermc.paper.registry.keys.tags.ItemTypeTagKeys; import io.papermc.paper.registry.keys.tags.ItemTypeTagKeys;
import io.papermc.paper.registry.tag.TagKey;
import io.papermc.paper.tag.TagEntry;
import net.kyori.adventure.key.Key; import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import org.bukkit.inventory.EquipmentSlotGroup; import org.bukkit.inventory.EquipmentSlotGroup;
import org.bukkit.inventory.ItemType;
import java.util.Set; import java.util.Set;
public class TelepathyEnchant extends EnchantioEnchant { public class TelepathyEnchant implements EnchantioEnchant {
public static final Key KEY = Key.key("enchantio:telepathy"); public static final Key KEY = Key.key("enchantio:telepathy");
public TelepathyEnchant() { @Override
super( public Key getKey() {
KEY, return KEY;
Component.translatable("enchantio.enchant.telepathy","Telepathy"),
1,
1,
4,
EnchantmentRegistryEntry.EnchantmentCost.of(15, 1),
EnchantmentRegistryEntry.EnchantmentCost.of(30, 1),
ItemTypeTagKeys.ENCHANTABLE_MINING,
ItemTypeTagKeys.ENCHANTABLE_MINING,
Set.of(EquipmentSlotGroup.ANY),
true
);
registerEnchant(this);
} }
@Override
public Component getDescription() {
return Component.translatable("enchantio.enchant.telepathy","Telepathy");
}
@Override
public int getAnvilCost() {
return 1;
}
@Override
public int getMaxLevel() {
return 1;
}
@Override
public int getWeight() {
return 5;
}
@Override
public EnchantmentRegistryEntry.EnchantmentCost getMinimumCost() {
return EnchantmentRegistryEntry.EnchantmentCost.of(15, 1);
}
@Override
public EnchantmentRegistryEntry.EnchantmentCost getMaximumCost() {
return EnchantmentRegistryEntry.EnchantmentCost.of(30, 1);
}
@Override
public Iterable<EquipmentSlotGroup> getActiveSlots() {
return Set.of(EquipmentSlotGroup.HAND);
}
@Override
public boolean canGetFromEnchantingTable() {
return true;
}
@Override
public TagKey<ItemType> getTagForSupportedItems() {
return TagKey.create(RegistryKey.ITEM, Key.key("enchantio:soulbound_enchantable"));
}
@Override
public Set<TagEntry<ItemType>> getSupportedItems() {
return Set.of(
TagEntry.tagEntry(ItemTypeTagKeys.ENCHANTABLE_MINING)
);
}
} }