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 lombok.Getter;
|
2020-07-07 01:38:10 +00:00
|
|
|
import org.geysermc.connector.bootstrap.GeyserBootstrap;
|
2019-09-15 23:34:14 +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.BlockTranslator;
|
|
|
|
import org.geysermc.connector.network.translators.world.chunk.ChunkPosition;
|
2019-09-15 23:34:14 +00:00
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
public class ChunkCache {
|
|
|
|
|
2020-07-07 01:38:10 +00:00
|
|
|
private final boolean cache;
|
2019-09-15 23:34:14 +00:00
|
|
|
|
|
|
|
@Getter
|
2020-09-03 23:00:36 +00:00
|
|
|
private final Map<ChunkPosition, Column> chunks = new HashMap<>();
|
2019-09-15 23:34:14 +00:00
|
|
|
|
|
|
|
public ChunkCache(GeyserSession session) {
|
2020-07-07 01:38:10 +00:00
|
|
|
if (session.getConnector().getWorldManager().getClass() == GeyserBootstrap.DEFAULT_CHUNK_MANAGER.getClass()) {
|
|
|
|
this.cache = session.getConnector().getConfig().isCacheChunks();
|
|
|
|
} else {
|
|
|
|
this.cache = false; // To prevent Spigot from initializing
|
|
|
|
}
|
2019-09-15 23:34:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void addToCache(Column chunk) {
|
2020-04-29 20:01:53 +00:00
|
|
|
if (!cache) {
|
|
|
|
return;
|
|
|
|
}
|
2019-09-15 23:34:14 +00:00
|
|
|
ChunkPosition position = new ChunkPosition(chunk.getX(), chunk.getZ());
|
2020-09-03 23:00:36 +00:00
|
|
|
if (chunk.getBiomeData() == null && chunks.containsKey(position)) {
|
|
|
|
Column newColumn = chunk;
|
|
|
|
chunk = chunks.get(position);
|
|
|
|
for (int i = 0; i < newColumn.getChunks().length; i++) {
|
|
|
|
if (newColumn.getChunks()[i] != null) {
|
|
|
|
chunk.getChunks()[i] = newColumn.getChunks()[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-15 23:34:14 +00:00
|
|
|
chunks.put(position, chunk);
|
|
|
|
}
|
|
|
|
|
2020-06-19 01:44:50 +00:00
|
|
|
public void updateBlock(Position position, int block) {
|
2020-04-29 20:01:53 +00:00
|
|
|
if (!cache) {
|
|
|
|
return;
|
|
|
|
}
|
2019-09-15 23:34:14 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-19 01:44:50 +00:00
|
|
|
public int getBlockAt(Position position) {
|
2020-04-29 20:01:53 +00:00
|
|
|
if (!cache) {
|
|
|
|
return BlockTranslator.AIR;
|
|
|
|
}
|
2019-09-15 23:34:14 +00:00
|
|
|
ChunkPosition chunkPosition = new ChunkPosition(position.getX() >> 4, position.getZ() >> 4);
|
|
|
|
if (!chunks.containsKey(chunkPosition))
|
2019-12-31 00:14:38 +00:00
|
|
|
return BlockTranslator.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-12-31 00:14:38 +00:00
|
|
|
return chunk.get(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ());
|
2019-09-15 23:34:14 +00:00
|
|
|
}
|
|
|
|
|
2019-12-31 00:14:38 +00:00
|
|
|
return BlockTranslator.AIR;
|
2019-09-15 23:34:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void removeChunk(ChunkPosition position) {
|
2020-04-29 20:01:53 +00:00
|
|
|
if (!cache) {
|
2019-09-15 23:34:14 +00:00
|
|
|
return;
|
2020-04-29 20:01:53 +00:00
|
|
|
}
|
|
|
|
chunks.remove(position);
|
2019-09-15 23:34:14 +00:00
|
|
|
}
|
|
|
|
}
|