feat: ditch TeamJoinerPlugin to prevent people performing suspicious activities with the bot

This commit is contained in:
ChomeNS
2025-04-13 17:36:58 +07:00
parent ae03603e9d
commit de43240755
6 changed files with 48 additions and 82 deletions

View File

@@ -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);

View File

@@ -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();

View File

@@ -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
);
}
}

View File

@@ -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
)
);
}
}