Skip to content

Commit

Permalink
* Fixed the mod reloading entirely when changing config
Browse files Browse the repository at this point in the history
* Fixed config not being changed correctly
* Fixed default maximum distance not being used right when not specified in AdvancedLocateCommand.java
* Fixed build workflow being called on pull request where it should not
  • Loading branch information
K0LALA committed Oct 26, 2024
1 parent 733d7a8 commit c86e083
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 38 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 3 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
* 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
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 5 additions & 4 deletions src/main/java/fr/kolala/advancedlocate/AdvancedLocate.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@ 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,
ConstantArgumentSerializer.of(DistanceArgumentType::distanceArgumentType));
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)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
Expand Down Expand Up @@ -87,15 +87,15 @@ private static Optional<? extends RegistryEntryList.ListBacked<Structure>> 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<Structure> 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<Structure> 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<Structure> 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<Structure> predicate, int amount, int maxDistance) throws CommandSyntaxException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
public class ConfiguratorCommand {

public static void register (CommandDispatcher<ServerCommandSource> 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")
Expand All @@ -25,9 +23,9 @@ public static void register (CommandDispatcher<ServerCommandSource> 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) {
Expand All @@ -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);
Expand All @@ -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;
}
}

Expand Down
19 changes: 7 additions & 12 deletions src/main/java/fr/kolala/advancedlocate/config/ConfigHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public class ConfigHelper {
"max_neighbour_radius", 5
);

public static Set<String> listFields() {
return defaultFieldValueMap.keySet();
}

public static int getDefaultValue(String field) {
return defaultFieldValueMap.get(field);
}
Expand Down Expand Up @@ -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) {
Expand All @@ -189,14 +194,4 @@ private static JsonElement get(String name) {
}
return json.get(name);
}

public static Set<String> listFields() {
JsonObject json = read();
if (json == null) {
AdvancedLocate.LOGGER.error("Couldn't list fields in json config!");
return new HashSet<>();
}
return json.keySet();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit c86e083

Please sign in to comment.