refactor: check server features using commands packet instead of tab completion (now tab completion is completely unused!)

This commit is contained in:
ChomeNS
2025-03-20 16:13:53 +07:00
parent 5587565325
commit bed87d2396
7 changed files with 72 additions and 60 deletions

View File

@@ -77,7 +77,7 @@ public class Bot extends SessionAdapter {
public CommandSpyPlugin commandSpy;
public PositionPlugin position;
public DatabasePlugin database;
public ServerPluginsManagerPlugin serverPluginsManager;
public ServerFeaturesPlugin serverFeatures;
public SelfCarePlugin selfCare;
public QueryPlugin query;
public ExtrasMessengerPlugin extrasMessenger;
@@ -129,7 +129,7 @@ public class Bot extends SessionAdapter {
this.chat = new ChatPlugin(this);
this.commandSpy = new CommandSpyPlugin(this);
this.position = new PositionPlugin(this);
this.serverPluginsManager = new ServerPluginsManagerPlugin(this);
this.serverFeatures = new ServerFeaturesPlugin(this);
this.selfCare = new SelfCarePlugin(this);
this.query = new QueryPlugin(this);
this.extrasMessenger = new ExtrasMessengerPlugin(this);

View File

@@ -37,7 +37,6 @@ import java.util.concurrent.TimeUnit;
public class CorePlugin extends PositionPlugin.Listener {
public static final int COMMAND_BLOCK_ID = 418;
public static final int REPEATING_COMMAND_BLOCK_ID = 537;
private final Bot bot;
@@ -158,7 +157,7 @@ public class CorePlugin extends PositionPlugin.Listener {
commandsPerTick++;
if (bot.serverPluginsManager.hasPlugin(ServerPluginsManagerPlugin.EXTRAS)) {
if (bot.serverFeatures.hasExtras) {
bot.session.send(new ServerboundSetCommandBlockPacket(
block,
command,
@@ -310,9 +309,7 @@ public class CorePlugin extends PositionPlugin.Listener {
new ServerboundSetCreativeModeSlotPacket(
(short) 36,
new ItemStack(
bot.serverPluginsManager.hasPlugin(ServerPluginsManagerPlugin.EXTRAS) ?
REPEATING_COMMAND_BLOCK_ID :
COMMAND_BLOCK_ID,
COMMAND_BLOCK_ID,
64,
dataComponents
)

View File

@@ -182,7 +182,7 @@ public class PositionPlugin extends Bot.Listener {
if (y > maxY + 500 || y < minY) {
String command = "/";
if (bot.serverPluginsManager.hasPlugin(ServerPluginsManagerPlugin.ESSENTIALS)) command += "essentials:";
if (bot.serverFeatures.hasEssentials) command += "essentials:";
command += String.format("tp ~ %s ~", maxY);

View File

@@ -34,8 +34,8 @@ public class SelfCarePlugin extends Bot.Listener {
public boolean visibility = false;
private int entityId;
private GameMode gamemode;
private int permissionLevel;
public GameMode gamemode;
public int permissionLevel;
private int positionPacketsPerSecond = 0;
private long usernameStartTime = System.currentTimeMillis();
@@ -103,8 +103,8 @@ public class SelfCarePlugin extends Bot.Listener {
public void check () {
final Configuration.SelfCare selfCares = bot.config.selfCare;
final boolean kaboom = bot.serverPluginsManager.hasPlugin(ServerPluginsManagerPlugin.EXTRAS);
final boolean hasEssentials = bot.serverPluginsManager.hasPlugin(ServerPluginsManagerPlugin.ESSENTIALS);
final boolean kaboom = bot.serverFeatures.hasExtras;
final boolean hasEssentials = bot.serverFeatures.hasEssentials;
final boolean creayun = bot.options.creayun;

View File

@@ -0,0 +1,62 @@
package me.chayapak1.chomens_bot.plugins;
import me.chayapak1.chomens_bot.Bot;
import org.geysermc.mcprotocollib.network.Session;
import org.geysermc.mcprotocollib.network.event.session.DisconnectedEvent;
import org.geysermc.mcprotocollib.network.packet.Packet;
import org.geysermc.mcprotocollib.protocol.data.game.command.CommandNode;
import org.geysermc.mcprotocollib.protocol.data.game.command.CommandType;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundCommandsPacket;
public class ServerFeaturesPlugin extends Bot.Listener {
private final Bot bot;
public boolean hasEssentials = false;
public boolean hasExtras = false;
public ServerFeaturesPlugin (Bot bot) {
this.bot = bot;
bot.addListener(this);
}
@Override
public void packetReceived (Session session, Packet packet) {
if (packet instanceof ClientboundCommandsPacket t_packet) packetReceived(t_packet);
}
public void packetReceived (ClientboundCommandsPacket packet) {
for (CommandNode node : packet.getNodes()) {
if (!node.isExecutable() || node.getType() != CommandType.LITERAL) continue;
final String name = node.getName();
if (name == null || !name.contains(":")) continue;
// 4 or 2?
if (bot.selfCare.permissionLevel < 4) {
// it'd be weird for servers that allow OP while not having OP
if (name.equals("minecraft:op")) hasExtras = true;
continue;
}
final String[] split = name.split(":");
if (split.length < 2) continue;
final String key = split[0].toLowerCase();
switch (key) {
case "extras" -> hasExtras = true;
case "essentials" -> hasEssentials = true;
}
}
}
@Override
public void disconnected (DisconnectedEvent event) {
hasEssentials = false;
hasExtras = false;
}
}

View File

@@ -1,47 +0,0 @@
package me.chayapak1.chomens_bot.plugins;
import org.geysermc.mcprotocollib.network.event.session.ConnectedEvent;
import org.geysermc.mcprotocollib.network.event.session.DisconnectedEvent;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundCommandSuggestionsPacket;
import me.chayapak1.chomens_bot.Bot;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
public class ServerPluginsManagerPlugin extends Bot.Listener {
public static final String EXTRAS = "Extras";
public static final String ESSENTIALS = "Essentials";
private final Bot bot;
public List<String> plugins = new ArrayList<>();
public ServerPluginsManagerPlugin (Bot bot) {
this.bot = bot;
bot.addListener(this);
}
@Override
public void connected (ConnectedEvent event) {
final CompletableFuture<ClientboundCommandSuggestionsPacket> future = bot.tabComplete.tabComplete("ver ");
future.thenApply((packet) -> {
final String[] matches = packet.getMatches();
// should i just use the plugins as the String array instead of a list?
plugins = new ArrayList<>(Arrays.asList(matches));
return packet;
});
}
@Override
public void disconnected (DisconnectedEvent event) {
plugins.clear();
}
public boolean hasPlugin (String plugin) { return plugins.contains(plugin); }
}