mirror of
https://github.com/YouHaveTrouble/Enchantio.git
synced 2026-05-11 21:56:55 +00:00
refactor yet again to allow encahts to support multiple tags
This commit is contained in:
@@ -8,6 +8,7 @@ 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 me.youhavetrouble.enchantio.enchants.SoulboundEnchant;
|
||||
@@ -24,11 +25,22 @@ public class EnchantioBootstrap implements PluginBootstrap {
|
||||
@Override
|
||||
public void bootstrap(@NotNull BootstrapContext context) {
|
||||
|
||||
EnchantioEnchant soulbound = new SoulboundEnchant();
|
||||
EnchantioEnchant telepathy = new TelepathyEnchant();
|
||||
Set<EnchantioEnchant> enchantioEnchants = Set.of(
|
||||
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 -> {
|
||||
for (EnchantioEnchant enchant : EnchantioEnchant.getEnchants().values()) {
|
||||
for (EnchantioEnchant enchant : enchantioEnchants) {
|
||||
event.registry().register(TypedKey.create(RegistryKey.ENCHANTMENT, enchant.getKey()), enchantment -> {
|
||||
enchantment.description(enchant.getDescription());
|
||||
enchantment.anvilCost(enchant.getAnvilCost());
|
||||
@@ -37,15 +49,14 @@ public class EnchantioBootstrap implements PluginBootstrap {
|
||||
enchantment.minimumCost(enchant.getMinimumCost());
|
||||
enchantment.maximumCost(enchant.getMaximumCost());
|
||||
enchantment.activeSlots(enchant.getActiveSlots());
|
||||
enchantment.supportedItems(event.getOrCreateTag(enchant.getSupportedItems()));
|
||||
enchantment.primaryItems(event.getOrCreateTag(enchant.getPrimaryItems()));
|
||||
enchantment.supportedItems(event.getOrCreateTag(enchant.getTagForSupportedItems()));
|
||||
});
|
||||
}
|
||||
}));
|
||||
|
||||
context.getLifecycleManager().registerEventHandler(LifecycleEvents.TAGS.preFlatten(RegistryKey.ENCHANTMENT).newHandler((event) -> {
|
||||
Set<TagEntry<Enchantment>> enchantTags = new HashSet<>(EnchantioEnchant.getEnchants().size());
|
||||
for (EnchantioEnchant enchant : EnchantioEnchant.getEnchants().values()) {
|
||||
Set<TagEntry<Enchantment>> enchantTags = new HashSet<>(enchantioEnchants.size());
|
||||
for (EnchantioEnchant enchant : enchantioEnchants) {
|
||||
if (!enchant.canGetFromEnchantingTable()) continue;
|
||||
enchantTags.add(enchant.getTagEntry());
|
||||
}
|
||||
|
||||
@@ -12,107 +12,36 @@ 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 {
|
||||
public interface EnchantioEnchant {
|
||||
|
||||
private static final Map<String, EnchantioEnchant> enchantioEnchants = new HashMap();
|
||||
public Key getKey();
|
||||
|
||||
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 TagKey<ItemType> supportedItems;
|
||||
private final TagKey<ItemType> primaryItems;
|
||||
private final Set<EquipmentSlotGroup> activeSlots;
|
||||
private final boolean canGetFromEnchantingTable;
|
||||
public Component getDescription();
|
||||
|
||||
public EnchantioEnchant(
|
||||
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 int getAnvilCost();
|
||||
|
||||
public Key getKey() {
|
||||
return key;
|
||||
}
|
||||
public int getMaxLevel();
|
||||
|
||||
public Component getDescription() {
|
||||
return description;
|
||||
}
|
||||
public int getWeight();
|
||||
|
||||
public int getAnvilCost() {
|
||||
return anvilCost;
|
||||
}
|
||||
public EnchantmentRegistryEntry.EnchantmentCost getMinimumCost();
|
||||
|
||||
public int getMaxLevel() {
|
||||
return maxLevel;
|
||||
}
|
||||
public EnchantmentRegistryEntry.EnchantmentCost getMaximumCost();
|
||||
|
||||
public int getWeight() {
|
||||
return weight;
|
||||
}
|
||||
public Iterable<EquipmentSlotGroup> getActiveSlots();
|
||||
|
||||
public EnchantmentRegistryEntry.EnchantmentCost getMinimumCost() {
|
||||
return minimumCost;
|
||||
}
|
||||
public boolean canGetFromEnchantingTable();
|
||||
|
||||
public EnchantmentRegistryEntry.EnchantmentCost getMaximumCost() {
|
||||
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() {
|
||||
public default TagEntry<Enchantment> getTagEntry() {
|
||||
return TagEntry.valueEntry(TypedKey.create(RegistryKey.ENCHANTMENT, getKey()));
|
||||
}
|
||||
|
||||
protected static void registerEnchant(EnchantioEnchant enchant) {
|
||||
enchantioEnchants.put(enchant.getKey().asString(), enchant);
|
||||
}
|
||||
public TagKey<ItemType> getTagForSupportedItems();
|
||||
|
||||
public static Map<String, EnchantioEnchant> getEnchants() {
|
||||
return Collections.unmodifiableMap(enchantioEnchants);
|
||||
}
|
||||
public Set<TagEntry<ItemType>> getSupportedItems();
|
||||
|
||||
}
|
||||
|
||||
@@ -1,30 +1,78 @@
|
||||
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.ItemTypeTagKeys;
|
||||
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.inventory.EquipmentSlotGroup;
|
||||
import org.bukkit.inventory.ItemType;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class SoulboundEnchant extends EnchantioEnchant {
|
||||
public class SoulboundEnchant implements EnchantioEnchant {
|
||||
|
||||
public static final Key KEY = Key.key("enchantio:soulbound");
|
||||
|
||||
public SoulboundEnchant() {
|
||||
super(
|
||||
KEY,
|
||||
Component.translatable("enchantio.enchant.soulbound","Soulbound"),
|
||||
1,
|
||||
1,
|
||||
10,
|
||||
EnchantmentRegistryEntry.EnchantmentCost.of(10, 1),
|
||||
EnchantmentRegistryEntry.EnchantmentCost.of(30, 1),
|
||||
ItemTypeTagKeys.ENCHANTABLE_ARMOR,
|
||||
ItemTypeTagKeys.ENCHANTABLE_ARMOR,
|
||||
Set.of(EquipmentSlotGroup.ANY),
|
||||
true
|
||||
@Override
|
||||
public Key getKey() {
|
||||
return KEY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component getDescription() {
|
||||
return Component.translatable("enchantio.enchant.soulbound","Soulbound");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAnvilCost() {
|
||||
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;
|
||||
|
||||
import io.papermc.paper.registry.RegistryKey;
|
||||
import io.papermc.paper.registry.data.EnchantmentRegistryEntry;
|
||||
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.text.Component;
|
||||
import org.bukkit.inventory.EquipmentSlotGroup;
|
||||
import org.bukkit.inventory.ItemType;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class TelepathyEnchant extends EnchantioEnchant {
|
||||
public class TelepathyEnchant implements EnchantioEnchant {
|
||||
|
||||
public static final Key KEY = Key.key("enchantio:telepathy");
|
||||
|
||||
public TelepathyEnchant() {
|
||||
super(
|
||||
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 Key getKey() {
|
||||
return KEY;
|
||||
}
|
||||
|
||||
@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)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user