moved tablist to common and made project title configurable
This commit is contained in:
@ -0,0 +1,91 @@
|
||||
package eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.tablist;
|
||||
|
||||
import eu.mhsl.craftattack.core.Main;
|
||||
import eu.mhsl.craftattack.core.appliance.Appliance;
|
||||
import eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.report.Report;
|
||||
import eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.settings.Settings;
|
||||
import eu.mhsl.craftattack.core.util.IteratorUtil;
|
||||
import eu.mhsl.craftattack.core.util.statistics.NetworkMonitor;
|
||||
import eu.mhsl.craftattack.core.util.text.ComponentUtil;
|
||||
import eu.mhsl.craftattack.core.util.text.RainbowComponent;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.util.Ticks;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.lang.management.OperatingSystemMXBean;
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class Tablist extends Appliance {
|
||||
private final String projectTitle = this.localConfig().getString("projectTitle", "Title not configured");
|
||||
private final RainbowComponent serverName = new RainbowComponent(String.format(" %s ", this.projectTitle), 7, 3);
|
||||
private NetworkMonitor networkMonitor;
|
||||
private OperatingSystemMXBean systemMonitor;
|
||||
|
||||
public Tablist() {
|
||||
super("tablist");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
Settings.instance().declareSetting(TechnicalTablistSetting.class);
|
||||
|
||||
int tabRefreshRate = 3;
|
||||
this.networkMonitor = new NetworkMonitor(this.localConfig().getString("interface"), Duration.ofSeconds(1));
|
||||
this.systemMonitor = ManagementFactory.getOperatingSystemMXBean();
|
||||
|
||||
Bukkit.getScheduler().runTaskTimerAsynchronously(
|
||||
Main.instance(),
|
||||
() -> IteratorUtil.onlinePlayers(this::updateHeader),
|
||||
tabRefreshRate * Ticks.TICKS_PER_SECOND,
|
||||
tabRefreshRate * Ticks.TICKS_PER_SECOND
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
this.networkMonitor.stop();
|
||||
}
|
||||
|
||||
public void fullUpdate(Player player) {
|
||||
this.updateHeader(player);
|
||||
this.updateFooter(player);
|
||||
}
|
||||
|
||||
private void updateHeader(Player player) {
|
||||
boolean detailedInfo = this.queryAppliance(Settings.class).getSetting(player, Settings.Key.TechnicalTab, Boolean.class);
|
||||
Component header = Component.newline()
|
||||
.append(this.serverName.getRainbowState()).appendNewline()
|
||||
.append(Component.text("mhsl.eu", NamedTextColor.GOLD)).appendNewline().appendNewline()
|
||||
.append(ComponentUtil.getFormattedTickTimes(detailedInfo)).appendNewline();
|
||||
|
||||
if(detailedInfo) {
|
||||
header = header
|
||||
.appendNewline()
|
||||
.append(ComponentUtil.getFormattedPing(player)).appendNewline()
|
||||
.append(ComponentUtil.getFormattedNetworkStats(
|
||||
this.networkMonitor.getTraffic(),
|
||||
this.networkMonitor.getPackets()
|
||||
)).appendNewline()
|
||||
.append(ComponentUtil.getFormattedSystemStats(this.systemMonitor)).appendNewline();
|
||||
}
|
||||
|
||||
player.sendPlayerListHeader(header);
|
||||
}
|
||||
|
||||
private void updateFooter(Player player) {
|
||||
player.sendPlayerListFooter(Report.helpText());
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
protected List<Listener> listeners() {
|
||||
return List.of(new TablistListener());
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.tablist;
|
||||
|
||||
import eu.mhsl.craftattack.core.appliance.ApplianceListener;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
|
||||
class TablistListener extends ApplianceListener<Tablist> {
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
this.getAppliance().fullUpdate(event.getPlayer());
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.tablist;
|
||||
|
||||
import eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.settings.CategorizedSetting;
|
||||
import eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.settings.SettingCategory;
|
||||
import eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.settings.Settings;
|
||||
import eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.settings.datatypes.BoolSetting;
|
||||
import org.bukkit.Material;
|
||||
|
||||
public class TechnicalTablistSetting extends BoolSetting implements CategorizedSetting {
|
||||
public TechnicalTablistSetting() {
|
||||
super(Settings.Key.TechnicalTab);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String title() {
|
||||
return "Technische Informationen";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String description() {
|
||||
return "Zeige erweiterte Informationen und Statistiken in der Tabliste an";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Material icon() {
|
||||
return Material.COMMAND_BLOCK_MINECART;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Boolean defaultValue() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SettingCategory category() {
|
||||
return SettingCategory.Misc;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user