-
Notifications
You must be signed in to change notification settings - Fork 7
/
ConvertCommand.java
59 lines (49 loc) · 2.81 KB
/
ConvertCommand.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
53
54
55
56
57
58
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;
}
}