2019-09-15 23:34:14 +00:00
|
|
|
/*
|
2020-01-09 03:05:42 +00:00
|
|
|
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
|
2019-09-15 23:34:14 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.geysermc.connector.network.session.cache;
|
|
|
|
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.chunk.Chunk;
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.chunk.Column;
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position;
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.world.block.BlockState;
|
|
|
|
import com.nukkitx.protocol.bedrock.packet.LevelChunkPacket;
|
|
|
|
import lombok.Getter;
|
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
|
|
|
import org.geysermc.connector.network.translators.TranslatorsInit;
|
2019-10-10 00:11:50 +00:00
|
|
|
import org.geysermc.connector.network.translators.block.BlockEntry;
|
2019-09-15 23:34:14 +00:00
|
|
|
import org.geysermc.connector.world.chunk.ChunkPosition;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
public class ChunkCache {
|
|
|
|
|
|
|
|
private GeyserSession session;
|
|
|
|
|
|
|
|
@Getter
|
|
|
|
private Map<ChunkPosition, Column> chunks;
|
|
|
|
|
|
|
|
public ChunkCache(GeyserSession session) {
|
|
|
|
this.session = session;
|
|
|
|
this.chunks = new HashMap<>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addToCache(Column chunk) {
|
|
|
|
ChunkPosition position = new ChunkPosition(chunk.getX(), chunk.getZ());
|
|
|
|
chunks.put(position, chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateBlock(Position position, BlockState block) {
|
|
|
|
ChunkPosition chunkPosition = new ChunkPosition(position.getX() >> 4, position.getZ() >> 4);
|
|
|
|
if (!chunks.containsKey(chunkPosition))
|
|
|
|
return;
|
|
|
|
|
|
|
|
Column column = chunks.get(chunkPosition);
|
|
|
|
Chunk chunk = column.getChunks()[position.getY() >> 4];
|
|
|
|
Position blockPosition = chunkPosition.getChunkBlock(position.getX(), position.getY(), position.getZ());
|
|
|
|
if (chunk != null) {
|
2019-10-13 20:28:03 +00:00
|
|
|
chunk.set(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), block);
|
2019-09-15 23:34:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-10 00:11:50 +00:00
|
|
|
public BlockEntry getBlockAt(Position position) {
|
2019-09-15 23:34:14 +00:00
|
|
|
ChunkPosition chunkPosition = new ChunkPosition(position.getX() >> 4, position.getZ() >> 4);
|
|
|
|
if (!chunks.containsKey(chunkPosition))
|
2019-10-10 00:11:50 +00:00
|
|
|
return BlockEntry.AIR;
|
2019-09-15 23:34:14 +00:00
|
|
|
|
|
|
|
Column column = chunks.get(chunkPosition);
|
|
|
|
Chunk chunk = column.getChunks()[position.getY() >> 4];
|
|
|
|
Position blockPosition = chunkPosition.getChunkBlock(position.getX(), position.getY(), position.getZ());
|
|
|
|
if (chunk != null) {
|
2019-10-13 20:28:03 +00:00
|
|
|
BlockState blockState = chunk.get(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ());
|
2019-12-21 05:05:20 +00:00
|
|
|
return TranslatorsInit.getBlockTranslator().getBlockEntry(blockState);
|
2019-09-15 23:34:14 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 00:11:50 +00:00
|
|
|
return BlockEntry.AIR;
|
2019-09-15 23:34:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void removeChunk(ChunkPosition position) {
|
2019-10-10 00:11:50 +00:00
|
|
|
chunks.remove(position);
|
2019-09-15 23:34:14 +00:00
|
|
|
sendEmptyChunk(position, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void sendEmptyChunk(ChunkPosition position) {
|
|
|
|
sendEmptyChunk(position, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void sendEmptyChunk(ChunkPosition position, boolean force) {
|
|
|
|
if (!force && chunks.containsKey(position))
|
|
|
|
return;
|
|
|
|
|
|
|
|
LevelChunkPacket levelChunkPacket = new LevelChunkPacket();
|
|
|
|
levelChunkPacket.setChunkX(position.getX());
|
|
|
|
levelChunkPacket.setChunkZ(position.getZ());
|
|
|
|
levelChunkPacket.setCachingEnabled(false);
|
|
|
|
levelChunkPacket.setSubChunksLength(0);
|
|
|
|
levelChunkPacket.setData(TranslatorsInit.EMPTY_LEVEL_CHUNK_DATA);
|
|
|
|
session.getUpstream().sendPacket(levelChunkPacket);
|
|
|
|
}
|
|
|
|
}
|