-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Load roles and command aliases from datapack
- Loading branch information
Showing
9 changed files
with
136 additions
and
118 deletions.
There are no files selected for viewing
57 changes: 26 additions & 31 deletions
57
src/main/java/com/lovetropics/perms/CommandAliasConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,51 @@ | ||
package com.lovetropics.perms; | ||
|
||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
import com.lovetropics.lib.codec.MoreCodecs; | ||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.DataResult; | ||
import com.mojang.serialization.JsonOps; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.server.packs.resources.Resource; | ||
import net.minecraft.server.packs.resources.ResourceManager; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public record CommandAliasConfiguration(Map<String, String[]> aliases) { | ||
private static final CommandAliasConfiguration EMPTY = new CommandAliasConfiguration(Map.of()); | ||
|
||
public static final Codec<CommandAliasConfiguration> CODEC = Codec.unboundedMap(Codec.STRING, MoreCodecs.arrayOrUnit(Codec.STRING, String[]::new)) | ||
.xmap(CommandAliasConfiguration::new, CommandAliasConfiguration::aliases); | ||
|
||
public static CommandAliasConfiguration load() { | ||
Path path = Paths.get("config/command_aliases.json"); | ||
if (!Files.exists(path)) { | ||
if (!setupDefaultConfig(path)) { | ||
return EMPTY; | ||
} | ||
} | ||
private static final ThreadLocal<ResourceManager> RESOURCE_MANAGER = new ThreadLocal<>(); | ||
|
||
try (BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) { | ||
JsonObject root = JsonParser.parseReader(reader).getAsJsonObject(); | ||
DataResult<CommandAliasConfiguration> result = CODEC.parse(JsonOps.INSTANCE, root); | ||
result.error().ifPresent(error -> { | ||
LTPermissions.LOGGER.warn("Malformed command aliases configuration: {}", error); | ||
}); | ||
public static void setResourceManager(ResourceManager resourceManager) { | ||
RESOURCE_MANAGER.set(resourceManager); | ||
} | ||
|
||
return result.result().orElse(EMPTY); | ||
} catch (IOException e) { | ||
LTPermissions.LOGGER.warn("Failed to load command_aliases.json configuration", e); | ||
return EMPTY; | ||
} | ||
public static void clearResourceManager() { | ||
RESOURCE_MANAGER.remove(); | ||
} | ||
|
||
private static boolean setupDefaultConfig(Path path) { | ||
try (InputStream input = LTPermissions.class.getResourceAsStream("/data/" + LTPermissions.ID + "/default_command_aliases.json")) { | ||
Files.copy(input, path); | ||
return true; | ||
public static CommandAliasConfiguration load() { | ||
ResourceManager resourceManager = RESOURCE_MANAGER.get(); | ||
if (resourceManager == null) { | ||
LTPermissions.LOGGER.error("Resources not available, not loading command aliases"); | ||
return CommandAliasConfiguration.EMPTY; | ||
} | ||
Optional<Resource> resource = resourceManager.getResource(ResourceLocation.fromNamespaceAndPath(LTPermissions.ID, "command_aliases.json")); | ||
if (resource.isEmpty()) { | ||
return CommandAliasConfiguration.EMPTY; | ||
} | ||
try (BufferedReader reader = resource.get().openAsReader()) { | ||
return CODEC.parse(JsonOps.INSTANCE, JsonParser.parseReader(reader)) | ||
.resultOrPartial(error -> LTPermissions.LOGGER.warn("Malformed command aliases configuration: {}", error)) | ||
.orElse(EMPTY); | ||
} catch (IOException e) { | ||
LTPermissions.LOGGER.warn("Failed to load default command_aliases.json configuration", e); | ||
return false; | ||
LTPermissions.LOGGER.warn("Failed to load command_aliases.json configuration", e); | ||
return EMPTY; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/com/lovetropics/perms/mixin/ReloadableServerResourcesMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.lovetropics.perms.mixin; | ||
|
||
import com.lovetropics.perms.CommandAliasConfiguration; | ||
import net.minecraft.commands.Commands; | ||
import net.minecraft.core.LayeredRegistryAccess; | ||
import net.minecraft.server.RegistryLayer; | ||
import net.minecraft.server.ReloadableServerResources; | ||
import net.minecraft.server.packs.resources.ResourceManager; | ||
import net.minecraft.world.flag.FeatureFlagSet; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
|
||
import java.util.concurrent.CompletionStage; | ||
import java.util.concurrent.Executor; | ||
|
||
@Mixin(ReloadableServerResources.class) | ||
public class ReloadableServerResourcesMixin { | ||
@Inject(method = "lambda$loadResources$5", at = @At("HEAD")) | ||
private static void beforeLoadResources(FeatureFlagSet featureFlags, Commands.CommandSelection commandSelection, int functionCompilationLevel, ResourceManager resourceManager, Executor backgroundExecutor, Executor gameExecutor, LayeredRegistryAccess<RegistryLayer> registryAccess, CallbackInfoReturnable<CompletionStage<?>> ci) { | ||
CommandAliasConfiguration.setResourceManager(resourceManager); | ||
} | ||
|
||
@Inject(method = "lambda$loadResources$5", at = @At("TAIL")) | ||
private static void afterLoadResources(FeatureFlagSet featureFlags, Commands.CommandSelection commandSelection, int functionCompilationLevel, ResourceManager resourceManager, Executor backgroundExecutor, Executor gameExecutor, LayeredRegistryAccess<RegistryLayer> registryAccess, CallbackInfoReturnable<CompletionStage<?>> ci) { | ||
CommandAliasConfiguration.clearResourceManager(); | ||
} | ||
} |
2 changes: 0 additions & 2 deletions
2
src/main/resources/data/ltpermissions/default_command_aliases.json
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.