Start work on a basic chunk cache and block collision detection

This commit is contained in:
Redned 2019-09-15 18:34:14 -05:00 committed by RednedEpic
parent 45a42116c1
commit 7f051bd6bc
6 changed files with 165 additions and 5 deletions

View file

@ -37,7 +37,7 @@ Links:
- [x] Entity support
- [ ] Chunks (Experimental)
- [x] Block translation (may be missing mappings)
- [ ] Block updates
- [x] Block updates
- [ ] Block entities
- [ ] Extra data
- [ ] Biome colors

View file

@ -75,10 +75,11 @@ public class GeyserSession implements Player {
private PlayerEntity playerEntity;
private PlayerInventory inventory;
private ChunkCache chunkCache;
private EntityCache entityCache;
private InventoryCache inventoryCache;
private WindowCache windowCache;
private ScoreboardCache scoreboardCache;
private WindowCache windowCache;
private DataCache<Packet> javaPacketCache;
@ -96,10 +97,11 @@ public class GeyserSession implements Player {
this.connector = connector;
this.upstream = bedrockServerSession;
this.chunkCache = new ChunkCache(this);
this.entityCache = new EntityCache(this);
this.inventoryCache = new InventoryCache(this);
this.windowCache = new WindowCache(this);
this.scoreboardCache = new ScoreboardCache(this);
this.windowCache = new WindowCache(this);
this.playerEntity = new PlayerEntity(UUID.randomUUID(), 1, 1, EntityType.PLAYER, new Vector3f(0, 0, 0), new Vector3f(0, 0, 0), new Vector3f(0, 0, 0));
this.inventory = new PlayerInventory();

View file

@ -0,0 +1,112 @@
/*
* Copyright (c) 2019 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
*/
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;
import org.geysermc.connector.network.translators.item.BedrockItem;
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) {
chunk.getBlocks().set(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), block);
}
}
public BedrockItem getBlockAt(Position position) {
ChunkPosition chunkPosition = new ChunkPosition(position.getX() >> 4, position.getZ() >> 4);
if (!chunks.containsKey(chunkPosition))
return BedrockItem.AIR;
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) {
BlockState blockState = chunk.getBlocks().get(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ());
return TranslatorsInit.getItemTranslator().getBedrockBlock(blockState);
}
return BedrockItem.AIR;
}
public void removeChunk(ChunkPosition position) {
if (chunks.containsKey(position)) {
chunks.remove(position);
}
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);
}
}

View file

@ -25,12 +25,14 @@
package org.geysermc.connector.network.translators.bedrock;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position;
import com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerPositionRotationPacket;
import com.nukkitx.protocol.bedrock.packet.MovePlayerPacket;
import org.geysermc.connector.entity.Entity;
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.network.translators.item.BedrockItem;
public class BedrockMovePlayerTranslator extends PacketTranslator<MovePlayerPacket> {
@ -40,12 +42,21 @@ public class BedrockMovePlayerTranslator extends PacketTranslator<MovePlayerPack
if (entity == null)
return;
// TODO: Implement collision support
ClientPlayerPositionRotationPacket playerPositionRotationPacket = new ClientPlayerPositionRotationPacket(
packet.isOnGround(), packet.getPosition().getX(), Math.ceil((packet.getPosition().getY() - EntityType.PLAYER.getOffset()) * 2) / 2,
packet.getPosition().getZ(), packet.getRotation().getY(), packet.getRotation().getX());
entity.moveAbsolute(packet.getPosition(), packet.getRotation());
boolean colliding = false;
Position position = new Position((int) packet.getPosition().getX(),
(int) Math.ceil((packet.getPosition().getY() - EntityType.PLAYER.getOffset()) * 2) / 2, (int) packet.getPosition().getZ());
BedrockItem block = session.getChunkCache().getBlockAt(position);
if (!block.getIdentifier().contains("air"))
colliding = true;
if (!colliding)
session.getDownstream().getSession().send(playerPositionRotationPacket);
}
}

View file

@ -64,6 +64,7 @@ public class JavaChunkDataTranslator extends PacketTranslator<ServerChunkDataPac
levelChunkPacket.setChunkZ(packet.getColumn().getZ());
levelChunkPacket.setData(payload);
session.getUpstream().sendPacket(levelChunkPacket);
session.getChunkCache().addToCache(packet.getColumn());
} catch (Exception ex) {
ex.printStackTrace();
}

View file

@ -0,0 +1,34 @@
package org.geysermc.connector.world.chunk;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
public class ChunkPosition {
private int x;
private int z;
public Position getBlock(int x, int y, int z) {
return new Position((this.x << 4) + x, y, (this.z << 4) + z);
}
public Position getChunkBlock(int x, int y, int z) {
int chunkX = x % 16;
int chunkY = y % 16;
int chunkZ = z % 16;
if (chunkX < 0)
chunkX = -chunkX;
if (chunkY < 0)
chunkY = -chunkY;
if (chunkZ < 0)
chunkZ = -chunkZ;
return new Position(chunkX, chunkY, chunkZ);
}
}