From de4324075511eff0f2207b02f06568edf1c2994a Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Sun, 13 Apr 2025 17:36:58 +0700 Subject: [PATCH] feat: ditch TeamJoinerPlugin to prevent people performing suspicious activities with the bot --- build-number.txt | 2 +- .../java/me/chayapak1/chomens_bot/Bot.java | 4 +- .../chayapak1/chomens_bot/Configuration.java | 1 - .../plugins/BotSelectorBroadcasterPlugin.java | 45 +++++++++++ .../chomens_bot/plugins/TeamJoinerPlugin.java | 77 ------------------- src/main/resources/default-config.yml | 1 - 6 files changed, 48 insertions(+), 82 deletions(-) create mode 100644 src/main/java/me/chayapak1/chomens_bot/plugins/BotSelectorBroadcasterPlugin.java delete mode 100644 src/main/java/me/chayapak1/chomens_bot/plugins/TeamJoinerPlugin.java diff --git a/build-number.txt b/build-number.txt index 389f0444..3411e3a3 100644 --- a/build-number.txt +++ b/build-number.txt @@ -1 +1 @@ -2675 \ No newline at end of file +2681 \ No newline at end of file diff --git a/src/main/java/me/chayapak1/chomens_bot/Bot.java b/src/main/java/me/chayapak1/chomens_bot/Bot.java index 76f8505a..ad0ef54b 100644 --- a/src/main/java/me/chayapak1/chomens_bot/Bot.java +++ b/src/main/java/me/chayapak1/chomens_bot/Bot.java @@ -105,7 +105,7 @@ public class Bot extends SessionAdapter { public final MailPlugin mail; public final PacketSnifferPlugin packetSniffer; public final VoiceChatPlugin voiceChat; - public final TeamJoinerPlugin teamJoiner; + public final BotSelectorBroadcasterPlugin selectorBroadcaster; public final ChomeNSModIntegrationPlugin chomeNSMod; public final AuthPlugin auth; public final ScreensharePlugin screenshare = null; @@ -161,7 +161,7 @@ public class Bot extends SessionAdapter { this.mail = new MailPlugin(this); this.packetSniffer = new PacketSnifferPlugin(this); this.voiceChat = new VoiceChatPlugin(this); - this.teamJoiner = new TeamJoinerPlugin(this); + this.selectorBroadcaster = new BotSelectorBroadcasterPlugin(this); this.chomeNSMod = new ChomeNSModIntegrationPlugin(this); this.auth = new AuthPlugin(this); // this.screenshare = new ScreensharePlugin(this); diff --git a/src/main/java/me/chayapak1/chomens_bot/Configuration.java b/src/main/java/me/chayapak1/chomens_bot/Configuration.java index c405a385..5a0f8f87 100644 --- a/src/main/java/me/chayapak1/chomens_bot/Configuration.java +++ b/src/main/java/me/chayapak1/chomens_bot/Configuration.java @@ -21,7 +21,6 @@ public class Configuration { public String weatherApiKey; public String namespace = "chomens_bot"; - public String teamName = "chomens_bot"; public Core core = new Core(); public Discord discord = new Discord(); diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/BotSelectorBroadcasterPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/BotSelectorBroadcasterPlugin.java new file mode 100644 index 00000000..b76a7f94 --- /dev/null +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/BotSelectorBroadcasterPlugin.java @@ -0,0 +1,45 @@ +package me.chayapak1.chomens_bot.plugins; + +import me.chayapak1.chomens_bot.Bot; +import me.chayapak1.chomens_bot.data.player.PlayerEntry; +import me.chayapak1.chomens_bot.util.UUIDUtilities; +import net.kyori.adventure.text.Component; + +// i don't really want to give players the exact bot selector, +// since that can actually be used to harm the bot, +// but it's used for the command suggestions +// (not sure what are some other ways to do it) +public class BotSelectorBroadcasterPlugin implements PlayersPlugin.Listener, CorePlugin.Listener { + private final Bot bot; + + private final String id; + + public BotSelectorBroadcasterPlugin (final Bot bot) { + this.bot = bot; + this.id = bot.config.namespace + "_selector"; // chomens_bot_selector, for example + + bot.players.addListener(this); + bot.core.addListener(this); + } + + @Override + public void playerJoined (final PlayerEntry target) { + sendSelector(UUIDUtilities.selector(target.profile.getId())); + } + + @Override + public void coreReady () { + sendSelector("@a"); + } + + private void sendSelector (final String playerSelector) { + bot.chat.actionBar( + Component.translatable( + "", + Component.text(id), + Component.text(UUIDUtilities.selector(bot.profile.getId())) + ), + playerSelector + ); + } +} diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/TeamJoinerPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/TeamJoinerPlugin.java deleted file mode 100644 index fb661eda..00000000 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/TeamJoinerPlugin.java +++ /dev/null @@ -1,77 +0,0 @@ -package me.chayapak1.chomens_bot.plugins; - -import me.chayapak1.chomens_bot.Bot; -import me.chayapak1.chomens_bot.data.team.Team; -import me.chayapak1.chomens_bot.util.UUIDUtilities; -import org.geysermc.mcprotocollib.network.event.session.ConnectedEvent; - -import java.util.concurrent.TimeUnit; - -// the name might sound confusing but it just adds the bot into its own team -public class TeamJoinerPlugin { - public final String teamName; - - private final Bot bot; - - public TeamJoinerPlugin (final Bot bot) { - this.bot = bot; - this.teamName = bot.config.teamName; - - bot.addListener(new Bot.Listener() { - @Override - public void connected (final ConnectedEvent event) { - TeamJoinerPlugin.this.connected(); - } - }); - - bot.executor.scheduleAtFixedRate(this::check, 0, 500, TimeUnit.MILLISECONDS); - } - - private void connected () { - addTeam(); - } - - public void check () { - try { - if (!bot.loggedIn) return; - - final Team team = bot.team.findTeamByName(teamName); - - if (team == null) { - addTeam(); - return; - } - - if (!team.players.contains(bot.username)) { - joinTeam(); - return; - } - - // checks if ONLY the bot is in the team, and not anyone else - if (team.players.size() == 1 && team.players.getFirst().equals(bot.username)) return; - - excludeOthers(); - } catch (final Exception e) { - bot.logger.error(e); - } - } - - private void addTeam () { - bot.core.run("minecraft:team add " + teamName); - joinTeam(); - } - - private void joinTeam () { - bot.core.run("minecraft:team join " + teamName + " " + bot.profile.getIdAsString()); - } - - private void excludeOthers () { - bot.core.run( - String.format( - "minecraft:team leave %s,team=%s]", - UUIDUtilities.exclusiveSelector(bot.profile.getId(), false), - teamName - ) - ); - } -} diff --git a/src/main/resources/default-config.yml b/src/main/resources/default-config.yml index b4137fc2..caa193a2 100644 --- a/src/main/resources/default-config.yml +++ b/src/main/resources/default-config.yml @@ -101,7 +101,6 @@ keys: weatherApiKey: 'key here' # weatherapi.com key namespace: 'default_chomens_bot' # useful when you make a clone of the bot -teamName: 'default_chomens_bot' # i recommend having this value the same as namespace core: # PLEASE give valid JSON component here