From 9e83de33c7687d67160b431f9d99060a94516e13 Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Sat, 30 Nov 2024 19:14:16 +0100 Subject: [PATCH] New ping checker It now averages And no ProtocolLib --- .../java/eu/m724/tweaks/TweaksConfig.java | 5 -- .../java/eu/m724/tweaks/TweaksPlugin.java | 1 - .../m724/tweaks/ping/CookiePingChecker.java | 35 +++++++++ .../eu/m724/tweaks/ping/F3NameListener.java | 4 +- .../java/eu/m724/tweaks/ping/MsptChecker.java | 2 +- .../java/eu/m724/tweaks/ping/PingChecker.java | 10 +-- .../eu/m724/tweaks/ping/PingCommands.java | 28 +------- .../java/eu/m724/tweaks/ping/PlayerPing.java | 51 ------------- .../m724/tweaks/ping/PlayerPingStorage.java | 55 ++++++++++++++ .../m724/tweaks/ping/ProtocolPingChecker.java | 72 ------------------- src/main/resources/config.yml | 2 +- src/main/resources/plugin.yml | 3 - 12 files changed, 96 insertions(+), 172 deletions(-) create mode 100644 src/main/java/eu/m724/tweaks/ping/CookiePingChecker.java delete mode 100644 src/main/java/eu/m724/tweaks/ping/PlayerPing.java create mode 100644 src/main/java/eu/m724/tweaks/ping/PlayerPingStorage.java delete mode 100644 src/main/java/eu/m724/tweaks/ping/ProtocolPingChecker.java diff --git a/src/main/java/eu/m724/tweaks/TweaksConfig.java b/src/main/java/eu/m724/tweaks/TweaksConfig.java index a8c1197..1bd5752 100644 --- a/src/main/java/eu/m724/tweaks/TweaksConfig.java +++ b/src/main/java/eu/m724/tweaks/TweaksConfig.java @@ -10,8 +10,6 @@ import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.plugin.Plugin; public record TweaksConfig( - boolean isProtocolLib, - boolean worldborderHide, boolean brandEnabled, @@ -56,8 +54,6 @@ public record TweaksConfig( plugin.saveDefaultConfig(); FileConfiguration config = plugin.getConfig(); - boolean isProtocolLib = plugin.getServer().getPluginManager().getPlugin("ProtocolLib") != null; - int configVersion = config.getInt("magic number don't modify this", 0); RuntimeException exception = new RuntimeException("Config version is %d, expected %d".formatted(configVersion, CONFIG_VERSION)); if (configVersion == 0) { @@ -102,7 +98,6 @@ public record TweaksConfig( boolean sleepInstant = config.getBoolean("sleep.instant"); TweaksConfig.config = new TweaksConfig( - isProtocolLib, hideWorldBorder, brandEnabled, brandText, brandShowPing, brandShowMspt, doorEnabled, doorDoubleOpen, doorKnocking, diff --git a/src/main/java/eu/m724/tweaks/TweaksPlugin.java b/src/main/java/eu/m724/tweaks/TweaksPlugin.java index 75aa267..4f92389 100644 --- a/src/main/java/eu/m724/tweaks/TweaksPlugin.java +++ b/src/main/java/eu/m724/tweaks/TweaksPlugin.java @@ -52,7 +52,6 @@ public class TweaksPlugin extends JavaPlugin { new PingChecker(this).init(); Objects.requireNonNull(getCommand("ping")).setExecutor(new PingCommands()); - Objects.requireNonNull(getCommand("dkick")).setExecutor(new PingCommands()); /*if (getServer().getPluginManager().getPlugin("voicechat") != null) { new MusicPlayer(this).init(); diff --git a/src/main/java/eu/m724/tweaks/ping/CookiePingChecker.java b/src/main/java/eu/m724/tweaks/ping/CookiePingChecker.java new file mode 100644 index 0000000..9dfab8d --- /dev/null +++ b/src/main/java/eu/m724/tweaks/ping/CookiePingChecker.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2024 Minecon724 + * Tweaks724 is licensed under the GNU General Public License. See the LICENSE.md file + * in the project root for the full license text. + */ + +package eu.m724.tweaks.ping; + +import org.bukkit.NamespacedKey; +import org.bukkit.plugin.Plugin; +import org.bukkit.scheduler.BukkitRunnable; + +public class CookiePingChecker extends BukkitRunnable { + private final Plugin plugin; + + private final NamespacedKey namespacedKey = NamespacedKey.fromString("chk_ping"); + + CookiePingChecker(Plugin plugin) { + this.plugin = plugin; + } + + public void start() { + this.runTaskTimerAsynchronously(plugin, 0, 40); // 2 secs + } + + @Override + public void run() { + plugin.getServer().getOnlinePlayers().forEach(player -> { + long start = System.nanoTime(); + player.retrieveCookie(namespacedKey).thenAccept(v -> { + PlayerPingStorage.submitPing(player, System.nanoTime() - start); + }); + }); + } +} diff --git a/src/main/java/eu/m724/tweaks/ping/F3NameListener.java b/src/main/java/eu/m724/tweaks/ping/F3NameListener.java index b2d2f5b..9577841 100644 --- a/src/main/java/eu/m724/tweaks/ping/F3NameListener.java +++ b/src/main/java/eu/m724/tweaks/ping/F3NameListener.java @@ -78,14 +78,14 @@ public class F3NameListener { String sep = text.isEmpty() ? "" : " | "; if (showMspt) { - double mspt = PlayerPing.getMillisPerTick(); + double mspt = PlayerPingStorage.getMillisPerTick(); if (mspt > 50.05) { // mspt is at least like 50.01 because of some measuring overheads (I think) _text += sep + "%.2f mspt".formatted(mspt); } } if (showPing) { - _text += sep + "%.2f ms".formatted(PlayerPing.getPingMillis(player)); + _text += sep + "%.2f ms".formatted(PlayerPingStorage.getPingMillis(player)); } return _text; diff --git a/src/main/java/eu/m724/tweaks/ping/MsptChecker.java b/src/main/java/eu/m724/tweaks/ping/MsptChecker.java index c7fb4d7..6d0cbbe 100644 --- a/src/main/java/eu/m724/tweaks/ping/MsptChecker.java +++ b/src/main/java/eu/m724/tweaks/ping/MsptChecker.java @@ -15,7 +15,7 @@ public class MsptChecker extends BukkitRunnable { @Override public void run() { long now = System.nanoTime(); - PlayerPing.nspt = (now - lastLoop) / 100; + PlayerPingStorage.submitNspt((now - lastLoop) / 100); lastLoop = now; } diff --git a/src/main/java/eu/m724/tweaks/ping/PingChecker.java b/src/main/java/eu/m724/tweaks/ping/PingChecker.java index d271d4d..00b9d1b 100644 --- a/src/main/java/eu/m724/tweaks/ping/PingChecker.java +++ b/src/main/java/eu/m724/tweaks/ping/PingChecker.java @@ -6,7 +6,6 @@ package eu.m724.tweaks.ping; -import eu.m724.tweaks.TweaksConfig; import org.bukkit.plugin.Plugin; public class PingChecker { @@ -17,14 +16,7 @@ public class PingChecker { } public void init() { - - if (TweaksConfig.getConfig().isProtocolLib()) { - plugin.getLogger().info("Using ProtocolLib for checking ping"); - new ProtocolPingChecker(plugin).start(); - } else { - plugin.getLogger().info("Using Spigot for checking ping"); - } - + new CookiePingChecker(plugin).start(); new MsptChecker().init(plugin); // TODO should this be here } } diff --git a/src/main/java/eu/m724/tweaks/ping/PingCommands.java b/src/main/java/eu/m724/tweaks/ping/PingCommands.java index 435f0a7..01d9a1c 100644 --- a/src/main/java/eu/m724/tweaks/ping/PingCommands.java +++ b/src/main/java/eu/m724/tweaks/ping/PingCommands.java @@ -6,11 +6,9 @@ package eu.m724.tweaks.ping; -import eu.m724.tweaks.TweaksConfig; import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.ComponentBuilder; -import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; @@ -23,33 +21,9 @@ public class PingCommands implements CommandExecutor { if (command.getName().equals("ping")) { Player player = (Player) sender; BaseComponent[] component = new ComponentBuilder("Ping: ").color(ChatColor.GOLD) - .append("%.2fms".formatted(PlayerPing.getPingMillis(player))).color(ChatColor.AQUA) + .append("%.2fms".formatted(PlayerPingStorage.getPingMillis(player))).color(ChatColor.AQUA) .create(); player.spigot().sendMessage(component); - } else if (command.getName().equals("dkick")) { - if (!TweaksConfig.getConfig().isProtocolLib()) { - sender.sendMessage("This feature is not available"); - return true; - } - - if (args.length == 0) { - sender.sendMessage("Include one or more player names"); - } else { - for (String name : args) { - Player player = Bukkit.getPlayer(name); - if (player == null) { - sender.sendMessage("Player %s is not online".formatted(name)); - } - if (PlayerPing.kick(player)) { - if (player.getName().equals(sender.getName())) { - sender.sendMessage("Kicking yourself? Very well"); - } - sender.sendMessage("Player %s will be kicked shortly".formatted(name)); - } else { - sender.sendMessage("Player %s is already queued".formatted(name)); - } - } - } } return true; } diff --git a/src/main/java/eu/m724/tweaks/ping/PlayerPing.java b/src/main/java/eu/m724/tweaks/ping/PlayerPing.java deleted file mode 100644 index 0903a22..0000000 --- a/src/main/java/eu/m724/tweaks/ping/PlayerPing.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (C) 2024 Minecon724 - * Tweaks724 is licensed under the GNU General Public License. See the LICENSE.md file - * in the project root for the full license text. - */ - -package eu.m724.tweaks.ping; - -import eu.m724.tweaks.TweaksConfig; -import org.bukkit.entity.Player; - -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; - -public class PlayerPing { - // if protocol lib is installed we use our own checker - private static final boolean external = TweaksConfig.getConfig().isProtocolLib(); - - // TODO remove this feature because why - static final Set kickQueue = ConcurrentHashMap.newKeySet(); - - // current player latencies, in nanos - static final Map pings = new ConcurrentHashMap<>(); - // nanoseconds per tick - static volatile long nspt = 0L; - - public static long getPingNanos(Player player) { - if (!external) { - return (long) player.getPing() * 1000000; - } else { - return pings.getOrDefault(player, -1L); - } - } - - public static double getPingMillis(Player player) { - return getPingNanos(player) / 1000000.0; // a mil ns in ms - } - - public static long getNanosPerTick() { - return nspt; - } - - public static double getMillisPerTick() { - return nspt / 1000000.0; // a mil ns in ms - } - - public static boolean kick(Player player) { - return kickQueue.add(player); - } -} diff --git a/src/main/java/eu/m724/tweaks/ping/PlayerPingStorage.java b/src/main/java/eu/m724/tweaks/ping/PlayerPingStorage.java new file mode 100644 index 0000000..e29d176 --- /dev/null +++ b/src/main/java/eu/m724/tweaks/ping/PlayerPingStorage.java @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2024 Minecon724 + * Tweaks724 is licensed under the GNU General Public License. See the LICENSE.md file + * in the project root for the full license text. + */ + +package eu.m724.tweaks.ping; + +import org.bukkit.entity.Player; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public class PlayerPingStorage { + // current player latencies, in queue of nanos + private static final Map pings = new ConcurrentHashMap<>(); + private static final Map pingsCount = new ConcurrentHashMap<>(); + + // nanoseconds per tick + private static volatile long nspt = 0L; + + public static long getPingNanos(Player player) { + return pings.getOrDefault(player, 0L); + } + + public static double getPingMillis(Player player) { + return getPingNanos(player) / 1000000.0; // a mil ns in ms + } + + public static long getNanosPerTick() { + return nspt; + } + + public static double getMillisPerTick() { + return nspt / 1000000.0; // a mil ns in ms + } + + static void submitPing(Player player, long currentPing) { + int count = pingsCount.getOrDefault(player, 0); + long average = pings.getOrDefault(player, 0L); + + if (count < 5) { + average = ((average * count) + currentPing) / (count + 1); + pingsCount.put(player, count + 1); + } else { + average += (currentPing - average) / 5; + } + + pings.put(player, average); + } + + static void submitNspt(long currentNspt) { + nspt = currentNspt; + } +} diff --git a/src/main/java/eu/m724/tweaks/ping/ProtocolPingChecker.java b/src/main/java/eu/m724/tweaks/ping/ProtocolPingChecker.java deleted file mode 100644 index 1db0b0b..0000000 --- a/src/main/java/eu/m724/tweaks/ping/ProtocolPingChecker.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (C) 2024 Minecon724 - * Tweaks724 is licensed under the GNU General Public License. See the LICENSE.md file - * in the project root for the full license text. - */ - -package eu.m724.tweaks.ping; - -import com.comphenix.protocol.PacketType; -import com.comphenix.protocol.ProtocolLibrary; -import com.comphenix.protocol.events.ListenerPriority; -import com.comphenix.protocol.events.PacketAdapter; -import com.comphenix.protocol.events.PacketContainer; -import com.comphenix.protocol.events.PacketEvent; -import com.comphenix.protocol.wrappers.MinecraftKey; -import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; -import org.bukkit.scheduler.BukkitRunnable; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -public class ProtocolPingChecker extends BukkitRunnable { - // this is in nanoseconds - private final Map pending = new ConcurrentHashMap<>(); - private final Plugin plugin; - - ProtocolPingChecker(Plugin plugin) { - this.plugin = plugin; - } - - public void start() { - ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter( - plugin, - ListenerPriority.NORMAL, - PacketType.Play.Client.COOKIE_RESPONSE - ) { - @Override - public void onPacketReceiving(PacketEvent event) { - // below line checks, whether the first (and sole) identifier field in the packet is minecraft:chk_ping - if (event.getPacket().getMinecraftKeys().read(0).getKey().equals("chk_ping")) { - Player player = event.getPlayer(); - - long start = pending.remove(player); - PlayerPing.pings.put(player, System.nanoTime() - start); - - // gotta cancel because the server will kick - if (!PlayerPing.kickQueue.contains(player)) { - event.setCancelled(true); - } else { - PlayerPing.kickQueue.remove(player); - } - } - } - }); - - this.runTaskTimerAsynchronously(plugin, 0, 200); // 10 secs - } - - @Override - public void run() { - plugin.getServer().getOnlinePlayers().forEach(player -> { - // dropped packets happen so timing out a request after 30 seconds - if (System.nanoTime() - pending.getOrDefault(player, 0L) < 30000000000L) return; - pending.put(player, System.nanoTime()); // here or at the bottom? probably doesn't matter - - PacketContainer packet = new PacketContainer(PacketType.Play.Server.COOKIE_REQUEST); - packet.getMinecraftKeys().write(0, new MinecraftKey("chk_ping")); - ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet); - }); - } -} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 06a901f..94c1766 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -42,8 +42,8 @@ chat: # Name of the default / global chatroom defaultName: "global" +# Compass shown in a text form on the actionbar compass: - # Compass will be shown in a text form enabled: true # How much points (each point is separated with a space) # I suggest making this uneven to make center... in center diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index e0b8f3f..2b366b2 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -19,9 +19,6 @@ commands: aliases: [cm, crm] ping: description: Your ping - dkick: - description: Kick a player discreetly - permission: tweaks724.dkick pomodoro: description: Pomodoro management permission: tweaks724.pomodoro