2020-01-09 03:05:42 +00:00
|
|
|
/*
|
2022-01-01 19:03:05 +00:00
|
|
|
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
|
2020-01-09 03:05:42 +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
|
|
|
|
*/
|
|
|
|
|
2021-11-20 23:29:46 +00:00
|
|
|
package org.geysermc.geyser.util;
|
2019-08-30 21:47:33 +00:00
|
|
|
|
2019-12-21 05:05:20 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position;
|
2021-11-14 04:49:31 +00:00
|
|
|
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.IntTag;
|
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;
|
2020-10-15 06:30:25 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.LevelChunkPacket;
|
|
|
|
import com.nukkitx.protocol.bedrock.packet.NetworkChunkPublisherUpdatePacket;
|
|
|
|
import com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket;
|
2021-07-15 01:14:53 +00:00
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
import io.netty.buffer.Unpooled;
|
2021-11-14 22:59:14 +00:00
|
|
|
import it.unimi.dsi.fastutil.ints.IntLists;
|
2020-10-15 06:30:25 +00:00
|
|
|
import lombok.experimental.UtilityClass;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.entity.type.ItemFrameEntity;
|
|
|
|
import org.geysermc.geyser.entity.type.player.SkullPlayerEntity;
|
|
|
|
import org.geysermc.geyser.level.block.BlockStateValues;
|
|
|
|
import org.geysermc.geyser.level.chunk.BlockStorage;
|
|
|
|
import org.geysermc.geyser.level.chunk.GeyserChunkSection;
|
|
|
|
import org.geysermc.geyser.level.chunk.bitarray.SingletonBitArray;
|
2021-11-20 21:34:30 +00:00
|
|
|
import org.geysermc.geyser.registry.BlockRegistries;
|
2021-12-21 02:31:19 +00:00
|
|
|
import org.geysermc.geyser.session.GeyserSession;
|
|
|
|
import org.geysermc.geyser.text.GeyserLocale;
|
|
|
|
import org.geysermc.geyser.translator.level.block.entity.BedrockOnlyBlockEntity;
|
2019-08-30 21:47:33 +00:00
|
|
|
|
2021-11-20 23:29:46 +00:00
|
|
|
import static org.geysermc.geyser.level.block.BlockStateValues.JAVA_AIR_ID;
|
2020-02-09 22:06:22 +00:00
|
|
|
|
2020-10-15 06:30:25 +00:00
|
|
|
@UtilityClass
|
2019-08-30 21:47:33 +00:00
|
|
|
public class ChunkUtils {
|
2021-06-08 03:09:42 +00:00
|
|
|
/**
|
|
|
|
* The minimum height Bedrock Edition will accept.
|
|
|
|
*/
|
2021-07-29 18:35:03 +00:00
|
|
|
public static final int MINIMUM_ACCEPTED_HEIGHT = 0;
|
2021-11-12 14:42:35 +00:00
|
|
|
public static final int MINIMUM_ACCEPTED_HEIGHT_OVERWORLD = -64;
|
2021-06-08 03:09:42 +00:00
|
|
|
/**
|
2021-07-14 00:48:45 +00:00
|
|
|
* The maximum chunk height Bedrock Edition will accept, from the lowest point to the highest.
|
2021-06-08 03:09:42 +00:00
|
|
|
*/
|
2021-11-14 04:49:31 +00:00
|
|
|
public static final int MAXIMUM_ACCEPTED_HEIGHT = 256;
|
|
|
|
public static final int MAXIMUM_ACCEPTED_HEIGHT_OVERWORLD = 384;
|
2021-07-15 01:14:53 +00:00
|
|
|
|
2021-11-14 22:59:14 +00:00
|
|
|
/**
|
|
|
|
* An empty subchunk.
|
|
|
|
*/
|
|
|
|
public static final byte[] SERIALIZED_CHUNK_DATA;
|
|
|
|
/**
|
|
|
|
* An empty chunk that can be safely passed on to a LevelChunkPacket with subcounts set to 0.
|
|
|
|
*/
|
2021-07-16 00:51:13 +00:00
|
|
|
public static final byte[] EMPTY_CHUNK_DATA;
|
2021-07-15 01:14:53 +00:00
|
|
|
public static final byte[] EMPTY_BIOME_DATA;
|
|
|
|
|
|
|
|
static {
|
2021-11-12 14:42:35 +00:00
|
|
|
ByteBuf byteBuf = Unpooled.buffer();
|
|
|
|
try {
|
2021-11-14 22:59:14 +00:00
|
|
|
new GeyserChunkSection(new BlockStorage[0])
|
|
|
|
.writeToNetwork(byteBuf);
|
|
|
|
SERIALIZED_CHUNK_DATA = new byte[byteBuf.readableBytes()];
|
|
|
|
byteBuf.readBytes(SERIALIZED_CHUNK_DATA);
|
|
|
|
} finally {
|
|
|
|
byteBuf.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
byteBuf = Unpooled.buffer();
|
|
|
|
try {
|
|
|
|
BlockStorage blockStorage = new BlockStorage(SingletonBitArray.INSTANCE, IntLists.singleton(0));
|
2021-11-12 14:42:35 +00:00
|
|
|
blockStorage.writeToNetwork(byteBuf);
|
|
|
|
|
|
|
|
EMPTY_BIOME_DATA = new byte[byteBuf.readableBytes()];
|
|
|
|
byteBuf.readBytes(EMPTY_BIOME_DATA);
|
|
|
|
} finally {
|
|
|
|
byteBuf.release();
|
|
|
|
}
|
2021-07-15 01:14:53 +00:00
|
|
|
|
2021-11-12 14:42:35 +00:00
|
|
|
byteBuf = Unpooled.buffer();
|
|
|
|
try {
|
|
|
|
for (int i = 0; i < 32; i++) {
|
|
|
|
byteBuf.writeBytes(EMPTY_BIOME_DATA);
|
|
|
|
}
|
2021-07-15 01:59:10 +00:00
|
|
|
|
2021-11-12 14:42:35 +00:00
|
|
|
byteBuf.writeByte(0); // Border
|
2021-07-24 01:42:08 +00:00
|
|
|
|
2021-11-12 14:42:35 +00:00
|
|
|
EMPTY_CHUNK_DATA = new byte[byteBuf.readableBytes()];
|
|
|
|
byteBuf.readBytes(EMPTY_CHUNK_DATA);
|
|
|
|
} finally {
|
|
|
|
byteBuf.release();
|
2021-07-15 01:14:53 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-21 05:32:32 +00:00
|
|
|
|
2021-11-14 04:49:31 +00:00
|
|
|
public static int indexYZXtoXZY(int yzx) {
|
2020-10-15 06:30:25 +00:00
|
|
|
return (yzx >> 8) | (yzx & 0x0F0) | ((yzx & 0x00F) << 8);
|
|
|
|
}
|
2019-08-30 21:47:33 +00:00
|
|
|
|
2021-11-22 19:52:26 +00:00
|
|
|
public static void updateChunkPosition(GeyserSession session, Vector3i position) {
|
2020-03-06 01:26:36 +00:00
|
|
|
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);
|
2022-01-16 01:29:00 +00:00
|
|
|
chunkPublisherUpdatePacket.setRadius(session.getServerRenderDistance() << 4);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(chunkPublisherUpdatePacket);
|
2020-03-06 01:26:36 +00:00
|
|
|
|
|
|
|
session.setLastChunkPosition(newChunkPos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-17 17:03:12 +00:00
|
|
|
/**
|
|
|
|
* Sends a block update to the Bedrock client. If chunk caching is enabled and the platform is not Spigot, this also
|
|
|
|
* adds that block to the cache.
|
|
|
|
* @param session the Bedrock session to send/register the block to
|
|
|
|
* @param blockState the Java block state of the block
|
|
|
|
* @param position the position of the block
|
|
|
|
*/
|
2021-11-22 19:52:26 +00:00
|
|
|
public static void updateBlock(GeyserSession session, int blockState, Position position) {
|
2019-12-21 05:05:20 +00:00
|
|
|
Vector3i pos = Vector3i.from(position.getX(), position.getY(), position.getZ());
|
2020-02-06 04:32:33 +00:00
|
|
|
updateBlock(session, blockState, pos);
|
|
|
|
}
|
|
|
|
|
2020-11-17 17:03:12 +00:00
|
|
|
/**
|
|
|
|
* Sends a block update to the Bedrock client. If chunk caching is enabled and the platform is not Spigot, this also
|
|
|
|
* adds that block to the cache.
|
|
|
|
* @param session the Bedrock session to send/register the block to
|
|
|
|
* @param blockState the Java block state of the block
|
|
|
|
* @param position the position of the block
|
|
|
|
*/
|
2021-11-22 19:52:26 +00:00
|
|
|
public static void updateBlock(GeyserSession session, int blockState, Vector3i position) {
|
2020-05-02 20:44:05 +00:00
|
|
|
// Checks for item frames so they aren't tripped up and removed
|
2021-05-09 19:44:41 +00:00
|
|
|
ItemFrameEntity itemFrameEntity = ItemFrameEntity.getItemFrameEntity(session, position);
|
|
|
|
if (itemFrameEntity != null) {
|
|
|
|
if (blockState == JAVA_AIR_ID) { // Item frame is still present and no block overrides that; refresh it
|
2021-11-19 01:44:03 +00:00
|
|
|
itemFrameEntity.updateBlock(true);
|
2021-06-03 23:04:28 +00:00
|
|
|
// Still update the chunk cache with the new block
|
|
|
|
session.getChunkCache().updateBlock(position.getX(), position.getY(), position.getZ(), blockState);
|
2020-11-05 21:36:22 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-05-09 19:44:41 +00:00
|
|
|
// Otherwise, let's still store our reference to the item frame, but let the new block take precedence for now
|
2020-05-02 20:44:05 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 21:55:24 +00:00
|
|
|
SkullPlayerEntity skull = session.getSkullCache().get(position);
|
|
|
|
if (skull != null && blockState != skull.getBlockState()) {
|
|
|
|
// Skull is gone
|
2021-11-18 03:02:38 +00:00
|
|
|
skull.despawnEntity(position);
|
2020-12-04 21:55:24 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 01:20:25 +00:00
|
|
|
// Prevent moving_piston from being placed
|
|
|
|
// It's used for extending piston heads, but it isn't needed on Bedrock and causes pistons to flicker
|
|
|
|
if (!BlockStateValues.isMovingPiston(blockState)) {
|
|
|
|
int blockId = session.getBlockMappings().getBedrockBlockId(blockState);
|
|
|
|
|
|
|
|
UpdateBlockPacket updateBlockPacket = new UpdateBlockPacket();
|
|
|
|
updateBlockPacket.setDataLayer(0);
|
|
|
|
updateBlockPacket.setBlockPosition(position);
|
|
|
|
updateBlockPacket.setRuntimeId(blockId);
|
|
|
|
updateBlockPacket.getFlags().add(UpdateBlockPacket.Flag.NEIGHBORS);
|
|
|
|
updateBlockPacket.getFlags().add(UpdateBlockPacket.Flag.NETWORK);
|
|
|
|
session.sendUpstreamPacket(updateBlockPacket);
|
|
|
|
|
|
|
|
UpdateBlockPacket waterPacket = new UpdateBlockPacket();
|
|
|
|
waterPacket.setDataLayer(1);
|
|
|
|
waterPacket.setBlockPosition(position);
|
|
|
|
if (BlockRegistries.WATERLOGGED.get().contains(blockState)) {
|
|
|
|
waterPacket.setRuntimeId(session.getBlockMappings().getBedrockWaterId());
|
|
|
|
} else {
|
|
|
|
waterPacket.setRuntimeId(session.getBlockMappings().getBedrockAirId());
|
|
|
|
}
|
|
|
|
session.sendUpstreamPacket(waterPacket);
|
2019-12-21 05:05:20 +00:00
|
|
|
}
|
2020-03-26 02:03:46 +00:00
|
|
|
|
2021-09-04 18:55:22 +00:00
|
|
|
BlockStateValues.getLecternBookStates().handleBlockChange(session, blockState, position);
|
2020-12-28 05:29:27 +00:00
|
|
|
|
2021-06-01 19:36:33 +00:00
|
|
|
// Iterates through all Bedrock-only block entity translators and determines if a manual block entity packet
|
|
|
|
// needs to be sent
|
2021-07-13 01:19:40 +00:00
|
|
|
for (BedrockOnlyBlockEntity bedrockOnlyBlockEntity : BlockEntityUtils.BEDROCK_ONLY_BLOCK_ENTITIES) {
|
2021-06-01 19:36:33 +00:00
|
|
|
if (bedrockOnlyBlockEntity.isBlock(blockState)) {
|
2020-05-05 02:32:02 +00:00
|
|
|
// Flower pots are block entities only in Bedrock and are not updated anywhere else like note blocks
|
2021-06-01 19:36:33 +00:00
|
|
|
bedrockOnlyBlockEntity.updateBlock(session, blockState, position);
|
2020-05-01 05:51:23 +00:00
|
|
|
break; //No block will be a part of two classes
|
2020-04-21 05:32:32 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-13 14:44:47 +00:00
|
|
|
session.getChunkCache().updateBlock(position.getX(), position.getY(), position.getZ(), blockState);
|
2019-12-21 05:05:20 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 05:25:33 +00:00
|
|
|
public static void sendEmptyChunk(GeyserSession session, int chunkX, int chunkZ, boolean forceUpdate) {
|
|
|
|
LevelChunkPacket data = new LevelChunkPacket();
|
|
|
|
data.setChunkX(chunkX);
|
|
|
|
data.setChunkZ(chunkZ);
|
|
|
|
data.setSubChunksLength(0);
|
|
|
|
data.setData(EMPTY_CHUNK_DATA);
|
|
|
|
data.setCachingEnabled(false);
|
|
|
|
session.sendUpstreamPacket(data);
|
|
|
|
|
|
|
|
if (forceUpdate) {
|
|
|
|
Vector3i pos = Vector3i.from(chunkX << 4, 80, chunkZ << 4);
|
|
|
|
UpdateBlockPacket blockPacket = new UpdateBlockPacket();
|
|
|
|
blockPacket.setBlockPosition(pos);
|
|
|
|
blockPacket.setDataLayer(0);
|
|
|
|
blockPacket.setRuntimeId(1);
|
|
|
|
session.sendUpstreamPacket(blockPacket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-22 19:52:26 +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++) {
|
2021-12-29 05:25:33 +00:00
|
|
|
sendEmptyChunk(session, chunkX + x, chunkZ + z, forceUpdate);
|
2019-12-28 13:35:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 03:09:42 +00:00
|
|
|
/**
|
2021-10-10 15:21:48 +00:00
|
|
|
* Process the minimum and maximum heights for this dimension, and processes the world coordinate scale.
|
2021-07-15 01:46:56 +00:00
|
|
|
* This must be done after the player has switched dimensions so we know what their dimension is
|
2021-06-08 03:09:42 +00:00
|
|
|
*/
|
2021-11-22 19:52:26 +00:00
|
|
|
public static void loadDimensionTag(GeyserSession session, CompoundTag dimensionTag) {
|
2021-06-08 03:09:42 +00:00
|
|
|
int minY = ((IntTag) dimensionTag.get("min_y")).getValue();
|
|
|
|
int maxY = ((IntTag) dimensionTag.get("height")).getValue();
|
|
|
|
// Logical height can be ignored probably - seems to be for artificial limits like the Nether.
|
|
|
|
|
|
|
|
if (minY % 16 != 0) {
|
|
|
|
throw new RuntimeException("Minimum Y must be a multiple of 16!");
|
|
|
|
}
|
|
|
|
if (maxY % 16 != 0) {
|
|
|
|
throw new RuntimeException("Maximum Y must be a multiple of 16!");
|
|
|
|
}
|
|
|
|
|
2021-07-17 00:06:09 +00:00
|
|
|
int dimension = DimensionUtils.javaToBedrock(session.getDimension());
|
2021-11-12 14:42:35 +00:00
|
|
|
boolean extendedHeight = dimension == 0;
|
2021-07-17 00:06:09 +00:00
|
|
|
session.getChunkCache().setExtendedHeight(extendedHeight);
|
|
|
|
|
2021-07-17 00:18:13 +00:00
|
|
|
// Yell in the console if the world height is too height in the current scenario
|
|
|
|
// The constraints change depending on if the player is in the overworld or not, and if experimental height is enabled
|
2021-07-17 00:06:09 +00:00
|
|
|
if (minY < (extendedHeight ? MINIMUM_ACCEPTED_HEIGHT_OVERWORLD : MINIMUM_ACCEPTED_HEIGHT)
|
|
|
|
|| maxY > (extendedHeight ? MAXIMUM_ACCEPTED_HEIGHT_OVERWORLD : MAXIMUM_ACCEPTED_HEIGHT)) {
|
2021-11-20 23:29:46 +00:00
|
|
|
session.getGeyser().getLogger().warning(GeyserLocale.getLocaleStringLog("geyser.network.translator.chunk.out_of_bounds",
|
2021-11-12 14:42:35 +00:00
|
|
|
extendedHeight ? MINIMUM_ACCEPTED_HEIGHT_OVERWORLD : MINIMUM_ACCEPTED_HEIGHT,
|
|
|
|
extendedHeight ? MAXIMUM_ACCEPTED_HEIGHT_OVERWORLD : MAXIMUM_ACCEPTED_HEIGHT,
|
|
|
|
session.getDimension()));
|
2021-06-08 03:09:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
session.getChunkCache().setMinY(minY);
|
2021-06-29 14:09:47 +00:00
|
|
|
session.getChunkCache().setHeightY(maxY);
|
2021-10-10 15:21:48 +00:00
|
|
|
|
|
|
|
// Load world coordinate scale for the world border
|
2021-12-21 02:31:19 +00:00
|
|
|
double coordinateScale = ((Number) dimensionTag.get("coordinate_scale").getValue()).doubleValue();
|
2021-10-10 15:21:48 +00:00
|
|
|
session.getWorldBorder().setWorldCoordinateScale(coordinateScale);
|
2021-12-21 03:54:34 +00:00
|
|
|
|
|
|
|
// Set if piglins/hoglins should shake
|
|
|
|
session.setDimensionPiglinSafe(((Number) dimensionTag.get("piglin_safe").getValue()).byteValue() != (byte) 0);
|
2021-06-08 03:09:42 +00:00
|
|
|
}
|
2019-08-30 21:47:33 +00:00
|
|
|
}
|