Update for final changes

This commit is contained in:
Octavia Togami 2021-05-29 10:35:31 -07:00
parent e1892b7bd4
commit 2f6b50a427
No known key found for this signature in database
GPG Key ID: CC364524D1983C99
5 changed files with 60 additions and 38 deletions

View File

@ -19,9 +19,9 @@
package com.sk89q.worldedit.extent.clipboard.io.sponge;
import com.google.common.collect.Maps;
import com.sk89q.jnbt.AdventureNBTConverter;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.CompoundTagBuilder;
import com.sk89q.jnbt.IntArrayTag;
import com.sk89q.jnbt.IntTag;
import com.sk89q.jnbt.ListTag;
@ -49,12 +49,14 @@
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import static com.google.common.base.Preconditions.checkState;
import static com.sk89q.worldedit.extent.clipboard.io.SchematicNbtUtil.getTag;
import static com.sk89q.worldedit.extent.clipboard.io.SchematicNbtUtil.requireTag;
/**
@ -135,7 +137,7 @@ static Map<Integer, BlockState> decodePalette(
static void initializeClipboardFromBlocks(
Clipboard clipboard, Map<Integer, BlockState> palette, byte[] blocks, ListTag tileEntities,
VersionedDataFixer fixer
VersionedDataFixer fixer, boolean dataIsNested
) throws IOException {
Map<BlockVector3, Map<String, Tag>> tileEntitiesMap = new HashMap<>();
if (tileEntities != null) {
@ -147,13 +149,23 @@ static void initializeClipboardFromBlocks(
for (Map<String, Tag> tileEntity : tileEntityTags) {
int[] pos = requireTag(tileEntity, "Pos", IntArrayTag.class).getValue();
final BlockVector3 pt = clipboard.getMinimumPoint().add(pos[0], pos[1], pos[2]);
Map<String, Tag> values = Maps.newHashMap(tileEntity);
Map<String, Tag> values;
if (dataIsNested) {
CompoundTag dataTag = getTag(tileEntity, "Data", CompoundTag.class);
if (dataTag != null) {
values = new LinkedHashMap<>(dataTag.getValue());
} else {
values = new LinkedHashMap<>();
}
} else {
values = new LinkedHashMap<>(tileEntity);
values.remove("Id");
values.remove("Pos");
}
values.put("x", new IntTag(pt.getBlockX()));
values.put("y", new IntTag(pt.getBlockY()));
values.put("z", new IntTag(pt.getBlockZ()));
values.put("id", values.get("Id"));
values.remove("Id");
values.remove("Pos");
values.put("id", tileEntity.get("Id"));
if (fixer.isActive()) {
tileEntity = ((CompoundTag) AdventureNBTConverter.fromAdventure(fixer.fixUp(
DataFixer.FixTypes.BLOCK_ENTITY,
@ -222,18 +234,32 @@ static void readEntities(BlockArrayClipboard clipboard, List<Tag> entList,
CompoundTag entityTag = (CompoundTag) et;
Map<String, Tag> tags = entityTag.getValue();
String id = requireTag(tags, "Id", StringTag.class).getValue();
entityTag = entityTag.createBuilder().putString("id", id).remove("Id").build();
entityTag = ((CompoundTag) AdventureNBTConverter.fromAdventure(fixer.fixUp(
CompoundTagBuilder dataTagBuilder = CompoundTagBuilder.create();
if (positionIsRelative) {
// then we're in version 3
CompoundTag subTag = getTag(entityTag.getValue(), "Data", CompoundTag.class);
if (subTag != null) {
dataTagBuilder.putAll(subTag.getValue());
}
} else {
// version 2
dataTagBuilder.putAll(tags);
dataTagBuilder.remove("Id");
dataTagBuilder.remove("Pos");
}
CompoundTag dataTag = dataTagBuilder.putString("id", id).build();
dataTag = ((CompoundTag) AdventureNBTConverter.fromAdventure(fixer.fixUp(
DataFixer.FixTypes.ENTITY,
entityTag.asBinaryTag()
dataTag.asBinaryTag()
)));
EntityType entityType = EntityTypes.get(id);
if (entityType != null) {
Location location = NBTConversions.toLocation(clipboard,
requireTag(tags, "Pos", ListTag.class),
requireTag(tags, "Rotation", ListTag.class));
BaseEntity state = new BaseEntity(entityType, entityTag);
requireTag(dataTag.getValue(), "Rotation", ListTag.class)
);
BaseEntity state = new BaseEntity(entityType, dataTag);
if (positionIsRelative) {
location = location.setPosition(
location.toVector().add(clipboard.getMinimumPoint().toVector3())

View File

@ -35,12 +35,10 @@
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.extent.clipboard.io.NBTSchematicReader;
import com.sk89q.worldedit.internal.Constants;
import com.sk89q.worldedit.internal.util.LogManagerCompat;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.world.block.BlockState;
import org.apache.logging.log4j.Logger;
import java.io.IOException;
import java.util.Map;
@ -143,7 +141,7 @@ static BlockArrayClipboard readVersion1(CompoundTag schematicTag, VersionedDataF
BlockArrayClipboard clipboard = new BlockArrayClipboard(region);
clipboard.setOrigin(origin);
ReaderUtil.initializeClipboardFromBlocks(
clipboard, palette, blocks, tileEntities, fixer
clipboard, palette, blocks, tileEntities, fixer, false
);
return clipboard;
}

View File

@ -167,7 +167,7 @@ private void decodeBlocksIntoClipboard(VersionedDataFixer fixer, Map<String, Tag
ListTag tileEntities = getTag(blockContainer, "BlockEntities", ListTag.class);
ReaderUtil.initializeClipboardFromBlocks(
clipboard, palette, blocks, tileEntities, fixer
clipboard, palette, blocks, tileEntities, fixer, true
);
}

View File

@ -23,6 +23,7 @@
import com.google.common.collect.Maps;
import com.sk89q.jnbt.ByteArrayTag;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.CompoundTagBuilder;
import com.sk89q.jnbt.IntArrayTag;
import com.sk89q.jnbt.IntTag;
import com.sk89q.jnbt.ListTag;
@ -186,24 +187,18 @@ private CompoundTag encodeBlocks(Clipboard clipboard) {
BaseBlock block = clipboard.getFullBlock(point);
// Also compute block entity side-effect here
if (block.getNbtData() != null) {
Map<String, Tag> values = new HashMap<>(block.getNbtData().getValue());
CompoundTagBuilder builder = CompoundTagBuilder.create();
values.remove("id"); // Remove 'id' if it exists. We want 'Id'
// Positions are kept in NBT, we don't want that.
values.remove("x");
values.remove("y");
values.remove("z");
values.put("Id", new StringTag(block.getNbtId()));
builder.putString("Id", block.getNbtId());
BlockVector3 adjustedPos = point.subtract(clipboard.getMinimumPoint());
values.put("Pos", new IntArrayTag(new int[] {
builder.putIntArray("Pos", new int[] {
adjustedPos.getBlockX(),
adjustedPos.getBlockY(),
adjustedPos.getBlockZ()
}));
});
builder.put("Data", block.getNbtData());
blockEntities.add(new CompoundTag(values));
blockEntities.add(builder.build());
}
return block.toImmutableState().getAsString();
});

View File

@ -20,12 +20,11 @@
package com.sk89q.worldedit.extent.clipboard.io.sponge;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.CompoundTagBuilder;
import com.sk89q.jnbt.DoubleTag;
import com.sk89q.jnbt.FloatTag;
import com.sk89q.jnbt.ListTag;
import com.sk89q.jnbt.StringTag;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
@ -33,7 +32,6 @@
import com.sk89q.worldedit.util.Location;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
@ -44,22 +42,27 @@ static ListTag encodeEntities(Clipboard clipboard, boolean positionIsRelative) {
if (state == null) {
return null;
}
Map<String, Tag> values = Maps.newHashMap();
CompoundTagBuilder fullTagBuilder = CompoundTagBuilder.create();
CompoundTagBuilder dataTagBuilder = CompoundTagBuilder.create();
CompoundTag rawData = state.getNbtData();
if (rawData != null) {
values.putAll(rawData.getValue());
dataTagBuilder.putAll(rawData.getValue());
dataTagBuilder.remove("id");
}
values.remove("id");
values.put("Id", new StringTag(state.getType().getId()));
final Location location = e.getLocation();
Vector3 pos = location.toVector();
dataTagBuilder.put("Rotation", encodeRotation(location));
if (positionIsRelative) {
pos = pos.subtract(clipboard.getMinimumPoint().toVector3());
}
values.put("Pos", encodeVector(pos));
values.put("Rotation", encodeRotation(location));
return new CompoundTag(values);
fullTagBuilder.put("Data", dataTagBuilder.build());
} else {
fullTagBuilder.putAll(dataTagBuilder.build().getValue());
}
fullTagBuilder.putString("Id", state.getType().getId());
fullTagBuilder.put("Pos", encodeVector(pos));
return fullTagBuilder.build();
}).filter(Objects::nonNull).collect(Collectors.toList());
if (entities.isEmpty()) {
return null;