Skip to content

Commit

Permalink
Yeet cache chunks
Browse files Browse the repository at this point in the history
So many features require this config option, and we don't intend on supporting it being both disabled and enabled.
  • Loading branch information
Camotoy committed Jun 1, 2021
1 parent 7b0099e commit ebf726c
Show file tree
Hide file tree
Showing 31 changed files with 93 additions and 291 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,4 @@ public void loadFloodgate(GeyserSpigotPlugin plugin) {

floodgateKeyPath = FloodgateKeyLoader.getKeyPath(this, floodgate, floodgateDataFolder, geyserDataFolder, plugin.getGeyserLogger());
}

@Override
public boolean isCacheChunks() {
return true; // We override this as with Bukkit, we have direct access to the server implementation
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ public interface GeyserConfiguration {

boolean isAboveBedrockNetherBuilding();

boolean isCacheChunks();

boolean isForceResourcePacks();

boolean isXboxAchievementsEnabled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ public abstract class GeyserJacksonConfiguration implements GeyserConfiguration
@JsonProperty("default-locale")
private String defaultLocale = null; // is null by default so system language takes priority

@JsonProperty("cache-chunks")
private boolean cacheChunks = false;

@JsonProperty("cache-images")
private int cacheImages = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,8 @@ protected float getGravity(GeyserSession session) {
* @return true if this entity is currently in air.
*/
protected boolean isInAir(GeyserSession session) {
if (session.getConnector().getConfig().isCacheChunks()) {
int block = session.getConnector().getWorldManager().getBlockAt(session, position.toInt());
return block == BlockTranslator.JAVA_AIR_ID;
}
return false;
int block = session.getConnector().getWorldManager().getBlockAt(session, position.toInt());
return block == BlockTranslator.JAVA_AIR_ID;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,9 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
Position bedPosition = (Position) entityMetadata.getValue();
if (bedPosition != null) {
metadata.put(EntityData.BED_POSITION, Vector3i.from(bedPosition.getX(), bedPosition.getY(), bedPosition.getZ()));
if (session.getConnector().getConfig().isCacheChunks()) {
int bed = session.getConnector().getWorldManager().getBlockAt(session, bedPosition);
// Bed has to be updated, or else player is floating in the air
ChunkUtils.updateBlock(session, bed, bedPosition);
}
int bed = session.getConnector().getWorldManager().getBlockAt(session, bedPosition);
// Bed has to be updated, or else player is floating in the air
ChunkUtils.updateBlock(session, bed, bedPosition);
// Indicate that the player should enter the sleep cycle
// Has to be a byte or it does not work
// (Bed position is what actually triggers sleep - "pose" is only optional)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,8 @@ protected float getDrag(GeyserSession session) {
* @return true if this entity is currently in water.
*/
protected boolean isInWater(GeyserSession session) {
if (session.getConnector().getConfig().isCacheChunks()) {
int block = session.getConnector().getWorldManager().getBlockAt(session, position.toInt());
return BlockStateValues.getWaterLevel(block) != -1;
}
return false;
int block = session.getConnector().getWorldManager().getBlockAt(session, position.toInt());
return BlockStateValues.getWaterLevel(block) != -1;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void moveRelative(GeyserSession session, double relX, double relY, double
float bedPositionSubtractorW = 0;
float bedPositionSubtractorN = 0;
Vector3i bedPosition = metadata.getPos(EntityData.BED_POSITION, null);
if (session.getConnector().getConfig().isCacheChunks() && bedPosition != null) {
if (bedPosition != null) {
bedId = session.getConnector().getWorldManager().getBlockAt(session, bedPosition);
}
String bedRotationZ = BlockTranslator.getJavaIdBlockMap().inverse().get(bedId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,6 @@ public class GeyserSession implements CommandSender {
@Setter
private boolean sprinting;

/**
* Not updated if cache chunks is enabled.
*/
@Setter
private boolean jumping;

/**
* Whether the player is swimming in water.
* Used to update speed when crawling.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ public class ChunkCache {
private int minY;

public ChunkCache(GeyserSession session) {
if (session.getConnector().getWorldManager().hasOwnChunkCache()) {
this.cache = false; // To prevent Spigot from initializing
} else {
this.cache = session.getConnector().getConfig().isCacheChunks();
}
this.cache = !session.getConnector().getWorldManager().hasOwnChunkCache(); // To prevent Spigot from initializing
chunks = cache ? new Long2ObjectOpenHashMap<>() : null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@
import org.geysermc.connector.network.translators.world.block.BlockTranslator;
import org.geysermc.connector.utils.BlockUtils;

import java.util.concurrent.TimeUnit;

@Translator(packet = PlayerActionPacket.class)
public class BedrockActionTranslator extends PacketTranslator<PlayerActionPacket> {

Expand Down Expand Up @@ -162,43 +160,42 @@ public void translate(PlayerActionPacket packet, GeyserSession session) {
// Otherwise handled in BedrockInventoryTransactionTranslator
break;
case START_BREAK:
if (session.getConnector().getConfig().isCacheChunks()) {
// Start the block breaking animation
if (session.getGameMode() != GameMode.CREATIVE) {
int blockState = session.getConnector().getWorldManager().getBlockAt(session, vector);
LevelEventPacket startBreak = new LevelEventPacket();
startBreak.setType(LevelEventType.BLOCK_START_BREAK);
startBreak.setPosition(vector.toFloat());
PlayerInventory inventory = session.getPlayerInventory();
GeyserItemStack item = inventory.getItemInHand();
ItemEntry itemEntry;
CompoundTag nbtData;
if (item != null) {
itemEntry = item.getItemEntry();
nbtData = item.getNbt();
} else {
itemEntry = null;
nbtData = new CompoundTag("");
}
double breakTime = Math.ceil(BlockUtils.getBreakTime(session, BlockTranslator.getBlockMapping(blockState), itemEntry, nbtData, true) * 20);
startBreak.setData((int) (65535 / breakTime));
session.setBreakingBlock(blockState);
session.sendUpstreamPacket(startBreak);
// Start the block breaking animation
if (session.getGameMode() != GameMode.CREATIVE) {
int blockState = session.getConnector().getWorldManager().getBlockAt(session, vector);
LevelEventPacket startBreak = new LevelEventPacket();
startBreak.setType(LevelEventType.BLOCK_START_BREAK);
startBreak.setPosition(vector.toFloat());
PlayerInventory inventory = session.getPlayerInventory();
GeyserItemStack item = inventory.getItemInHand();
ItemEntry itemEntry;
CompoundTag nbtData;
if (item != null) {
itemEntry = item.getItemEntry();
nbtData = item.getNbt();
} else {
itemEntry = null;
nbtData = new CompoundTag("");
}
double breakTime = Math.ceil(BlockUtils.getBreakTime(session, BlockTranslator.getBlockMapping(blockState), itemEntry, nbtData, true) * 20);
startBreak.setData((int) (65535 / breakTime));
session.setBreakingBlock(blockState);
session.sendUpstreamPacket(startBreak);
}

// Account for fire - the client likes to hit the block behind.
Vector3i fireBlockPos = BlockUtils.getBlockPosition(packet.getBlockPosition(), packet.getFace());
int blockUp = session.getConnector().getWorldManager().getBlockAt(session, fireBlockPos);
String identifier = BlockTranslator.getJavaIdBlockMap().inverse().get(blockUp);
if (identifier.startsWith("minecraft:fire") || identifier.startsWith("minecraft:soul_fire")) {
ClientPlayerActionPacket startBreakingPacket = new ClientPlayerActionPacket(PlayerAction.START_DIGGING, new Position(fireBlockPos.getX(),
fireBlockPos.getY(), fireBlockPos.getZ()), BlockFace.values()[packet.getFace()]);
session.sendDownstreamPacket(startBreakingPacket);
if (session.getGameMode() == GameMode.CREATIVE) {
break;
}
// Account for fire - the client likes to hit the block behind.
Vector3i fireBlockPos = BlockUtils.getBlockPosition(packet.getBlockPosition(), packet.getFace());
int blockUp = session.getConnector().getWorldManager().getBlockAt(session, fireBlockPos);
String identifier = BlockTranslator.getJavaIdBlockMap().inverse().get(blockUp);
if (identifier.startsWith("minecraft:fire") || identifier.startsWith("minecraft:soul_fire")) {
ClientPlayerActionPacket startBreakingPacket = new ClientPlayerActionPacket(PlayerAction.START_DIGGING, new Position(fireBlockPos.getX(),
fireBlockPos.getY(), fireBlockPos.getZ()), BlockFace.values()[packet.getFace()]);
session.sendDownstreamPacket(startBreakingPacket);
if (session.getGameMode() == GameMode.CREATIVE) {
break;
}
}

ClientPlayerActionPacket startBreakingPacket = new ClientPlayerActionPacket(PlayerAction.START_DIGGING, position, BlockFace.values()[packet.getFace()]);
session.sendDownstreamPacket(startBreakingPacket);
break;
Expand Down Expand Up @@ -246,11 +243,7 @@ public void translate(PlayerActionPacket packet, GeyserSession session) {
session.getEntityCache().updateBossBars();
break;
case JUMP:
if (!session.getConnector().getConfig().isCacheChunks()) {
// Save the jumping status for determining teleport status
session.setJumping(true);
session.getConnector().getGeneralThreadPool().schedule(() -> session.setJumping(false), 1, TimeUnit.SECONDS);
}
// Leaving as a potential placeholder for an event or soul sand fixing
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,32 +141,22 @@ public Vector3d adjustBedrockPosition(Vector3f bedrockPosition, boolean onGround
Vector3d position = Vector3d.from(Double.parseDouble(Float.toString(bedrockPosition.getX())), javaY,
Double.parseDouble(Float.toString(bedrockPosition.getZ())));

if (session.getConnector().getConfig().isCacheChunks()) {
// With chunk caching, we can do some proper collision checks
updatePlayerBoundingBox(position);

// Correct player position
if (!correctPlayerPosition()) {
// Cancel the movement if it needs to be cancelled
recalculatePosition();
return null;
}
updatePlayerBoundingBox(position);

position = Vector3d.from(playerBoundingBox.getMiddleX(),
playerBoundingBox.getMiddleY() - (playerBoundingBox.getSizeY() / 2),
playerBoundingBox.getMiddleZ());
// Correct player position
if (!correctPlayerPosition()) {
// Cancel the movement if it needs to be cancelled
recalculatePosition();
return null;
}

if (!onGround) {
// Trim the position to prevent rounding errors that make Java think we are clipping into a block
position = Vector3d.from(position.getX(), Double.parseDouble(DECIMAL_FORMAT.format(position.getY())), position.getZ());
}
} else {
// When chunk caching is off, we have to rely on this
// It rounds the Y position up to the nearest 0.5
// This snaps players to snap to the top of stairs and slabs like on Java Edition
// However, it causes issues such as the player floating on carpets
if (onGround) javaY = Math.ceil(javaY * 2) / 2;
position = position.up(javaY - position.getY());
position = Vector3d.from(playerBoundingBox.getMiddleX(),
playerBoundingBox.getMiddleY() - (playerBoundingBox.getSizeY() / 2),
playerBoundingBox.getMiddleZ());

if (!onGround) {
// Trim the position to prevent rounding errors that make Java think we are clipping into a block
position = Vector3d.from(position.getX(), Double.parseDouble(DECIMAL_FORMAT.format(position.getY())), position.getZ());
}

return position;
Expand Down Expand Up @@ -268,10 +258,6 @@ public boolean correctPlayerPosition() {
* were they not sneaking
*/
public boolean isUnderSlab() {
if (!session.getConnector().getConfig().isCacheChunks()) {
// We can't reliably determine this
return false;
}
Vector3i position = session.getPlayerEntity().getPosition().toInt();
BlockCollision collision = CollisionTranslator.getCollisionAt(session, position.getX(), position.getY(), position.getZ());
if (collision != null) {
Expand All @@ -289,8 +275,7 @@ public boolean isUnderSlab() {
* @return if the player is currently in a water block
*/
public boolean isPlayerInWater() {
return session.getConnector().getConfig().isCacheChunks()
&& session.getConnector().getWorldManager().getBlockAt(session, session.getPlayerEntity().getPosition().toInt()) == BlockTranslator.JAVA_WATER_ID;
return session.getConnector().getWorldManager().getBlockAt(session, session.getPlayerEntity().getPosition().toInt()) == BlockTranslator.JAVA_WATER_ID;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ public class CollisionTranslator {
private static final Int2ObjectMap<BlockCollision> COLLISION_MAP = new Int2ObjectOpenHashMap<>();

public static void init() {
// If chunk caching is off then don't initialize
if (!GeyserConnector.getInstance().getConfig().isCacheChunks()) {
return;
}

List<Class<?>> collisionTypes = new ArrayList<>();

Map<Class<?>, CollisionRemapper> annotationMap = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,6 @@
public class BeaconInventoryTranslator extends AbstractBlockInventoryTranslator {
public BeaconInventoryTranslator() {
super(1, new BlockInventoryHolder("minecraft:beacon", ContainerType.BEACON) {
@Override
public void prepareInventory(InventoryTranslator translator, GeyserSession session, Inventory inventory) {
if (!session.getConnector().getConfig().isCacheChunks()) {
// Beacons cannot work without knowing their physical location
return;
}
super.prepareInventory(translator, session, inventory);
}

@Override
protected boolean checkInteractionPosition(GeyserSession session) {
// Since we can't fall back to a virtual inventory, let's make opening one easier
Expand All @@ -71,7 +62,7 @@ protected boolean checkInteractionPosition(GeyserSession session) {

@Override
public void openInventory(InventoryTranslator translator, GeyserSession session, Inventory inventory) {
if (!session.getConnector().getConfig().isCacheChunks() || !((BeaconContainer) inventory).isUsingRealBlock()) {
if (!((BeaconContainer) inventory).isUsingRealBlock()) {
InventoryUtils.closeInventory(session, inventory.getId(), false);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ public void translate(ServerEntitySetPassengersPacket packet, GeyserSession sess
passenger = session.getPlayerEntity();
session.setRidingVehicleEntity(entity);
// We need to confirm teleports before entering a vehicle, or else we will likely exit right out
if (session.getConnector().getConfig().isCacheChunks()) {
session.confirmTeleport(passenger.getPosition().sub(0, EntityType.PLAYER.getOffset(), 0).toDouble());
}
session.confirmTeleport(passenger.getPosition().sub(0, EntityType.PLAYER.getOffset(), 0).toDouble());
}
// Passenger hasn't loaded in (likely since we're waiting for a skin response)
// and entity link needs to be set later
Expand Down
Loading

0 comments on commit ebf726c

Please sign in to comment.