Skip to content

Commit

Permalink
Update for final changes
Browse files Browse the repository at this point in the history
  • Loading branch information
octylFractal committed May 29, 2021
1 parent ad9afcd commit 163ecc9
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 37 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
Expand Down Expand Up @@ -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())
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Expand Up @@ -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
);
}

Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
});
Expand Down
Expand Up @@ -20,20 +20,18 @@
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;
import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldedit.util.Location;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

Expand All @@ -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());

fullTagBuilder.put("Data", dataTagBuilder.build());
} else {
fullTagBuilder.putAll(dataTagBuilder.build().getValue());
}
values.put("Pos", encodeVector(pos));
values.put("Rotation", encodeRotation(location));
fullTagBuilder.putString("Id", state.getType().getId());
fullTagBuilder.put("Pos", encodeVector(pos));

return new CompoundTag(values);
return fullTagBuilder.build();
}).filter(Objects::nonNull).collect(Collectors.toList());
if (entities.isEmpty()) {
return null;
Expand Down

0 comments on commit 163ecc9

Please sign in to comment.