From df601296533a67aa573f1117f779bf6827d7d49c Mon Sep 17 00:00:00 2001 From: YouHaveTrouble Date: Mon, 21 Oct 2024 19:53:50 +0200 Subject: [PATCH] config! --- .../enchantio/EnchantioBootstrap.java | 17 +- .../enchantio/EnchantioConfig.java | 171 +++++++++++++++++- .../enchantio/enchants/ReplantingEnchant.java | 37 +++- .../enchantio/enchants/SoulboundEnchant.java | 39 ++-- .../enchantio/enchants/TelepathyEnchant.java | 37 +++- src/main/resources/config.yml | 42 +++++ 6 files changed, 301 insertions(+), 42 deletions(-) diff --git a/src/main/java/me/youhavetrouble/enchantio/EnchantioBootstrap.java b/src/main/java/me/youhavetrouble/enchantio/EnchantioBootstrap.java index 9c750bb..968aa78 100644 --- a/src/main/java/me/youhavetrouble/enchantio/EnchantioBootstrap.java +++ b/src/main/java/me/youhavetrouble/enchantio/EnchantioBootstrap.java @@ -10,12 +10,10 @@ 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.ReplantingEnchant; -import me.youhavetrouble.enchantio.enchants.SoulboundEnchant; -import me.youhavetrouble.enchantio.enchants.TelepathyEnchant; import org.bukkit.enchantments.Enchantment; import org.jetbrains.annotations.NotNull; +import java.io.IOException; import java.util.HashSet; import java.util.Set; @@ -24,11 +22,14 @@ public class EnchantioBootstrap implements PluginBootstrap { @Override public void bootstrap(@NotNull BootstrapContext context) { - Set enchantioEnchants = Set.of( - new SoulboundEnchant(), - new TelepathyEnchant(), - new ReplantingEnchant() - ); + EnchantioConfig config; + try { + config = new EnchantioConfig(context.getDataDirectory()); + } catch (IOException e) { + throw new RuntimeException(e); + } + + Set enchantioEnchants = config.enchants; context.getLifecycleManager().registerEventHandler(LifecycleEvents.TAGS.preFlatten(RegistryKey.ITEM).newHandler((event) -> { for (EnchantioEnchant enchant : enchantioEnchants) { diff --git a/src/main/java/me/youhavetrouble/enchantio/EnchantioConfig.java b/src/main/java/me/youhavetrouble/enchantio/EnchantioConfig.java index e289d33..eece81f 100644 --- a/src/main/java/me/youhavetrouble/enchantio/EnchantioConfig.java +++ b/src/main/java/me/youhavetrouble/enchantio/EnchantioConfig.java @@ -1,16 +1,177 @@ package me.youhavetrouble.enchantio; +import io.papermc.paper.registry.RegistryKey; +import io.papermc.paper.registry.TypedKey; +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 me.youhavetrouble.enchantio.enchants.EnchantioEnchant; +import me.youhavetrouble.enchantio.enchants.ReplantingEnchant; +import me.youhavetrouble.enchantio.enchants.SoulboundEnchant; +import me.youhavetrouble.enchantio.enchants.TelepathyEnchant; +import net.kyori.adventure.key.Key; +import org.bukkit.Bukkit; +import org.bukkit.Tag; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.inventory.ItemType; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Path; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +@SuppressWarnings("UnstableApiUsage") public class EnchantioConfig { - private final Enchantio plugin; + public final Set enchants = new HashSet<>(); + protected EnchantioConfig(Path filePath) throws IOException { + File file = filePath.toFile(); + if (!file.exists()) { + file.mkdirs(); + } - protected EnchantioConfig(Enchantio plugin) { - this.plugin = plugin; - plugin.saveDefaultConfig(); - plugin.reloadConfig(); + File configFile = new File(filePath.toFile(), "config.yml"); + if (!configFile.exists()) { + try (InputStream in = getClass().getResourceAsStream("/config.yml")) { + if (in == null) { + throw new IOException("Failed to load config.yml from resources"); + } + + try (FileOutputStream out = new FileOutputStream(configFile)) { + byte[] buffer = new byte[1024]; + int bytesRead; + while ((bytesRead = in.read(buffer)) != -1) { + out.write(buffer, 0, bytesRead); + } + } + + } catch (IOException e) { + throw new IOException("Failed saving default config", e); + } + } + + FileConfiguration configuration = YamlConfiguration.loadConfiguration(configFile); + + ConfigurationSection enchantsSection = configuration.getConfigurationSection("enchants"); + if (enchantsSection == null) { + throw new IOException("Failed to load enchants section from config"); + } + + ConfigurationSection soulboundSection = enchantsSection.getConfigurationSection("soulbound"); + if (soulboundSection == null) { + soulboundSection = enchantsSection.createSection("soulbound"); + } + enchants.add(new SoulboundEnchant( + soulboundSection.getInt("anvilCost", 1), + soulboundSection.getInt("weight", 10), + EnchantmentRegistryEntry.EnchantmentCost.of( + soulboundSection.getInt("minimumCost.base", 10), + soulboundSection.getInt("minimumCost.additionalPerLevel", 1) + ), + EnchantmentRegistryEntry.EnchantmentCost.of( + soulboundSection.getInt("maximumCost.base", 65), + soulboundSection.getInt("maximumCost.additionalPerLevel", 1) + ), + soulboundSection.getBoolean("canGetFromEnchantingTable", true), + getTagsFromList(getStringList( + soulboundSection, + "supportedItemTags", + List.of( + "#minecraft:enchantable/armor", + "#minecraft:enchantable/weapon", + "#minecraft:enchantable/mining" + ) + )) + )); + + ConfigurationSection telepathySection = enchantsSection.getConfigurationSection("telepathy"); + if (telepathySection == null) { + telepathySection = enchantsSection.createSection("telepathy"); + } + enchants.add(new TelepathyEnchant( + telepathySection.getInt("anvilCost", 1), + telepathySection.getInt("weight", 5), + EnchantmentRegistryEntry.EnchantmentCost.of( + telepathySection.getInt("minimumCost.base", 15), + telepathySection.getInt("minimumCost.additionalPerLevel", 1) + ), + EnchantmentRegistryEntry.EnchantmentCost.of( + telepathySection.getInt("maximumCost.base", 65), + telepathySection.getInt("maximumCost.additionalPerLevel", 1) + ), + telepathySection.getBoolean("canGetFromEnchantingTable", true), + getTagsFromList(getStringList( + telepathySection, + "supportedItemTags", + List.of( + "#minecraft:enchantable/mining" + ) + )) + )); + + ConfigurationSection replantingSection = enchantsSection.getConfigurationSection("replanting"); + if (replantingSection == null) { + replantingSection = enchantsSection.createSection("replanting"); + } + + enchants.add(new ReplantingEnchant( + replantingSection.getInt("anvilCost", 1), + replantingSection.getInt("weight", 10), + EnchantmentRegistryEntry.EnchantmentCost.of( + replantingSection.getInt("minimumCost.base", 1), + replantingSection.getInt("minimumCost.additionalPerLevel", 1) + ), + EnchantmentRegistryEntry.EnchantmentCost.of( + replantingSection.getInt("maximumCost.base", 65), + replantingSection.getInt("maximumCost.additionalPerLevel", 1) + ), + replantingSection.getBoolean("canGetFromEnchantingTable", true), + getTagsFromList(getStringList( + replantingSection, + "supportedItemTags", + List.of( + "#minecraft:hoes" + ) + )) + )); } + private List getStringList(ConfigurationSection section, String key, List defaultValue) { + List list = section.contains(key) ? section.getStringList(key) : null; + if (list == null) return defaultValue; + return list; + } + + private Set> getTagsFromList(List tags) { + Set> supportedItemTags = new HashSet<>(); + for (String itemTag : tags) { + if (itemTag == null) continue; + if (itemTag.startsWith("#")) { + itemTag = itemTag.substring(1); + } else { + System.out.println("Only item tags are supported for now, item tags need to begin with #"); + continue; + } + try { + Key key = Key.key(itemTag); + TagKey tagKey = ItemTypeTagKeys.create(key); + TagEntry tagEntry = TagEntry.tagEntry(tagKey); + supportedItemTags.add(tagEntry); + } catch (IllegalArgumentException e) { + System.out.println("Failed to create tag entry for " + itemTag); + } + } + return supportedItemTags; + } + } diff --git a/src/main/java/me/youhavetrouble/enchantio/enchants/ReplantingEnchant.java b/src/main/java/me/youhavetrouble/enchantio/enchants/ReplantingEnchant.java index 9365c14..bf2ea78 100644 --- a/src/main/java/me/youhavetrouble/enchantio/enchants/ReplantingEnchant.java +++ b/src/main/java/me/youhavetrouble/enchantio/enchants/ReplantingEnchant.java @@ -2,7 +2,6 @@ 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; @@ -17,6 +16,28 @@ public class ReplantingEnchant implements EnchantioEnchant { public static final Key KEY = Key.key("enchantio:replanting"); + private final int anvilCost, weight; + private final EnchantmentRegistryEntry.EnchantmentCost minimumCost; + private final EnchantmentRegistryEntry.EnchantmentCost maximumCost; + private final boolean canGetFromEnchantingTable; + private final Set> supportedItemTags; + + public ReplantingEnchant( + int anvilCost, + int weight, + EnchantmentRegistryEntry.EnchantmentCost minimumCost, + EnchantmentRegistryEntry.EnchantmentCost maximumCost, + boolean canGetFromEnchantingTable, + Set> supportedItemTags + ) { + this.anvilCost = anvilCost; + this.weight = weight; + this.minimumCost = minimumCost; + this.maximumCost = maximumCost; + this.canGetFromEnchantingTable = canGetFromEnchantingTable; + this.supportedItemTags = supportedItemTags; + } + @Override public Key getKey() { return KEY; @@ -29,7 +50,7 @@ public class ReplantingEnchant implements EnchantioEnchant { @Override public int getAnvilCost() { - return 1; + return anvilCost; } @Override @@ -39,17 +60,17 @@ public class ReplantingEnchant implements EnchantioEnchant { @Override public int getWeight() { - return 10; + return weight; } @Override public EnchantmentRegistryEntry.EnchantmentCost getMinimumCost() { - return EnchantmentRegistryEntry.EnchantmentCost.of(1, 1); + return minimumCost; } @Override public EnchantmentRegistryEntry.EnchantmentCost getMaximumCost() { - return EnchantmentRegistryEntry.EnchantmentCost.of(65, 1); + return maximumCost; } @Override @@ -59,7 +80,7 @@ public class ReplantingEnchant implements EnchantioEnchant { @Override public boolean canGetFromEnchantingTable() { - return true; + return canGetFromEnchantingTable; } @Override @@ -69,9 +90,7 @@ public class ReplantingEnchant implements EnchantioEnchant { @Override public Set> getSupportedItems() { - return Set.of( - TagEntry.tagEntry(ItemTypeTagKeys.HOES) - ); + return supportedItemTags; } } diff --git a/src/main/java/me/youhavetrouble/enchantio/enchants/SoulboundEnchant.java b/src/main/java/me/youhavetrouble/enchantio/enchants/SoulboundEnchant.java index eec0b25..063415c 100644 --- a/src/main/java/me/youhavetrouble/enchantio/enchants/SoulboundEnchant.java +++ b/src/main/java/me/youhavetrouble/enchantio/enchants/SoulboundEnchant.java @@ -2,7 +2,6 @@ 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; @@ -17,6 +16,28 @@ public class SoulboundEnchant implements EnchantioEnchant { public static final Key KEY = Key.key("enchantio:soulbound"); + private final int anvilCost, weight; + private final EnchantmentRegistryEntry.EnchantmentCost minimumCost; + private final EnchantmentRegistryEntry.EnchantmentCost maximumCost; + private final boolean canGetFromEnchantingTable; + private final Set> supportedItemTags; + + public SoulboundEnchant( + int anvilCost, + int weight, + EnchantmentRegistryEntry.EnchantmentCost minimumCost, + EnchantmentRegistryEntry.EnchantmentCost maximumCost, + boolean canGetFromEnchantingTable, + Set> supportedItemTags + ) { + this.anvilCost = anvilCost; + this.weight = weight; + this.minimumCost = minimumCost; + this.maximumCost = maximumCost; + this.canGetFromEnchantingTable = canGetFromEnchantingTable; + this.supportedItemTags = supportedItemTags; + } + @Override public Key getKey() { return KEY; @@ -29,7 +50,7 @@ public class SoulboundEnchant implements EnchantioEnchant { @Override public int getAnvilCost() { - return 1; + return anvilCost; } @Override @@ -39,17 +60,17 @@ public class SoulboundEnchant implements EnchantioEnchant { @Override public int getWeight() { - return 10; + return weight; } @Override public EnchantmentRegistryEntry.EnchantmentCost getMinimumCost() { - return EnchantmentRegistryEntry.EnchantmentCost.of(10, 1); + return minimumCost; } @Override public EnchantmentRegistryEntry.EnchantmentCost getMaximumCost() { - return EnchantmentRegistryEntry.EnchantmentCost.of(65, 1); + return maximumCost; } @Override @@ -59,7 +80,7 @@ public class SoulboundEnchant implements EnchantioEnchant { @Override public boolean canGetFromEnchantingTable() { - return true; + return canGetFromEnchantingTable; } @Override @@ -69,11 +90,7 @@ public class SoulboundEnchant implements EnchantioEnchant { @Override public Set> getSupportedItems() { - return Set.of( - TagEntry.tagEntry(ItemTypeTagKeys.ENCHANTABLE_ARMOR), - TagEntry.tagEntry(ItemTypeTagKeys.ENCHANTABLE_WEAPON), - TagEntry.tagEntry(ItemTypeTagKeys.ENCHANTABLE_MINING) - ); + return supportedItemTags; } } diff --git a/src/main/java/me/youhavetrouble/enchantio/enchants/TelepathyEnchant.java b/src/main/java/me/youhavetrouble/enchantio/enchants/TelepathyEnchant.java index 9e92d4f..c8963c8 100644 --- a/src/main/java/me/youhavetrouble/enchantio/enchants/TelepathyEnchant.java +++ b/src/main/java/me/youhavetrouble/enchantio/enchants/TelepathyEnchant.java @@ -2,7 +2,6 @@ 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; @@ -17,6 +16,28 @@ public class TelepathyEnchant implements EnchantioEnchant { public static final Key KEY = Key.key("enchantio:telepathy"); + private final int anvilCost, weight; + private final EnchantmentRegistryEntry.EnchantmentCost minimumCost; + private final EnchantmentRegistryEntry.EnchantmentCost maximumCost; + private final boolean canGetFromEnchantingTable; + private final Set> supportedItemTags; + + public TelepathyEnchant( + int anvilCost, + int weight, + EnchantmentRegistryEntry.EnchantmentCost minimumCost, + EnchantmentRegistryEntry.EnchantmentCost maximumCost, + boolean canGetFromEnchantingTable, + Set> supportedItemTags + ) { + this.anvilCost = anvilCost; + this.weight = weight; + this.minimumCost = minimumCost; + this.maximumCost = maximumCost; + this.canGetFromEnchantingTable = canGetFromEnchantingTable; + this.supportedItemTags = supportedItemTags; + } + @Override public Key getKey() { return KEY; @@ -29,7 +50,7 @@ public class TelepathyEnchant implements EnchantioEnchant { @Override public int getAnvilCost() { - return 1; + return anvilCost; } @Override @@ -39,17 +60,17 @@ public class TelepathyEnchant implements EnchantioEnchant { @Override public int getWeight() { - return 5; + return weight; } @Override public EnchantmentRegistryEntry.EnchantmentCost getMinimumCost() { - return EnchantmentRegistryEntry.EnchantmentCost.of(15, 1); + return minimumCost; } @Override public EnchantmentRegistryEntry.EnchantmentCost getMaximumCost() { - return EnchantmentRegistryEntry.EnchantmentCost.of(65, 1); + return maximumCost; } @Override @@ -59,7 +80,7 @@ public class TelepathyEnchant implements EnchantioEnchant { @Override public boolean canGetFromEnchantingTable() { - return true; + return canGetFromEnchantingTable; } @Override @@ -69,9 +90,7 @@ public class TelepathyEnchant implements EnchantioEnchant { @Override public Set> getSupportedItems() { - return Set.of( - TagEntry.tagEntry(ItemTypeTagKeys.ENCHANTABLE_MINING) - ); + return supportedItemTags; } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index e69de29..1a1b8ba 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -0,0 +1,42 @@ +enchants: + soulbound: + enabled: true + canGetFromEnchantingTable: true + anvilCost: 1 + weight: 10 + minimumCost: + base: 10 + additionalPerLevel: 1 + maximumCost: + base: 65 + additionalPerLevel: 1 + supportedItemTags: + - "#minecraft:enchantable/armor" + - "#minecraft:enchantable/weapon" + - "#minecraft:enchantable/mining" + telepathy: + enabled: true + canGetFromEnchantingTable: true + anvilCost: 1 + weight: 5 + minimumCost: + base: 15 + additionalPerLevel: 1 + maximumCost: + base: 65 + additionalPerLevel: 1 + supportedItemTags: + - "#minecraft:enchantable/mining" + replanting: + enabled: true + canGetFromEnchantingTable: true + anvilCost: 1 + weight: 10 + minimumCost: + base: 1 + additionalPerLevel: 1 + maximumCost: + base: 65 + additionalPerLevel: 1 + supportedItemTags: + - "#minecraft:hoes"