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-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;
|
|
|
|
import com.nukkitx.protocol.bedrock.packet.NetworkChunkPublisherUpdatePacket;
|
2019-12-21 05:05:20 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket;
|
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
2019-08-30 21:47:33 +00:00
|
|
|
import org.geysermc.connector.network.translators.TranslatorsInit;
|
2019-12-31 00:14:38 +00:00
|
|
|
import org.geysermc.connector.network.translators.block.BlockTranslator;
|
2019-08-30 21:47:33 +00:00
|
|
|
import org.geysermc.connector.world.chunk.ChunkSection;
|
|
|
|
|
|
|
|
public class ChunkUtils {
|
|
|
|
public static ChunkData translateToBedrock(Column column) {
|
|
|
|
ChunkData chunkData = new ChunkData();
|
|
|
|
|
2019-09-13 09:39:38 +00:00
|
|
|
Chunk[] chunks = column.getChunks();
|
|
|
|
int chunkSectionCount = chunks.length;
|
|
|
|
chunkData.sections = new ChunkSection[chunkSectionCount];
|
2019-08-30 21:47:33 +00:00
|
|
|
|
2019-09-13 09:39:38 +00:00
|
|
|
for (int chunkY = 0; chunkY < chunkSectionCount; 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];
|
2019-08-30 21:47:33 +00:00
|
|
|
|
2019-09-08 20:46:10 +00:00
|
|
|
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
|
|
|
|
2019-12-31 00:14:38 +00:00
|
|
|
section.getBlockStorageArray()[0].setFullBlock(ChunkSection.blockPosition(x, y, z), id);
|
2019-10-10 00:11:50 +00:00
|
|
|
|
2019-12-31 00:14:38 +00:00
|
|
|
int waterloggedId = BlockTranslator.getBedrockWaterLoggedId(blockState);
|
|
|
|
if (waterloggedId != -1) {
|
|
|
|
section.getBlockStorageArray()[1].setFullBlock(ChunkSection.blockPosition(x, y, z), waterloggedId);
|
2019-10-10 00:11:50 +00:00
|
|
|
}
|
2019-09-08 20:46:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-30 21:47:33 +00:00
|
|
|
}
|
2019-09-08 20:46:10 +00:00
|
|
|
return chunkData;
|
2019-08-30 21:47:33 +00:00
|
|
|
}
|
|
|
|
|
2019-12-21 05:05:20 +00:00
|
|
|
public static void updateBlock(GeyserSession session, BlockState blockState, Position position) {
|
2019-12-31 00:14:38 +00:00
|
|
|
int blockId = BlockTranslator.getBedrockBlockId(blockState);
|
2019-12-21 05:05:20 +00:00
|
|
|
Vector3i pos = Vector3i.from(position.getX(), position.getY(), position.getZ());
|
|
|
|
|
|
|
|
UpdateBlockPacket updateBlockPacket = new UpdateBlockPacket();
|
|
|
|
updateBlockPacket.setDataLayer(0);
|
|
|
|
updateBlockPacket.setBlockPosition(pos);
|
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);
|
|
|
|
waterPacket.setBlockPosition(pos);
|
2019-12-31 00:14:38 +00:00
|
|
|
int waterloggedId = BlockTranslator.getBedrockWaterLoggedId(blockState);
|
|
|
|
if (waterloggedId != -1) {
|
|
|
|
waterPacket.setRuntimeId(waterloggedId);
|
2019-12-21 05:05:20 +00:00
|
|
|
} else {
|
|
|
|
waterPacket.setRuntimeId(0);
|
|
|
|
}
|
|
|
|
session.getUpstream().sendPacket(waterPacket);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
NetworkChunkPublisherUpdatePacket chunkPublisherUpdatePacket = new NetworkChunkPublisherUpdatePacket();
|
|
|
|
chunkPublisherUpdatePacket.setPosition(position);
|
2019-12-29 03:17:00 +00:00
|
|
|
chunkPublisherUpdatePacket.setRadius(radius + 1 << 4);
|
2019-12-28 13:35:21 +00:00
|
|
|
session.getUpstream().sendPacket(chunkPublisherUpdatePacket);
|
|
|
|
session.setLastChunkPosition(null);
|
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);
|
|
|
|
data.setData(TranslatorsInit.EMPTY_LEVEL_CHUNK_DATA);
|
|
|
|
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);
|
|
|
|
blockPacket.setDataLayer(1);
|
|
|
|
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;
|
|
|
|
|
|
|
|
public byte[] biomes = new byte[256];
|
|
|
|
public byte[] blockEntities = new byte[0];
|
2019-08-30 21:47:33 +00:00
|
|
|
}
|
|
|
|
}
|