Skip to content

Commit

Permalink
Move some things around here and there
Browse files Browse the repository at this point in the history
  • Loading branch information
RealTriassic committed Oct 9, 2024
1 parent f498c48 commit 08ffeed
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 108 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.triassic.geyserdebuginfo.placeholder.placeholders;

import com.triassic.geyserdebuginfo.placeholder.PlaceholderProvider;
import com.triassic.geyserdebuginfo.util.ChunkUtil;
import org.cloudburstmc.math.vector.Vector3f;
import org.geysermc.geyser.entity.EntityDefinitions;
import org.geysermc.geyser.entity.type.player.SessionPlayerEntity;
Expand All @@ -10,6 +9,10 @@

public class PlayerPlaceholderProvider extends PlaceholderProvider {

private static final int CHUNK_SIZE = 16;
private static final int REGION_SIZE = 32;
private static final int CHUNK_MASK = REGION_SIZE - 1;

@Override
public String getIdentifier() {
return "player";
Expand All @@ -19,7 +22,7 @@ public String getIdentifier() {
public String onRequest(final GeyserSession session, @NotNull final String params) {
final SessionPlayerEntity player = session.getPlayerEntity();
final Vector3f pos = adjustForPlayerOffset(player.getPosition());
int[] relativeChunkCoords = ChunkUtil.getRelativeCoordinates(pos);
int[] relativeChunkCoords = getRelativeCoordinates(pos);

return switch (params) {
case "x" -> String.format("%.3f", pos.getX());
Expand All @@ -30,14 +33,14 @@ public String onRequest(final GeyserSession session, @NotNull final String param
case "facing" -> getFacingDirection(player.getYaw());

case "chunk_x" -> String.valueOf(session.getLastChunkPosition().getX());
case "chunk_y" -> String.valueOf(ChunkUtil.calculateChunkY(pos.getFloorY()));
case "chunk_y" -> String.valueOf(calculateChunkY(pos.getFloorY()));
case "chunk_z" -> String.valueOf(session.getLastChunkPosition().getY());
case "relative_x" -> String.valueOf(relativeChunkCoords[0]);
case "relative_y" -> String.valueOf(relativeChunkCoords[1]);
case "relative_z" -> String.valueOf(relativeChunkCoords[2]);
case "global_x" -> String.valueOf(ChunkUtil.getRelativeChunkCoordinates(pos.getX(), pos.getZ())[0]);
case "global_z" -> String.valueOf(ChunkUtil.getRelativeChunkCoordinates(pos.getX(), pos.getZ())[1]);
case "region_file" -> ChunkUtil.getRegionFileName(pos.getX(), pos.getZ());
case "global_x" -> String.valueOf(getRelativeChunkCoordinates(pos.getX(), pos.getZ())[0]);
case "global_z" -> String.valueOf(getRelativeChunkCoordinates(pos.getX(), pos.getZ())[1]);
case "region_file" -> getRegionFileName(pos.getX(), pos.getZ());

case "allow_flight" -> String.valueOf(session.isCanFly());
case "gamemode" -> String.valueOf(session.getGameMode());
Expand Down Expand Up @@ -69,7 +72,7 @@ public String onRequest(final GeyserSession session, @NotNull final String param
* @param position the original position of the player as a Vector3f
* @return a new Vector3f with the y-coordinate adjusted by subtracting the PLAYER_OFFSET
*/
public static Vector3f adjustForPlayerOffset(Vector3f position) {
private static Vector3f adjustForPlayerOffset(Vector3f position) {
return Vector3f.from(position.getX(), position.getY() - EntityDefinitions.PLAYER.offset(), position.getZ()); // TODO: https://github.com/GeyserMC/Geyser/issues/5061.
}

Expand All @@ -95,4 +98,92 @@ private static String getFacingDirection(float yaw) {
return "south (Towards positive Z)";
}
}

/**
* Calculates the chunk Y coordinate based on the absolute Y position.
*
* @param absoluteY The absolute Y coordinate.
* @return The chunk Y coordinate.
*/
private static int calculateChunkY(int absoluteY) {
return (absoluteY < 0) ? (absoluteY - 15) / 16 : absoluteY / 16;
}

/**
* Calculates the global chunk coordinates from player position.
*
* @param playerX The player's X coordinate.
* @param playerZ The player's Z coordinate.
* @return An array containing the global chunk coordinates: [chunkX, chunkZ].
*/
private static int[] getGlobalChunkCoordinates(float playerX, float playerZ) {
int chunkX = (int) Math.floor(playerX / CHUNK_SIZE);
int chunkZ = (int) Math.floor(playerZ / CHUNK_SIZE);
return new int[]{chunkX, chunkZ};
}

/**
* Calculates the relative chunk coordinates within its region from player position.
*
* @param playerX The player's X coordinate.
* @param playerZ The player's Z coordinate.
* @return An array containing the relative chunk coordinates: [relativeChunkX, relativeChunkZ].
*/
private static int[] getRelativeChunkCoordinates(float playerX, float playerZ) {
int[] globalCoords = getGlobalChunkCoordinates(playerX, playerZ);
return getRelativeChunkCoordinates(globalCoords[0], globalCoords[1]);
}

/**
* Calculates the relative chunk coordinates within its region from global chunk coordinates.
*
* @param chunkX The global chunk X coordinate.
* @param chunkZ The global chunk Z coordinate.
* @return An array containing the relative chunk coordinates: [relativeChunkX, relativeChunkZ].
*/
private static int[] getRelativeChunkCoordinates(int chunkX, int chunkZ) {
int relativeChunkX = chunkX & CHUNK_MASK;
int relativeChunkZ = chunkZ & CHUNK_MASK;
return new int[]{relativeChunkX, relativeChunkZ};
}

/**
* Calculates the region file name based on the player's position.
*
* @param playerX The player's X coordinate.
* @param playerZ The player's Z coordinate.
* @return The name of the region file corresponding to the player's position.
*/
private static String getRegionFileName(float playerX, float playerZ) {
int[] globalCoords = getGlobalChunkCoordinates(playerX, playerZ);
int regionX = globalCoords[0] >> 5;
int regionZ = globalCoords[1] >> 5;

return String.format("r.%d.%d.mca", regionX, regionZ);
}

/**
* Calculates the relative chunk coordinates (within a chunk) for a given absolute Vector3f coordinate.
* Each component of the Vector3f is normalized to a range of [0, 15].
*
* @param absolutePosition The absolute coordinates as a Vector3f.
* @return An array of integers containing the relative chunk coordinates [relativeX, relativeY, relativeZ].
*/
private static int[] getRelativeCoordinates(Vector3f absolutePosition) {
return normalizeToChunkCoordinate(absolutePosition);
}

/**
* Normalizes the absolute coordinates of a Vector3f to a range of [0, 15].
*
* @param absolutePosition The absolute coordinates as a Vector3f.
* @return An array of integers containing the normalized coordinates [normalizedX, normalizedY, normalizedZ].
*/
private static int[] normalizeToChunkCoordinate(Vector3f absolutePosition) {
return new int[]{
Math.floorMod(absolutePosition.getFloorX(), CHUNK_SIZE),
Math.floorMod(absolutePosition.getFloorY(), CHUNK_SIZE),
Math.floorMod(absolutePosition.getFloorZ(), CHUNK_SIZE)
};
}
}
101 changes: 0 additions & 101 deletions src/main/java/com/triassic/geyserdebuginfo/util/ChunkUtil.java

This file was deleted.

19 changes: 19 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# GeyserDebugInfo Configuration File
# A Geyser extension that strives to provide F3-like debug information for Bedrock Edition players.
#
# Report any issues on our GitHub repository:
# https://github.com/RealTriassic/GeyserDebugInfo

display:
bossbar:
text:
- "Geyser Debug Information"
- ""
- "%player_world%"
- ""
- "XYZ: %player_x% / %player_y% / %player_z%"
- "Block: %player_x:floor% %player_y:floor% %player_z:floor% [%player_relative_x% %player_relative_y% %player_relative_z%]"
- "Chunk: %player_chunk_x% %player_chunk_y% %player_chunk_z% [%player_global_x% %player_global_z% in %player_region_file%]"
- "Facing: %player_facing% (%player_yaw% / %player_pitch%)"

config-version: 1

0 comments on commit 08ffeed

Please sign in to comment.