-
Notifications
You must be signed in to change notification settings - Fork 7
/
TemplatesToSnbtCommand.java
52 lines (44 loc) · 2.25 KB
/
TemplatesToSnbtCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package de.melanx.skyblockbuilder.commands.helper;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import de.melanx.skyblockbuilder.SkyblockBuilder;
import de.melanx.skyblockbuilder.util.SkyPaths;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtIo;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.network.chat.Component;
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() {
// Highlights all spawns for a few seconds
return Commands.literal("templates_to_snbt").requires(source -> source.hasPermission(2))
.executes(TemplatesToSnbtCommand::convert);
}
private static int convert(CommandContext<CommandSourceStack> context) {
for (File original : Objects.requireNonNull(SkyPaths.TEMPLATES_DIR.toFile().listFiles())) {
if (original.toString().endsWith(".nbt")) {
Path fileName = original.toPath().getFileName();
String convertedName = fileName.toString().substring(0, fileName.toString().length() - ".nbt".length()) + ".snbt";
Path converted = SkyPaths.TEMPLATES_DIR.resolve(convertedName);
try {
CompoundTag nbt = NbtIo.readCompressed(original);
Files.writeString(converted, NbtUtils.structureToSnbt(nbt));
context.getSource().sendSuccess(() -> Component.translatable("skyblockbuilder.command.success.convert_template", fileName, convertedName), true);
} catch (IOException e) {
SkyblockBuilder.getLogger().error("Failed to convert " + original + " to " + convertedName, e);
}
}
}
context.getSource().sendSystemMessage(Component.literal("This command will be removed, use \"/skyblock convert\" in future"));
return 1;
}
}