work on datapack support

This commit is contained in:
alexcrea 2025-04-27 15:42:15 +02:00
parent 64d5bafc7a
commit 012e6b1368
No known key found for this signature in database
GPG key ID: 027DD67D2D3280C5
12 changed files with 397 additions and 41 deletions

View file

@ -25,17 +25,17 @@ import java.util.logging.Level;
*/
public class CABukkitEnchantment extends CAEnchantmentBase {
private final @NotNull Enchantment enchantment;
public final @NotNull Enchantment bukkit;
public CABukkitEnchantment(@NotNull Enchantment enchantment, @Nullable EnchantmentRarity rarity){
super(enchantment.getKey(),
public CABukkitEnchantment(@NotNull Enchantment bukkit, @Nullable EnchantmentRarity rarity){
super(bukkit.getKey(),
rarity,
enchantment.getMaxLevel());
this.enchantment = enchantment;
bukkit.getMaxLevel());
this.bukkit = bukkit;
}
public CABukkitEnchantment(@NotNull Enchantment enchantment){
this(enchantment, getRarity(enchantment));
public CABukkitEnchantment(@NotNull Enchantment bukkit){
this(bukkit, getRarity(bukkit));
}
@Override
@ -51,9 +51,9 @@ public class CABukkitEnchantment extends CAEnchantmentBase {
@Override
public int getLevel(@NotNull ItemStack item, @NotNull ItemMeta meta) {
if (ItemUtil.INSTANCE.isEnchantedBook(item)) {
return ((EnchantmentStorageMeta)meta).getStoredEnchantLevel(this.enchantment);
return ((EnchantmentStorageMeta)meta).getStoredEnchantLevel(this.bukkit);
} else {
return meta.getEnchantLevel(this.enchantment);
return meta.getEnchantLevel(this.bukkit);
}
}
@ -62,9 +62,9 @@ public class CABukkitEnchantment extends CAEnchantmentBase {
if (ItemUtil.INSTANCE.isEnchantedBook(item)) {
EnchantmentStorageMeta bookMeta = ((EnchantmentStorageMeta)meta);
return bookMeta.getStoredEnchants().containsKey(this.enchantment);
return bookMeta.getStoredEnchants().containsKey(this.bukkit);
}else{
return item.containsEnchantment(this.enchantment);
return item.containsEnchantment(this.bukkit);
}
}
@ -74,10 +74,10 @@ public class CABukkitEnchantment extends CAEnchantmentBase {
EnchantmentStorageMeta bookMeta = ((EnchantmentStorageMeta)item.getItemMeta());
assert bookMeta != null;
bookMeta.addStoredEnchant(this.enchantment, level, true);
bookMeta.addStoredEnchant(this.bukkit, level, true);
item.setItemMeta(bookMeta);
} else {
item.addUnsafeEnchantment(this.enchantment, level);
item.addUnsafeEnchantment(this.bukkit, level);
}
}
@ -88,10 +88,10 @@ public class CABukkitEnchantment extends CAEnchantmentBase {
EnchantmentStorageMeta bookMeta = ((EnchantmentStorageMeta)item.getItemMeta());
assert bookMeta != null;
bookMeta.removeStoredEnchant(this.enchantment);
bookMeta.removeStoredEnchant(this.bukkit);
item.setItemMeta(bookMeta);
}else{
item.removeEnchantment(this.enchantment);
item.removeEnchantment(this.bukkit);
}
}
@ -107,7 +107,7 @@ public class CABukkitEnchantment extends CAEnchantmentBase {
@NotNull
protected Enchantment getEnchant() {
return this.enchantment;
return this.bukkit;
}
private static Method getAnvilCostMethod;
@ -163,7 +163,7 @@ public class CABukkitEnchantment extends CAEnchantmentBase {
return false;
}
return this.enchantment.equals(other.getEnchant());
return this.bukkit.equals(other.getEnchant());
}
}

View file

@ -0,0 +1,35 @@
package xyz.alexcrea.cuanvil.enchant.wrapped;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xyz.alexcrea.cuanvil.enchant.*;
import java.util.Map;
/**
* Represent an enchantment incompatible with every other enchantments
*/
public class CAIncompatibleAllEnchant extends CABukkitEnchantment implements AdditionalTestEnchantment {
public CAIncompatibleAllEnchant(@NotNull Enchantment enchantment, @Nullable EnchantmentRarity rarity) {
super(enchantment, rarity);
}
public CAIncompatibleAllEnchant(@NotNull Enchantment enchantment) {
super(enchantment);
}
@Override
public boolean isEnchantConflict(@NotNull Map<CAEnchantment, Integer> enchantments, @NotNull Material itemMat) {
return true;
}
@Override
public boolean isItemConflict(@NotNull Map<CAEnchantment, Integer> enchantments, @NotNull Material itemMat, @NotNull ItemStack item) {
return false;
}
}

View file

@ -10,18 +10,18 @@ import java.util.List;
public class UpdateUtils {
public static final String MINECRAFT_VERSION_PATH = "lowMinecraftVersion";
public static Version currentMinecraftVersion(){
public static Version currentMinecraftVersion() {
String versionString = Bukkit.getServer().getBukkitVersion().split("-")[0];
return Version.fromString(versionString);
}
@Deprecated
public static int[] currentMinecraftVersionArray(){
public static int[] currentMinecraftVersionArray() {
String versionString = Bukkit.getServer().getBukkitVersion().split("-")[0];
return UpdateUtils.readVersionFromString(versionString);
}
public static int[] readVersionFromString(String versionString){
public static int[] readVersionFromString(String versionString) {
String[] partialVersion = versionString.split("\\.");
int[] versionParts = new int[]{0, 0, 0};
@ -31,11 +31,22 @@ public class UpdateUtils {
return versionParts;
}
public static void addToStringList(FileConfiguration config, String path, String... toAdd){
public static void addToStringList(FileConfiguration config, String path, String... toAdd) {
List<String> groups = new ArrayList<>(config.getStringList(path));
groups.addAll(Arrays.asList(toAdd));
config.set(path, groups);
}
public static void addAbsentToList(FileConfiguration config, String path, String... toAdd) {
List<String> groups = new ArrayList<>(config.getStringList(path));
for (String val : toAdd) {
if (groups.contains(val)) continue;
groups.add(val);
}
config.set(path, groups);
}
}

View file

@ -4,7 +4,7 @@ import io.delilaheve.CustomAnvil;
import org.bukkit.configuration.file.FileConfiguration;
import xyz.alexcrea.cuanvil.config.ConfigHolder;
import static xyz.alexcrea.cuanvil.update.UpdateUtils.addToStringList;
import static xyz.alexcrea.cuanvil.update.UpdateUtils.addAbsentToList;
// This is a temporary class that aim to handle 1.21 update.
// It will be replaced by a better system later.
@ -40,26 +40,26 @@ public class Update_1_21 {
// Add mace to groups
groupConfig.set("mace.type", "include");
addToStringList(groupConfig, "mace.items", "mace");
addAbsentToList(groupConfig, "mace.items", "mace");
addToStringList(groupConfig, "can_unbreak.groups", "mace");
addAbsentToList(groupConfig, "can_unbreak.groups", "mace");
// Add new enchant conflicts
addToStringList(conflictConfig, "restriction_density.enchantments", "minecraft:density");
addToStringList(conflictConfig, "restriction_density.notAffectedGroups", "mace", "enchanted_book");
addAbsentToList(conflictConfig, "restriction_density.enchantments", "minecraft:density");
addAbsentToList(conflictConfig, "restriction_density.notAffectedGroups", "mace", "enchanted_book");
addToStringList(conflictConfig, "restriction_breach.enchantments", "minecraft:breach");
addToStringList(conflictConfig, "restriction_breach.notAffectedGroups", "mace", "enchanted_book");
addAbsentToList(conflictConfig, "restriction_breach.enchantments", "minecraft:breach");
addAbsentToList(conflictConfig, "restriction_breach.notAffectedGroups", "mace", "enchanted_book");
addToStringList(conflictConfig, "restriction_wind_burst.enchantments", "minecraft:wind_burst");
addToStringList(conflictConfig, "restriction_wind_burst.notAffectedGroups", "mace", "enchanted_book");
addAbsentToList(conflictConfig, "restriction_wind_burst.enchantments", "minecraft:wind_burst");
addAbsentToList(conflictConfig, "restriction_wind_burst.notAffectedGroups", "mace", "enchanted_book");
// Add mace to conflicts
addToStringList(conflictConfig, "restriction_fire_aspect.notAffectedGroups", "mace");
addToStringList(conflictConfig, "restriction_smite.notAffectedGroups", "mace");
addToStringList(conflictConfig, "restriction_bane_of_arthropods.notAffectedGroups", "mace");
addAbsentToList(conflictConfig, "restriction_fire_aspect.notAffectedGroups", "mace");
addAbsentToList(conflictConfig, "restriction_smite.notAffectedGroups", "mace");
addAbsentToList(conflictConfig, "restriction_bane_of_arthropods.notAffectedGroups", "mace");
addToStringList(conflictConfig, "sword_enchant_conflict.enchantments",
addAbsentToList(conflictConfig, "sword_enchant_conflict.enchantments",
"minecraft:density", "minecraft:breach");
// Add level limit

View file

@ -1,6 +1,7 @@
package xyz.alexcrea.cuanvil.update;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public record Version(int major, int minor, int patch) {
@ -11,7 +12,9 @@ public record Version(int major, int minor, int patch) {
this(major, 0, 0);
}
public static Version fromString(@Nonnull String versionString){
public static Version fromString(@Nullable String versionString){
if(versionString == null) return new Version(0, 0, 0);
String[] partialVersion = versionString.split("\\.");
int[] versionParts = new int[]{0, 0, 0};
@ -45,4 +48,8 @@ public record Version(int major, int minor, int patch) {
this.patch <= other.patch)));
}
@Override
public String toString() {
return major + "." + minor + "." + patch;
}
}

View file

@ -8,7 +8,7 @@ import javax.annotation.Nonnull;
import java.util.List;
import java.util.Set;
import static xyz.alexcrea.cuanvil.update.UpdateUtils.addToStringList;
import static xyz.alexcrea.cuanvil.update.UpdateUtils.addAbsentToList;
public class PUpdate_1_11_0 {
@ -57,7 +57,7 @@ public class PUpdate_1_11_0 {
}
// Finally we know both conflict are default. so we fix
addToStringList(config, "sword_enchant_conflict.enchantments",
addAbsentToList(config, "sword_enchant_conflict.enchantments",
"minecraft:density", "minecraft:breach");
config.set("mace_enchant_conflict", null);

View file

@ -1,13 +1,12 @@
package xyz.alexcrea.cuanvil.update.plugin;
import io.delilaheve.CustomAnvil;
import org.bukkit.configuration.file.FileConfiguration;
import xyz.alexcrea.cuanvil.config.ConfigHolder;
import javax.annotation.Nonnull;
import java.util.Set;
import static xyz.alexcrea.cuanvil.update.UpdateUtils.addToStringList;
import static xyz.alexcrea.cuanvil.update.UpdateUtils.addAbsentToList;
public class PUpdate_1_6_2 {
@ -30,7 +29,7 @@ public class PUpdate_1_6_2 {
}
if(!contained){
addToStringList(config, path, "enchanted_book");
addAbsentToList(config, path, "enchanted_book");
conflictUpdated = true;
}
}

View file

@ -9,6 +9,7 @@ import org.bukkit.event.inventory.PrepareAnvilEvent
import org.bukkit.inventory.AnvilInventory
import org.bukkit.inventory.ItemStack
import xyz.alexcrea.cuanvil.config.ConfigHolder
import xyz.alexcrea.cuanvil.dependency.datapack.DataPackTest
import xyz.alexcrea.cuanvil.dependency.gui.ExternGuiTester
import xyz.alexcrea.cuanvil.dependency.gui.GuiTesterSelector
import xyz.alexcrea.cuanvil.dependency.packet.PacketManager
@ -78,12 +79,13 @@ object DependencyManager {
havenBagsCompatibility = HavenBagsDependency()
havenBagsCompatibility!!.redirectListeners()
}
}
fun handleCompatibilityConfig() {
enchantmentSquaredCompatibility?.registerPluginConfiguration()
// datapacks
DataPackTest.handleDatapackConfigs()
}
fun registerEnchantments() {

View file

@ -0,0 +1,183 @@
package xyz.alexcrea.cuanvil.dependency.datapack
import io.delilaheve.CustomAnvil
import org.bukkit.NamespacedKey
import org.bukkit.configuration.file.FileConfiguration
import org.bukkit.configuration.file.YamlConfiguration
import xyz.alexcrea.cuanvil.api.ConflictBuilder
import xyz.alexcrea.cuanvil.api.EnchantmentApi
import xyz.alexcrea.cuanvil.config.ConfigHolder
import xyz.alexcrea.cuanvil.enchant.wrapped.CABukkitEnchantment
import xyz.alexcrea.cuanvil.enchant.wrapped.CAIncompatibleAllEnchant
import xyz.alexcrea.cuanvil.update.UpdateUtils
import xyz.alexcrea.cuanvil.update.Version
import java.io.InputStreamReader
object DataPackTest {
private val START_DETECT_VERSION = Version(1, 19, 0)
/**
* Map of the latest CA update related to the pack
*/
private val LASTEST_VERSION = mapOf(
Pair("bracken", Version(1, 11, 0))
)
val enabledDatapack: List<String>
get() {
val version: Version = UpdateUtils.currentMinecraftVersion()
if (version.lesserThan(START_DETECT_VERSION)) return emptyList()
return DataPackTester.enabledPacks
}
fun handleDatapackConfigs() {
val enabledDatapack = enabledDatapack
for (packName in enabledDatapack) {
// Handling of pack name is horrible: it is based on file name
// So if someone rename a datapack it will make me sad
if (packName.startsWith("file/bp_post_scarcity")) {
handlePack("bracken")
continue
}
}
}
private fun handlePack(pack: String) {
val defConfig = ConfigHolder.DEFAULT_CONFIG
val version = LASTEST_VERSION[pack]
val currentVersion = Version.fromString(defConfig.config.getString("datapack.$pack"))
if (currentVersion.greaterEqual(version!!)) return
// Add pack value or do update from previous version
// note: update thingy is not yet implemented
configureDatapack(pack)
// Finally, set current pack version to config
//defConfig.config.set("datapack.$pack", version.toString()) // temporary disabled for easier test
defConfig.saveToDisk(true)
}
private fun configureDatapack(pack: String) {
val itemConflict = javaClass.getResource("/datapack/$pack/item_conflict.yml")
val enchantConflict = javaClass.getResource("/datapack/$pack/enchant_conflict.yml")
val newConflictList = ArrayList<ConflictBuilder>()
var needSave = false
if (itemConflict != null) {
val reader = InputStreamReader(itemConflict.openStream())
val yml = YamlConfiguration.loadConfiguration(reader)
addItemConflicts(yml, newConflictList)
}
if (enchantConflict != null) {
val reader = InputStreamReader(enchantConflict.openStream())
val yml = YamlConfiguration.loadConfiguration(reader)
needSave = needSave || addEnchantConflict(yml, newConflictList)
}
for (conflict in newConflictList) {
needSave = !conflict.registerIfAbsent() && needSave
}
if (needSave) {
ConfigHolder.CONFLICT_HOLDER.saveToDisk(true)
}
}
private fun addItemConflicts(yml: FileConfiguration, conflictList: MutableList<ConflictBuilder>) {
for (ench in yml.getKeys(false)) {
val groups = yml.getStringList(ench)
val conflict = ConflictBuilder(
"restriction_${ench.replace(":", "_")}",
CustomAnvil.instance)
conflict.addEnchantment(NamespacedKey.fromString(ench)!!)
for (group in groups) {
conflict.addExcludedGroup(group)
}
conflict.addExcludedGroup("enchanted_book")
conflictList.add(conflict)
}
}
private fun addEnchantConflict(yml: YamlConfiguration, conflictList: MutableList<ConflictBuilder>): Boolean {
var needSave = false
val conflicts = HashMap<String, ConflictBuilder>()
for (ench in yml.getKeys(false)) {
val groups = yml.getStringList(ench)
for (group in groups) {
if (group.startsWith('#')) {
needSave = needSave || joinGroup(conflicts, group.substring(1), ench)
} else {
createConflict(conflictList, ench, group)
}
}
}
conflictList.addAll(conflicts.values)
return needSave
}
private fun createConflict(conflictList: MutableList<ConflictBuilder>, ench: String, other: String) {
val conflict = ConflictBuilder(
"conflict_" +
"${ench.replace(":", "_")}_" +
other.replace(":", "_"),
CustomAnvil.instance
)
conflict.addEnchantment(NamespacedKey.fromString(ench)!!)
conflict.addEnchantment(NamespacedKey.fromString(other)!!)
conflict.setMaxBeforeConflict(1)
conflictList.add(conflict)
}
private fun joinGroup(conflicts: HashMap<String, ConflictBuilder>, group: String, ench: String): Boolean {
if ("all".equals(group, ignoreCase = true)) {
// We assume current is not null and of type CABukkitEnchantment
val current = EnchantmentApi.getByKey(NamespacedKey.fromString(ench)!!) as CABukkitEnchantment
// We need to replace current wrapped enchantment with the all conflict wrapper
EnchantmentApi.unregisterEnchantment(current)
EnchantmentApi.registerEnchantment(CAIncompatibleAllEnchant(current.bukkit, current.defaultRarity()))
return false
} else {
val config = ConfigHolder.CONFLICT_HOLDER.config
// If conflict do not yet exist
if(!config.isConfigurationSection(group)) {
val conflict = conflicts.getOrPut(group) {
val conflict = ConflictBuilder(group, CustomAnvil.instance)
conflict.setMaxBeforeConflict(1)
conflict
}
conflict.addEnchantment(NamespacedKey.fromString(ench)!!)
}
// Find current conflict
val manager = ConfigHolder.CONFLICT_HOLDER.conflictManager
// This assumes that:
// - the conflict existing in the config exist in the runtime config
// - the enchantment exist and is provided correctly
val conflict = manager.conflictList.find { it.name == group }!!
conflict.addEnchantment(EnchantmentApi.getByKey(NamespacedKey.fromString(ench)!!)!!)
UpdateUtils.addAbsentToList(config, "$group.enchantments", ench)
return true
}
}
}

View file

@ -0,0 +1,35 @@
"bracken:abstraction": ['#sword_enchant_conflict']
"bracken:antivenom": ['#protection_enchant_conflict']
"bracken:blinding_fix": ['minecraft:fire_aspect']
"bracken:boldness": ['#protection_enchant_conflict']
"bracken:butchering": ['#sword_enchant_conflict']
"bracken:defusing": ['#sword_enchant_conflict']
"bracken:dentine_touch": ['#tool_conflict']
"bracken:dullness_curse": ['minecraft:sharpness']
"bracken:famine_walker": ['#boot_conflict']
"bracken:ferocity": ['#sword_enchant_conflict']
"bracken:flame_walker": ['#boot_conflict']
"bracken:float_walker": ['#boot_conflict']
"bracken:flood_walker": ['#boot_conflict']
"bracken:fragility_curse": ['minecraft:unbreaking']
"bracken:freezing_fix": ['minecraft:fire_aspect']
"bracken:guarding": ['#sword_enchant_conflict']
"bracken:huskiness": ['#bracken_pet_armor']
"bracken:infused_fire_aspect_fix": ['minecraft:fire_aspect']
"bracken:infused_frost_walker_fix": ['#boot_conflict']
"bracken:infused_mending_fix": ['minecraft:mending']
"bracken:infused_thorns_fix": ['minecraft:thorns']
"bracken:lethargy_curse": ['bracken:ferocity']
"bracken:lifesteal": ['#bow_conflict']
"bracken:litheness": ['#bracken_pet_armor']
"bracken:lunar_concordance_armor": ['#bracken_lunarite_armor']
"bracken:lunar_concordance_weapon": ['#bracken_lunarite_weapons']
"bracken:maiming_curse": ['bracken:vitality_fix']
"bracken:mortality": ['#sword_enchant_conflict']
"bracken:poisoning_fix": ['minecraft:fire_aspect']
"bracken:pugnacity": ['#bracken_pet_armor']
"bracken:rending": ['#crossbow_conflict']
"bracken:silvered_fix": ['#all']
"bracken:vitality_fix": ['bracken:maiming_curse']
"bracken:weakening_fix": ['minecraft:fire_aspect']
"bracken:withering_fix": ['minecraft:fire_aspect']

View file

@ -0,0 +1,54 @@
"bracken:abstraction": ['melee_weapons', 'mace']
"bracken:antivenom": ['armors']
"bracken:astuteness_fix": ['armors']
"bracken:blinding_fix": ['can_unbreak']
"bracken:boldness": ['armors']
"bracken:butchering": ['melee_weapons', 'mace']
"bracken:chiseling_fix": ['axes', 'pickaxes', 'shovels', 'hoes']
"bracken:decaying_fix": ['can_unbreak']
"bracken:defusing": ['melee_weapons', 'mace']
"bracken:dentine_touch": ['axes', 'pickaxes', 'shovels', 'hoes']
"bracken:devouring_curse": ['melee_weapons', 'mace']
"bracken:dullness_curse": ['melee_weapons', 'mace']
"bracken:famine_walker": ['boots']
"bracken:ferocity": ['melee_weapons', 'mace']
"bracken:flame_walker": ['boots']
"bracken:float_walker": ['boots']
"bracken:flood_walker": ['boots']
"bracken:fragility_curse": ['can_unbreak']
"bracken:freezing_fix": ['can_unbreak']
"bracken:guarding": ['melee_weapons', 'mace']
"bracken:huskiness": ['pet_armor']
"bracken:infused_fire_aspect_fix": ['melee_weapons', 'mace']
"bracken:infused_frost_walker_fix": ['boots']
"bracken:infused_mending_fix": ['can_unbreak']
"bracken:infused_thorns_fix": ['armors']
"bracken:integrity_fix": ['armors']
"bracken:lethargy_curse": ['melee_weapons', 'mace']
"bracken:lifesteal": ['bow']
"bracken:litheness": ['pet_armor']
"bracken:maiming_curse": ['armors']
"bracken:mortality": ['melee_weapons', 'mace']
"bracken:poisoning_fix": ['can_unbreak']
"bracken:pugnacity": ['wolf_armor']
"bracken:pushback": ['armors']
"bracken:quenching_fix": ['armors']
"bracken:rending": ['crossbow']
"bracken:reverse_thorns": ['axes']
"bracken:searing_surface": ['armors']
"bracken:sentience_curse_1": ['can_unbreak']
"bracken:sentience_curse_2": ['can_unbreak']
"bracken:sentience_curse_3": ['can_unbreak']
"bracken:sentience_curse_4": ['can_unbreak']
"bracken:sentience_curse_5": ['can_unbreak']
"bracken:sentience_curse_6": ['can_unbreak']
"bracken:sentience_curse_7": ['can_unbreak']
"bracken:shrewdness": ['swords']
"bracken:silvered_fix": ['can_unbreak']
"bracken:spectrality_fix": ['melee_weapons', 'mace']
"bracken:splintering": ['bow']
"bracken:trampling": ['horse_armor']
"bracken:vitality_fix": ['armors']
"bracken:weakening_fix": ['can_unbreak']
"bracken:wisdom": ['tools']
"bracken:withering_fix": ['can_unbreak']