fix discord not logging after disconnect

This commit is contained in:
Chayapak 2023-07-03 19:25:50 +07:00
parent 5e7f10cb0d
commit 16b19f1c1f
3 changed files with 9 additions and 32 deletions

View file

@ -51,7 +51,7 @@ public class DiscordPlugin {
public void loadedPlugins() { public void loadedPlugins() {
bot.tick.addListener(new TickPlugin.Listener() { bot.tick.addListener(new TickPlugin.Listener() {
@Override @Override
public void onTick() { public void onAlwaysTick () {
onDiscordTick(channelId); onDiscordTick(channelId);
} }
}); });

View file

@ -1,7 +1,5 @@
package land.chipmunk.chayapak.chomens_bot.plugins; package land.chipmunk.chayapak.chomens_bot.plugins;
import com.github.steveice10.mc.protocol.MinecraftProtocol;
import com.github.steveice10.mc.protocol.data.ProtocolState;
import com.github.steveice10.mc.protocol.data.game.ClientCommand; import com.github.steveice10.mc.protocol.data.game.ClientCommand;
import com.github.steveice10.mc.protocol.data.game.entity.EntityEvent; import com.github.steveice10.mc.protocol.data.game.entity.EntityEvent;
import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode; import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode;
@ -14,7 +12,6 @@ import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundCl
import com.github.steveice10.packetlib.Session; import com.github.steveice10.packetlib.Session;
import com.github.steveice10.packetlib.event.session.DisconnectedEvent; import com.github.steveice10.packetlib.event.session.DisconnectedEvent;
import com.github.steveice10.packetlib.packet.Packet; import com.github.steveice10.packetlib.packet.Packet;
import com.github.steveice10.packetlib.packet.PacketProtocol;
import land.chipmunk.chayapak.chomens_bot.Bot; import land.chipmunk.chayapak.chomens_bot.Bot;
import land.chipmunk.chayapak.chomens_bot.Configuration; import land.chipmunk.chayapak.chomens_bot.Configuration;
import land.chipmunk.chayapak.chomens_bot.util.ComponentUtilities; import land.chipmunk.chayapak.chomens_bot.util.ComponentUtilities;
@ -138,15 +135,7 @@ public class SelfCarePlugin extends Bot.Listener {
positionPacketsPerSecond = 0; positionPacketsPerSecond = 0;
final Runnable task = () -> { final Runnable task = () -> {
final Session session = bot.session; if (!bot.loggedIn) return;
final PacketProtocol protocol = session.getPacketProtocol();
if (
!session.isConnected() ||
(
protocol instanceof MinecraftProtocol &&
((MinecraftProtocol) protocol).getState() != ProtocolState.GAME
)
) return;
check(); check();
}; };

View file

@ -1,41 +1,28 @@
package land.chipmunk.chayapak.chomens_bot.plugins; package land.chipmunk.chayapak.chomens_bot.plugins;
import com.github.steveice10.packetlib.event.session.ConnectedEvent;
import com.github.steveice10.packetlib.event.session.DisconnectedEvent;
import land.chipmunk.chayapak.chomens_bot.Bot; import land.chipmunk.chayapak.chomens_bot.Bot;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class TickPlugin extends Bot.Listener { public class TickPlugin {
private final Bot bot; private final Bot bot;
private ScheduledFuture<?> tickTask;
private final List<Listener> listeners = new ArrayList<>(); private final List<Listener> listeners = new ArrayList<>();
public TickPlugin (Bot bot) { public TickPlugin (Bot bot) {
this.bot = bot; this.bot = bot;
bot.addListener(this); bot.executor.scheduleAtFixedRate(this::tick, 0, 50, TimeUnit.MILLISECONDS);
}
@Override
public void connected(ConnectedEvent event) {
tickTask = bot.executor.scheduleAtFixedRate(this::tick, 0, 50, TimeUnit.MILLISECONDS);
} }
private void tick () { private void tick () {
for (Listener listener : listeners) { for (Listener listener : listeners) listener.onAlwaysTick();
listener.onTick();
}
}
@Override if (!bot.loggedIn) return;
public void disconnected (DisconnectedEvent event) {
tickTask.cancel(true); for (Listener listener : listeners) listener.onTick();
} }
public void addListener (Listener listener) { public void addListener (Listener listener) {
@ -44,5 +31,6 @@ public class TickPlugin extends Bot.Listener {
public static class Listener { public static class Listener {
public void onTick () {} public void onTick () {}
public void onAlwaysTick () {}
} }
} }