2014-04-04 22:03:18 +00:00
|
|
|
/*
|
|
|
|
* WorldEdit, a Minecraft world manipulation toolkit
|
|
|
|
* Copyright (C) sk89q <http://www.sk89q.com>
|
|
|
|
* Copyright (C) WorldEdit team and contributors
|
|
|
|
*
|
2020-08-25 01:31:47 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
2014-04-04 22:03:18 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2020-08-25 01:31:47 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2014-04-04 22:03:18 +00:00
|
|
|
*
|
2020-08-25 01:31:47 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2014-04-04 22:03:18 +00:00
|
|
|
*/
|
|
|
|
|
2014-04-03 02:08:50 +00:00
|
|
|
package com.sk89q.worldedit.world.chunk;
|
2012-03-05 06:05:17 +00:00
|
|
|
|
2014-07-29 18:04:04 +00:00
|
|
|
import com.sk89q.jnbt.CompoundTag;
|
2018-09-20 06:56:46 +00:00
|
|
|
import com.sk89q.worldedit.WorldEdit;
|
2018-12-23 16:19:33 +00:00
|
|
|
import com.sk89q.worldedit.math.BlockVector3;
|
2021-07-01 20:16:25 +00:00
|
|
|
import com.sk89q.worldedit.util.nbt.BinaryTag;
|
|
|
|
import com.sk89q.worldedit.util.nbt.BinaryTagTypes;
|
|
|
|
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.util.nbt.NbtUtils;
|
2014-04-03 02:08:50 +00:00
|
|
|
import com.sk89q.worldedit.world.DataException;
|
2018-12-27 00:39:10 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BaseBlock;
|
2018-07-24 07:12:50 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BlockState;
|
2018-09-20 06:56:46 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BlockTypes;
|
2018-07-24 07:12:50 +00:00
|
|
|
import com.sk89q.worldedit.world.registry.LegacyMapper;
|
2014-04-03 02:08:50 +00:00
|
|
|
import com.sk89q.worldedit.world.storage.InvalidFormatException;
|
2012-03-05 06:05:17 +00:00
|
|
|
|
2014-04-05 09:59:38 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2020-07-14 02:50:59 +00:00
|
|
|
import javax.annotation.Nullable;
|
2014-04-05 09:59:38 +00:00
|
|
|
|
2012-03-05 06:05:17 +00:00
|
|
|
public class AnvilChunk implements Chunk {
|
|
|
|
|
2021-07-01 20:16:25 +00:00
|
|
|
private final CompoundBinaryTag rootTag;
|
2020-07-14 02:50:59 +00:00
|
|
|
private final byte[][] blocks;
|
|
|
|
private final byte[][] blocksAdd;
|
|
|
|
private final byte[][] data;
|
|
|
|
private final int rootX;
|
|
|
|
private final int rootZ;
|
2012-03-05 06:05:17 +00:00
|
|
|
|
2021-07-01 20:16:25 +00:00
|
|
|
private Map<BlockVector3, CompoundBinaryTag> tileEntities;
|
|
|
|
|
2012-03-05 06:05:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct the chunk with a compound tag.
|
2019-06-25 17:07:47 +00:00
|
|
|
*
|
2012-10-20 07:54:21 +00:00
|
|
|
* @param tag the tag to read
|
|
|
|
* @throws DataException on a data error
|
2021-07-01 20:16:25 +00:00
|
|
|
* @deprecated Use {@link #AnvilChunk(CompoundBinaryTag)}
|
2012-03-05 06:05:17 +00:00
|
|
|
*/
|
2020-01-02 21:30:44 +00:00
|
|
|
public AnvilChunk(CompoundTag tag) throws DataException {
|
2021-07-01 20:16:25 +00:00
|
|
|
this(tag.asBinaryTag());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct the chunk with a compound tag.
|
|
|
|
*
|
|
|
|
* @param tag the tag to read
|
|
|
|
* @throws DataException on a data error
|
|
|
|
*/
|
|
|
|
public AnvilChunk(CompoundBinaryTag tag) throws DataException {
|
2012-03-05 06:05:17 +00:00
|
|
|
rootTag = tag;
|
|
|
|
|
2021-07-01 20:16:25 +00:00
|
|
|
rootX = NbtUtils.getChildTag(rootTag, "xPos", BinaryTagTypes.INT).value();
|
|
|
|
rootZ = NbtUtils.getChildTag(rootTag, "zPos", BinaryTagTypes.INT).value();
|
2012-03-05 06:05:17 +00:00
|
|
|
|
2012-10-20 07:54:21 +00:00
|
|
|
blocks = new byte[16][16 * 16 * 16];
|
|
|
|
blocksAdd = new byte[16][16 * 16 * 8];
|
|
|
|
data = new byte[16][16 * 16 * 8];
|
2019-06-25 17:07:47 +00:00
|
|
|
|
2021-07-01 20:16:25 +00:00
|
|
|
ListBinaryTag sections = NbtUtils.getChildTag(rootTag, "Sections", BinaryTagTypes.LIST);
|
2019-06-25 17:07:47 +00:00
|
|
|
|
2021-07-01 20:16:25 +00:00
|
|
|
for (BinaryTag rawSectionTag : sections) {
|
|
|
|
if (!(rawSectionTag instanceof CompoundBinaryTag)) {
|
2012-10-20 07:54:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
2019-06-25 17:07:47 +00:00
|
|
|
|
2021-07-01 20:16:25 +00:00
|
|
|
CompoundBinaryTag sectionTag = (CompoundBinaryTag) rawSectionTag;
|
|
|
|
if (sectionTag.get("Y") == null) {
|
2012-10-20 07:54:21 +00:00
|
|
|
continue; // Empty section.
|
|
|
|
}
|
2019-06-25 17:07:47 +00:00
|
|
|
|
2021-07-01 20:16:25 +00:00
|
|
|
int y = NbtUtils.getChildTag(sectionTag, "Y", BinaryTagTypes.BYTE).value();
|
2012-10-20 07:54:21 +00:00
|
|
|
if (y < 0 || y >= 16) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-07-01 20:16:25 +00:00
|
|
|
blocks[y] = NbtUtils.getChildTag(sectionTag,
|
|
|
|
"Blocks", BinaryTagTypes.BYTE_ARRAY).value();
|
|
|
|
data[y] = NbtUtils.getChildTag(sectionTag, "Data",
|
|
|
|
BinaryTagTypes.BYTE_ARRAY).value();
|
2012-10-20 07:54:21 +00:00
|
|
|
|
|
|
|
// 4096 ID block support
|
2021-07-01 20:16:25 +00:00
|
|
|
if (sectionTag.get("Add") != null) {
|
|
|
|
blocksAdd[y] = NbtUtils.getChildTag(sectionTag,
|
|
|
|
"Add", BinaryTagTypes.BYTE_ARRAY).value();
|
2012-10-20 07:54:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int sectionsize = 16 * 16 * 16;
|
2014-07-29 18:04:04 +00:00
|
|
|
for (byte[] block : blocks) {
|
|
|
|
if (block.length != sectionsize) {
|
2012-10-20 07:54:21 +00:00
|
|
|
throw new InvalidFormatException(
|
2020-10-05 17:41:41 +00:00
|
|
|
"Chunk blocks byte array expected " + "to be "
|
|
|
|
+ sectionsize + " bytes; found "
|
|
|
|
+ block.length);
|
2012-03-05 06:05:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-29 18:04:04 +00:00
|
|
|
for (byte[] aData : data) {
|
|
|
|
if (aData.length != (sectionsize / 2)) {
|
2020-10-05 17:41:41 +00:00
|
|
|
throw new InvalidFormatException("Chunk block data byte array "
|
|
|
|
+ "expected to be " + sectionsize + " bytes; found "
|
|
|
|
+ aData.length);
|
2012-03-05 06:05:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-25 17:07:47 +00:00
|
|
|
|
2018-12-23 16:19:33 +00:00
|
|
|
private int getBlockID(BlockVector3 position) throws DataException {
|
|
|
|
int x = position.getX() - rootX * 16;
|
|
|
|
int y = position.getY();
|
|
|
|
int z = position.getZ() - rootZ * 16;
|
2012-10-20 07:54:21 +00:00
|
|
|
|
2012-03-05 06:05:17 +00:00
|
|
|
int section = y >> 4;
|
2012-10-20 07:54:21 +00:00
|
|
|
if (section < 0 || section >= blocks.length) {
|
2014-07-29 18:04:04 +00:00
|
|
|
throw new DataException("Chunk does not contain position " + position);
|
2012-10-20 07:54:21 +00:00
|
|
|
}
|
2019-06-25 17:07:47 +00:00
|
|
|
|
2012-10-20 07:54:21 +00:00
|
|
|
int yindex = y & 0x0F;
|
|
|
|
|
2012-03-05 06:05:17 +00:00
|
|
|
int index = x + (z * 16 + (yindex * 16 * 16));
|
2019-06-25 17:07:47 +00:00
|
|
|
|
2012-03-05 06:05:17 +00:00
|
|
|
try {
|
2012-10-20 07:54:21 +00:00
|
|
|
int addId = 0;
|
2019-06-25 17:07:47 +00:00
|
|
|
|
2013-02-24 06:39:59 +00:00
|
|
|
// The block ID is the combination of the Blocks byte array with the
|
|
|
|
// Add byte array. 'Blocks' stores the lowest 8 bits of a block's ID, and
|
|
|
|
// 'Add' stores the highest 4 bits of the ID. The first block is stored
|
|
|
|
// in the lowest nibble in the Add byte array.
|
2012-10-20 07:54:21 +00:00
|
|
|
if (index % 2 == 0) {
|
2013-02-24 06:39:59 +00:00
|
|
|
addId = (blocksAdd[section][index >> 1] & 0x0F) << 8;
|
2012-10-20 07:54:21 +00:00
|
|
|
} else {
|
2013-02-24 06:39:59 +00:00
|
|
|
addId = (blocksAdd[section][index >> 1] & 0xF0) << 4;
|
2012-10-20 07:54:21 +00:00
|
|
|
}
|
2019-06-25 17:07:47 +00:00
|
|
|
|
2013-02-24 19:57:34 +00:00
|
|
|
return (blocks[section][index] & 0xFF) + addId;
|
2012-03-05 06:05:17 +00:00
|
|
|
} catch (IndexOutOfBoundsException e) {
|
2014-07-29 18:04:04 +00:00
|
|
|
throw new DataException("Chunk does not contain position " + position);
|
2012-03-05 06:05:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-23 16:19:33 +00:00
|
|
|
private int getBlockData(BlockVector3 position) throws DataException {
|
|
|
|
int x = position.getX() - rootX * 16;
|
|
|
|
int y = position.getY();
|
|
|
|
int z = position.getZ() - rootZ * 16;
|
2012-10-20 07:54:21 +00:00
|
|
|
|
2012-03-05 06:05:17 +00:00
|
|
|
int section = y >> 4;
|
2012-10-20 07:54:21 +00:00
|
|
|
int yIndex = y & 0x0F;
|
2019-06-25 17:07:47 +00:00
|
|
|
|
2012-10-20 07:54:21 +00:00
|
|
|
if (section < 0 || section >= blocks.length) {
|
2014-07-29 18:04:04 +00:00
|
|
|
throw new DataException("Chunk does not contain position " + position);
|
2012-10-20 07:54:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int index = x + (z * 16 + (yIndex * 16 * 16));
|
2012-03-05 06:05:17 +00:00
|
|
|
boolean shift = index % 2 == 0;
|
|
|
|
index /= 2;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (!shift) {
|
|
|
|
return (data[section][index] & 0xF0) >> 4;
|
|
|
|
} else {
|
|
|
|
return data[section][index] & 0xF;
|
|
|
|
}
|
|
|
|
} catch (IndexOutOfBoundsException e) {
|
2014-07-29 18:04:04 +00:00
|
|
|
throw new DataException("Chunk does not contain position " + position);
|
2012-03-05 06:05:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to load the tile entities.
|
|
|
|
*/
|
|
|
|
private void populateTileEntities() throws DataException {
|
2021-07-01 20:16:25 +00:00
|
|
|
ListBinaryTag tags = NbtUtils.getChildTag(rootTag, "TileEntities", BinaryTagTypes.LIST);
|
2012-03-05 06:05:17 +00:00
|
|
|
|
2018-06-16 06:36:55 +00:00
|
|
|
tileEntities = new HashMap<>();
|
2012-03-05 06:05:17 +00:00
|
|
|
|
2021-07-01 20:16:25 +00:00
|
|
|
for (BinaryTag tag : tags) {
|
|
|
|
if (!(tag instanceof CompoundBinaryTag)) {
|
2018-06-18 07:53:33 +00:00
|
|
|
throw new InvalidFormatException("CompoundTag expected in TileEntities");
|
2012-03-05 06:05:17 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 20:16:25 +00:00
|
|
|
CompoundBinaryTag t = (CompoundBinaryTag) tag;
|
2012-03-05 06:05:17 +00:00
|
|
|
|
|
|
|
int x = 0;
|
|
|
|
int y = 0;
|
|
|
|
int z = 0;
|
|
|
|
|
2021-07-01 20:16:25 +00:00
|
|
|
CompoundBinaryTag.Builder values = CompoundBinaryTag.builder();
|
2012-03-05 06:05:17 +00:00
|
|
|
|
2021-07-01 20:16:25 +00:00
|
|
|
for (String key : t.keySet()) {
|
|
|
|
BinaryTag value = t.get(key);
|
|
|
|
switch (key) {
|
2018-06-16 06:36:55 +00:00
|
|
|
case "x":
|
2021-07-01 20:16:25 +00:00
|
|
|
if (value instanceof IntBinaryTag) {
|
|
|
|
x = ((IntBinaryTag) value).value();
|
2018-06-16 06:36:55 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "y":
|
2021-07-01 20:16:25 +00:00
|
|
|
if (value instanceof IntBinaryTag) {
|
|
|
|
y = ((IntBinaryTag) value).value();
|
2018-06-16 06:36:55 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "z":
|
2021-07-01 20:16:25 +00:00
|
|
|
if (value instanceof IntBinaryTag) {
|
|
|
|
z = ((IntBinaryTag) value).value();
|
2018-06-16 06:36:55 +00:00
|
|
|
}
|
|
|
|
break;
|
2020-07-14 02:50:59 +00:00
|
|
|
default:
|
|
|
|
break;
|
2012-03-05 06:05:17 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 20:16:25 +00:00
|
|
|
values.put(key, value);
|
2012-03-05 06:05:17 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 20:13:32 +00:00
|
|
|
BlockVector3 vec = BlockVector3.at(x, y, z);
|
2021-07-01 20:16:25 +00:00
|
|
|
tileEntities.put(vec, values.build());
|
2012-03-05 06:05:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the map of tags keyed to strings for a block's tile entity data. May
|
|
|
|
* return null if there is no tile entity data. Not public yet because
|
|
|
|
* what this function returns isn't ideal for usage.
|
|
|
|
*
|
2014-07-29 18:04:04 +00:00
|
|
|
* @param position the position
|
|
|
|
* @return the compound tag for that position, which may be null
|
|
|
|
* @throws DataException thrown if there is a data error
|
2012-03-05 06:05:17 +00:00
|
|
|
*/
|
2014-07-29 18:04:04 +00:00
|
|
|
@Nullable
|
2021-07-01 20:16:25 +00:00
|
|
|
private CompoundBinaryTag getBlockTileEntity(BlockVector3 position) throws DataException {
|
2012-03-05 06:05:17 +00:00
|
|
|
if (tileEntities == null) {
|
|
|
|
populateTileEntities();
|
|
|
|
}
|
|
|
|
|
2021-07-01 20:16:25 +00:00
|
|
|
CompoundBinaryTag values = tileEntities.get(position);
|
2012-08-24 23:50:32 +00:00
|
|
|
if (values == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-07-29 18:04:04 +00:00
|
|
|
|
2021-07-01 20:16:25 +00:00
|
|
|
return values;
|
2012-03-05 06:05:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-12-27 00:39:10 +00:00
|
|
|
public BaseBlock getBlock(BlockVector3 position) throws DataException {
|
2014-07-29 18:04:04 +00:00
|
|
|
int id = getBlockID(position);
|
|
|
|
int data = getBlockData(position);
|
2012-03-05 06:05:17 +00:00
|
|
|
|
2018-07-24 07:12:50 +00:00
|
|
|
BlockState state = LegacyMapper.getInstance().getBlockFromLegacy(id, data);
|
2018-09-20 06:56:46 +00:00
|
|
|
if (state == null) {
|
2020-10-05 17:41:41 +00:00
|
|
|
WorldEdit.logger.warn("Unknown legacy block " + id + ":" + data + " found when loading legacy anvil chunk.");
|
2018-12-27 00:39:10 +00:00
|
|
|
return BlockTypes.AIR.getDefaultState().toBaseBlock();
|
2018-09-20 06:56:46 +00:00
|
|
|
}
|
2021-07-01 20:16:25 +00:00
|
|
|
CompoundBinaryTag tileEntity = getBlockTileEntity(position);
|
2019-06-25 17:07:47 +00:00
|
|
|
|
2020-01-27 23:54:31 +00:00
|
|
|
if (tileEntity != null) {
|
|
|
|
return state.toBaseBlock(tileEntity);
|
|
|
|
}
|
2019-06-25 17:07:47 +00:00
|
|
|
|
2018-12-27 00:39:10 +00:00
|
|
|
return state.toBaseBlock();
|
|
|
|
}
|
2012-03-05 06:05:17 +00:00
|
|
|
|
|
|
|
}
|