Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
plugins {
id("java-library")
id("com.github.johnrengelman.shadow") version "8.1.1"
id("org.allaymc.gradle.plugin") version "0.1.2"
}

group = "org.allaymc.serverinfo"
description = "Show some information through scoreboard"
version = "1.4.0"
description = "Show some information through scoreboard"

java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}
allay {
api = "0.17.0"
apiOnly = true

repositories {
mavenCentral()
maven("https://jitpack.io/")
maven("https://repo.opencollab.dev/maven-releases/")
maven("https://repo.opencollab.dev/maven-snapshots/")
maven("https://storehouse.okaeri.eu/repository/maven-public/")
plugin {
entrance = ".ServerInfo"
apiVersion = ">=0.17.0"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line can be removed

authors += "daoge_cmd"
}
}

dependencies {
compileOnly(group = "org.allaymc.allay", name = "api", version = "0.6.0")
compileOnly(group = "org.projectlombok", name = "lombok", version = "1.18.34")

annotationProcessor(group = "org.projectlombok", name = "lombok", version = "1.18.34")
}
105 changes: 54 additions & 51 deletions src/main/java/org/allaymc/serverinfo/ServerInfo.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
package org.allaymc.serverinfo;

import eu.okaeri.configs.ConfigManager;
import eu.okaeri.configs.yaml.snakeyaml.YamlSnakeYamlConfigurer;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.allaymc.api.bossbar.BossBar;
import org.allaymc.api.bossbar.BossBarColor;
import org.allaymc.api.entity.component.attribute.AttributeType;
import org.allaymc.api.entity.component.EntityPlayerBaseComponent;
import org.allaymc.api.entity.interfaces.EntityPlayer;
import org.allaymc.api.eventbus.EventHandler;
import org.allaymc.api.eventbus.event.entity.EntityTeleportEvent;
import org.allaymc.api.eventbus.event.player.PlayerJoinEvent;
import org.allaymc.api.eventbus.event.player.PlayerQuitEvent;
import org.allaymc.api.eventbus.event.server.PlayerJoinEvent;
import org.allaymc.api.eventbus.event.server.PlayerQuitEvent;
import org.allaymc.api.math.MathUtils;
import org.allaymc.api.player.Player;
import org.allaymc.api.plugin.Plugin;
import org.allaymc.api.registry.Registries;
import org.allaymc.api.scoreboard.Scoreboard;
import org.allaymc.api.scoreboard.data.DisplaySlot;
import org.allaymc.api.server.Server;
import org.allaymc.api.math.MathUtils;
import org.allaymc.api.utils.config.Config;
import org.allaymc.api.world.World;
import org.joml.Vector3d;

import java.util.*;

@Slf4j
@Getter
public final class ServerInfo extends Plugin {

Expand All @@ -36,22 +34,24 @@ public final class ServerInfo extends Plugin {
@Override
public void onLoad() {
INSTANCE = this;
log.info("ServerInfo loaded!");
SETTINGS = ConfigManager.create(Settings.class, config -> {
config.withConfigurer(new YamlSnakeYamlConfigurer());
config.withBindFile(pluginContainer.dataFolder().resolve("config.yml"));
config.withRemoveOrphans(true);
config.saveDefaults();
config.load(true);
});
getPluginLogger().info("ServerInfo loaded!");
var configFile = pluginContainer.dataFolder().resolve("config.yml").toFile();
var config = new Config(configFile, Config.YAML);
SETTINGS = new Settings();
SETTINGS.showWorldInfo = config.getBoolean("show-world-info", true);
SETTINGS.showPlayerInfo = config.getBoolean("show-player-info", true);
SETTINGS.showChunkInfo = config.getBoolean("show-chunk-info", true);
SETTINGS.showLightInfo = config.getBoolean("show-light-info", true);
SETTINGS.showMiscInfo = config.getBoolean("show-misc-info", true);
SETTINGS.showMSPTBar = config.getBoolean("show-mspt-bar", true);
}

@Override
public void onEnable() {
log.info("ServerInfo enabled!");
getPluginLogger().info("ServerInfo enabled!");
Server.getInstance().getEventBus().registerListener(this);
Registries.COMMANDS.register(new ServerInfoCommand());
if (SETTINGS.showMSPTBar()) {
if (SETTINGS.showMSPTBar) {
Server.getInstance().getScheduler().scheduleRepeating(this, () -> {
updateMSPTBars();
return true;
Expand All @@ -61,7 +61,7 @@ public void onEnable() {

@Override
public void onDisable() {
log.info("ServerInfo disabled!");
getPluginLogger().info("ServerInfo disabled!");
Server.getInstance().getEventBus().unregisterListener(this);
}

Expand All @@ -84,9 +84,10 @@ public void setScoreboardDisabled(EntityPlayer player, boolean disabled) {
@EventHandler
private void onPlayerJoin(PlayerJoinEvent event) {
var player = event.getPlayer();
var entity = player.getControlledEntity();

if (SETTINGS.showMSPTBar()) {
getOrCreateWorldMSPTBar(player.getWorld()).addViewer(player);
if (SETTINGS.showMSPTBar) {
getOrCreateWorldMSPTBar(entity.getWorld()).addViewer(player);
}

var scoreboard = new Scoreboard("Dashboard");
Expand All @@ -96,9 +97,9 @@ private void onPlayerJoin(PlayerJoinEvent event) {
if (player.isDisconnected()) {
return false;
}
if (!isScoreboardDisabled(player)) {
if (!isScoreboardDisabled(entity)) {
scoreboard.addViewer(player, DisplaySlot.SIDEBAR);
updateScoreboard(player, scoreboard);
updateScoreboard(entity, scoreboard);
} else {
scoreboard.removeViewer(player, DisplaySlot.SIDEBAR);
}
Expand All @@ -109,10 +110,11 @@ private void onPlayerJoin(PlayerJoinEvent event) {
@EventHandler
private void onPlayerQuit(PlayerQuitEvent event) {
var player = event.getPlayer();
if (SETTINGS.showMSPTBar()) {
getOrCreateWorldMSPTBar(player.getWorld()).removeViewer(player);
var entity = player.getControlledEntity();
if (SETTINGS.showMSPTBar) {
getOrCreateWorldMSPTBar(entity.getWorld()).removeViewer(player);
}
SCOREBOARD_DISABLED.remove(player);
SCOREBOARD_DISABLED.remove(entity);
}

@EventHandler
Expand All @@ -125,8 +127,11 @@ private void onPlayerTeleport(EntityTeleportEvent event) {
return;
}

getOrCreateWorldMSPTBar(event.getFrom().dimension().getWorld()).removeViewer(player);
getOrCreateWorldMSPTBar(event.getTo().dimension().getWorld()).addViewer(player);
var controller = player.getController();
if (controller != null) {
getOrCreateWorldMSPTBar(event.getFrom().dimension().getWorld()).removeViewer(controller);
getOrCreateWorldMSPTBar(event.getTo().dimension().getWorld()).addViewer(controller);
}
}

private void updateMSPTBars() {
Expand All @@ -151,17 +156,17 @@ private void updateScoreboard(EntityPlayer player, Scoreboard scoreboard) {
if (!player.isInWorld()) return;

var lines = new ArrayList<String>();
var controller = player.getController();

if (SETTINGS.showWorldInfo()) {
// World info
if (SETTINGS.showWorldInfo) {
var worldInfo = "World: §a" + player.getWorld().getWorldData().getDisplayName() + "\n§f" +
"Time: §a" + player.getWorld().getWorldData().getTimeOfDay() + "\n§f" +
"TPS: §a" + MathUtils.round(player.getWorld().getTPS(), 2) + "\n§f" +
"MSPT: §a" + MathUtils.round(player.getWorld().getMSPT(), 2);
lines.add(worldInfo);
}

if (SETTINGS.showMiscInfo()) {
if (SETTINGS.showMiscInfo) {
var itemInHand = player.getItemInHand();

lines.add(
Expand All @@ -170,42 +175,40 @@ private void updateScoreboard(EntityPlayer player, Scoreboard scoreboard) {
);
}

if (SETTINGS.showChunkInfo()) {
if (SETTINGS.showChunkInfo) {
var chunk = player.getCurrentChunk();
var chunkManager = player.getDimension().getChunkManager();
var chunkInfo =
"Chunk: §a" + chunk.getX() + "," + chunk.getZ() + "\n§f" +
"Loaded: §a" + player.getDimension().getChunkService().getLoadedChunks().size() + "\n§f" +
"Loading: §a" + player.getDimension().getChunkService().getLoadingChunks().size() + "\n§f";
"Loaded: §a" + chunkManager.getLoadedChunks().size() + "\n§f";
try {
var floorLoc = player.getLocation().floor(new Vector3d());
chunkInfo += "Biome:\n§a" + player.getCurrentChunk().getBiome((int) floorLoc.x() & 15, (int) floorLoc.y(), (int) floorLoc.z() & 15).toString().toLowerCase();
} catch (IllegalArgumentException e) {
// y coordinate is out of range
chunkInfo += "Biome: §aN/A";
}
lines.add(chunkInfo);
}

if (SETTINGS.showPlayerInfo()) {
var playerInfo = "Ping: §a" + player.getPing() + "\n§f" +
"Food: §a" + player.getFoodLevel() + "/" + (int) AttributeType.PLAYER_HUNGER.getMaxValue() + "\n§f" +
"Exhaustion: §a" + MathUtils.round(player.getFoodExhaustionLevel(), 2) + "/" + (int) AttributeType.PLAYER_EXHAUSTION.getMaxValue() + "\n§f" +
"Saturation: §a" + MathUtils.round(player.getFoodSaturationLevel(), 2) + "/" + (int) AttributeType.PLAYER_SATURATION.getMaxValue() + "\n§f" +
"Exp: §a" + player.getExperienceInCurrentLevel() + "/" + player.getRequireExperienceForCurrentLevel();
if (SETTINGS.showPlayerInfo && controller != null) {
var playerInfo = "Ping: §a" + controller.getPing() + "\n§f" +
"Food: §a" + player.getFoodLevel() + "/" + EntityPlayerBaseComponent.MAX_FOOD_LEVEL + "\n§f" +
"Exhaustion: §a" + MathUtils.round(player.getFoodExhaustionLevel(), 2) + "/" + EntityPlayerBaseComponent.MAX_FOOD_EXHAUSTION_LEVEL + "\n§f" +
"Saturation: §a" + MathUtils.round(player.getFoodSaturationLevel(), 2) + "/" + EntityPlayerBaseComponent.MAX_FOOD_SATURATION_LEVEL + "\n§f" +
"Exp: §a" + player.getExperienceInCurrentLevel() + "/" + player.getRequiredExperienceForCurrentLevel();
lines.add(playerInfo);
}

if (SETTINGS.showLightInfo()) {
if (SETTINGS.showLightInfo) {
var floorLoc = player.getLocation().floor(new Vector3d());
int x = (int) floorLoc.x;
int y = (int) floorLoc.y;
int z = (int) floorLoc.z;
var lightService = player.getDimension().getLightService();
var lightInfo = "Itl: §a" + lightService.getInternalLight(x, y, z) + "\n§f" +
"Block: §a" + lightService.getBlockLight(x, y, z) + "\n§f" +
"Sky: §a" + lightService.getSkyLight(x, y, z) + "\n§f" +
"ItlSky: §a" + lightService.getInternalSkyLight(x, y, z) + "\n§f" +
"Queue: §a" + lightService.getQueuedUpdateCount();
int x = (int) floorLoc.x();
int y = (int) floorLoc.y();
int z = (int) floorLoc.z();
var lightEngine = player.getDimension().getLightEngine();
var lightInfo = "Itl: §a" + lightEngine.getInternalLight(x, y, z) + "\n§f" +
"Block: §a" + lightEngine.getBlockLight(x, y, z) + "\n§f" +
"Sky: §a" + lightEngine.getSkyLight(x, y, z) + "\n§f" +
"ItlSky: §a" + lightEngine.getInternalSkyLight(x, y, z);
lines.add(lightInfo);
}

Expand Down
27 changes: 15 additions & 12 deletions src/main/java/org/allaymc/serverinfo/ServerInfoCommand.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,43 @@
package org.allaymc.serverinfo;

import org.allaymc.api.command.Command;
import org.allaymc.api.command.SenderType;
import org.allaymc.api.command.SimpleCommand;
import org.allaymc.api.command.tree.CommandTree;

/**
* @author daoge_cmd
*/
public class ServerInfoCommand extends SimpleCommand {
public class ServerInfoCommand extends Command {
public ServerInfoCommand() {
super("serverinfo", "Control the server info");
super("serverinfo", "Control the server info", null);
}

@Override
public void prepareCommandTree(CommandTree tree) {
tree.getRoot()
.key("msptbar")
.bool("show")
.exec((context, player) -> {
.exec((context, entity) -> {
boolean show = context.getResult(1);
var bossBar = ServerInfo.INSTANCE.getOrCreateWorldMSPTBar(player.getWorld());
var controller = entity.getController();
if (controller == null) {
return context.fail();
}
var bossBar = ServerInfo.INSTANCE.getOrCreateWorldMSPTBar(entity.getWorld());
if (show) {
bossBar.addViewer(player);
bossBar.addViewer(controller);
} else {
bossBar.removeViewer(player);
bossBar.removeViewer(controller);
}
return context.success();
}, SenderType.PLAYER)
}, SenderType.ACTUAL_PLAYER)
.root()
.key("scoreboard")
.bool("show")
.exec((context, player) -> {
.exec((context, entity) -> {
boolean show = context.getResult(1);
ServerInfo.INSTANCE.setScoreboardDisabled(player, !show);
ServerInfo.INSTANCE.setScoreboardDisabled(entity, !show);
return context.success();
}, SenderType.PLAYER);

}, SenderType.ACTUAL_PLAYER);
}
}
32 changes: 9 additions & 23 deletions src/main/java/org/allaymc/serverinfo/Settings.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
package org.allaymc.serverinfo;

import eu.okaeri.configs.OkaeriConfig;
import eu.okaeri.configs.annotation.CustomKey;
import lombok.Getter;
import lombok.experimental.Accessors;
import lombok.Setter;

/**
* @author daoge_cmd
*/
@Getter
@Accessors(fluent = true)
public class Settings extends OkaeriConfig {

@CustomKey("show-world-info")
private boolean showWorldInfo = true;

@CustomKey("show-player-info")
private boolean showPlayerInfo = true;

@CustomKey("show-chunk-info")
private boolean showChunkInfo = true;

@CustomKey("show-light-info")
private boolean showLightInfo = true;

@CustomKey("show-misc-info")
private boolean showMiscInfo = true;

@CustomKey("show-mspt-bar")
private boolean showMSPTBar = true;
@Setter
public class Settings {
public boolean showWorldInfo = true;
public boolean showPlayerInfo = true;
public boolean showChunkInfo = true;
public boolean showLightInfo = true;
public boolean showMiscInfo = true;
public boolean showMSPTBar = true;
}