This commit is contained in:
2024-10-21 19:53:50 +02:00
parent 9b310bdcc7
commit df60129653
6 changed files with 301 additions and 42 deletions
@@ -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<EnchantioEnchant> 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<EnchantioEnchant> enchantioEnchants = config.enchants;
context.getLifecycleManager().registerEventHandler(LifecycleEvents.TAGS.preFlatten(RegistryKey.ITEM).newHandler((event) -> {
for (EnchantioEnchant enchant : enchantioEnchants) {
@@ -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<EnchantioEnchant> 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<String> getStringList(ConfigurationSection section, String key, List<String> defaultValue) {
List<String> list = section.contains(key) ? section.getStringList(key) : null;
if (list == null) return defaultValue;
return list;
}
private Set<TagEntry<ItemType>> getTagsFromList(List<String> tags) {
Set<TagEntry<ItemType>> 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<ItemType> tagKey = ItemTypeTagKeys.create(key);
TagEntry<ItemType> tagEntry = TagEntry.tagEntry(tagKey);
supportedItemTags.add(tagEntry);
} catch (IllegalArgumentException e) {
System.out.println("Failed to create tag entry for " + itemTag);
}
}
return supportedItemTags;
}
}
@@ -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<TagEntry<ItemType>> supportedItemTags;
public ReplantingEnchant(
int anvilCost,
int weight,
EnchantmentRegistryEntry.EnchantmentCost minimumCost,
EnchantmentRegistryEntry.EnchantmentCost maximumCost,
boolean canGetFromEnchantingTable,
Set<TagEntry<ItemType>> 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<TagEntry<ItemType>> getSupportedItems() {
return Set.of(
TagEntry.tagEntry(ItemTypeTagKeys.HOES)
);
return supportedItemTags;
}
}
@@ -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<TagEntry<ItemType>> supportedItemTags;
public SoulboundEnchant(
int anvilCost,
int weight,
EnchantmentRegistryEntry.EnchantmentCost minimumCost,
EnchantmentRegistryEntry.EnchantmentCost maximumCost,
boolean canGetFromEnchantingTable,
Set<TagEntry<ItemType>> 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<TagEntry<ItemType>> getSupportedItems() {
return Set.of(
TagEntry.tagEntry(ItemTypeTagKeys.ENCHANTABLE_ARMOR),
TagEntry.tagEntry(ItemTypeTagKeys.ENCHANTABLE_WEAPON),
TagEntry.tagEntry(ItemTypeTagKeys.ENCHANTABLE_MINING)
);
return supportedItemTags;
}
}
@@ -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<TagEntry<ItemType>> supportedItemTags;
public TelepathyEnchant(
int anvilCost,
int weight,
EnchantmentRegistryEntry.EnchantmentCost minimumCost,
EnchantmentRegistryEntry.EnchantmentCost maximumCost,
boolean canGetFromEnchantingTable,
Set<TagEntry<ItemType>> 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<TagEntry<ItemType>> getSupportedItems() {
return Set.of(
TagEntry.tagEntry(ItemTypeTagKeys.ENCHANTABLE_MINING)
);
return supportedItemTags;
}
}