Skip to content

Commit

Permalink
add convert command to convert structures to (s)nbt
Browse files Browse the repository at this point in the history
  • Loading branch information
MelanX committed Jul 29, 2023
1 parent bda4c5c commit 76b6ef7
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 15 deletions.
6 changes: 2 additions & 4 deletions src/main/java/de/melanx/skyblockbuilder/EventListener.java
Expand Up @@ -6,10 +6,7 @@
import de.melanx.skyblockbuilder.api.SkyblockBuilderAPI;
import de.melanx.skyblockbuilder.client.GameProfileCache;
import de.melanx.skyblockbuilder.commands.*;
import de.melanx.skyblockbuilder.commands.helper.InventoryCommand;
import de.melanx.skyblockbuilder.commands.helper.ListCommand;
import de.melanx.skyblockbuilder.commands.helper.SpawnsCommand;
import de.melanx.skyblockbuilder.commands.helper.TemplatesToSnbtCommand;
import de.melanx.skyblockbuilder.commands.helper.*;
import de.melanx.skyblockbuilder.commands.invitation.AcceptCommand;
import de.melanx.skyblockbuilder.commands.invitation.DeclineCommand;
import de.melanx.skyblockbuilder.commands.invitation.InviteCommand;
Expand Down Expand Up @@ -83,6 +80,7 @@ public static void onRegisterCommands(RegisterCommandsEvent event) {
event.getDispatcher().register(Commands.literal("skyblock")
.requires(source -> SkyblockBuilderAPI.teamManagementEnabled())
.then(AcceptCommand.register())
.then(ConvertCommand.register())
.then(CreateCommand.register())
.then(DeclineCommand.register())
.then(HomeCommand.register())
Expand Down
@@ -0,0 +1,59 @@
package de.melanx.skyblockbuilder.commands.helper;

import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import de.melanx.skyblockbuilder.SkyblockBuilder;
import de.melanx.skyblockbuilder.util.SkyPaths;
import de.melanx.skyblockbuilder.util.TemplateUtil;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Objects;

public class ConvertCommand {

public static ArgumentBuilder<CommandSourceStack, ?> register() {
// Highlights all spawns for a few seconds
return Commands.literal("convert").requires(source -> source.hasPermission(2))
.executes(ConvertCommand::convert);
}

private static int convert(CommandContext<CommandSourceStack> context) {
for (File original : Objects.requireNonNull(SkyPaths.CONVERT_INPUT.toFile().listFiles())) {
Path fileName = original.toPath().getFileName();
if (original.toString().endsWith(".nbt")) {
String convertedName = fileName.toString().substring(0, fileName.toString().length() - ".nbt".length()) + ".snbt";
Path converted = SkyPaths.CONVERT_OUTPUT.resolve(convertedName);

try {
CompoundTag nbt = TemplateUtil.readTemplate(original.toPath(), false);
TemplateUtil.writeTemplate(converted, nbt, true);

context.getSource().sendSuccess(() -> Component.translatable("skyblockbuilder.command.success.convert_template", fileName, convertedName), true);
} catch (IOException | CommandSyntaxException e) {
SkyblockBuilder.getLogger().error("Failed to convert " + original + " to " + convertedName, e);
}
} else if (original.toString().endsWith(".snbt")) {
String convertedName = fileName.toString().substring(0, fileName.toString().length() - ".snbt".length()) + ".nbt";
Path converted = SkyPaths.CONVERT_OUTPUT.resolve(convertedName);

try {
CompoundTag nbt = TemplateUtil.readTemplate(original.toPath(), true);
TemplateUtil.writeTemplate(converted, nbt, false);

context.getSource().sendSuccess(() -> Component.translatable("skyblockbuilder.command.success.convert_template", fileName, convertedName), true);
} catch (IOException | CommandSyntaxException e) {
SkyblockBuilder.getLogger().error("Failed to convert " + original + " to " + convertedName, e);
}
}
}

return 1;
}
}
Expand Up @@ -6,21 +6,20 @@
import de.melanx.skyblockbuilder.util.SkyPaths;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtIo;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.network.chat.Component;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
import org.moddingx.libx.annotation.meta.RemoveIn;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;

@RemoveIn(minecraft = "1.20.2")
@Deprecated(forRemoval = true) // use ConvertCommand instead
public class TemplatesToSnbtCommand {

public static ArgumentBuilder<CommandSourceStack, ?> register() {
Expand All @@ -36,11 +35,8 @@ private static int convert(CommandContext<CommandSourceStack> context) {
String convertedName = fileName.toString().substring(0, fileName.toString().length() - ".nbt".length()) + ".snbt";
Path converted = SkyPaths.TEMPLATES_DIR.resolve(convertedName);

StructureTemplate template = new StructureTemplate();
try {
CompoundTag nbt = NbtIo.readCompressed(original);
HolderLookup.RegistryLookup<Block> blockRegistryLookup = context.getSource().getServer().registryAccess().registryOrThrow(Registries.BLOCK).asLookup();
template.load(blockRegistryLookup, nbt);
Files.writeString(converted, NbtUtils.structureToSnbt(nbt));

context.getSource().sendSuccess(() -> Component.translatable("skyblockbuilder.command.success.convert_template", fileName, convertedName), true);
Expand All @@ -49,6 +45,7 @@ private static int convert(CommandContext<CommandSourceStack> context) {
}
}
}
context.getSource().sendSystemMessage(Component.literal("This command will be removed, use \"/skyblock convert\" in future"));

return 1;
}
Expand Down
Expand Up @@ -39,6 +39,5 @@ public class TemplatesConfig {
"}"})
public static Optional<TemplateInfo> spawn = Optional.empty();

public record Spawn(BlockPos pos, WorldUtil.Directions direction) {
}
public record Spawn(BlockPos pos, WorldUtil.Directions direction) {}
}
Expand Up @@ -44,7 +44,7 @@ public static void updateTemplates() {
TEMPLATE_MAP.put(info.name().toLowerCase(Locale.ROOT), template);
}

if (TEMPLATE_MAP.size() == 0) {
if (TEMPLATE_MAP.isEmpty()) {
throw new IllegalStateException("You need at least one configured template.");
}

Expand Down
8 changes: 7 additions & 1 deletion src/main/java/de/melanx/skyblockbuilder/util/SkyPaths.java
Expand Up @@ -30,7 +30,10 @@ public class SkyPaths {

// paths
public static final Path MOD_CONFIG = FMLPaths.CONFIGDIR.get().resolve("skyblockbuilder");
public static final Path MOD_EXPORTS = FMLPaths.GAMEDIR.get().resolve("skyblock_exports");
public static final Path SKYBLOCK_UTILS = FMLPaths.GAMEDIR.get().resolve("skyblockbuilder");
public static final Path MOD_EXPORTS = SKYBLOCK_UTILS.resolve("exports");
public static final Path CONVERT_INPUT = SKYBLOCK_UTILS.resolve("convert_input");
public static final Path CONVERT_OUTPUT = SKYBLOCK_UTILS.resolve("convert_output");
public static final Path TEMPLATES_DIR = MOD_CONFIG.resolve("templates");
public static final Path ICONS_DIR = TEMPLATES_DIR.resolve("icons");
public static final Path DATA_DIR = MOD_CONFIG.resolve("data");
Expand All @@ -47,7 +50,10 @@ public class SkyPaths {
public static void createDirectories() {
try {
Files.createDirectories(MOD_CONFIG);
Files.createDirectories(SKYBLOCK_UTILS);
Files.createDirectories(MOD_EXPORTS);
Files.createDirectories(CONVERT_INPUT);
Files.createDirectories(CONVERT_OUTPUT);
Files.createDirectories(TEMPLATES_DIR);
Files.createDirectories(ICONS_DIR);
Files.createDirectories(DATA_DIR);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/de/melanx/skyblockbuilder/util/TemplateUtil.java
Expand Up @@ -2,13 +2,15 @@

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import de.melanx.skyblockbuilder.config.common.TemplatesConfig;
import de.melanx.skyblockbuilder.config.common.WorldConfig;
import de.melanx.skyblockbuilder.data.Team;
import de.melanx.skyblockbuilder.world.IslandPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtIo;
import net.minecraft.nbt.NbtUtils;
import org.apache.commons.io.IOUtils;

import javax.annotation.Nonnull;
import java.io.IOException;
Expand Down Expand Up @@ -98,4 +100,12 @@ public static void writeTemplate(Path path, CompoundTag template, boolean asSnbt
throw new IllegalStateException("Failed to create template file", e);
}
}

public static CompoundTag readTemplate(Path path, boolean snbt) throws IOException, CommandSyntaxException {
if (snbt) {
return NbtUtils.snbtToStructure(IOUtils.toString(Files.newBufferedReader(path)));
} else {
return NbtIo.readCompressed(path.toFile());
}
}
}

0 comments on commit 76b6ef7

Please sign in to comment.