-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
more command helpers and dump tags command
- Loading branch information
Showing
14 changed files
with
259 additions
and
29 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
Common/src/main/java/dev/upcraft/sparkweave/api/command/CommandHelper.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,19 @@ | ||
package dev.upcraft.sparkweave.api.command; | ||
|
||
import com.mojang.brigadier.arguments.ArgumentType; | ||
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType; | ||
import dev.upcraft.sparkweave.api.platform.Services; | ||
import dev.upcraft.sparkweave.api.platform.services.SparkweaveHelperService; | ||
import net.minecraft.commands.synchronization.ArgumentTypeInfo; | ||
import net.minecraft.network.chat.Component; | ||
|
||
public class CommandHelper { | ||
|
||
private static final SparkweaveHelperService HELPER = Services.getService(SparkweaveHelperService.class); | ||
|
||
public static final DynamicCommandExceptionType IO_EXCEPTION = new DynamicCommandExceptionType(ex -> Component.translatable("commands.sparkweave.error.io_exception", ex instanceof Throwable t ? t.getMessage() : ex)); | ||
|
||
public static <A extends ArgumentType<?>, T extends ArgumentTypeInfo.Template<A>> ArgumentTypeInfo<A, T> createArgumentInfo(Class<A> clazz, ArgumentTypeInfo<A, T> info) { | ||
return HELPER.create(clazz, info); | ||
} | ||
} |
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
118 changes: 118 additions & 0 deletions
118
Common/src/main/java/dev/upcraft/sparkweave/command/DumpTagsCommand.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,118 @@ | ||
package dev.upcraft.sparkweave.command; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonObject; | ||
import com.mojang.brigadier.builder.LiteralArgumentBuilder; | ||
import com.mojang.brigadier.context.CommandContext; | ||
import com.mojang.brigadier.exceptions.CommandSyntaxException; | ||
import dev.upcraft.sparkweave.SparkweaveMod; | ||
import dev.upcraft.sparkweave.api.command.CommandHelper; | ||
import dev.upcraft.sparkweave.api.command.argument.RegistryArgumentType; | ||
import dev.upcraft.sparkweave.api.platform.Services; | ||
import net.minecraft.ChatFormatting; | ||
import net.minecraft.commands.CommandSourceStack; | ||
import net.minecraft.commands.Commands; | ||
import net.minecraft.core.Registry; | ||
import net.minecraft.network.chat.ClickEvent; | ||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.network.chat.HoverEvent; | ||
import net.minecraft.resources.ResourceKey; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
public class DumpTagsCommand { | ||
|
||
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); | ||
|
||
public static void register(LiteralArgumentBuilder<CommandSourceStack> $) { | ||
$.then(Commands.literal("dump_tags") | ||
.requires(src -> src.hasPermission(Commands.LEVEL_OWNERS)) | ||
.executes(DumpTagsCommand::dumpAllTags) | ||
.then(Commands.argument("type", RegistryArgumentType.registry()) | ||
.executes(ctx -> dumpTags(ctx, RegistryArgumentType.getRegistry(ctx, "type"))) | ||
) | ||
.then(Commands.literal("all") | ||
.executes(DumpTagsCommand::dumpAllTags) | ||
) | ||
); | ||
} | ||
|
||
private static int dumpAllTags(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException { | ||
var player = ctx.getSource().getPlayerOrException(); | ||
var registryAccess = ctx.getSource().getServer().registries().compositeAccess(); | ||
var registries = registryAccess.listRegistries().toList(); | ||
var dir = Services.PLATFORM.getGameDir().resolve(SparkweaveMod.MODID).resolve("tag_export"); | ||
|
||
for (ResourceKey<? extends Registry<?>> registryKey : registries) { | ||
var registry = registryAccess.registry(registryKey).orElseThrow(); | ||
saveTags(registry, dir); | ||
} | ||
|
||
if (ctx.getSource().getServer().isSingleplayerOwner(player.getGameProfile())) { | ||
var path = Component.literal(dir.toString()).withStyle(style -> style | ||
.applyFormats(ChatFormatting.BLUE, ChatFormatting.UNDERLINE) | ||
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Component.translatable("chat.sparkweave.open_folder"))) | ||
.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_FILE, dir.toString())) | ||
); | ||
|
||
//TODO directly send to client to bypass message click event filtering | ||
ctx.getSource().sendSuccess(() -> Component.translatable("commands.sparkweave.debug.dump_tags.multi_success_path", registries.size(), path), true); | ||
} else { | ||
ctx.getSource().sendSuccess(() -> Component.translatable("commands.sparkweave.debug.dump_tags.multi_success", registries.size()), true); | ||
} | ||
|
||
return registries.size(); | ||
} | ||
|
||
private static void saveTags(Registry<?> registry, Path dir) throws CommandSyntaxException { | ||
Path rootDir = dir.resolve(registry.key().location().getNamespace()).resolve(registry.key().location().getPath()); | ||
|
||
var tags = registry.getTags().toList(); | ||
for (var tagPair : tags) { | ||
var name = tagPair.getFirst().location(); | ||
var outputFile = rootDir.resolve(name.getNamespace()).resolve(name.getPath() + ".json"); | ||
|
||
var json = new JsonObject(); | ||
var array = new JsonArray(); | ||
tagPair.getSecond().stream() | ||
.map(holder -> holder.unwrap().map(k -> k.location().toString(), Object::toString)) | ||
.forEach(array::add); | ||
json.add("values", array); | ||
|
||
try { | ||
Files.createDirectories(outputFile.getParent()); | ||
try (var writer = Files.newBufferedWriter(outputFile)) { | ||
GSON.toJson(json, writer); | ||
} | ||
} catch (IOException e) { | ||
throw CommandHelper.IO_EXCEPTION.create(e); | ||
} | ||
} | ||
} | ||
|
||
private static int dumpTags(CommandContext<CommandSourceStack> ctx, Registry<?> registry) throws CommandSyntaxException { | ||
var player = ctx.getSource().getPlayerOrException(); | ||
|
||
var dir = Services.PLATFORM.getGameDir().resolve("sparkweave").resolve("tag_export"); | ||
saveTags(registry, dir); | ||
|
||
if (ctx.getSource().getServer().isSingleplayerOwner(player.getGameProfile())) { | ||
var resolvedDir = dir.resolve(registry.key().location().getNamespace()).resolve(registry.key().location().getPath()).toString(); | ||
var path = Component.literal(resolvedDir).withStyle(style -> style | ||
.applyFormats(ChatFormatting.BLUE, ChatFormatting.UNDERLINE) | ||
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Component.translatable("chat.sparkweave.open_folder"))) | ||
.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_FILE, resolvedDir)) | ||
); | ||
|
||
//TODO directly send to client to bypass message click event filtering | ||
ctx.getSource().sendSuccess(() -> Component.translatable("commands.sparkweave.debug.dump_tags.success_path", registry.key().location(), path), true); | ||
} else { | ||
ctx.getSource().sendSuccess(() -> Component.translatable("commands.sparkweave.debug.dump_tags.success", registry.key().location()), true); | ||
} | ||
return 0; | ||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
Common/src/main/java/dev/upcraft/sparkweave/registry/SparkweaveCommandArgumentTypes.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,17 @@ | ||
package dev.upcraft.sparkweave.registry; | ||
|
||
import dev.upcraft.sparkweave.SparkweaveMod; | ||
import dev.upcraft.sparkweave.api.command.CommandHelper; | ||
import dev.upcraft.sparkweave.api.command.argument.RegistryArgumentType; | ||
import dev.upcraft.sparkweave.api.registry.RegistryHandler; | ||
import dev.upcraft.sparkweave.api.registry.RegistrySupplier; | ||
import net.minecraft.commands.synchronization.ArgumentTypeInfo; | ||
import net.minecraft.commands.synchronization.SingletonArgumentInfo; | ||
import net.minecraft.core.registries.Registries; | ||
|
||
public class SparkweaveCommandArgumentTypes { | ||
|
||
public static final RegistryHandler<ArgumentTypeInfo<?, ?>> ARGUMENT_TYPES = RegistryHandler.create(Registries.COMMAND_ARGUMENT_TYPE, SparkweaveMod.MODID); | ||
|
||
public static final RegistrySupplier<ArgumentTypeInfo<RegistryArgumentType, ?>> REGISTRY = ARGUMENT_TYPES.register("registry", () -> CommandHelper.createArgumentInfo(RegistryArgumentType.class, SingletonArgumentInfo.contextFree(RegistryArgumentType::registry))); | ||
} |
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
Oops, something went wrong.