ValidateCPBCommand.java
package com.hypixel.hytale.server.core.command.commands.utility;
import com.hypixel.hytale.assetstore.AssetPack;
import com.hypixel.hytale.common.util.PathUtil;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.asset.AssetModule;
import com.hypixel.hytale.server.core.command.system.CommandContext;
import com.hypixel.hytale.server.core.command.system.arguments.system.OptionalArg;
import com.hypixel.hytale.server.core.command.system.arguments.types.ArgTypes;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractAsyncCommand;
import com.hypixel.hytale.server.core.prefab.selection.buffer.BsonPrefabBufferDeserializer;
import com.hypixel.hytale.server.core.util.BsonUtil;
import com.hypixel.hytale.server.core.util.io.FileUtil;
import com.hypixel.hytale.sneakythrow.SneakyThrow;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
public class ValidateCPBCommand extends AbstractAsyncCommand {
private static final String UNABLE_TO_LOAD_MODEL = "Unable to load entity with model ";
private static final String FAILED_TO_FIND_BLOCK = "Failed to find block ";
@Nonnull
private final OptionalArg<String> pathArg;
public ValidateCPBCommand() {
super("validatecpb", "server.commands.validatecpb.desc");
this.pathArg = this.withOptionalArg("path", "server.commands.validatecpb.path.desc", ArgTypes.STRING);
}
@Nonnull
protected CompletableFuture<Void> executeAsync(@Nonnull CommandContext context) {
if (this.pathArg.provided(context)) {
String path = (String)this.pathArg.get(context);
return CompletableFuture.runAsync(() -> convertPrefabs(context, PathUtil.get(path)));
} else {
return CompletableFuture.runAsync(() -> {
for(AssetPack pack : AssetModule.get().getAssetPacks()) {
convertPrefabs(context, pack.getRoot());
}
});
}
}
private static void convertPrefabs(@Nonnull CommandContext context, @Nonnull Path assetPath) {
List<String> failed = new ObjectArrayList<String>();
try {
Stream<Path> stream = Files.walk(assetPath, FileUtil.DEFAULT_WALK_TREE_OPTIONS_ARRAY);
try {
CompletableFuture[] futures = (CompletableFuture[])stream.filter((path) -> Files.isRegularFile(path, new LinkOption[0]) && path.toString().endsWith(".prefab.json")).map((path) -> BsonUtil.readDocument(path, false).thenAccept((document) -> {
BsonPrefabBufferDeserializer.INSTANCE.deserialize(path, document);
context.sendMessage(Message.translation("server.general.loadedPrefab").param("name", path.toString()));
}).exceptionally((throwable) -> {
String message = throwable.getCause().getMessage();
if (message != null) {
if (message.contains("Failed to find block ")) {
String var5 = String.valueOf(path);
failed.add("Failed to load " + var5 + " because " + message);
return null;
}
if (message.contains("Unable to load entity with model ")) {
String var4 = String.valueOf(path);
failed.add("Failed to load " + var4 + " because " + message);
return null;
}
}
String var10001 = String.valueOf(path);
failed.add("Failed to load " + var10001 + " because " + String.valueOf(message != null ? message : throwable.getCause().getClass()));
(new Exception("Failed to load " + String.valueOf(path), throwable.getCause())).printStackTrace();
return null;
})).toArray((x$0) -> new CompletableFuture[x$0]);
CompletableFuture.allOf(futures).join();
} catch (Throwable var7) {
if (stream != null) {
try {
stream.close();
} catch (Throwable var6) {
var7.addSuppressed(var6);
}
}
throw var7;
}
if (stream != null) {
stream.close();
}
} catch (IOException e) {
throw SneakyThrow.sneakyThrow(e);
}
if (!failed.isEmpty()) {
context.sendMessage(Message.translation("server.commands.validatecpb.failed").param("failed", failed.toString()));
}
context.sendMessage(Message.translation("server.commands.prefabConvertionDone").param("path", assetPath.toString()));
}
}