From f6aa62547fb5e150effe1aea17ad44cd025a03a2 Mon Sep 17 00:00:00 2001 From: Frank Date: Sun, 10 Jul 2022 01:06:22 +0200 Subject: [PATCH] [Feature] `/bclib print dimensions` command --- .../bclib/commands/CommandRegistry.java | 7 ++++ .../betterx/bclib/commands/DumpDatapack.java | 19 +++++++-- .../org/betterx/bclib/commands/PrintInfo.java | 40 +++++++++++++++++++ 3 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 src/main/java/org/betterx/bclib/commands/PrintInfo.java diff --git a/src/main/java/org/betterx/bclib/commands/CommandRegistry.java b/src/main/java/org/betterx/bclib/commands/CommandRegistry.java index 1ccd2962..b5a87765 100644 --- a/src/main/java/org/betterx/bclib/commands/CommandRegistry.java +++ b/src/main/java/org/betterx/bclib/commands/CommandRegistry.java @@ -48,6 +48,13 @@ public class CommandRegistry { .requires(source -> source.hasPermission(Commands.LEVEL_OWNERS)) .executes(ctx -> DumpDatapack.dumpDatapack(ctx)) ) + .then(Commands.literal("print") + .requires(source -> source.hasPermission(Commands.LEVEL_OWNERS)) + .then(Commands.literal("dimensions") + .requires(source -> source.hasPermission(Commands.LEVEL_OWNERS)) + .executes(ctx -> PrintInfo.printDimensions(ctx)) + ) + ) .then(Commands.literal("debug_ore") .requires(source -> source.hasPermission(Commands.LEVEL_OWNERS)) .executes(ctx -> revealOre(ctx)) diff --git a/src/main/java/org/betterx/bclib/commands/DumpDatapack.java b/src/main/java/org/betterx/bclib/commands/DumpDatapack.java index 5f3d4003..7b6f4f06 100644 --- a/src/main/java/org/betterx/bclib/commands/DumpDatapack.java +++ b/src/main/java/org/betterx/bclib/commands/DumpDatapack.java @@ -13,6 +13,8 @@ import com.mojang.serialization.codecs.KeyDispatchCodec; import net.minecraft.commands.CommandSourceStack; import net.minecraft.core.Holder; import net.minecraft.core.RegistryAccess; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.Style; import net.minecraft.resources.RegistryOps; import net.minecraft.tags.TagEntry; import net.minecraft.tags.TagFile; @@ -45,24 +47,32 @@ import java.nio.file.Files; public class DumpDatapack { static int dumpDatapack(CommandContext ctx) { - dumpDatapack(ctx.getSource().getLevel().registryAccess()); + File base = new File(System.getProperty("user.dir"), "bclib_datapack_dump"); + dumpDatapack(base, ctx.getSource().getLevel().registryAccess()); + + ctx.getSource().sendSuccess( + Component.literal("Succesfully written to:\n ").append( + Component.literal(base.toString()).setStyle(Style.EMPTY.withUnderlined(true)) + ), + false + ); return Command.SINGLE_SUCCESS; } - public static void dumpDatapack(RegistryAccess registryAccess) { + public static void dumpDatapack(File base, RegistryAccess registryAccess) { final RegistryOps registryOps = RegistryOps.create(JsonOps.INSTANCE, registryAccess); GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder = gsonBuilder.setPrettyPrinting(); Gson gson = gsonBuilder.create(); - registryAccess.registries().forEach(r -> dumpDatapack(r, registryOps, gson)); + registryAccess.registries().forEach(r -> dumpDatapack(base, r, registryOps, gson)); } private static void dumpDatapack( + File base, RegistryAccess.RegistryEntry registry, RegistryOps registryOps, Gson gson ) { - File base = new File(System.getProperty("user.dir"), "bclib_datapack_dump"); BCLib.LOGGER.info(registry.key().toString()); // Tag Output registry.value() @@ -261,5 +271,6 @@ public class DumpDatapack { BCLib.LOGGER.error(" !!! Could not determine Codec: " + obj.getClass()); } }); + } } diff --git a/src/main/java/org/betterx/bclib/commands/PrintInfo.java b/src/main/java/org/betterx/bclib/commands/PrintInfo.java new file mode 100644 index 00000000..4fdba12f --- /dev/null +++ b/src/main/java/org/betterx/bclib/commands/PrintInfo.java @@ -0,0 +1,40 @@ +package org.betterx.bclib.commands; + +import com.mojang.brigadier.Command; +import com.mojang.brigadier.context.CommandContext; +import net.minecraft.ChatFormatting; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.MutableComponent; +import net.minecraft.network.chat.Style; +import net.minecraft.world.level.Level; + +public class PrintInfo { + static int printDimensions(CommandContext ctx) { + + MutableComponent result = Component.literal("World Dimensions: ") + .setStyle(Style.EMPTY.withBold(true).withColor(ChatFormatting.BLUE)); + + for (var serverLevel : ctx.getSource().getLevel().getServer().getAllLevels()) { + var generator = serverLevel.getChunkSource().getGenerator(); + String output = "\n - " + serverLevel.dimension().location().toString() + ": " + + "\n " + generator.toString().trim() + " " + + generator + .getBiomeSource() + .toString() + .replace("\n", "\n "); + var cl = ChatFormatting.LIGHT_PURPLE; + if (serverLevel.dimension().location().equals(Level.OVERWORLD.location())) + cl = ChatFormatting.WHITE; + else if (serverLevel.dimension().location().equals(Level.NETHER.location())) + cl = ChatFormatting.RED; + if (serverLevel.dimension().location().equals(Level.END.location())) + cl = ChatFormatting.YELLOW; + Component dimComponent = Component.literal(output) + .setStyle(Style.EMPTY.withBold(false).withColor(cl)); + result.append(dimComponent); + } + ctx.getSource().sendSuccess(result, false); + return Command.SINGLE_SUCCESS; + } +}