Fix 1.21 config being wrong:

fix 1.21 config updater and automatically repair previously broken config version:
- Allow 1.21 enchantment in enchantment book
- Add unit repair of mace with breeze rod

Also version up
This commit is contained in:
alexcrea 2024-09-18 12:32:17 +02:00
parent 14fd247b0b
commit f4f467f6fe
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
10 changed files with 173 additions and 16 deletions

View file

@ -14,7 +14,7 @@ plugins {
} }
group = "xyz.alexcrea" group = "xyz.alexcrea"
version = "1.6.1" version = "1.6.2"
repositories { repositories {
// EcoEnchants // EcoEnchants

View file

@ -10,7 +10,13 @@ import java.util.List;
public class UpdateUtils { public class UpdateUtils {
public static final String MINECRAFT_VERSION_PATH = "lowMinecraftVersion"; public static final String MINECRAFT_VERSION_PATH = "lowMinecraftVersion";
public static int[] currentMinecraftVersion(){ public static Version currentMinecraftVersion(){
String versionString = Bukkit.getServer().getBukkitVersion().split("-")[0];
return Version.fromString(versionString);
}
@Deprecated
public static int[] currentMinecraftVersionArray(){
String versionString = Bukkit.getServer().getBukkitVersion().split("-")[0]; String versionString = Bukkit.getServer().getBukkitVersion().split("-")[0];
return UpdateUtils.readVersionFromString(versionString); return UpdateUtils.readVersionFromString(versionString);
} }
@ -25,7 +31,7 @@ public class UpdateUtils {
return versionParts; return versionParts;
} }
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)); List<String> groups = new ArrayList<>(config.getStringList(path));
groups.addAll(Arrays.asList(toAdd)); groups.addAll(Arrays.asList(toAdd));
config.set(path, groups); config.set(path, groups);

View file

@ -10,22 +10,21 @@ import static xyz.alexcrea.cuanvil.update.UpdateUtils.addToStringList;
// It will be replaced by a better system later. // It will be replaced by a better system later.
public class Update_1_21 { public class Update_1_21 {
private static final Version V1_21 = new Version(1, 21);
public static void handleUpdate(){ public static void handleUpdate(){
// Assume if version path is not null then it's 1.21 // Assume if version path is not null then it's 1.21
String oldVersion = ConfigHolder.DEFAULT_CONFIG.getConfig().getString(UpdateUtils.MINECRAFT_VERSION_PATH); String oldVersion = ConfigHolder.DEFAULT_CONFIG.getConfig().getString(UpdateUtils.MINECRAFT_VERSION_PATH);
if(oldVersion != null){ if(oldVersion != null){
int[] versionParts = UpdateUtils.readVersionFromString(oldVersion); Version version = Version.fromString(oldVersion);
// Test 1.21 // Test 1.21
if((versionParts[0] >= 1) && (versionParts[1] >= 21)){ if(V1_21.greaterEqual(version)) return;
return;
} }
} Version current = UpdateUtils.currentMinecraftVersion();
int[] versionParts = UpdateUtils.currentMinecraftVersion();
// Test 1.21 // Test 1.21
if((versionParts[0] >= 1) && (versionParts[1] >= 21)){ if(current.greaterEqual(V1_21)){
doUpdate(); doUpdate();
} }
@ -37,6 +36,7 @@ public class Update_1_21 {
FileConfiguration baseConfig = ConfigHolder.DEFAULT_CONFIG.getConfig(); FileConfiguration baseConfig = ConfigHolder.DEFAULT_CONFIG.getConfig();
FileConfiguration groupConfig = ConfigHolder.ITEM_GROUP_HOLDER.getConfig(); FileConfiguration groupConfig = ConfigHolder.ITEM_GROUP_HOLDER.getConfig();
FileConfiguration conflictConfig = ConfigHolder.CONFLICT_HOLDER.getConfig(); FileConfiguration conflictConfig = ConfigHolder.CONFLICT_HOLDER.getConfig();
FileConfiguration unitConfig = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig();
// Add mace to groups // Add mace to groups
groupConfig.set("mace.type", "include"); groupConfig.set("mace.type", "include");
@ -46,13 +46,13 @@ public class Update_1_21 {
// Add new enchant conflicts // Add new enchant conflicts
addToStringList(conflictConfig, "restriction_density.enchantments", "density"); addToStringList(conflictConfig, "restriction_density.enchantments", "density");
addToStringList(conflictConfig, "restriction_density.notAffectedGroups", "mace"); addToStringList(conflictConfig, "restriction_density.notAffectedGroups", "mace", "enchanted_book");
addToStringList(conflictConfig, "restriction_breach.enchantments", "breach"); addToStringList(conflictConfig, "restriction_breach.enchantments", "breach");
addToStringList(conflictConfig, "restriction_breach.notAffectedGroups", "mace"); addToStringList(conflictConfig, "restriction_breach.notAffectedGroups", "mace", "enchanted_book");
addToStringList(conflictConfig, "restriction_wind_burst.enchantments", "wind_burst"); addToStringList(conflictConfig, "restriction_wind_burst.enchantments", "wind_burst");
addToStringList(conflictConfig, "restriction_wind_burst.notAffectedGroups", "mace"); addToStringList(conflictConfig, "restriction_wind_burst.notAffectedGroups", "mace", "enchanted_book");
// Add mace to conflicts // Add mace to conflicts
addToStringList(conflictConfig, "restriction_fire_aspect.notAffectedGroups", "mace"); addToStringList(conflictConfig, "restriction_fire_aspect.notAffectedGroups", "mace");
@ -77,6 +77,9 @@ public class Update_1_21 {
baseConfig.set("enchant_values.wind_burst.item", 4); baseConfig.set("enchant_values.wind_burst.item", 4);
baseConfig.set("enchant_values.wind_burst.book", 2); baseConfig.set("enchant_values.wind_burst.book", 2);
// Add unit repair for mace
unitConfig.set("breeze_rod.mace", 0.25);
// Set version string as 1.21 // Set version string as 1.21
baseConfig.set(UpdateUtils.MINECRAFT_VERSION_PATH, "1.21"); baseConfig.set(UpdateUtils.MINECRAFT_VERSION_PATH, "1.21");
@ -84,6 +87,7 @@ public class Update_1_21 {
ConfigHolder.DEFAULT_CONFIG.saveToDisk(true); ConfigHolder.DEFAULT_CONFIG.saveToDisk(true);
ConfigHolder.ITEM_GROUP_HOLDER.saveToDisk(true); ConfigHolder.ITEM_GROUP_HOLDER.saveToDisk(true);
ConfigHolder.CONFLICT_HOLDER.saveToDisk(true); ConfigHolder.CONFLICT_HOLDER.saveToDisk(true);
ConfigHolder.UNIT_REPAIR_HOLDER.saveToDisk(true);
// imply reload of CONFLICT_HOLDER // imply reload of CONFLICT_HOLDER
// We also do not need to reload base config as there is no object related to it. // We also do not need to reload base config as there is no object related to it.

View file

@ -0,0 +1,48 @@
package xyz.alexcrea.cuanvil.update;
import javax.annotation.Nonnull;
public record Version(int major, int minor, int patch) {
public Version(int major, int minor){
this(major, minor, 0);
}
public Version(int major){
this(major, 0, 0);
}
public static Version fromString(@Nonnull String versionString){
String[] partialVersion = versionString.split("\\.");
int[] versionParts = new int[]{0, 0, 0};
for (int i = 0; i < Math.min(3, partialVersion.length); i++) {
versionParts[i] = Integer.parseInt(partialVersion[i]);
}
return new Version(versionParts[0], versionParts[1], versionParts[2]);
}
public boolean greaterThan(@Nonnull Version other){
return this.major > other.major || (this.major == other.major &&
(this.minor > other.minor || (this.minor == other.minor &&
this.patch > other.patch)));
}
public boolean greaterEqual(@Nonnull Version other){
return this.major > other.major || (this.major == other.major &&
(this.minor > other.minor || (this.minor == other.minor &&
this.patch >= other.patch)));
}
public boolean lesserThan(@Nonnull Version other){
return this.major < other.major || (this.major == other.major &&
(this.minor < other.minor || (this.minor == other.minor &&
this.patch < other.patch)));
}
public boolean lesserEqual(@Nonnull Version other){
return this.major < other.major || (this.major == other.major &&
(this.minor < other.minor || (this.minor == other.minor &&
this.patch <= other.patch)));
}
}

View file

@ -0,0 +1,56 @@
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;
public class PUpdate_1_6_2 {
private static final String[] toUpdate = new String[] {"restriction_density", "restriction_breach", "restriction_wind_burst"};
public static void handleUpdate(@Nonnull Set<ConfigHolder> toSave) {
FileConfiguration config = ConfigHolder.CONFLICT_HOLDER.getConfig();
boolean conflictUpdated = false;
for (String restriction : toUpdate) {
if(!config.isConfigurationSection(restriction)) continue;
String path = restriction + ".notAffectedGroups";
boolean contained = false;
for (String value : config.getStringList(path)) {
if(value.equalsIgnoreCase("enchanted_book")) {
contained = true;
break;
}
}
if(!contained){
addToStringList(config, path, "enchanted_book");
conflictUpdated = true;
}
}
if(conflictUpdated){
toSave.add(ConfigHolder.CONFLICT_HOLDER);
// May not be the most efficient for later revision, maybe move to PluginUpdates
ConfigHolder.CONFLICT_HOLDER.reload();
}
// Then we add the unit repair
config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig();
String unitRepairPath = "breeze_rod.mace";
if(!config.isConfigurationSection(unitRepairPath)){
config.set(unitRepairPath, 0.25);
toSave.add(ConfigHolder.UNIT_REPAIR_HOLDER);
}
}
}

View file

@ -0,0 +1,39 @@
package xyz.alexcrea.cuanvil.update.plugin;
import io.delilaheve.CustomAnvil;
import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.update.Version;
import javax.annotation.Nonnull;
import java.util.HashSet;
import java.util.Set;
public class PluginUpdates {
private static final String CONFIG_VERSION_PATH = "configVersion";
public static void handlePluginUpdate(){
String versionString = ConfigHolder.DEFAULT_CONFIG.getConfig().getString(CONFIG_VERSION_PATH);
Version current = versionString == null ? new Version(0) : Version.fromString(versionString);
Set<ConfigHolder> toSave = new HashSet<>();
if(new Version(1, 6, 2).greaterThan(current)){
PUpdate_1_6_2.handleUpdate(toSave);
finishConfiguration("1.6.2", toSave);
}
}
private static void finishConfiguration(@Nonnull String newVersion, @Nonnull Set<ConfigHolder> toSave) {
CustomAnvil.instance.getLogger().info("Configuration file updated to " + newVersion);
ConfigHolder.DEFAULT_CONFIG.getConfig().set(CONFIG_VERSION_PATH, newVersion);
toSave.add(ConfigHolder.DEFAULT_CONFIG);
for (ConfigHolder configHolder : toSave) {
configHolder.saveToDisk(true);
}
}
}

View file

@ -16,6 +16,7 @@ import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant
import xyz.alexcrea.cuanvil.listener.ChatEventListener import xyz.alexcrea.cuanvil.listener.ChatEventListener
import xyz.alexcrea.cuanvil.update.PluginSetDefault import xyz.alexcrea.cuanvil.update.PluginSetDefault
import xyz.alexcrea.cuanvil.update.Update_1_21 import xyz.alexcrea.cuanvil.update.Update_1_21
import xyz.alexcrea.cuanvil.update.plugin.PluginUpdates
import xyz.alexcrea.cuanvil.util.Metrics import xyz.alexcrea.cuanvil.util.Metrics
import java.io.File import java.io.File
import java.io.FileReader import java.io.FileReader
@ -136,6 +137,9 @@ class CustomAnvil : JavaPlugin() {
// temporary: handle 1.21 update // temporary: handle 1.21 update
Update_1_21.handleUpdate() Update_1_21.handleUpdate()
// plugin configuration updates
PluginUpdates.handlePluginUpdate()
// Register enchantment of compatible plugin and load configuration change. // Register enchantment of compatible plugin and load configuration change.
DependencyManager.handleCompatibilityConfig() DependencyManager.handleCompatibilityConfig()

View file

@ -21,7 +21,7 @@ object PacketManagerSelector {
NoPacketManager() NoPacketManager()
private val versionSpecificManager: PacketManagerBase? private val versionSpecificManager: PacketManagerBase?
get() { get() {
val versionParts = UpdateUtils.currentMinecraftVersion() val versionParts = UpdateUtils.currentMinecraftVersionArray()
if (versionParts[0] != 1) return null if (versionParts[0] != 1) return null
return when (versionParts[1]) { return when (versionParts[1]) {

View file

@ -283,4 +283,4 @@ debug_log_verbose: false
# ProtocoLib may also be used if the server is in an "unsupported" version even if this option is disabled. # ProtocoLib may also be used if the server is in an "unsupported" version even if this option is disabled.
force_protocolib: false force_protocolib: false
configVersion: 1.4.5 configVersion: 1.6.2

View file

@ -1,7 +1,7 @@
main: io.delilaheve.CustomAnvil main: io.delilaheve.CustomAnvil
name: CustomAnvil name: CustomAnvil
prefix: "Custom Anvil" prefix: "Custom Anvil"
version: 1.6.1 version: 1.6.2
folia-supported: true folia-supported: true
description: Allow to customise anvil mechanics description: Allow to customise anvil mechanics
api-version: 1.16 api-version: 1.16