From a456337d3eb8321eb592be286e143eb73b32e5f5 Mon Sep 17 00:00:00 2001 From: Triassic Date: Sun, 6 Oct 2024 18:57:32 +0300 Subject: [PATCH] Rename placeholder providers, add more placeholders --- .../geyserdebuginfo/GeyserDebugInfo.java | 10 +-- .../geyserdebuginfo/config/Configuration.java | 10 +-- .../ChunkPlaceholderProvider.java | 35 -------- .../PlayerPlaceholderProvider.java | 89 +++++++++++++++++++ .../PositionPlaceholderProvider.java | 55 ------------ .../ServerPlaceholderProvider.java | 45 ++++++++++ .../SessionPlaceholderProvider.java | 24 ----- 7 files changed, 143 insertions(+), 125 deletions(-) delete mode 100644 src/main/java/com/triassic/geyserdebuginfo/placeholder/placeholders/ChunkPlaceholderProvider.java create mode 100644 src/main/java/com/triassic/geyserdebuginfo/placeholder/placeholders/PlayerPlaceholderProvider.java delete mode 100644 src/main/java/com/triassic/geyserdebuginfo/placeholder/placeholders/PositionPlaceholderProvider.java create mode 100644 src/main/java/com/triassic/geyserdebuginfo/placeholder/placeholders/ServerPlaceholderProvider.java delete mode 100644 src/main/java/com/triassic/geyserdebuginfo/placeholder/placeholders/SessionPlaceholderProvider.java diff --git a/src/main/java/com/triassic/geyserdebuginfo/GeyserDebugInfo.java b/src/main/java/com/triassic/geyserdebuginfo/GeyserDebugInfo.java index f195527..731cacd 100644 --- a/src/main/java/com/triassic/geyserdebuginfo/GeyserDebugInfo.java +++ b/src/main/java/com/triassic/geyserdebuginfo/GeyserDebugInfo.java @@ -2,6 +2,7 @@ import com.triassic.geyserdebuginfo.placeholder.modifiers.MathModifierProvider; import com.triassic.geyserdebuginfo.placeholder.modifiers.TextModifierProvider; +import com.triassic.geyserdebuginfo.placeholder.placeholders.ServerPlaceholderProvider; import org.geysermc.event.subscribe.Subscribe; import org.geysermc.geyser.api.event.lifecycle.GeyserDefineCommandsEvent; import org.geysermc.geyser.api.event.lifecycle.GeyserPreInitializeEvent; @@ -15,9 +16,7 @@ import com.triassic.geyserdebuginfo.manager.BossBarManager; import com.triassic.geyserdebuginfo.manager.PlaceholderManager; import com.triassic.geyserdebuginfo.manager.PlayerDataManager; -import com.triassic.geyserdebuginfo.placeholder.placeholders.ChunkPlaceholderProvider; -import com.triassic.geyserdebuginfo.placeholder.placeholders.PositionPlaceholderProvider; -import com.triassic.geyserdebuginfo.placeholder.placeholders.SessionPlaceholderProvider; +import com.triassic.geyserdebuginfo.placeholder.placeholders.PlayerPlaceholderProvider; import java.io.File; import java.util.stream.Stream; @@ -50,9 +49,8 @@ public void onPostInitialize(GeyserPreInitializeEvent event) { this.eventBus().register(new PlayerJoinListener(this)); Stream.of( - new ChunkPlaceholderProvider(), - new PositionPlaceholderProvider(), - new SessionPlaceholderProvider() + new PlayerPlaceholderProvider(), + new ServerPlaceholderProvider() ).forEach(placeholderManager::registerProvider); Stream.of( diff --git a/src/main/java/com/triassic/geyserdebuginfo/config/Configuration.java b/src/main/java/com/triassic/geyserdebuginfo/config/Configuration.java index 5e549d9..15feae1 100644 --- a/src/main/java/com/triassic/geyserdebuginfo/config/Configuration.java +++ b/src/main/java/com/triassic/geyserdebuginfo/config/Configuration.java @@ -21,12 +21,12 @@ public static class BossBarSettings { private List displayFormat = Arrays.asList( "Geyser Debug Information", "", - "%session_world%", + "%player_world%", "", - "XYZ: %position_x% / %position_y% / %position_z%", - "Block: %position_x:floor% %position_y:floor% %position_z:floor% [%chunk_relative_x% %chunk_relative_y% %chunk_relative_z%]", - "Chunk: %chunk_x% %chunk_y% %chunk_z% [%chunk_global_x% %chunk_global_z% in %chunk_region_file%]", - "Facing: %position_facing% (%position_yaw% / %position_pitch%)" + "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%)" ); public long getRefreshInterval() { diff --git a/src/main/java/com/triassic/geyserdebuginfo/placeholder/placeholders/ChunkPlaceholderProvider.java b/src/main/java/com/triassic/geyserdebuginfo/placeholder/placeholders/ChunkPlaceholderProvider.java deleted file mode 100644 index 326db05..0000000 --- a/src/main/java/com/triassic/geyserdebuginfo/placeholder/placeholders/ChunkPlaceholderProvider.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.triassic.geyserdebuginfo.placeholder.placeholders; - -import com.triassic.geyserdebuginfo.placeholder.PlaceholderProvider; -import com.triassic.geyserdebuginfo.util.ChunkUtil; -import com.triassic.geyserdebuginfo.util.PositionUtil; -import org.cloudburstmc.math.vector.Vector3f; -import org.geysermc.geyser.session.GeyserSession; -import org.jetbrains.annotations.NotNull; - -public class ChunkPlaceholderProvider extends PlaceholderProvider { - - @Override - public String getIdentifier() { - return "chunk"; - } - - @Override - public String onRequest(final GeyserSession session, @NotNull final String params) { - Vector3f pos = PositionUtil.adjustForPlayerOffset(session.getPlayerEntity().getPosition()); - int[] relativeChunkCoords = ChunkUtil.getRelativeCoordinates(pos); - - return switch (params) { - case "x" -> String.valueOf(session.getLastChunkPosition().getX()); - case "y" -> String.valueOf(ChunkUtil.calculateChunkY(pos.getFloorY())); - case "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()); - default -> null; - }; - } -} diff --git a/src/main/java/com/triassic/geyserdebuginfo/placeholder/placeholders/PlayerPlaceholderProvider.java b/src/main/java/com/triassic/geyserdebuginfo/placeholder/placeholders/PlayerPlaceholderProvider.java new file mode 100644 index 0000000..f3e5c5d --- /dev/null +++ b/src/main/java/com/triassic/geyserdebuginfo/placeholder/placeholders/PlayerPlaceholderProvider.java @@ -0,0 +1,89 @@ +package com.triassic.geyserdebuginfo.placeholder.placeholders; + +import com.triassic.geyserdebuginfo.placeholder.PlaceholderProvider; +import com.triassic.geyserdebuginfo.util.ChunkUtil; +import com.triassic.geyserdebuginfo.util.PositionUtil; +import org.cloudburstmc.math.vector.Vector3f; +import org.cloudburstmc.math.vector.Vector3i; +import org.geysermc.geyser.entity.type.player.SessionPlayerEntity; +import org.geysermc.geyser.session.GeyserSession; +import org.jetbrains.annotations.NotNull; + +public class PlayerPlaceholderProvider extends PlaceholderProvider { + + /** + * Determines the player's facing direction based on the yaw value. + * Yaw is normalized to a range of [0, 360) and mapped to cardinal directions. + * + * @param yaw The yaw value of the player. + * @return The direction the player is facing as a string. + */ + private static String getFacingDirection(float yaw) { + // Normalize yaw to a range of [0, 360) + yaw = (yaw % 360 + 360) % 360; + + // Determine direction based on yaw + if (yaw >= 45 && yaw < 135) { + return "west (Towards negative X)"; + } else if (yaw >= 135 && yaw < 225) { + return "north (Towards negative Z)"; + } else if (yaw >= 225 && yaw < 315) { + return "east (Towards positive X)"; + } else { + return "south (Towards positive Z)"; + } + } + + @Override + public String getIdentifier() { + return "player"; + } + + @Override + public String onRequest(final GeyserSession session, @NotNull final String params) { + final SessionPlayerEntity player = session.getPlayerEntity(); + final Vector3f pos = PositionUtil.adjustForPlayerOffset(player.getPosition()); + final Vector3i bedPos = player.getBedPosition(); + int[] relativeChunkCoords = ChunkUtil.getRelativeCoordinates(pos); + + return switch (params) { + case "x" -> String.format("%.3f", pos.getX()); + case "y" -> String.format("%.3f", pos.getY()); + case "z" -> String.format("%.3f", pos.getZ()); + case "yaw" -> String.format("%.1f", player.getYaw()); + case "pitch" -> String.format("%.1f", player.getPitch()); + case "facing" -> getFacingDirection(player.getYaw()); + + case "chunk_x" -> String.valueOf(session.getLastChunkPosition().getX()); + case "chunk_y" -> String.valueOf(ChunkUtil.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 "allow_flight" -> String.valueOf(session.isCanFly()); + case "gamemode" -> String.valueOf(session.getGameMode()); + case "uuid" -> String.valueOf(player.getUuid()); + case "locale" -> session.locale(); + case "version" -> session.version(); + case "ip" -> String.valueOf(session.getSocketAddress()); + + case "health" -> String.valueOf(player.getHealth()); + case "max_health" -> String.valueOf(player.getMaxHealth()); + + case "world" -> String.valueOf(session.getWorldName()); + case "difficulty" -> String.valueOf(session.getWorldCache().getDifficulty()); + case "time" -> String.valueOf(session.getTicks()); + case "view_distance" -> String.valueOf(session.getClientRenderDistance()); + + case "name" -> session.bedrockUsername(); + case "ping" -> String.valueOf(session.ping()); + case "platform" -> session.platform().toString(); + + default -> null; + }; + } +} diff --git a/src/main/java/com/triassic/geyserdebuginfo/placeholder/placeholders/PositionPlaceholderProvider.java b/src/main/java/com/triassic/geyserdebuginfo/placeholder/placeholders/PositionPlaceholderProvider.java deleted file mode 100644 index 9718189..0000000 --- a/src/main/java/com/triassic/geyserdebuginfo/placeholder/placeholders/PositionPlaceholderProvider.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.triassic.geyserdebuginfo.placeholder.placeholders; - -import com.triassic.geyserdebuginfo.placeholder.PlaceholderProvider; -import com.triassic.geyserdebuginfo.util.PositionUtil; -import org.cloudburstmc.math.vector.Vector3f; -import org.geysermc.geyser.entity.type.player.SessionPlayerEntity; -import org.geysermc.geyser.session.GeyserSession; -import org.jetbrains.annotations.NotNull; - -public class PositionPlaceholderProvider extends PlaceholderProvider { - - @Override - public String getIdentifier() { - return "position"; - } - - @Override - public String onRequest(final GeyserSession session, @NotNull final String params) { - SessionPlayerEntity player = session.getPlayerEntity(); - Vector3f pos = PositionUtil.adjustForPlayerOffset(player.getPosition()); - - return switch (params) { - case "x" -> String.format("%.3f", pos.getX()); - case "y" -> String.format("%.3f", pos.getY()); - case "z" -> String.format("%.3f", pos.getZ()); - case "facing" -> getFacingDirection(player.getYaw()); - case "yaw" -> String.format("%.1f", player.getYaw()); - case "pitch" -> String.format("%.1f", player.getPitch()); - default -> null; - }; - } - - /** - * Determines the player's facing direction based on the yaw value. - * Yaw is normalized to a range of [0, 360) and mapped to cardinal directions. - * - * @param yaw The yaw value of the player. - * @return The direction the player is facing as a string. - */ - private static String getFacingDirection(float yaw) { - // Normalize yaw to a range of [0, 360) - yaw = (yaw % 360 + 360) % 360; - - // Determine direction based on yaw - if (yaw >= 45 && yaw < 135) { - return "west (Towards negative X)"; - } else if (yaw >= 135 && yaw < 225) { - return "north (Towards negative Z)"; - } else if (yaw >= 225 && yaw < 315) { - return "east (Towards positive X)"; - } else { - return "south (Towards positive Z)"; - } - } -} diff --git a/src/main/java/com/triassic/geyserdebuginfo/placeholder/placeholders/ServerPlaceholderProvider.java b/src/main/java/com/triassic/geyserdebuginfo/placeholder/placeholders/ServerPlaceholderProvider.java new file mode 100644 index 0000000..0b89f5e --- /dev/null +++ b/src/main/java/com/triassic/geyserdebuginfo/placeholder/placeholders/ServerPlaceholderProvider.java @@ -0,0 +1,45 @@ +package com.triassic.geyserdebuginfo.placeholder.placeholders; + +import com.triassic.geyserdebuginfo.placeholder.PlaceholderProvider; +import org.geysermc.geyser.GeyserImpl; +import org.geysermc.geyser.session.GeyserSession; +import org.jetbrains.annotations.NotNull; + +public class ServerPlaceholderProvider extends PlaceholderProvider { + + private final Runtime runtime = Runtime.getRuntime(); + + @Override + public String getIdentifier() { + return "server"; + } + + @Override + public String onRequest(final GeyserSession session, @NotNull final String params) { + final GeyserImpl geyser = session.getGeyser(); + final int i = 1024 * 1024; // Mebibytes conversion factor (1 MiB = 1024 * 1024 bytes) + + return switch (params) { + case "name" -> geyser.bedrockListener().serverName(); + case "address" -> geyser.bedrockListener().address(); + case "port" -> String.valueOf(geyser.bedrockListener().port()); + case "version" -> GeyserImpl.VERSION; + case "build" -> GeyserImpl.BUILD_NUMBER; + case "platform" -> geyser.getPlatformType().platformName(); + case "online" -> String.valueOf(geyser.onlineConnectionsCount()); + case "view_distance" -> String.valueOf(session.getServerRenderDistance()); + + case "remote_address" -> geyser.defaultRemoteServer().address(); + case "remote_port" -> String.valueOf(geyser.defaultRemoteServer().port()); + case "remote_version" -> geyser.defaultRemoteServer().minecraftVersion(); + case "remote_protocol" -> String.valueOf(geyser.defaultRemoteServer().protocolVersion()); + case "remote_auth_type" -> geyser.defaultRemoteServer().authType().toString(); + + case "ram_used" -> String.valueOf((runtime.totalMemory() - runtime.freeMemory()) / i); + case "ram_free" -> String.valueOf(runtime.freeMemory() / i); + case "ram_total" -> String.valueOf(runtime.totalMemory() / i); + case "ram_max" -> String.valueOf(runtime.maxMemory() / i); + default -> null; + }; + } +} diff --git a/src/main/java/com/triassic/geyserdebuginfo/placeholder/placeholders/SessionPlaceholderProvider.java b/src/main/java/com/triassic/geyserdebuginfo/placeholder/placeholders/SessionPlaceholderProvider.java deleted file mode 100644 index dd2271a..0000000 --- a/src/main/java/com/triassic/geyserdebuginfo/placeholder/placeholders/SessionPlaceholderProvider.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.triassic.geyserdebuginfo.placeholder.placeholders; - -import com.triassic.geyserdebuginfo.placeholder.PlaceholderProvider; -import org.geysermc.geyser.session.GeyserSession; -import org.jetbrains.annotations.NotNull; - -public class SessionPlaceholderProvider extends PlaceholderProvider { - - @Override - public String getIdentifier() { - return "session"; - } - - @Override - public String onRequest(final GeyserSession session, @NotNull final String params) { - return switch (params) { - case "world" -> String.valueOf(session.getWorldName()); - case "gamemode" -> String.valueOf(session.getGameMode()); - case "difficulty" -> String.valueOf(session.getWorldCache().getDifficulty()); - case "time" -> String.valueOf(session.getTicks()); - default -> null; - }; - } -}