From 210dd7aaa1d7af22766316fe0a30966d560b3381 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Sat, 22 Mar 2025 11:39:00 +0700 Subject: [PATCH] fix: some fixes to make the bot work on bagel fabric kaboom clone and also vanilla servers can't believe that paragraph in InfoCommand was wrong the whole time ("works on vanilla servers too") --- build-number.txt | 2 +- .../java/me/chayapak1/chomens_bot/Bot.java | 14 ++++++++++++++ .../chayapak1/chomens_bot/Configuration.java | 1 - .../chomens_bot/plugins/ChatPlugin.java | 2 +- .../chomens_bot/plugins/CorePlugin.java | 10 ++++++---- .../plugins/ExtrasMessengerPlugin.java | 2 ++ .../chomens_bot/plugins/PlayersPlugin.java | 12 +++++++++--- .../plugins/ServerFeaturesPlugin.java | 9 ++++++++- .../chomens_bot/util/StringUtilities.java | 19 +++++++++++++++++++ src/main/resources/default-config.yml | 2 -- 10 files changed, 60 insertions(+), 13 deletions(-) diff --git a/build-number.txt b/build-number.txt index 595d6a09..6fc392cb 100644 --- a/build-number.txt +++ b/build-number.txt @@ -1 +1 @@ -2054 \ No newline at end of file +2073 \ 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 80ce4a8a..ce702f02 100644 --- a/src/main/java/me/chayapak1/chomens_bot/Bot.java +++ b/src/main/java/me/chayapak1/chomens_bot/Bot.java @@ -18,6 +18,7 @@ import org.geysermc.mcprotocollib.protocol.data.game.entity.player.HandPreferenc import org.geysermc.mcprotocollib.protocol.data.game.setting.ChatVisibility; import org.geysermc.mcprotocollib.protocol.data.game.setting.ParticleStatus; import org.geysermc.mcprotocollib.protocol.data.game.setting.SkinPart; +import org.geysermc.mcprotocollib.protocol.packet.common.clientbound.ClientboundCustomPayloadPacket; import org.geysermc.mcprotocollib.protocol.packet.common.clientbound.ClientboundStoreCookiePacket; import org.geysermc.mcprotocollib.protocol.packet.common.clientbound.ClientboundTransferPacket; import org.geysermc.mcprotocollib.protocol.packet.common.serverbound.ServerboundClientInformationPacket; @@ -32,6 +33,7 @@ import org.geysermc.mcprotocollib.protocol.packet.login.clientbound.ClientboundL import org.geysermc.mcprotocollib.protocol.packet.login.clientbound.ClientboundLoginFinishedPacket; import org.geysermc.mcprotocollib.protocol.packet.login.serverbound.ServerboundCustomQueryAnswerPacket; +import java.nio.charset.StandardCharsets; import java.util.*; import java.util.concurrent.ExecutorService; import java.util.concurrent.ScheduledExecutorService; @@ -217,6 +219,7 @@ public class Bot extends SessionAdapter { else if (packet instanceof ClientboundTransferPacket t_packet) packetReceived(t_packet); else if (packet instanceof ClientboundStoreCookiePacket t_packet) packetReceived(t_packet); else if (packet instanceof ClientboundLoginCompressionPacket t_packet) packetReceived(t_packet); + else if (packet instanceof ClientboundCustomPayloadPacket t_packet) packetReceived(t_packet); } public void packetReceived (ClientboundLoginFinishedPacket packet) { @@ -242,6 +245,17 @@ public class Bot extends SessionAdapter { session.send(new ServerboundCustomQueryAnswerPacket(packet.getMessageId(), null)); } + public void packetReceived (ClientboundCustomPayloadPacket packet) { + if (!packet.getChannel().asString().equals("minecraft:register")) return; + + session.send( + new ServerboundCustomPayloadPacket( + Key.key("minecraft", "register"), + "\0".getBytes(StandardCharsets.UTF_8) + ) + ); + } + public void packetReceived (ClientboundCookieRequestPacket packet) { session.send( new ServerboundCookieResponsePacket( diff --git a/src/main/java/me/chayapak1/chomens_bot/Configuration.java b/src/main/java/me/chayapak1/chomens_bot/Configuration.java index ee6b65d3..1daa57e4 100644 --- a/src/main/java/me/chayapak1/chomens_bot/Configuration.java +++ b/src/main/java/me/chayapak1/chomens_bot/Configuration.java @@ -183,7 +183,6 @@ public class Configuration { public boolean coreCommandSpy = false; public boolean resolveSRV = true; public int reconnectDelay = 2000; - public boolean removeNamespaces = false; public int chatQueueDelay = 125; public EssentialsMessages essentialsMessages = new EssentialsMessages(); public CoreRateLimit coreRateLimit = new CoreRateLimit(); diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/ChatPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/ChatPlugin.java index 9b3c972e..af19b21e 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/ChatPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/ChatPlugin.java @@ -254,7 +254,7 @@ public class ChatPlugin extends Bot.Listener { if (message.startsWith("/")) { String removedMessage = message.substring(1); - if (bot.options.removeNamespaces) { + if (!bot.serverFeatures.hasNamespaces) { final String[] splittedSpace = removedMessage.split("\\s+"); // [minecraft:test, arg1, arg2, ...] final String[] splittedColon = splittedSpace[0].split(":"); // [minecraft, test] if (splittedColon.length >= 2) { diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/CorePlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/CorePlugin.java index 21bb67f2..beef0921 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/CorePlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/CorePlugin.java @@ -3,6 +3,7 @@ package me.chayapak1.chomens_bot.plugins; import com.google.gson.JsonSyntaxException; import me.chayapak1.chomens_bot.Bot; import me.chayapak1.chomens_bot.util.MathUtilities; +import me.chayapak1.chomens_bot.util.StringUtilities; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.JoinConfiguration; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; @@ -26,10 +27,7 @@ import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.inventory.S import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.player.ServerboundPlayerActionPacket; import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.player.ServerboundUseItemOnPacket; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ScheduledFuture; @@ -157,6 +155,8 @@ public class CorePlugin extends PositionPlugin.Listener { commandsPerTick++; + if (!bot.serverFeatures.hasNamespaces) command = StringUtilities.removeNamespace(command); + if (bot.serverFeatures.hasExtras) { bot.session.send(new ServerboundSetCommandBlockPacket( block, @@ -256,6 +256,8 @@ public class CorePlugin extends PositionPlugin.Listener { public void forceRunPlaceBlock (String command) { if (!ready || !bot.options.useCore) return; + if (!bot.serverFeatures.hasNamespaces) command = StringUtilities.removeNamespace(command); + final NbtMapBuilder blockEntityTagBuilder = NbtMap.builder(); blockEntityTagBuilder.putString("id", "minecraft:command_block"); diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/ExtrasMessengerPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/ExtrasMessengerPlugin.java index ac432432..22bdcae9 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/ExtrasMessengerPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/ExtrasMessengerPlugin.java @@ -67,6 +67,8 @@ public class ExtrasMessengerPlugin extends Bot.Listener { return; } + System.out.println("YES. supported"); + isSupported = true; final List channels = new ArrayList<>(); diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/PlayersPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/PlayersPlugin.java index 2fccf7a3..5d94b4e5 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/PlayersPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/PlayersPlugin.java @@ -298,7 +298,7 @@ public class PlayersPlugin extends Bot.Listener { } private void onLastKnownNameTick () { - if (!bot.loggedIn) return; + if (!bot.loggedIn || !bot.serverFeatures.hasNamespaces) return; // hasNamespaces also means vanilla/non-bukkit for (PlayerEntry target : new ArrayList<>(list)) { check(target); @@ -310,9 +310,15 @@ public class PlayersPlugin extends Bot.Listener { if (target == null) return; - pendingLeftPlayers.add(target); + if (!bot.serverFeatures.hasNamespaces) { + list.remove(target); - check(target); + for (Listener listener : listeners) listener.playerLeft(target); + } else { + pendingLeftPlayers.add(target); + + check(target); + } } @Override diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/ServerFeaturesPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/ServerFeaturesPlugin.java index 234e95b1..a88d0dd9 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/ServerFeaturesPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/ServerFeaturesPlugin.java @@ -11,6 +11,8 @@ import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.Clientbound public class ServerFeaturesPlugin extends Bot.Listener { private final Bot bot; + public boolean hasNamespaces = false; + public boolean hasEssentials = false; public boolean hasExtras = false; @@ -31,7 +33,11 @@ public class ServerFeaturesPlugin extends Bot.Listener { final String name = node.getName(); - if (name == null || !name.contains(":")) continue; + if (name == null) continue; + + if (name.contains(":")) hasNamespaces = true; + + if (!name.contains(":")) continue; // 4 or 2? if (bot.selfCare.permissionLevel < 4) { @@ -56,6 +62,7 @@ public class ServerFeaturesPlugin extends Bot.Listener { @Override public void disconnected (DisconnectedEvent event) { + hasNamespaces = false; hasEssentials = false; hasExtras = false; } diff --git a/src/main/java/me/chayapak1/chomens_bot/util/StringUtilities.java b/src/main/java/me/chayapak1/chomens_bot/util/StringUtilities.java index 8b4cae68..f7155da4 100644 --- a/src/main/java/me/chayapak1/chomens_bot/util/StringUtilities.java +++ b/src/main/java/me/chayapak1/chomens_bot/util/StringUtilities.java @@ -6,9 +6,28 @@ import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import java.nio.charset.CodingErrorAction; import java.nio.charset.StandardCharsets; +import java.util.Arrays; import java.util.Map; public class StringUtilities { + public static String removeNamespace (String command) { + String removedCommand = command; + + final String[] splittedSpace = removedCommand.split("\\s+"); // [minecraft:test, arg1, arg2, ...] + final String[] splittedColon = splittedSpace[0].split(":"); // [minecraft, test] + + if (splittedColon.length >= 2) { + removedCommand = String.join(":", Arrays.copyOfRange(splittedColon, 1, splittedColon.length)); + + if (splittedSpace.length > 1) { + removedCommand += " "; + removedCommand += String.join(" ", Arrays.copyOfRange(splittedSpace, 1, splittedSpace.length)); + } + } + + return removedCommand; + } + // https://stackoverflow.com/a/35148974/18518424 public static String truncateToFitUtf8ByteLength (String s, int maxBytes) { if (s == null) { diff --git a/src/main/resources/default-config.yml b/src/main/resources/default-config.yml index 6e9ad746..ad9b44e3 100644 --- a/src/main/resources/default-config.yml +++ b/src/main/resources/default-config.yml @@ -145,7 +145,6 @@ bots: # useChat - when the bot tellraws it will chat instead of using the core to run tellraw # coreCommandSpy - set to true if server supports enabling player's commandspy though command block # resolveSRV - whether to resolve SRV records on the server. the notchian minecraft doesn't resolve them - # removeNamespaces - when the bot sends a command it will remove like `minecraft:` for example if set to true # essentialsMessages - the messages in essentials that the bot uses for self care (no example is intentional) # coreRateLimit - will ignore commands if reached the ratelimit @@ -167,7 +166,6 @@ bots: # coreCommandSpy: false # resolveSRV: true # reconnectDelay: 2000 - # removeNamespaces: false # chatQueueDelay: 125 # coreRateLimit: # limit: 10