Try fixing world load bug

This commit is contained in:
SD 2021-04-05 07:35:52 +05:30
parent 55768bcaa9
commit b8ed937187
3 changed files with 63 additions and 29 deletions

View file

@ -7,7 +7,6 @@ public final class ModCommands {
CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> {
DimTeleportCommand.register(dispatcher);
PocketCommand.register(dispatcher);
PocketCommand.register(dispatcher);
});
}
}

View file

@ -29,7 +29,6 @@ import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtIo;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import net.fabricmc.loader.api.FabricLoader;
@ -70,34 +69,11 @@ public class PocketCommand {
}
private static int load(ServerCommandSource source, PocketTemplate template) throws CommandSyntaxException {
ServerPlayerEntity player = source.getPlayer();
boolean async = DimensionalDoorsInitializer.getConfig().getPocketsConfig().asyncWorldEditPocketLoading;
Consumer<Runnable> taskAcceptor = async ? r -> source.getMinecraftServer().execute(r) : Runnable::run;
Runnable task = () -> {
CompoundTag tag = Schematic.toTag(template.getSchematic());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
NbtIo.writeCompressed(tag, stream);
} catch (IOException e) {
throw new RuntimeException(e); // Can't happen, the stream is a ByteArrayOutputStream
}
Clipboard clipboard;
try {
clipboard = new SpongeSchematicReader(new NBTInputStream(new ByteArrayInputStream(stream.toByteArray()))).read();
} catch (IOException e) {
throw new RuntimeException(e); // Can't happen, the stream is a ByteArrayInputStream
}
taskAcceptor.accept(() -> {
WorldEdit.getInstance().getSessionManager().get(FabricAdapter.adaptPlayer(player)).setClipboard(new ClipboardHolder(clipboard));
source.sendFeedback(new TranslatableText("commands.pocket.loadedSchem", template.getId()), true);
});
};
if (async) {
CompletableFuture.runAsync(task);
} else {
task.run();
try {
return WorldeditHelper.load(source, template);
} catch (NoClassDefFoundError e) {
return 0;
}
return Command.SINGLE_SUCCESS;
}
private static int place(ServerPlayerEntity source, PocketTemplate template, BlockPlacementType blockPlacementType) throws CommandSyntaxException {

View file

@ -0,0 +1,59 @@
package org.dimdev.dimdoors.command;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.sk89q.jnbt.NBTInputStream;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.extent.clipboard.io.SpongeSchematicReader;
import com.sk89q.worldedit.fabric.FabricAdapter;
import com.sk89q.worldedit.session.ClipboardHolder;
import org.dimdev.dimdoors.DimensionalDoorsInitializer;
import org.dimdev.dimdoors.pockets.PocketTemplate;
import org.dimdev.dimdoors.util.schematic.Schematic;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtIo;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.TranslatableText;
public class WorldeditHelper {
static int load(ServerCommandSource source, PocketTemplate template) throws CommandSyntaxException {
ServerPlayerEntity player = source.getPlayer();
boolean async = DimensionalDoorsInitializer.getConfig().getPocketsConfig().asyncWorldEditPocketLoading;
Consumer<Runnable> taskAcceptor = async ? r -> source.getMinecraftServer().execute(r) : Runnable::run;
Runnable task = () -> {
CompoundTag tag = Schematic.toTag(template.getSchematic());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
NbtIo.writeCompressed(tag, stream);
} catch (IOException e) {
throw new RuntimeException(e); // Can't happen, the stream is a ByteArrayOutputStream
}
Clipboard clipboard;
try {
clipboard = new SpongeSchematicReader(new NBTInputStream(new ByteArrayInputStream(stream.toByteArray()))).read();
} catch (IOException e) {
throw new RuntimeException(e); // Can't happen, the stream is a ByteArrayInputStream
}
taskAcceptor.accept(() -> {
WorldEdit.getInstance().getSessionManager().get(FabricAdapter.adaptPlayer(player)).setClipboard(new ClipboardHolder(clipboard));
source.sendFeedback(new TranslatableText("commands.pocket.loadedSchem", template.getId()), true);
});
};
if (async) {
CompletableFuture.runAsync(task);
} else {
task.run();
}
return Command.SINGLE_SUCCESS;
}
}