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
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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 Lesser General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
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.ByteArrayTag;
|
|
|
|
import com.sk89q.jnbt.ByteTag;
|
|
|
|
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;
|
2018-09-20 06:56:46 +00:00
|
|
|
import com.sk89q.worldedit.WorldEdit;
|
2018-08-12 14:03:07 +00:00
|
|
|
import com.sk89q.worldedit.blocks.BaseBlock;
|
2018-12-23 16:19:33 +00:00
|
|
|
import com.sk89q.worldedit.math.BlockVector3;
|
2014-04-03 02:08:50 +00:00
|
|
|
import com.sk89q.worldedit.world.DataException;
|
2014-04-05 09:59:38 +00:00
|
|
|
import com.sk89q.worldedit.world.World;
|
2018-07-24 07:12:50 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BlockState;
|
2018-08-12 14:03:07 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BlockStateHolder;
|
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
|
|
|
|
2018-09-25 05:35:21 +00:00
|
|
|
import javax.annotation.Nullable;
|
2014-04-05 09:59:38 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2012-03-05 06:05:17 +00:00
|
|
|
public class AnvilChunk implements Chunk {
|
|
|
|
|
|
|
|
private CompoundTag rootTag;
|
|
|
|
private byte[][] blocks;
|
2012-10-20 07:54:21 +00:00
|
|
|
private byte[][] blocksAdd;
|
2012-03-05 06:05:17 +00:00
|
|
|
private byte[][] data;
|
|
|
|
private int rootX;
|
|
|
|
private int rootZ;
|
|
|
|
|
2018-12-23 16:19:33 +00:00
|
|
|
private Map<BlockVector3, Map<String,Tag>> tileEntities;
|
2012-03-05 06:05:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct the chunk with a compound tag.
|
2012-10-20 07:54:21 +00:00
|
|
|
*
|
|
|
|
* @param world the world to construct the chunk for
|
|
|
|
* @param tag the tag to read
|
|
|
|
* @throws DataException on a data error
|
2012-03-05 06:05:17 +00:00
|
|
|
*/
|
2014-04-05 09:59:38 +00:00
|
|
|
public AnvilChunk(World world, CompoundTag tag) throws DataException {
|
2012-03-05 06:05:17 +00:00
|
|
|
rootTag = tag;
|
|
|
|
|
2012-10-20 07:54:21 +00:00
|
|
|
rootX = NBTUtils.getChildTag(rootTag.getValue(), "xPos", IntTag.class).getValue();
|
|
|
|
rootZ = NBTUtils.getChildTag(rootTag.getValue(), "zPos", IntTag.class).getValue();
|
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];
|
|
|
|
|
2012-03-05 06:05:17 +00:00
|
|
|
List<Tag> sections = NBTUtils.getChildTag(rootTag.getValue(), "Sections", ListTag.class).getValue();
|
|
|
|
|
2012-10-20 07:54:21 +00:00
|
|
|
for (Tag rawSectionTag : sections) {
|
|
|
|
if (!(rawSectionTag instanceof CompoundTag)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
CompoundTag sectionTag = (CompoundTag) rawSectionTag;
|
|
|
|
if (!sectionTag.getValue().containsKey("Y")) {
|
|
|
|
continue; // Empty section.
|
|
|
|
}
|
|
|
|
|
|
|
|
int y = NBTUtils.getChildTag(sectionTag.getValue(), "Y", ByteTag.class).getValue();
|
|
|
|
if (y < 0 || y >= 16) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
blocks[y] = NBTUtils.getChildTag(sectionTag.getValue(),
|
|
|
|
"Blocks", ByteArrayTag.class).getValue();
|
|
|
|
data[y] = NBTUtils.getChildTag(sectionTag.getValue(), "Data",
|
|
|
|
ByteArrayTag.class).getValue();
|
|
|
|
|
|
|
|
// 4096 ID block support
|
|
|
|
if (sectionTag.getValue().containsKey("Add")) {
|
|
|
|
blocksAdd[y] = NBTUtils.getChildTag(sectionTag.getValue(),
|
|
|
|
"Add", ByteArrayTag.class).getValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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(
|
|
|
|
"Chunk blocks byte array expected " + "to be "
|
|
|
|
+ sectionsize + " bytes; found "
|
2014-07-29 18:04:04 +00:00
|
|
|
+ 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)) {
|
2012-03-05 06:05:17 +00:00
|
|
|
throw new InvalidFormatException("Chunk block data byte array "
|
2012-10-20 07:54:21 +00:00
|
|
|
+ "expected to be " + sectionsize + " bytes; found "
|
2014-07-29 18:04:04 +00:00
|
|
|
+ aData.length);
|
2012-03-05 06:05:17 +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
|
|
|
}
|
2012-03-05 06:05:17 +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));
|
2012-10-20 07:54:21 +00:00
|
|
|
|
2012-03-05 06:05:17 +00:00
|
|
|
try {
|
2012-10-20 07:54:21 +00:00
|
|
|
int addId = 0;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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;
|
2012-03-05 06:05:17 +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.
|
|
|
|
*
|
|
|
|
* @throws DataException
|
|
|
|
*/
|
|
|
|
private void populateTileEntities() throws DataException {
|
2012-10-20 07:54:21 +00:00
|
|
|
List<Tag> tags = NBTUtils.getChildTag(rootTag.getValue(),
|
|
|
|
"TileEntities", ListTag.class).getValue();
|
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
|
|
|
|
|
|
|
for (Tag tag : tags) {
|
|
|
|
if (!(tag instanceof CompoundTag)) {
|
2018-06-18 07:53:33 +00:00
|
|
|
throw new InvalidFormatException("CompoundTag expected in TileEntities");
|
2012-03-05 06:05:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CompoundTag t = (CompoundTag) tag;
|
|
|
|
|
|
|
|
int x = 0;
|
|
|
|
int y = 0;
|
|
|
|
int z = 0;
|
|
|
|
|
2018-06-16 06:36:55 +00:00
|
|
|
Map<String, Tag> values = new HashMap<>();
|
2012-03-05 06:05:17 +00:00
|
|
|
|
|
|
|
for (Map.Entry<String, Tag> entry : t.getValue().entrySet()) {
|
2018-06-16 06:36:55 +00:00
|
|
|
switch (entry.getKey()) {
|
|
|
|
case "x":
|
|
|
|
if (entry.getValue() instanceof IntTag) {
|
|
|
|
x = ((IntTag) entry.getValue()).getValue();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "y":
|
|
|
|
if (entry.getValue() instanceof IntTag) {
|
|
|
|
y = ((IntTag) entry.getValue()).getValue();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "z":
|
|
|
|
if (entry.getValue() instanceof IntTag) {
|
|
|
|
z = ((IntTag) entry.getValue()).getValue();
|
|
|
|
}
|
|
|
|
break;
|
2012-03-05 06:05:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
values.put(entry.getKey(), entry.getValue());
|
|
|
|
}
|
|
|
|
|
2018-12-23 16:19:33 +00:00
|
|
|
BlockVector3 vec = new BlockVector3(x, y, z);
|
2012-03-05 06:05:17 +00:00
|
|
|
tileEntities.put(vec, values);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2018-12-23 16:19:33 +00:00
|
|
|
private CompoundTag getBlockTileEntity(BlockVector3 position) throws DataException {
|
2012-03-05 06:05:17 +00:00
|
|
|
if (tileEntities == null) {
|
|
|
|
populateTileEntities();
|
|
|
|
}
|
|
|
|
|
2018-12-23 16:19:33 +00:00
|
|
|
Map<String, Tag> 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
|
|
|
|
2014-11-14 19:38:54 +00:00
|
|
|
return new CompoundTag(values);
|
2012-03-05 06:05:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-12-23 16:19:33 +00:00
|
|
|
public BlockStateHolder 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) {
|
|
|
|
WorldEdit.logger.warning("Unknown legacy block " + id + ":" + data + " found when loading legacy anvil chunk.");
|
|
|
|
return BlockTypes.AIR.getDefaultState();
|
|
|
|
}
|
2018-09-25 05:35:21 +00:00
|
|
|
if (state.getMaterial().hasContainer()) {
|
|
|
|
CompoundTag tileEntity = getBlockTileEntity(position);
|
|
|
|
if (tileEntity != null) {
|
|
|
|
return new BaseBlock(state, tileEntity);
|
|
|
|
}
|
2018-09-20 06:56:46 +00:00
|
|
|
}
|
|
|
|
return state;
|
2012-03-05 06:05:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|