From a9b40c0ceb203781f297355880c561c8893c2f43 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Tue, 25 Apr 2023 13:11:39 +0700 Subject: [PATCH] add connecting message --- .idea/workspace.xml | 26 ++++++++++--------- .../chipmunk/chayapak/chomens_bot/Bot.java | 14 +++++++--- .../plugins/BossbarManagerPlugin.java | 3 +-- .../chomens_bot/plugins/ChatPlugin.java | 3 +-- .../chomens_bot/plugins/CorePlugin.java | 3 +-- .../chomens_bot/plugins/DiscordPlugin.java | 24 ++++++++++++++--- .../chomens_bot/plugins/LoggerPlugin.java | 22 +++++++++++++--- .../plugins/MusicPlayerPlugin.java | 3 +-- .../chomens_bot/plugins/PlayersPlugin.java | 8 +++--- .../chomens_bot/plugins/PositionPlugin.java | 3 +-- .../chomens_bot/plugins/SelfCarePlugin.java | 3 +-- .../chomens_bot/plugins/TPSPlugin.java | 3 +-- .../plugins/TabCompletePlugin.java | 3 +-- 13 files changed, 78 insertions(+), 40 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 567676d..9e42102 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -4,10 +4,12 @@ - @@ -482,7 +483,8 @@ - diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java index d7bfd35..e98217d 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java @@ -19,7 +19,7 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class Bot { - private final ArrayList listeners = new ArrayList<>(); + private final ArrayList listeners = new ArrayList<>(); @Getter private final String host; @Getter private final int port; @@ -108,7 +108,11 @@ public class Bot { reconnect(); } - public void reconnect () { + private void reconnect () { + for (Listener listener : listeners) { + listener.connecting(); + } + final String _username = options.username(); if (_username == null) username = RandomStringUtils.randomAlphabetic(8); @@ -189,7 +193,11 @@ public class Bot { session.connect(); } - public void addListener (SessionListener listener) { + public void addListener (Listener listener) { listeners.add(listener); } + + public static class Listener extends SessionAdapter { + public void connecting () {} + } } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/BossbarManagerPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/BossbarManagerPlugin.java index ce31726..eb24073 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/BossbarManagerPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/BossbarManagerPlugin.java @@ -1,7 +1,6 @@ package land.chipmunk.chayapak.chomens_bot.plugins; import com.github.steveice10.packetlib.event.session.DisconnectedEvent; -import com.github.steveice10.packetlib.event.session.SessionAdapter; import land.chipmunk.chayapak.chomens_bot.Bot; import land.chipmunk.chayapak.chomens_bot.data.BossBar; import lombok.Getter; @@ -13,7 +12,7 @@ import java.util.Map; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; -public class BossbarManagerPlugin extends SessionAdapter { +public class BossbarManagerPlugin extends Bot.Listener { private final Bot bot; private ScheduledFuture tickTask; diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatPlugin.java index 0fbdf37..435d486 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatPlugin.java @@ -6,7 +6,6 @@ import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundSy import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundChatCommandPacket; import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundChatPacket; import com.github.steveice10.packetlib.Session; -import com.github.steveice10.packetlib.event.session.SessionAdapter; import com.github.steveice10.packetlib.packet.Packet; import land.chipmunk.chayapak.chomens_bot.Bot; import land.chipmunk.chayapak.chomens_bot.chatParsers.ChomeNSCustomChatParser; @@ -27,7 +26,7 @@ import java.time.Instant; import java.util.*; import java.util.concurrent.TimeUnit; -public class ChatPlugin extends SessionAdapter { +public class ChatPlugin extends Bot.Listener { private final Bot bot; private final List chatParsers; diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CorePlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CorePlugin.java index d7f3072..211c2a3 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CorePlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CorePlugin.java @@ -17,7 +17,6 @@ import com.github.steveice10.opennbt.tag.builtin.StringTag; import com.github.steveice10.opennbt.tag.builtin.Tag; import com.github.steveice10.packetlib.Session; import com.github.steveice10.packetlib.event.session.DisconnectedEvent; -import com.github.steveice10.packetlib.event.session.SessionAdapter; import com.github.steveice10.packetlib.packet.Packet; import com.nukkitx.math.vector.Vector3i; import land.chipmunk.chayapak.chomens_bot.Bot; @@ -58,7 +57,7 @@ public class CorePlugin extends PositionPlugin.PositionListener { bot.position().addListener(this); - bot.addListener(new SessionAdapter() { + bot.addListener(new Bot.Listener() { @Override public void disconnected (DisconnectedEvent event) { ready = false; diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/DiscordPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/DiscordPlugin.java index bad6e8e..8868ed4 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/DiscordPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/DiscordPlugin.java @@ -2,7 +2,6 @@ package land.chipmunk.chayapak.chomens_bot.plugins; import com.github.steveice10.packetlib.event.session.ConnectedEvent; import com.github.steveice10.packetlib.event.session.DisconnectedEvent; -import com.github.steveice10.packetlib.event.session.SessionAdapter; import land.chipmunk.chayapak.chomens_bot.Bot; import land.chipmunk.chayapak.chomens_bot.Configuration; import land.chipmunk.chayapak.chomens_bot.Main; @@ -44,12 +43,31 @@ public class DiscordPlugin { for (Bot bot : Main.allBots) { String channelId = servers.get(bot.host() + ":" + bot.port()); - bot.addListener(new SessionAdapter() { + bot.addListener(new Bot.Listener() { + @Override + public void connecting() { + sendMessageInstantly( + String.format( + "Connecting to: `%s:%s`", + bot.host(), + bot.port() + ), + channelId + ); + } + @Override public void connected (ConnectedEvent event) { boolean channelAlreadyAddedListeners = alreadyAddedListeners.getOrDefault(channelId, false); - sendMessageInstantly("Successfully connected to: " + "`" + bot.host() + ":" + bot.port() + "`", channelId); + sendMessageInstantly( + String.format( + "Successfully connected to: `%s:%s`", + bot.host(), + bot.port() + ), + channelId + ); if (channelAlreadyAddedListeners) return; diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/LoggerPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/LoggerPlugin.java index 9b36835..83a2e74 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/LoggerPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/LoggerPlugin.java @@ -2,7 +2,6 @@ package land.chipmunk.chayapak.chomens_bot.plugins; import com.github.steveice10.packetlib.event.session.ConnectedEvent; import com.github.steveice10.packetlib.event.session.DisconnectedEvent; -import com.github.steveice10.packetlib.event.session.SessionAdapter; import land.chipmunk.chayapak.chomens_bot.Bot; import land.chipmunk.chayapak.chomens_bot.Logger; import land.chipmunk.chayapak.chomens_bot.util.ComponentUtilities; @@ -22,10 +21,27 @@ public class LoggerPlugin extends ChatPlugin.ChatListener { public LoggerPlugin(Bot bot) { this.bot = bot; - bot.addListener(new SessionAdapter() { + bot.addListener(new Bot.Listener() { + @Override + public void connecting() { + info( + String.format( + "Connecting to: %s:%s", + bot.host(), + bot.port() + ) + ); + } + @Override public void connected (ConnectedEvent event) { - info("Successfully connected to: " + bot.host() + ":" + bot.port()); + info( + String.format( + "Successfully connected to: %s:%s", + bot.host(), + bot.port() + ) + ); if (addedListener) return; bot.chat().addListener(LoggerPlugin.this); diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MusicPlayerPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MusicPlayerPlugin.java index 2742a0f..2d41910 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MusicPlayerPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MusicPlayerPlugin.java @@ -1,7 +1,6 @@ package land.chipmunk.chayapak.chomens_bot.plugins; import com.github.steveice10.packetlib.event.session.DisconnectedEvent; -import com.github.steveice10.packetlib.event.session.SessionAdapter; import land.chipmunk.chayapak.chomens_bot.Bot; import land.chipmunk.chayapak.chomens_bot.data.BossBar; import land.chipmunk.chayapak.chomens_bot.data.BossBarColor; @@ -21,7 +20,7 @@ import java.util.LinkedList; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; -public class MusicPlayerPlugin extends SessionAdapter { +public class MusicPlayerPlugin extends Bot.Listener { private final Bot bot; private ScheduledFuture playTask; diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/PlayersPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/PlayersPlugin.java index a6d39d9..2ea4330 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/PlayersPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/PlayersPlugin.java @@ -7,16 +7,18 @@ import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundPl import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundPlayerInfoUpdatePacket; import com.github.steveice10.packetlib.Session; import com.github.steveice10.packetlib.event.session.DisconnectedEvent; -import com.github.steveice10.packetlib.event.session.SessionAdapter; import com.github.steveice10.packetlib.packet.Packet; import land.chipmunk.chayapak.chomens_bot.Bot; import land.chipmunk.chayapak.chomens_bot.chatParsers.data.MutablePlayerListEntry; import lombok.Getter; import net.kyori.adventure.text.Component; -import java.util.*; +import java.util.ArrayList; +import java.util.EnumSet; +import java.util.List; +import java.util.UUID; -public class PlayersPlugin extends SessionAdapter { +public class PlayersPlugin extends Bot.Listener { private final Bot bot; @Getter private final List list = new ArrayList<>(); diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/PositionPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/PositionPlugin.java index 7e45e1a..e6410a5 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/PositionPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/PositionPlugin.java @@ -8,7 +8,6 @@ import com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.player import com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.spawn.ClientboundAddPlayerPacket; import com.github.steveice10.mc.protocol.packet.ingame.serverbound.level.ServerboundAcceptTeleportationPacket; import com.github.steveice10.packetlib.Session; -import com.github.steveice10.packetlib.event.session.SessionAdapter; import com.github.steveice10.packetlib.packet.Packet; import com.nukkitx.math.vector.Vector3f; import com.nukkitx.math.vector.Vector3i; @@ -23,7 +22,7 @@ import java.util.List; import java.util.Map; // some part of the code used to be in a test plugin but i thought it would be useful in the future so i moved it here -public class PositionPlugin extends SessionAdapter { +public class PositionPlugin extends Bot.Listener { private final Bot bot; private final List listeners = new ArrayList<>(); diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/SelfCarePlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/SelfCarePlugin.java index 3cd5f8b..c4f54e2 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/SelfCarePlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/SelfCarePlugin.java @@ -13,7 +13,6 @@ import com.github.steveice10.mc.protocol.packet.ingame.clientbound.level.Clientb import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundClientCommandPacket; import com.github.steveice10.packetlib.Session; import com.github.steveice10.packetlib.event.session.DisconnectedEvent; -import com.github.steveice10.packetlib.event.session.SessionAdapter; import com.github.steveice10.packetlib.packet.Packet; import com.github.steveice10.packetlib.packet.PacketProtocol; import com.nukkitx.math.vector.Vector3i; @@ -27,7 +26,7 @@ import net.kyori.adventure.text.Component; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; -public class SelfCarePlugin extends SessionAdapter { +public class SelfCarePlugin extends Bot.Listener { private final Bot bot; private ScheduledFuture checkTask; diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/TPSPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/TPSPlugin.java index 11d634b..50d2f18 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/TPSPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/TPSPlugin.java @@ -3,7 +3,6 @@ package land.chipmunk.chayapak.chomens_bot.plugins; import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundLoginPacket; import com.github.steveice10.mc.protocol.packet.ingame.clientbound.level.ClientboundSetTimePacket; import com.github.steveice10.packetlib.Session; -import com.github.steveice10.packetlib.event.session.SessionAdapter; import com.github.steveice10.packetlib.packet.Packet; import land.chipmunk.chayapak.chomens_bot.Bot; import land.chipmunk.chayapak.chomens_bot.data.BossBar; @@ -16,7 +15,7 @@ import net.kyori.adventure.text.format.NamedTextColor; import java.util.Arrays; import java.util.concurrent.TimeUnit; -public class TPSPlugin extends SessionAdapter { +public class TPSPlugin extends Bot.Listener { private final Bot bot; private boolean enabled = false; diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/TabCompletePlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/TabCompletePlugin.java index 97137e5..d83c439 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/TabCompletePlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/TabCompletePlugin.java @@ -3,7 +3,6 @@ package land.chipmunk.chayapak.chomens_bot.plugins; import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundCommandSuggestionsPacket; import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundCommandSuggestionPacket; import com.github.steveice10.packetlib.Session; -import com.github.steveice10.packetlib.event.session.SessionAdapter; import com.github.steveice10.packetlib.packet.Packet; import land.chipmunk.chayapak.chomens_bot.Bot; @@ -11,7 +10,7 @@ import java.util.HashMap; import java.util.Map; import java.util.concurrent.CompletableFuture; -public class TabCompletePlugin extends SessionAdapter { +public class TabCompletePlugin extends Bot.Listener { private final Bot bot; private int nextTransactionId = 0; private final Map> transactions = new HashMap<>();