From 8b326ad8c9f5e4506320f70511d49f37573dd4cd Mon Sep 17 00:00:00 2001 From: TudbuT Date: Sat, 5 Oct 2024 02:58:29 +0200 Subject: [PATCH] add PacketCounter, move ping to Client category --- .../java/de/com/baseband/client/Setup.java | 1 + .../com/baseband/client/event/EventBus.java | 2 +- .../feature/modules/client/PacketCounter.java | 89 +++++++++++++++++++ .../modules/{ingame => client}/Ping.java | 10 +-- .../util/interact/ServerDataManager.java | 2 +- 5 files changed, 97 insertions(+), 7 deletions(-) create mode 100644 Client/src/main/java/de/com/baseband/client/feature/modules/client/PacketCounter.java rename Client/src/main/java/de/com/baseband/client/feature/modules/{ingame => client}/Ping.java (94%) diff --git a/Client/src/main/java/de/com/baseband/client/Setup.java b/Client/src/main/java/de/com/baseband/client/Setup.java index 766e890..4e1ed9f 100644 --- a/Client/src/main/java/de/com/baseband/client/Setup.java +++ b/Client/src/main/java/de/com/baseband/client/Setup.java @@ -70,6 +70,7 @@ public class Setup { new NoRender(), new NoSlowDown(), new Notifier(), + new PacketCounter(), new Ping(), new PlayerLog(), new PlayerSelector(), diff --git a/Client/src/main/java/de/com/baseband/client/event/EventBus.java b/Client/src/main/java/de/com/baseband/client/event/EventBus.java index fd2963c..8af4eac 100644 --- a/Client/src/main/java/de/com/baseband/client/event/EventBus.java +++ b/Client/src/main/java/de/com/baseband/client/event/EventBus.java @@ -56,7 +56,7 @@ public class EventBus { for (SubscriberMethod subscriberMethod : subscriberMethods) { Class[] parameterTypes = subscriberMethod.method.getParameterTypes(); - if(parameterTypes.length == 1 && eventType.isAssignableFrom(parameterTypes[0])) { + if(parameterTypes.length == 1 && parameterTypes[0].isAssignableFrom(eventType)) { subscriberMethod.invoke(event); } } diff --git a/Client/src/main/java/de/com/baseband/client/feature/modules/client/PacketCounter.java b/Client/src/main/java/de/com/baseband/client/feature/modules/client/PacketCounter.java new file mode 100644 index 0000000..aa64f49 --- /dev/null +++ b/Client/src/main/java/de/com/baseband/client/feature/modules/client/PacketCounter.java @@ -0,0 +1,89 @@ +package de.com.baseband.client.feature.modules.client; + +import de.com.baseband.client.event.events.PacketEvent; +import de.com.baseband.client.feature.Feature; +import de.com.baseband.client.feature.category.ClientCategory; +import de.com.baseband.client.feature.category.Experimental; +import de.com.baseband.client.registry.annotation.Config; +import de.com.baseband.client.registry.annotation.Description; +import de.com.baseband.client.registry.annotation.Trigger; + +import java.util.LinkedList; +import java.util.Queue; + +@ClientCategory +@Description("Shows how many packets per second are being sent/received.") +public class PacketCounter extends Feature { + + long totUp = 0, totDown = 0; + Queue up = new LinkedList<>(), down = new LinkedList<>(); + + @Config("Abbreviate") + @Description("Abbreviates numbers with ...K and ...M once they get too large.") + public boolean abbrev; + + @Trigger("Reset") + public void reset() { + totUp = totDown = 0; + } + + @Override + public void onEveryTick() { + meta = abbreviate(totUp) + "↑/↓" + abbreviate(totDown) + "] [" + + (up.isEmpty() ? "-" : "§c" + up.size()) + "↑§7/" + + (down.isEmpty() ? "↓-" : "§c↓" + down.size()) + "§7"; + } + + private String abbreviate(long n) { + if(n < 10_000L) { + return String.valueOf(n); + } + if(n < 10_000_000L) { + return n / 1000L + "K"; + } + return n / 1_000_000L + "M"; + } + + @Override + public void onTick() { + processQueue(up); + processQueue(down); + } + + private void processQueue(Queue queue) { + while (!queue.isEmpty()) { + if(queue.peek().passed()) + queue.remove(); + else + break; + } + } + + @Override + public String toString() { + return "PacketCounter"; + } + + public void onPacket(PacketEvent event) { + if(event instanceof PacketEvent.Send) { + totUp ++; + up.add(new PacketTimestamp()); + } + if(event instanceof PacketEvent.Receive) { + totDown ++; + down.add(new PacketTimestamp()); + } + } + + public static class PacketTimestamp { + long time; + + public PacketTimestamp() { + this.time = System.currentTimeMillis(); + } + + public boolean passed() { + return (System.currentTimeMillis() - time) >= 1000; + } + } +} diff --git a/Client/src/main/java/de/com/baseband/client/feature/modules/ingame/Ping.java b/Client/src/main/java/de/com/baseband/client/feature/modules/client/Ping.java similarity index 94% rename from Client/src/main/java/de/com/baseband/client/feature/modules/ingame/Ping.java rename to Client/src/main/java/de/com/baseband/client/feature/modules/client/Ping.java index b420364..98f1141 100644 --- a/Client/src/main/java/de/com/baseband/client/feature/modules/ingame/Ping.java +++ b/Client/src/main/java/de/com/baseband/client/feature/modules/client/Ping.java @@ -1,9 +1,9 @@ -package de.com.baseband.client.feature.modules.ingame; +package de.com.baseband.client.feature.modules.client; import de.com.baseband.client.BaseBand; import de.com.baseband.client.event.events.PrePacketEvent; import de.com.baseband.client.feature.Feature; -import de.com.baseband.client.feature.category.Ingame; +import de.com.baseband.client.feature.category.ClientCategory; import de.com.baseband.client.registry.annotation.Config; import de.com.baseband.client.registry.annotation.Description; import de.com.baseband.client.registry.annotation.Range; @@ -16,7 +16,7 @@ import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import java.util.LinkedList; import java.util.Queue; -@Ingame +@ClientCategory public class Ping extends Feature { Queue holderSend = new LinkedList<>(); @@ -33,7 +33,7 @@ public class Ping extends Feature { @Description("How many milliseconds to add to your ping.\n" + "§lWarning§r: You might get timed out on high settings (Time-out happens at 15 seconds including real ping).") @Range("0..5000") - public int milliseconds; + public int milliseconds = 300; Packet allowRecv = null; Packet allowSend = null; @@ -47,7 +47,7 @@ public class Ping extends Feature { public void onEveryTick() { meta = milliseconds + "ms: " + (holderSend.isEmpty() ? "-" : "§c" + holderSend.size()) + "↑§7/" + - (holderRecv.isEmpty() ? "-" : "§c" + holderRecv.size()) + "↓§7"; + (holderRecv.isEmpty() ? "↓-" : "§c↓" + holderRecv.size()) + "§7"; } @SubscribeEvent diff --git a/Client/src/main/java/de/com/baseband/client/util/interact/ServerDataManager.java b/Client/src/main/java/de/com/baseband/client/util/interact/ServerDataManager.java index 728a18a..3d7432b 100644 --- a/Client/src/main/java/de/com/baseband/client/util/interact/ServerDataManager.java +++ b/Client/src/main/java/de/com/baseband/client/util/interact/ServerDataManager.java @@ -4,7 +4,7 @@ import de.com.baseband.client.BaseBand; import de.com.baseband.client.Setup; import de.com.baseband.client.feature.Features; import de.com.baseband.client.feature.modules.client.Client; -import de.com.baseband.client.feature.modules.ingame.Ping; +import de.com.baseband.client.feature.modules.client.Ping; import de.com.baseband.client.util.adapt.ServerPing; import de.tudbut.tools.Lock; import net.minecraft.client.network.NetworkPlayerInfo;