diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a812889..2515591 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,10 +1,10 @@ name: Build and Publish -on: [ push, pull_request, workflow_dispatch ] +on: [ push, workflow_dispatch ] env: MINECRAFT_VERSION: 1.21.1 JAVA_VERSION: 21 - VERSION: 1.4.0+1.21.1 + VERSION: 1.4.1+1.21.1 LOADERS: fabric RELEASE_NAME: Advanced Locate MODRINTH_ID: xX0k30jj diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a551e1..961e5e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,3 @@ -* Can now specify a maximum distance when searching for nearest structures -* Repairing config file if not valid -* Updated translations -* Actually shows tooltip in config screen -* Updated Fabric API version \ No newline at end of file +* Fixed the mod reloading entirely when changing config +* Fixed config not being changed correctly +* Fixed default maximum distance not being used right when not specified in AdvancedLocateCommand.java \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index 0906e56..2c4ecd9 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,7 +9,7 @@ yarn_mappings=1.21.1+build.3 loader_version=0.16.7 # Mod Properties -mod_version=1.4.0-1.21.1 +mod_version=1.4.1-1.21.1 maven_group=fr.kolala archives_base_name=advancedlocate diff --git a/src/main/java/fr/kolala/advancedlocate/AdvancedLocate.java b/src/main/java/fr/kolala/advancedlocate/AdvancedLocate.java index 95e72c1..7088186 100644 --- a/src/main/java/fr/kolala/advancedlocate/AdvancedLocate.java +++ b/src/main/java/fr/kolala/advancedlocate/AdvancedLocate.java @@ -19,10 +19,6 @@ public class AdvancedLocate implements ModInitializer { @Override public void onInitialize() { - registerCommands(); - } - - public static void registerCommands() { LOGGER.info("Registering custom argument type."); ArgumentTypeRegistry.registerArgumentType(Identifier.of(MOD_ID, "distance"), DistanceArgumentType.class, @@ -30,6 +26,11 @@ public static void registerCommands() { LOGGER.info("Checking config file integrity."); if (!ConfigHelper.checkConfigFileIntegrity()) LOGGER.error("Config file is not valid! The mod may not work when trying to execute a command, be aware!"); + registerCommands(); + } + + public static void registerCommands() { + LOGGER.info("Registering commands."); CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> AdvancedLocateCommand.register(dispatcher)); CommandRegistrationCallback.EVENT.register(((dispatcher, registryAccess, environment) -> ConfiguratorCommand.register(dispatcher))); diff --git a/src/main/java/fr/kolala/advancedlocate/command/AdvancedLocateCommand.java b/src/main/java/fr/kolala/advancedlocate/command/AdvancedLocateCommand.java index a139f11..4f616df 100644 --- a/src/main/java/fr/kolala/advancedlocate/command/AdvancedLocateCommand.java +++ b/src/main/java/fr/kolala/advancedlocate/command/AdvancedLocateCommand.java @@ -29,9 +29,9 @@ import java.util.*; public class AdvancedLocateCommand { - private static final int MAX_AMOUNT = ConfigHelper.getInt("max_amount"); - private static final int MAX_RADIUS = ConfigHelper.getInt("max_radius"); - private static final int MAX_NEIGHBOUR_RADIUS = ConfigHelper.getInt("max_neighbour_radius"); + private static final int MAX_AMOUNT = ConfigHelper.getIntOrDefault("max_amount"); + private static final int MAX_RADIUS = ConfigHelper.getIntOrDefault("max_radius"); + private static final int MAX_NEIGHBOUR_RADIUS = ConfigHelper.getIntOrDefault("max_neighbour_radius"); private static final DynamicCommandExceptionType STRUCTURE_NOT_FOUND_EXCEPTION = new DynamicCommandExceptionType( id -> Text.translatable("commands.locate.structure.not_found", id) ); @@ -87,15 +87,15 @@ private static Optional> getSt // STRUCTURE being the structure type(s) you want to search for, // max_distance being the maximum distance (in blocks) of the structures (default: 1600) private static int executeLocateNearestStructureDefault(ServerCommandSource source, RegistryPredicateArgumentType.RegistryPredicate predicate) throws CommandSyntaxException { - return executeLocateNearestStructure(source, predicate, ConfigHelper.getInt("default_amount"), ConfigHelper.getInt("default_max_distance")); + return executeLocateNearestStructure(source, predicate, ConfigHelper.getIntOrDefault("default_amount"), ConfigHelper.getIntOrDefault("default_max_distance") / 16); } private static int executeLocateNearestStructureAmount(ServerCommandSource source, RegistryPredicateArgumentType.RegistryPredicate predicate, int amount) throws CommandSyntaxException { - return executeLocateNearestStructure(source, predicate, amount, ConfigHelper.getInt("default_max_distance")); + return executeLocateNearestStructure(source, predicate, amount, ConfigHelper.getIntOrDefault("default_max_distance") / 16); } private static int executeLocateNearestStructureMaxDistance(ServerCommandSource source, RegistryPredicateArgumentType.RegistryPredicate predicate, int maxDistance) throws CommandSyntaxException { - return executeLocateNearestStructure(source, predicate, ConfigHelper.getInt("default_amount"), maxDistance); + return executeLocateNearestStructure(source, predicate, ConfigHelper.getIntOrDefault("default_amount"), maxDistance); } private static int executeLocateNearestStructure(ServerCommandSource source, RegistryPredicateArgumentType.RegistryPredicate predicate, int amount, int maxDistance) throws CommandSyntaxException { diff --git a/src/main/java/fr/kolala/advancedlocate/command/ConfiguratorCommand.java b/src/main/java/fr/kolala/advancedlocate/command/ConfiguratorCommand.java index bd2c6fb..d85ea9e 100644 --- a/src/main/java/fr/kolala/advancedlocate/command/ConfiguratorCommand.java +++ b/src/main/java/fr/kolala/advancedlocate/command/ConfiguratorCommand.java @@ -12,8 +12,6 @@ public class ConfiguratorCommand { public static void register (CommandDispatcher dispatcher) { - ConfigHelper.createConfigFile(); - for (String field : ConfigHelper.listFields()) { dispatcher.register(CommandManager.literal("advancedlocate").requires(source -> source.hasPermissionLevel(2)).then(CommandManager.literal("config") .then(CommandManager.literal("get") @@ -25,9 +23,9 @@ public static void register (CommandDispatcher dispatcher) } private static int getIntValue(ServerCommandSource source, String field) { - source.sendFeedback(() -> Text.translatable("command.advancedlocate.config.get", field, String.valueOf(ConfigHelper.getInt(field))), false); + source.sendFeedback(() -> Text.translatable("command.advancedlocate.config.get", field, String.valueOf(ConfigHelper.getIntOrDefault(field))), false); - return 0; + return 1; } private static int setIntValue(ServerCommandSource source, String field, int value) { @@ -36,7 +34,7 @@ private static int setIntValue(ServerCommandSource source, String field, int val if (json == null) { source.sendFeedback(() -> Text.translatable("command.advancedlocate.config.fail", field, value), false); AdvancedLocate.LOGGER.error("Couldn't get json config."); - return 1; + return 0; } json.addProperty(field, value); @@ -47,13 +45,13 @@ private static int setIntValue(ServerCommandSource source, String field, int val // Reload structure commands AdvancedLocate.registerCommands(); - return 0; + return 1; } else { source.sendFeedback(() -> Text.translatable("command.advancedlocate.config.fail", field, value), false); AdvancedLocate.LOGGER.info("Couldn't change config."); - return 1; + return 0; } } diff --git a/src/main/java/fr/kolala/advancedlocate/config/ConfigHelper.java b/src/main/java/fr/kolala/advancedlocate/config/ConfigHelper.java index 6b928c5..a78a2aa 100644 --- a/src/main/java/fr/kolala/advancedlocate/config/ConfigHelper.java +++ b/src/main/java/fr/kolala/advancedlocate/config/ConfigHelper.java @@ -18,6 +18,10 @@ public class ConfigHelper { "max_neighbour_radius", 5 ); + public static Set listFields() { + return defaultFieldValueMap.keySet(); + } + public static int getDefaultValue(String field) { return defaultFieldValueMap.get(field); } @@ -177,8 +181,9 @@ public static boolean write(JsonObject json) { // Config related methods - public static int getInt(String name) { - return Objects.requireNonNull(get(name)).getAsInt(); + public static int getIntOrDefault(String name) { + JsonElement value = get(name); + return value == null ? defaultFieldValueMap.get(name) : value.getAsInt(); } private static JsonElement get(String name) { @@ -189,14 +194,4 @@ private static JsonElement get(String name) { } return json.get(name); } - - public static Set listFields() { - JsonObject json = read(); - if (json == null) { - AdvancedLocate.LOGGER.error("Couldn't list fields in json config!"); - return new HashSet<>(); - } - return json.keySet(); - } - } \ No newline at end of file diff --git a/src/main/java/fr/kolala/advancedlocate/config/ConfigScreen.java b/src/main/java/fr/kolala/advancedlocate/config/ConfigScreen.java index 13eadac..474c3c2 100644 --- a/src/main/java/fr/kolala/advancedlocate/config/ConfigScreen.java +++ b/src/main/java/fr/kolala/advancedlocate/config/ConfigScreen.java @@ -22,7 +22,7 @@ public ConfigScreen(Screen parent) { ConfigEntryBuilder entryBuilder = builder.entryBuilder(); content = ConfigHelper.getDefaultJson(); for (String entry : ConfigHelper.listFields()) { - general.addEntry(entryBuilder.startIntField(Text.translatable("option.advancedlocate." + entry), ConfigHelper.getInt(entry)) + general.addEntry(entryBuilder.startIntField(Text.translatable("option.advancedlocate." + entry), ConfigHelper.getIntOrDefault(entry)) .setDefaultValue(ConfigHelper.getDefaultValue(entry)) .setTooltip(Text.translatable("tooltip.advancedlocate." + entry)) .setSaveConsumer(newValue -> content.addProperty(entry, newValue))