feat: tick using debug sample

refactor: remove onAlwaysTick because no one uses it
This commit is contained in:
ChomeNS
2025-05-07 17:51:59 +07:00
parent dc38ef86c6
commit e8a4c03e8c
5 changed files with 57 additions and 20 deletions

View File

@@ -1 +1 @@
3156
3165

View File

@@ -29,7 +29,7 @@ public interface Listener {
// ticker
default void onTick () { }
default void onAlwaysTick () { }
default void onLocalTick () { }
default void onSecondTick () { }
default void onLocalSecondTick () { }

View File

@@ -25,7 +25,7 @@ public class BruhifyPlugin implements Listener {
}
@Override
public void onTick () {
public void onLocalTick () {
if (bruhifyText.isBlank()) return;
final int increment = 360 / Math.max(bruhifyText.length(), 20);

View File

@@ -94,8 +94,6 @@ public class CorePlugin implements Listener {
@Override
public void onTick () {
if (commandsPerTick.get() > 0) commandsPerTick.decrementAndGet();
if (!pendingCommands.isEmpty() && exists) {
// people that pre-order on TEMU application. Shop Like A Billionaire!!!
if (pendingCommands.size() > 256) {
@@ -121,6 +119,11 @@ public class CorePlugin implements Listener {
forceRunPlaceBlock(command);
}
@Override
public void onLocalTick () {
if (commandsPerTick.get() > 0) commandsPerTick.decrementAndGet();
}
@Override
public void onLocalSecondTick () {
resizeTick();

View File

@@ -3,8 +3,12 @@ package me.chayapak1.chomens_bot.plugins;
import me.chayapak1.chomens_bot.Bot;
import me.chayapak1.chomens_bot.data.listener.Listener;
import org.geysermc.mcprotocollib.network.Session;
import org.geysermc.mcprotocollib.network.event.session.ConnectedEvent;
import org.geysermc.mcprotocollib.network.packet.Packet;
import org.geysermc.mcprotocollib.protocol.data.game.RemoteDebugSampleType;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundDebugSamplePacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.level.ClientboundSetTimePacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.ServerboundDebugSampleSubscriptionPacket;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
@@ -15,6 +19,9 @@ public class TickPlugin implements Listener {
public final AtomicLong lastTickTime = new AtomicLong();
public final AtomicLong lastSecondTickTime = new AtomicLong();
private boolean receivedDebugSample = false;
private final AtomicLong lastDebugSubscriptionTime = new AtomicLong();
public TickPlugin (final Bot bot) {
this.bot = bot;
@@ -24,18 +31,49 @@ public class TickPlugin implements Listener {
bot.executor.scheduleAtFixedRate(this::tickLocalSecond, 0, 1, TimeUnit.SECONDS);
}
@Override
public void connected (final ConnectedEvent event) {
resubscribeDebug();
}
private void tick () {
if (!bot.loggedIn) return;
bot.listener.dispatch(listener -> {
try {
listener.onAlwaysTick();
listener.onLocalTick();
} catch (final Throwable e) {
bot.logger.error("Caught exception in an always tick listener!");
bot.logger.error("Caught exception in a local tick listener!");
bot.logger.error(e);
}
});
if (!receivedDebugSample) dispatchTick();
}
private void tickLocalSecond () {
if (!bot.loggedIn) return;
if (System.currentTimeMillis() - lastDebugSubscriptionTime.get() >= 5 * 1000) {
resubscribeDebug();
lastDebugSubscriptionTime.set(System.currentTimeMillis());
}
bot.listener.dispatch(listener -> {
try {
listener.onLocalSecondTick();
} catch (final Throwable e) {
bot.logger.error("Caught exception in a local second tick listener!");
bot.logger.error(e);
}
});
}
private void resubscribeDebug () {
bot.session.send(new ServerboundDebugSampleSubscriptionPacket(RemoteDebugSampleType.TICK_TIME));
}
private void dispatchTick () {
bot.listener.dispatch(listener -> {
try {
listener.onTick();
@@ -48,22 +86,10 @@ public class TickPlugin implements Listener {
lastTickTime.set(System.currentTimeMillis());
}
private void tickLocalSecond () {
if (!bot.loggedIn) return;
bot.listener.dispatch(listener -> {
try {
listener.onLocalSecondTick();
} catch (final Throwable e) {
bot.logger.error("Caught exception in a local second tick listener!");
bot.logger.error(e);
}
});
}
@Override
public void packetReceived (final Session session, final Packet packet) {
if (packet instanceof final ClientboundSetTimePacket t_packet) packetReceived(t_packet);
else if (packet instanceof final ClientboundDebugSamplePacket t_packet) packetReceived(t_packet);
}
private void packetReceived (final ClientboundSetTimePacket ignoredPacket) {
@@ -78,4 +104,12 @@ public class TickPlugin implements Listener {
lastSecondTickTime.set(System.currentTimeMillis());
}
private void packetReceived (final ClientboundDebugSamplePacket packet) {
if (packet.getDebugSampleType() != RemoteDebugSampleType.TICK_TIME) return;
receivedDebugSample = true;
dispatchTick();
}
}