2020-01-09 03:05:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
|
|
|
*/
|
|
|
|
|
2019-08-30 21:47:33 +00:00
|
|
|
package org.geysermc.connector.utils;
|
|
|
|
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.chunk.Chunk;
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.chunk.Column;
|
2019-12-21 05:05:20 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position;
|
2019-11-28 00:14:38 +00:00
|
|
|
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
|
2020-06-10 23:02:29 +00:00
|
|
|
import com.github.steveice10.opennbt.tag.builtin.StringTag;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.Tag;
|
2020-03-06 01:26:36 +00:00
|
|
|
import com.nukkitx.math.vector.Vector2i;
|
2019-12-21 05:05:20 +00:00
|
|
|
import com.nukkitx.math.vector.Vector3i;
|
2020-07-05 20:58:43 +00:00
|
|
|
import com.nukkitx.nbt.NBTOutputStream;
|
|
|
|
import com.nukkitx.nbt.NbtMap;
|
2020-05-25 01:07:05 +00:00
|
|
|
import com.nukkitx.nbt.NbtUtils;
|
2020-05-01 05:51:23 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.*;
|
2020-04-06 15:16:16 +00:00
|
|
|
import it.unimi.dsi.fastutil.objects.Object2IntMap;
|
|
|
|
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
|
2020-05-05 02:32:02 +00:00
|
|
|
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
2020-04-21 05:32:32 +00:00
|
|
|
import lombok.Getter;
|
2020-03-06 02:00:14 +00:00
|
|
|
import org.geysermc.connector.GeyserConnector;
|
2020-05-02 20:44:05 +00:00
|
|
|
import org.geysermc.connector.entity.Entity;
|
|
|
|
import org.geysermc.connector.entity.ItemFrameEntity;
|
2019-12-21 05:05:20 +00:00
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
2020-05-05 02:32:02 +00:00
|
|
|
import org.geysermc.connector.network.translators.world.block.BlockStateValues;
|
2020-04-29 20:01:53 +00:00
|
|
|
import org.geysermc.connector.network.translators.world.block.entity.*;
|
|
|
|
import org.geysermc.connector.network.translators.world.block.BlockTranslator;
|
|
|
|
import org.geysermc.connector.network.translators.world.chunk.ChunkPosition;
|
|
|
|
import org.geysermc.connector.network.translators.world.chunk.ChunkSection;
|
2019-08-30 21:47:33 +00:00
|
|
|
|
2020-05-25 01:07:05 +00:00
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.IOException;
|
2020-04-06 15:16:16 +00:00
|
|
|
|
2020-05-03 04:06:53 +00:00
|
|
|
import static org.geysermc.connector.network.translators.world.block.BlockTranslator.AIR;
|
2020-04-29 20:01:53 +00:00
|
|
|
import static org.geysermc.connector.network.translators.world.block.BlockTranslator.BEDROCK_WATER_ID;
|
2020-02-09 22:06:22 +00:00
|
|
|
|
2019-08-30 21:47:33 +00:00
|
|
|
public class ChunkUtils {
|
|
|
|
|
2020-04-21 05:32:32 +00:00
|
|
|
/**
|
|
|
|
* Temporarily stores positions of BlockState values that are needed for certain block entities actively
|
|
|
|
*/
|
2020-06-19 01:44:50 +00:00
|
|
|
public static final Object2IntMap<Position> CACHED_BLOCK_ENTITIES = new Object2IntOpenHashMap<>();
|
2020-04-21 05:32:32 +00:00
|
|
|
|
2020-07-05 20:58:43 +00:00
|
|
|
private static final NbtMap EMPTY_TAG = NbtMap.builder().build();
|
2020-05-25 01:07:05 +00:00
|
|
|
public static final byte[] EMPTY_LEVEL_CHUNK_DATA;
|
|
|
|
|
|
|
|
static {
|
|
|
|
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
|
|
|
|
outputStream.write(new byte[258]); // Biomes + Border Size + Extra Data Size
|
|
|
|
|
|
|
|
try (NBTOutputStream stream = NbtUtils.createNetworkWriter(outputStream)) {
|
2020-07-05 20:58:43 +00:00
|
|
|
stream.writeTag(EMPTY_TAG);
|
2020-05-25 01:07:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EMPTY_LEVEL_CHUNK_DATA = outputStream.toByteArray();
|
|
|
|
}catch (IOException e) {
|
|
|
|
throw new AssertionError("Unable to generate empty level chunk data");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 21:47:33 +00:00
|
|
|
public static ChunkData translateToBedrock(Column column) {
|
|
|
|
ChunkData chunkData = new ChunkData();
|
2019-09-13 09:39:38 +00:00
|
|
|
Chunk[] chunks = column.getChunks();
|
2019-12-31 03:55:17 +00:00
|
|
|
chunkData.sections = new ChunkSection[chunks.length];
|
2019-08-30 21:47:33 +00:00
|
|
|
|
2019-12-31 03:55:17 +00:00
|
|
|
CompoundTag[] blockEntities = column.getTileEntities();
|
2020-04-21 05:32:32 +00:00
|
|
|
// Temporarily stores positions of BlockState values per chunk load
|
2020-06-19 01:44:50 +00:00
|
|
|
Object2IntMap<Position> blockEntityPositions = new Object2IntOpenHashMap<>();
|
2020-04-21 05:32:32 +00:00
|
|
|
|
2020-05-05 02:32:02 +00:00
|
|
|
// Temporarily stores compound tags of Bedrock-only block entities
|
2020-07-05 20:58:43 +00:00
|
|
|
ObjectArrayList<NbtMap> bedrockOnlyBlockEntities = new ObjectArrayList<>();
|
2020-05-05 02:32:02 +00:00
|
|
|
|
2019-12-31 03:55:17 +00:00
|
|
|
for (int chunkY = 0; chunkY < chunks.length; chunkY++) {
|
2019-10-16 03:21:44 +00:00
|
|
|
chunkData.sections[chunkY] = new ChunkSection();
|
2019-09-13 09:39:38 +00:00
|
|
|
Chunk chunk = chunks[chunkY];
|
2019-08-30 21:47:33 +00:00
|
|
|
|
2019-09-08 20:46:10 +00:00
|
|
|
if (chunk == null || chunk.isEmpty())
|
|
|
|
continue;
|
2019-08-30 21:47:33 +00:00
|
|
|
|
2019-09-08 20:46:10 +00:00
|
|
|
ChunkSection section = chunkData.sections[chunkY];
|
|
|
|
for (int x = 0; x < 16; x++) {
|
|
|
|
for (int y = 0; y < 16; y++) {
|
|
|
|
for (int z = 0; z < 16; z++) {
|
2020-06-19 01:44:50 +00:00
|
|
|
int blockState = chunk.get(x, y, z);
|
2019-12-31 00:14:38 +00:00
|
|
|
int id = BlockTranslator.getBedrockBlockId(blockState);
|
2019-08-30 21:47:33 +00:00
|
|
|
|
2020-04-21 05:32:32 +00:00
|
|
|
// Check to see if the name is in BlockTranslator.getBlockEntityString, and therefore must be handled differently
|
|
|
|
if (BlockTranslator.getBlockEntityString(blockState) != null) {
|
2020-03-26 02:03:46 +00:00
|
|
|
Position pos = new ChunkPosition(column.getX(), column.getZ()).getBlock(x, (chunkY << 4) + y, z);
|
2020-04-21 05:32:32 +00:00
|
|
|
blockEntityPositions.put(pos, blockState);
|
2019-12-31 03:55:17 +00:00
|
|
|
}
|
2019-10-10 00:11:50 +00:00
|
|
|
|
2020-05-06 21:52:57 +00:00
|
|
|
section.getBlockStorageArray()[0].setFullBlock(ChunkSection.blockPosition(x, y, z), id);
|
|
|
|
|
2020-05-05 02:32:02 +00:00
|
|
|
// Check if block is piston or flower - only block entities in Bedrock
|
2020-06-19 01:44:50 +00:00
|
|
|
if (BlockStateValues.getFlowerPotValues().containsKey(blockState) ||
|
|
|
|
BlockStateValues.getPistonValues().containsKey(blockState)) {
|
2020-05-05 02:32:02 +00:00
|
|
|
Position pos = new ChunkPosition(column.getX(), column.getZ()).getBlock(x, (chunkY << 4) + y, z);
|
|
|
|
bedrockOnlyBlockEntities.add(BedrockOnlyBlockEntity.getTag(Vector3i.from(pos.getX(), pos.getY(), pos.getZ()), blockState));
|
|
|
|
}
|
|
|
|
|
2020-02-09 22:06:22 +00:00
|
|
|
if (BlockTranslator.isWaterlogged(blockState)) {
|
|
|
|
section.getBlockStorageArray()[1].setFullBlock(ChunkSection.blockPosition(x, y, z), BEDROCK_WATER_ID);
|
2019-10-10 00:11:50 +00:00
|
|
|
}
|
2019-09-08 20:46:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-21 05:32:32 +00:00
|
|
|
|
2019-08-30 21:47:33 +00:00
|
|
|
}
|
2019-11-28 00:14:38 +00:00
|
|
|
|
2020-07-05 20:58:43 +00:00
|
|
|
NbtMap[] bedrockBlockEntities = new NbtMap[blockEntities.length + bedrockOnlyBlockEntities.size()];
|
2020-05-05 02:32:02 +00:00
|
|
|
int i = 0;
|
|
|
|
while (i < blockEntities.length) {
|
2019-12-31 03:55:17 +00:00
|
|
|
CompoundTag tag = blockEntities[i];
|
|
|
|
String tagName;
|
2020-06-10 23:02:29 +00:00
|
|
|
if (tag.contains("id")) {
|
2019-12-31 03:55:17 +00:00
|
|
|
tagName = (String) tag.get("id").getValue();
|
2020-06-10 23:02:29 +00:00
|
|
|
} else {
|
|
|
|
tagName = "Empty";
|
|
|
|
// Sometimes legacy tags have their ID be a StringTag with empty value
|
|
|
|
for (Tag subTag : tag) {
|
|
|
|
if (subTag instanceof StringTag) {
|
|
|
|
StringTag stringTag = (StringTag) subTag;
|
|
|
|
if (stringTag.getValue().equals("")) {
|
|
|
|
tagName = stringTag.getName();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tagName.equals("Empty")) {
|
|
|
|
GeyserConnector.getInstance().getLogger().debug("Got tag with no id: " + tag.getValue());
|
|
|
|
}
|
2019-11-28 00:14:38 +00:00
|
|
|
}
|
|
|
|
|
2019-12-31 03:55:17 +00:00
|
|
|
String id = BlockEntityUtils.getBedrockBlockEntityId(tagName);
|
2019-11-28 00:14:38 +00:00
|
|
|
BlockEntityTranslator blockEntityTranslator = BlockEntityUtils.getBlockEntityTranslator(id);
|
2020-05-05 02:32:02 +00:00
|
|
|
Position pos = new Position((int) tag.get("x").getValue(), (int) tag.get("y").getValue(), (int) tag.get("z").getValue());
|
2020-06-19 01:44:50 +00:00
|
|
|
int blockState = blockEntityPositions.getOrDefault(pos, 0);
|
2020-04-21 05:32:32 +00:00
|
|
|
bedrockBlockEntities[i] = blockEntityTranslator.getBlockEntityTag(tagName, tag, blockState);
|
2020-05-05 02:32:02 +00:00
|
|
|
i++;
|
|
|
|
}
|
2020-07-05 20:58:43 +00:00
|
|
|
for (NbtMap tag : bedrockOnlyBlockEntities) {
|
2020-05-05 02:32:02 +00:00
|
|
|
bedrockBlockEntities[i] = tag;
|
|
|
|
i++;
|
2019-11-28 00:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
chunkData.blockEntities = bedrockBlockEntities;
|
2019-09-08 20:46:10 +00:00
|
|
|
return chunkData;
|
2019-08-30 21:47:33 +00:00
|
|
|
}
|
|
|
|
|
2020-03-06 01:26:36 +00:00
|
|
|
public static void updateChunkPosition(GeyserSession session, Vector3i position) {
|
|
|
|
Vector2i chunkPos = session.getLastChunkPosition();
|
|
|
|
Vector2i newChunkPos = Vector2i.from(position.getX() >> 4, position.getZ() >> 4);
|
|
|
|
|
|
|
|
if (chunkPos == null || !chunkPos.equals(newChunkPos)) {
|
|
|
|
NetworkChunkPublisherUpdatePacket chunkPublisherUpdatePacket = new NetworkChunkPublisherUpdatePacket();
|
|
|
|
chunkPublisherUpdatePacket.setPosition(position);
|
|
|
|
chunkPublisherUpdatePacket.setRadius(session.getRenderDistance() << 4);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(chunkPublisherUpdatePacket);
|
2020-03-06 01:26:36 +00:00
|
|
|
|
|
|
|
session.setLastChunkPosition(newChunkPos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-19 01:44:50 +00:00
|
|
|
public static void updateBlock(GeyserSession session, int blockState, Position position) {
|
2019-12-21 05:05:20 +00:00
|
|
|
Vector3i pos = Vector3i.from(position.getX(), position.getY(), position.getZ());
|
2020-02-06 04:32:33 +00:00
|
|
|
updateBlock(session, blockState, pos);
|
|
|
|
}
|
|
|
|
|
2020-06-19 01:44:50 +00:00
|
|
|
public static void updateBlock(GeyserSession session, int blockState, Vector3i position) {
|
2020-05-02 20:44:05 +00:00
|
|
|
// Checks for item frames so they aren't tripped up and removed
|
2020-06-19 01:44:50 +00:00
|
|
|
if (ItemFrameEntity.positionContainsItemFrame(session, position) && blockState == AIR) {
|
2020-05-02 20:44:05 +00:00
|
|
|
((ItemFrameEntity) session.getEntityCache().getEntityByJavaId(ItemFrameEntity.getItemFrameEntityId(session, position))).updateBlock(session);
|
|
|
|
return;
|
|
|
|
} else if (ItemFrameEntity.positionContainsItemFrame(session, position)) {
|
|
|
|
Entity entity = session.getEntityCache().getEntityByJavaId(ItemFrameEntity.getItemFrameEntityId(session, position));
|
|
|
|
if (entity != null) {
|
|
|
|
session.getEntityCache().removeEntity(entity, false);
|
|
|
|
} else {
|
|
|
|
ItemFrameEntity.removePosition(session, position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-11 02:38:56 +00:00
|
|
|
int blockId = BlockTranslator.getBedrockBlockId(blockState);
|
2019-12-21 05:05:20 +00:00
|
|
|
|
|
|
|
UpdateBlockPacket updateBlockPacket = new UpdateBlockPacket();
|
|
|
|
updateBlockPacket.setDataLayer(0);
|
2020-02-06 04:32:33 +00:00
|
|
|
updateBlockPacket.setBlockPosition(position);
|
2019-12-31 00:14:38 +00:00
|
|
|
updateBlockPacket.setRuntimeId(blockId);
|
2019-12-21 05:05:20 +00:00
|
|
|
updateBlockPacket.getFlags().add(UpdateBlockPacket.Flag.NEIGHBORS);
|
2020-06-29 20:46:29 +00:00
|
|
|
updateBlockPacket.getFlags().add(UpdateBlockPacket.Flag.NETWORK);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(updateBlockPacket);
|
2019-12-21 05:05:20 +00:00
|
|
|
|
|
|
|
UpdateBlockPacket waterPacket = new UpdateBlockPacket();
|
|
|
|
waterPacket.setDataLayer(1);
|
2020-02-06 04:32:33 +00:00
|
|
|
waterPacket.setBlockPosition(position);
|
2020-02-09 22:06:22 +00:00
|
|
|
if (BlockTranslator.isWaterlogged(blockState)) {
|
|
|
|
waterPacket.setRuntimeId(BEDROCK_WATER_ID);
|
2019-12-21 05:05:20 +00:00
|
|
|
} else {
|
|
|
|
waterPacket.setRuntimeId(0);
|
|
|
|
}
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(waterPacket);
|
2020-03-26 02:03:46 +00:00
|
|
|
|
2020-04-21 05:32:32 +00:00
|
|
|
// Since Java stores bed colors/skull information as part of the namespaced ID and Bedrock stores it as a tag
|
2020-03-26 02:03:46 +00:00
|
|
|
// This is the only place I could find that interacts with the Java block state and block updates
|
2020-04-21 05:32:32 +00:00
|
|
|
// Iterates through all block entity translators and determines if the block state needs to be saved
|
2020-05-25 01:07:05 +00:00
|
|
|
for (RequiresBlockState requiresBlockState : BlockEntityTranslator.REQUIRES_BLOCK_STATE_LIST) {
|
2020-05-01 05:51:23 +00:00
|
|
|
if (requiresBlockState.isBlock(blockState)) {
|
2020-05-05 02:32:02 +00:00
|
|
|
// Flower pots are block entities only in Bedrock and are not updated anywhere else like note blocks
|
|
|
|
if (requiresBlockState instanceof BedrockOnlyBlockEntity) {
|
|
|
|
((BedrockOnlyBlockEntity) requiresBlockState).updateBlock(session, blockState, position);
|
|
|
|
break;
|
|
|
|
}
|
2020-05-01 05:51:23 +00:00
|
|
|
CACHED_BLOCK_ENTITIES.put(new Position(position.getX(), position.getY(), position.getZ()), blockState);
|
|
|
|
break; //No block will be a part of two classes
|
2020-04-21 05:32:32 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-29 20:01:53 +00:00
|
|
|
session.getChunkCache().updateBlock(new Position(position.getX(), position.getY(), position.getZ()), blockState);
|
2019-12-21 05:05:20 +00:00
|
|
|
}
|
|
|
|
|
2019-12-29 03:17:00 +00:00
|
|
|
public static void sendEmptyChunks(GeyserSession session, Vector3i position, int radius, boolean forceUpdate) {
|
2019-12-28 13:35:21 +00:00
|
|
|
int chunkX = position.getX() >> 4;
|
|
|
|
int chunkZ = position.getZ() >> 4;
|
2019-12-29 03:17:00 +00:00
|
|
|
for (int x = -radius; x <= radius; x++) {
|
|
|
|
for (int z = -radius; z <= radius; z++) {
|
2019-12-28 13:35:21 +00:00
|
|
|
LevelChunkPacket data = new LevelChunkPacket();
|
|
|
|
data.setChunkX(chunkX + x);
|
|
|
|
data.setChunkZ(chunkZ + z);
|
|
|
|
data.setSubChunksLength(0);
|
2020-05-25 01:07:05 +00:00
|
|
|
data.setData(EMPTY_LEVEL_CHUNK_DATA);
|
2019-12-28 13:35:21 +00:00
|
|
|
data.setCachingEnabled(false);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(data);
|
2019-12-29 03:17:00 +00:00
|
|
|
|
|
|
|
if (forceUpdate) {
|
|
|
|
Vector3i pos = Vector3i.from(chunkX + x << 4, 80, chunkZ + z << 4);
|
|
|
|
UpdateBlockPacket blockPacket = new UpdateBlockPacket();
|
|
|
|
blockPacket.setBlockPosition(pos);
|
2020-02-06 04:32:33 +00:00
|
|
|
blockPacket.setDataLayer(0);
|
2019-12-29 03:17:00 +00:00
|
|
|
blockPacket.setRuntimeId(1);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(blockPacket);
|
2019-12-29 03:17:00 +00:00
|
|
|
}
|
2019-12-28 13:35:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 20:46:10 +00:00
|
|
|
public static final class ChunkData {
|
|
|
|
public ChunkSection[] sections;
|
|
|
|
|
2020-04-21 05:32:32 +00:00
|
|
|
@Getter
|
2020-07-05 20:58:43 +00:00
|
|
|
private NbtMap[] blockEntities = new NbtMap[0];
|
2019-08-30 21:47:33 +00:00
|
|
|
}
|
|
|
|
}
|