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-08-30 21:47:33 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.world.block.BlockState;
|
2019-11-28 00:14:38 +00:00
|
|
|
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
|
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;
|
2019-12-28 13:35:21 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.LevelChunkPacket;
|
2020-03-06 01:26:36 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.NetworkChunkPublisherUpdatePacket;
|
2019-12-21 05:05:20 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket;
|
2019-12-31 03:55:17 +00:00
|
|
|
|
2020-04-06 15:16:16 +00:00
|
|
|
import it.unimi.dsi.fastutil.objects.Object2IntMap;
|
|
|
|
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
|
|
|
|
|
2020-04-21 05:32:32 +00:00
|
|
|
import lombok.Getter;
|
2020-03-06 02:00:14 +00:00
|
|
|
import org.geysermc.connector.GeyserConnector;
|
2019-12-21 05:05:20 +00:00
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
2020-04-29 20:01:53 +00:00
|
|
|
import org.geysermc.connector.network.translators.world.block.entity.*;
|
2020-03-24 04:03:33 +00:00
|
|
|
import org.geysermc.connector.network.translators.Translators;
|
2020-04-29 20:01:53 +00:00
|
|
|
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-04-06 15:16:16 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public static final Map<Position, BlockState> CACHED_BLOCK_ENTITIES = new HashMap<>();
|
|
|
|
|
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
|
|
|
|
Map<Position, BlockState> blockEntityPositions = new HashMap<>();
|
|
|
|
|
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++) {
|
2019-10-13 20:28:03 +00:00
|
|
|
BlockState 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) {
|
|
|
|
// Get the block entity translator
|
|
|
|
BlockEntityTranslator blockEntityTranslator = BlockEntityUtils.getBlockEntityTranslator(BlockTranslator.getBlockEntityString(blockState));
|
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);
|
|
|
|
// If there is a delay required for the block, allow it.
|
|
|
|
if (blockEntityTranslator.getClass().getAnnotation(BlockEntity.class).delay()) {
|
|
|
|
chunkData.loadBlockEntitiesLater.put(blockEntityTranslator.getDefaultBedrockTag(BlockEntityUtils.getBedrockBlockEntityId(BlockTranslator.getBlockEntityString(blockState)),
|
|
|
|
pos.getX(), pos.getY(), pos.getZ()), blockState.getId());
|
|
|
|
} else {
|
|
|
|
section.getBlockStorageArray()[0].setFullBlock(ChunkSection.blockPosition(x, y, z), id);
|
|
|
|
}
|
2019-12-31 03:55:17 +00:00
|
|
|
} else {
|
2020-03-06 02:00:14 +00:00
|
|
|
section.getBlockStorageArray()[0].setFullBlock(ChunkSection.blockPosition(x, y, z), id);
|
2019-12-31 03:55:17 +00:00
|
|
|
}
|
2019-10-10 00:11:50 +00:00
|
|
|
|
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
|
|
|
|
2019-12-31 03:55:17 +00:00
|
|
|
com.nukkitx.nbt.tag.CompoundTag[] bedrockBlockEntities = new com.nukkitx.nbt.tag.CompoundTag[blockEntities.length];
|
2020-03-06 02:00:14 +00:00
|
|
|
for (int i = 0; i < blockEntities.length; i++) {
|
2019-12-31 03:55:17 +00:00
|
|
|
CompoundTag tag = blockEntities[i];
|
|
|
|
String tagName;
|
|
|
|
if (!tag.contains("id")) {
|
2020-03-06 02:00:14 +00:00
|
|
|
GeyserConnector.getInstance().getLogger().debug("Got tag with no id: " + tag.getValue());
|
2019-12-31 03:55:17 +00:00
|
|
|
tagName = "Empty";
|
|
|
|
} else {
|
|
|
|
tagName = (String) tag.get("id").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-04-21 05:32:32 +00:00
|
|
|
BlockState blockState = blockEntityPositions.get(new Position((int) tag.get("x").getValue(), (int) tag.get("y").getValue(), (int) tag.get("z").getValue()));
|
|
|
|
bedrockBlockEntities[i] = blockEntityTranslator.getBlockEntityTag(tagName, tag, blockState);
|
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);
|
|
|
|
session.getUpstream().sendPacket(chunkPublisherUpdatePacket);
|
|
|
|
|
|
|
|
session.setLastChunkPosition(newChunkPos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-21 05:05:20 +00:00
|
|
|
public static void updateBlock(GeyserSession session, BlockState blockState, Position position) {
|
|
|
|
Vector3i pos = Vector3i.from(position.getX(), position.getY(), position.getZ());
|
2020-02-06 04:32:33 +00:00
|
|
|
updateBlock(session, blockState, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void updateBlock(GeyserSession session, BlockState blockState, Vector3i 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);
|
|
|
|
session.getUpstream().sendPacket(updateBlockPacket);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
session.getUpstream().sendPacket(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
|
|
|
|
for (Map.Entry<String, BlockEntityTranslator> entry : Translators.getBlockEntityTranslators().entrySet()) {
|
|
|
|
if (entry.getValue() instanceof RequiresBlockState) {
|
|
|
|
RequiresBlockState requiresBlockState = (RequiresBlockState) entry.getValue();
|
|
|
|
if (requiresBlockState.isBlock(blockState)) {
|
|
|
|
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-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-03-24 04:03:33 +00:00
|
|
|
data.setData(Translators.EMPTY_LEVEL_CHUNK_DATA);
|
2019-12-28 13:35:21 +00:00
|
|
|
data.setCachingEnabled(false);
|
|
|
|
session.getUpstream().sendPacket(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);
|
|
|
|
session.getUpstream().sendPacket(blockPacket);
|
|
|
|
}
|
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
|
|
|
|
private com.nukkitx.nbt.tag.CompoundTag[] blockEntities = new com.nukkitx.nbt.tag.CompoundTag[0];
|
|
|
|
@Getter
|
|
|
|
private Object2IntMap<com.nukkitx.nbt.tag.CompoundTag> loadBlockEntitiesLater = new Object2IntOpenHashMap<>();
|
2019-08-30 21:47:33 +00:00
|
|
|
}
|
|
|
|
}
|