Skip to content

Commit

Permalink
Also migrate the OldChunk
Browse files Browse the repository at this point in the history
  • Loading branch information
me4502 committed Feb 6, 2021
1 parent ed41a87 commit 5f7c2a9
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public BaseItemStack(ItemType itemType, int amount) {
* @param id The item type
* @param tag Tag value
* @param amount amount in the stack
* @deprecated Use {@link BaseItemStack(ItemType, CompoundBinaryTag, int)}
* @deprecated Use {@link #BaseItemStack(ItemType, CompoundBinaryTag, int)}
*/
@Deprecated
public BaseItemStack(ItemType id, CompoundTag tag, int amount) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class AnvilChunk implements Chunk {
*
* @param tag the tag to read
* @throws DataException on a data error
* @deprecated Use {@link AnvilChunk(CompoundBinaryTag)}
* @deprecated Use {@link #AnvilChunk(CompoundBinaryTag)}
*/
public AnvilChunk(CompoundTag tag) throws DataException {
this(tag.asBinaryTag());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class AnvilChunk13 implements Chunk {
*
* @param tag the tag to read
* @throws DataException on a data error
* @deprecated Use {@link AnvilChunk13(CompoundBinaryTag)}
* @deprecated Use {@link #AnvilChunk13(CompoundBinaryTag)}
*/
@Deprecated
public AnvilChunk13(CompoundTag tag) throws DataException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class AnvilChunk16 extends AnvilChunk13 {
*
* @param tag the tag to read
* @throws DataException on a data error
* @deprecated Use {@link AnvilChunk16(CompoundBinaryTag)}
* @deprecated Use {@link #AnvilChunk16(CompoundBinaryTag)}
*/
@Deprecated
public AnvilChunk16(CompoundTag tag) throws DataException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@

package com.sk89q.worldedit.world.chunk;

import com.sk89q.jnbt.ByteArrayTag;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.IntTag;
import com.sk89q.jnbt.ListTag;
import com.sk89q.jnbt.NBTUtils;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.util.NbtUtils;
import com.sk89q.worldedit.util.nbt.BinaryTag;
import com.sk89q.worldedit.util.nbt.ByteArrayBinaryTag;
import com.sk89q.worldedit.util.nbt.CompoundBinaryTag;
import com.sk89q.worldedit.util.nbt.IntBinaryTag;
import com.sk89q.worldedit.util.nbt.ListBinaryTag;
import com.sk89q.worldedit.world.DataException;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockState;
Expand All @@ -35,35 +36,47 @@
import com.sk89q.worldedit.world.storage.InvalidFormatException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Represents an Alpha chunk.
*/
public class OldChunk implements Chunk {

private final CompoundTag rootTag;
private final CompoundBinaryTag rootTag;
private final byte[] blocks;
private final byte[] data;
private final int rootX;
private final int rootZ;

private Map<BlockVector3, Map<String, Tag>> tileEntities;
private Map<BlockVector3, CompoundBinaryTag> tileEntities;


/**
* Construct the chunk with a compound tag.
*
* @param tag the tag
* @throws DataException if there is an error getting the chunk data
* @deprecated Use {@link #OldChunk(CompoundBinaryTag)}
*/
@Deprecated
public OldChunk(CompoundTag tag) throws DataException {
this(tag.asBinaryTag());
}

/**
* Construct the chunk with a compound tag.
*
* @param tag the tag
* @throws DataException if there is an error getting the chunk data
*/
public OldChunk(CompoundBinaryTag tag) throws DataException {
rootTag = tag;

blocks = NBTUtils.getChildTag(rootTag.getValue(), "Blocks", ByteArrayTag.class).getValue();
data = NBTUtils.getChildTag(rootTag.getValue(), "Data", ByteArrayTag.class).getValue();
rootX = NBTUtils.getChildTag(rootTag.getValue(), "xPos", IntTag.class).getValue();
rootZ = NBTUtils.getChildTag(rootTag.getValue(), "zPos", IntTag.class).getValue();
blocks = NbtUtils.getChildTag(rootTag, "Blocks", ByteArrayBinaryTag.class).value();
data = NbtUtils.getChildTag(rootTag, "Data", ByteArrayBinaryTag.class).value();
rootX = NbtUtils.getChildTag(rootTag, "xPos", IntBinaryTag.class).value();
rootZ = NbtUtils.getChildTag(rootTag, "zPos", IntBinaryTag.class).value();

int size = 16 * 16 * 128;
if (blocks.length != size) {
Expand All @@ -83,51 +96,50 @@ public OldChunk(CompoundTag tag) throws DataException {
* @throws DataException if there is an error getting the chunk data
*/
private void populateTileEntities() throws DataException {
List<Tag> tags = NBTUtils.getChildTag(
rootTag.getValue(), "TileEntities", ListTag.class)
.getValue();
ListBinaryTag tags = NbtUtils.getChildTag(rootTag, "TileEntities", ListBinaryTag.class);

tileEntities = new HashMap<>();

for (Tag tag : tags) {
if (!(tag instanceof CompoundTag)) {
for (BinaryTag tag : tags) {
if (!(tag instanceof CompoundBinaryTag)) {
throw new InvalidFormatException("CompoundTag expected in TileEntities");
}

CompoundTag t = (CompoundTag) tag;
CompoundBinaryTag t = (CompoundBinaryTag) tag;

int x = 0;
int y = 0;
int z = 0;

Map<String, Tag> values = new HashMap<>();
CompoundBinaryTag.Builder values = CompoundBinaryTag.builder();

for (Map.Entry<String, Tag> entry : t.getValue().entrySet()) {
switch (entry.getKey()) {
for (String key : t.keySet()) {
BinaryTag value = t.get(key);
switch (key) {
case "x":
if (entry.getValue() instanceof IntTag) {
x = ((IntTag) entry.getValue()).getValue();
if (value instanceof IntBinaryTag) {
x = ((IntBinaryTag) value).value();
}
break;
case "y":
if (entry.getValue() instanceof IntTag) {
y = ((IntTag) entry.getValue()).getValue();
if (value instanceof IntBinaryTag) {
y = ((IntBinaryTag) value).value();
}
break;
case "z":
if (entry.getValue() instanceof IntTag) {
z = ((IntTag) entry.getValue()).getValue();
if (value instanceof IntBinaryTag) {
z = ((IntBinaryTag) value).value();
}
break;
default:
break;
}

values.put(entry.getKey(), entry.getValue());
values.put(key, value);
}

BlockVector3 vec = BlockVector3.at(x, y, z);
tileEntities.put(vec, values);
tileEntities.put(vec, values.build());
}
}

Expand All @@ -140,16 +152,16 @@ private void populateTileEntities() throws DataException {
* @return a tag
* @throws DataException if there is an error getting the chunk data
*/
private CompoundTag getBlockTileEntity(BlockVector3 position) throws DataException {
private CompoundBinaryTag getBlockTileEntity(BlockVector3 position) throws DataException {
if (tileEntities == null) {
populateTileEntities();
}

Map<String, Tag> values = tileEntities.get(position);
CompoundBinaryTag values = tileEntities.get(position);
if (values == null) {
return null;
}
return new CompoundTag(values);
return values;
}

@Override
Expand Down Expand Up @@ -189,7 +201,7 @@ public BaseBlock getBlock(BlockVector3 position) throws DataException {
return BlockTypes.AIR.getDefaultState().toBaseBlock();
}

CompoundTag tileEntity = getBlockTileEntity(position);
CompoundBinaryTag tileEntity = getBlockTileEntity(position);

if (tileEntity != null) {
return state.toBaseBlock(tileEntity);
Expand Down

0 comments on commit 5f7c2a9

Please sign in to comment.