refactor: refactor everything to prepare for open source

This commit is contained in:
Chayapak 2025-02-19 15:01:12 +07:00
parent 57f13599e2
commit ac781cd224
Signed by: ChomeNS
SSH key fingerprint: SHA256:0YoxhdyXsgbc0nfeB2N6FYE60mxMU7DS4uCUMaw2mvA
13 changed files with 773 additions and 607 deletions

View file

@ -14,7 +14,7 @@ import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Parser {
public class ComponentParser {
public static final Pattern ARG_PATTERN = Pattern.compile("%(?:(\\d+)\\$)?([s%])");
public static final Map<String, String> language = loadJsonStringMap("language.json");

View file

@ -1,583 +1,19 @@
package me.chayapak1.testingbot;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.cloudburstmc.math.vector.Vector3i;
import org.geysermc.mcprotocollib.auth.GameProfile;
import org.geysermc.mcprotocollib.network.BuiltinFlags;
import org.geysermc.mcprotocollib.network.Session;
import org.geysermc.mcprotocollib.network.event.session.*;
import org.geysermc.mcprotocollib.network.packet.Packet;
import org.geysermc.mcprotocollib.network.tcp.TcpClientSession;
import org.geysermc.mcprotocollib.protocol.MinecraftProtocol;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundDisguisedChatPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundLoginPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundPlayerChatPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundSystemChatPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.entity.player.ClientboundPlayerPositionPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.level.ClientboundTagQueryPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.ServerboundChatCommandPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.ServerboundChatPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.level.ServerboundBlockEntityTagQueryPacket;
import org.geysermc.mcprotocollib.protocol.packet.login.clientbound.ClientboundCustomQueryPacket;
import org.geysermc.mcprotocollib.protocol.packet.login.clientbound.ClientboundLoginFinishedPacket;
import org.geysermc.mcprotocollib.protocol.packet.login.serverbound.ServerboundCustomQueryAnswerPacket;
import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.jse.JsePlatform;
import java.io.*;
import java.time.Instant;
import java.util.*;
import me.chayapak1.testingbot.bots.*;
public class Main {
private static void keepAlive () {
final Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {}
}, 0, 30 * 1000);
}
public static void main_core_test (String[] args) {
keepAlive();
final MinecraftProtocol protocol = new MinecraftProtocol("core");
final Session session = new TcpClientSession("kaboom.pw", 25565, protocol);
final Core core = new Core(session);
session.addListener(new SessionAdapter() {
@Override
public void packetReceived(Session session, Packet packet) {
if (packet instanceof ClientboundPlayerPositionPacket) {
core.run("say testing core Works.");
for (int i = 0; i < 50; i++) {
core.run("msg chayapak " + i);
}
}
}
});
session.connect();
}
public static void main (String[] args) {
keepAlive();
final MinecraftProtocol protocol = new MinecraftProtocol("vm");
final Session session = new TcpClientSession(args[0], Integer.parseInt(args[1]), protocol);
final Core core = new Core(session);
session.setFlag(BuiltinFlags.ATTEMPT_SRV_RESOLVE, false);
session.addListener(new SessionAdapter() {
private boolean loggedIn = false;
private Process process;
@Override
public void packetReceived(Session session, Packet packet) {
if (packet instanceof ClientboundPlayerPositionPacket) {
if (loggedIn) return;
loggedIn = true;
new Thread(() -> {
try {
process = new ProcessBuilder(
"/usr/bin/qemu-system-x86_64",
"-nographic",
"-enable-kvm",
"-cdrom",
"image.iso",
"-m",
"512M",
"-serial",
"mon:stdio",
"-net",
"user",
"-nic",
"user",
"-drive",
"format=raw,file=disk.img",
"-display",
"none",
"-netdev",
"user,id=net0",
"-device",
"rtl8139,netdev=net0"
).start();
final BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String output;
while ((output = reader.readLine()) != null) {
if (output.isBlank()) continue;
System.out.println(output);
core.run("minecraft:tellraw @a " +
GsonComponentSerializer.gson().serialize(
Component.text(
output
// INSANE regex.
.replaceAll("[\\u001B\\u009B][\\[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?(?:\\u0007|\\u001B\\u005C|\\u009C))|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))", "")
.strip()
)
)
);
}
} catch (IOException e) {
core.run("minecraft:tellraw @a " + GsonComponentSerializer.gson().serialize(getErrorComponent(e)));
}
}).start();
} else if (packet instanceof ClientboundCustomQueryPacket t_packet) {
session.send(new ServerboundCustomQueryAnswerPacket(t_packet.getMessageId(), null));
} else if (packet instanceof ClientboundPlayerChatPacket t_packet) {
if (process == null) return;
final String content = t_packet.getContent();
try {
final OutputStream output = process.getOutputStream();
if (content.trim().equalsIgnoreCase("sendc")) {
// ALSO clear the core queue
core.clearQueue();
output.write("\u0003".getBytes());
output.flush();
return;
}
if (!content.toLowerCase().startsWith("send")) return;
final String input = content.substring("send".length());
output.write((input.stripLeading() + "\n").getBytes());
output.flush();
} catch (IOException e) {
core.run("minecraft:tellraw @a " + GsonComponentSerializer.gson().serialize(getErrorComponent(e)));
}
}
}
});
session.connect();
}
private static Component getErrorComponent (Throwable e) {
final String stacktrace = getStacktraceString(e);
return Component
.text(e.getMessage() != null ? e.toString() : "Error")
.color(NamedTextColor.RED)
.hoverEvent(
HoverEvent.showText(
Component.text(stacktrace)
.color(NamedTextColor.RED)
)
);
}
private static String getStacktraceString (Throwable throwable) {
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw, true);
throwable.printStackTrace(pw);
return sw.getBuffer().toString();
}
public static void main_bot (String[] args) {
final Timer timer = new Timer();
final MinecraftProtocol protocol = new MinecraftProtocol("botty bot");
final Session session = new TcpClientSession("chipmunk.land", 25565, protocol);
final Core core = new Core(session);
final boolean[] loggedIn = {false};
final GameProfile[] profile = new GameProfile[1];
final List<String> playersSecrets = new ArrayList<>();
final List<String> dataSecrets = new ArrayList<>();
final Map<String, Object> playerSNBTs = new HashMap<>();
final SNBTParser parser = new SNBTParser();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if (!loggedIn[0]) return;
final String secret = UUID.randomUUID().toString();
playersSecrets.add(secret);
core.run(
String.format(
"minecraft:tellraw @a[name=\"%s\"] %s",
profile[0].getName().replace("\\", "\\\\").replace("\"", "\\\""),
GsonComponentSerializer.gson().serialize(
Component.text(secret)
.append(
Component.selector("@a")
.separator(Component.empty())
)
)
)
);
}
}, 0, 2000);
session.addListener(new SessionAdapter() {
@Override
public void packetReceived(Session session, Packet packet) {
try {
packetReceived(packet);
} catch (Exception e) {
e.printStackTrace();
}
}
private void packetReceived (Packet packet) {
if (packet instanceof ClientboundLoginFinishedPacket t_packet) {
profile[0] = t_packet.getProfile();
} else if (packet instanceof ClientboundLoginPacket t_packet) {
System.out.println("logged in, entity id " + t_packet.getEntityId());
} else if (packet instanceof ClientboundPlayerPositionPacket) {
loggedIn[0] = true;
} else if (packet instanceof ClientboundPlayerChatPacket t_packet) {
final String message = t_packet.getContent();
if (!message.equalsIgnoreCase("bot list")) return;
final List<String> bots = new ArrayList<>();
for (Map.Entry<String, Object> entry : playerSNBTs.entrySet()) {
if (!(entry.getValue() instanceof Map<?, ?> compound)) continue;
final Object objectPos = compound.get("Pos");
if (!(objectPos instanceof List<?> pos) || !(pos.get(0) instanceof Double)) continue;
final Double[] posArray = pos.toArray(Double[]::new);
final Object objectRotation = compound.get("Rotation");
if (!(objectRotation instanceof List<?> rotation) || !(rotation.get(0) instanceof Float)) continue;
final Float[] rotationArray = rotation.toArray(Float[]::new);
if (
((posArray[0] - Math.floor(posArray[0])) != 0.5) &&
((posArray[1] - Math.floor(posArray[1])) != 0.0) &&
((posArray[2] - Math.floor(posArray[2])) != 0.5) &&
((rotationArray[0] - Math.floor(rotationArray[0])) != 0.0) &&
((rotationArray[1] - Math.floor(rotationArray[1])) != 0.0)
) continue;
bots.add(entry.getKey());
}
final Component component = Component
.text("Bot list:")
.color(NamedTextColor.GREEN)
.append(Component.newline())
.append(
Component.join(
JoinConfiguration.newlines(),
bots.stream().map(
bot -> Component.text(bot).color(NamedTextColor.AQUA)
).toList()
)
);
core.run("minecraft:tellraw @a " + GsonComponentSerializer.gson().serialize(component));
} else if (packet instanceof ClientboundSystemChatPacket t_packet) {
if (!(t_packet.getContent() instanceof TextComponent textComponent)) return;
final String targetSecret = textComponent.content();
if (playersSecrets.contains(targetSecret)) {
playersSecrets.remove(targetSecret);
final List<Component> children = t_packet.getContent().children();
final Component selectorComponent = children.getFirst();
if (selectorComponent == null) return;
final List<Component> selectorChildren = selectorComponent.children();
final List<String> players = new ArrayList<>();
for (Component child : selectorChildren) {
if (
!(child instanceof TextComponent childText) ||
child.hoverEvent() == null ||
child.clickEvent() == null
) continue;
players.add(childText.content());
}
for (Map.Entry<String, Object> entry : playerSNBTs.entrySet()) {
if (!players.contains(entry.getKey())) playerSNBTs.remove(entry.getKey());
}
int i = 50;
for (String player : players) {
final String secret = UUID.randomUUID().toString();
dataSecrets.add(secret);
timer.schedule(new TimerTask() {
@Override
public void run() {
core.run(
String.format(
"minecraft:tellraw @a[name=\"%s\"] %s",
profile[0].getName().replace("\\", "\\\\").replace("\"", "\\\""),
GsonComponentSerializer.gson().serialize(
Component.text(secret)
.append(
Component.entityNBT(
"",
String.format(
"@a[limit=1,name=\"%s\"]",
player.replace("\\", "\\\\").replace("\"", "\\\"")
)
)
)
.append(Component.text(player))
)
)
);
}
}, i);
i += 50;
}
} else if (dataSecrets.contains(targetSecret)) {
dataSecrets.remove(targetSecret);
final List<Component> children = t_packet.getContent().children();
if (children.size() != 2) return;
final Component componentData = children.getFirst();
if (!(componentData instanceof TextComponent textComponentData)) return;
final Component playerNameComponent = children.getLast();
if (!(playerNameComponent instanceof TextComponent playerNameTextComponent)) return;
final String playerName = playerNameTextComponent.content();
final String data = textComponentData.content();
timer.schedule(new TimerTask() {
@Override
public void run() {
final Object parsed = parser.parse(data);
playerSNBTs.put(playerName, parsed);
}
}, 1);
}
}
}
@Override
public void disconnected(DisconnectedEvent event) {
if (event.getCause() != null) event.getCause().printStackTrace();
else System.out.println(Parser.parse(event.getReason()));
}
});
session.connect();
}
public static void sendChatMessage (String message, Session session) {
if (message.startsWith("/")) {
session.send(new ServerboundChatCommandPacket(message.substring(1)));
} else {
session.send(new ServerboundChatPacket(
stripIllegalCharacters(message),
Instant.now().toEpochMilli(),
0L,
null,
0,
new BitSet()
));
final String botName = String.join(" ", args).trim().toLowerCase();
switch (botName) {
case "bots" -> new BotsBot();
case "chatparser" -> new ChatParserBot();
case "corespy" -> new CoreSpyBot();
case "coretest" -> new CoreTestBot();
case "lua" -> new LuaBot();
case "vm" -> new VMBot();
default -> System.err.println("Invalid bot name: " + botName);
}
}
private static String stripIllegalCharacters (String string) {
final StringBuilder replaced = new StringBuilder();
for (char character : string.toCharArray()) {
if (
character == '\u007f' ||
// check if character is a control code, also space is the first character after
// the control characters so this is why we can do `character < ' '`
character < ' '
) continue;
if (character == '§') replaced.append('&');
else replaced.append(character);
}
return replaced.toString();
}
public static void main_lua (String[] args) {
keepAlive();
final Globals globals = JsePlatform.standardGlobals();
final LuaValue chunk = globals.loadfile("main.lua");
chunk.invoke();
}
public static void main_spy (String[] args) {
final MinecraftProtocol protocol = new MinecraftProtocol("spy but java");
final Session session = new TcpClientSession("chipmunk.land", 25565, protocol);
keepAlive();
final int[] transactionId = {0};
final Map<Integer, Vector3i> transactionsMap = new HashMap<>();
final boolean[] loggedIn = {false};
new Thread(() -> {
try {
while (true) {
if (!loggedIn[0]) continue;
for (int y = 0; y < 3; y++) {
for (int z = 0; z < 15; z++) {
for (int x = 0; x < 15; x++) {
final int id = transactionId[0]++;
final Vector3i location = Vector3i.from(x + (-440784), y + (-64), z + (406064));
transactionsMap.put(id, location);
System.out.println(location);
session.send(new ServerboundBlockEntityTagQueryPacket(
id,
location
));
// try {
// Thread.sleep(50);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}).start();
session.addListener(new SessionAdapter() {
@Override
public void packetReceived(Session session, Packet packet) {
if (packet instanceof ClientboundLoginPacket) {
loggedIn[0] = true;
} else if (packet instanceof ClientboundTagQueryPacket t_packet) {
final Vector3i location = transactionsMap.get(t_packet.getTransactionId());
if (location == null) return;
transactionsMap.remove(t_packet.getTransactionId());
if (t_packet.getNbt() == null) return;
final String command = t_packet.getNbt().getString("Command");
if (command == null || command.isEmpty()) return;
System.out.printf("block %s executed %s%n", location, command);
}
}
});
session.connect();
}
public static void main_parser (String[] args) {
final MinecraftProtocol protocol = new MinecraftProtocol("some bot");
final Session session = new TcpClientSession("chipmunk.land", 25565, protocol);
final Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {}
}, 0, 1000);
session.addListener(new SessionAdapter() {
@Override
public void packetReceived(Session session, Packet packet) {
if (packet instanceof ClientboundLoginFinishedPacket) {
System.out.println("login finished");
} else if (packet instanceof ClientboundLoginPacket t_packet) {
session.send(new ServerboundChatPacket(
"Hello! I have logged in. My entity ID is " + t_packet.getEntityId(),
Instant.now().toEpochMilli(),
0L,
null,
0,
new BitSet()
));
} else if (packet instanceof ClientboundSystemChatPacket t_packet) {
parseAndPrint(t_packet.getContent());
} else if (packet instanceof ClientboundPlayerChatPacket t_packet) {
parseAndPrint(t_packet.getUnsignedContent());
} else if (packet instanceof ClientboundDisguisedChatPacket t_packet) {
parseAndPrint(t_packet.getMessage());
}
}
@Override
public void packetError(PacketErrorEvent event) {
if (event.getCause() != null) event.getCause().printStackTrace();
event.setSuppress(true);
}
@Override
public void disconnected(DisconnectedEvent event) {
if (event.getCause() != null) event.getCause().printStackTrace();
System.out.println("disconnect " + Parser.parse(event.getReason()));
}
});
session.connect();
}
public static void parseAndPrint(Component component) {
System.out.println(Parser.parse(component));
}
}

View file

@ -2,6 +2,13 @@ package me.chayapak1.testingbot;
import java.util.*;
// Author: ChatGPT
// this is ported from the javascript version
// (which ChatGPT has made), and also
// ported by ChatGPT itself
// i had to make some slight modifications of course,
// but it works perfectly fine
public class SNBTParser {
private int index;
private String input;

View file

@ -0,0 +1,53 @@
package me.chayapak1.testingbot;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import java.io.PrintWriter;
import java.io.StringWriter;
// all in 1 class unlike ChomeNS Bot because laziness
public class Utilities {
public static Component getErrorComponent (Throwable e) {
final String stacktrace = getStacktraceString(e);
return Component
.text(e.getMessage() != null ? e.toString() : "Error")
.color(NamedTextColor.RED)
.hoverEvent(
HoverEvent.showText(
Component.text(stacktrace)
.color(NamedTextColor.RED)
)
);
}
public static String getStacktraceString (Throwable throwable) {
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw, true);
throwable.printStackTrace(pw);
return sw.getBuffer().toString();
}
public static String stripIllegalCharacters (String string) {
final StringBuilder replaced = new StringBuilder();
for (char character : string.toCharArray()) {
if (
character == '\u007f' ||
// check if character is a control code, also space is the first character after
// the control characters so this is why we can do `character < ' '`
character < ' '
) continue;
if (character == '§') replaced.append('&');
else replaced.append(character);
}
return replaced.toString();
}
}

View file

@ -0,0 +1,59 @@
package me.chayapak1.testingbot.base;
import org.geysermc.mcprotocollib.network.Session;
import org.geysermc.mcprotocollib.network.event.session.DisconnectedEvent;
import org.geysermc.mcprotocollib.network.event.session.SessionAdapter;
import org.geysermc.mcprotocollib.network.packet.Packet;
import org.geysermc.mcprotocollib.network.tcp.TcpClientSession;
import org.geysermc.mcprotocollib.protocol.MinecraftProtocol;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundLoginPacket;
import java.util.Timer;
import java.util.TimerTask;
public class BaseBot extends SessionAdapter {
public Session session;
public boolean loggedIn = false;
public Chat chat;
public Core core;
public BaseBot (String username) {
// keepalive process
final Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
}
}, 0, 30 * 1000);
final MinecraftProtocol protocol = new MinecraftProtocol(username);
session = new TcpClientSession(
System.getProperty("host", "localhost"),
Integer.parseInt(System.getProperty("port", "25565")),
protocol
);
this.chat = new Chat(this);
this.core = new Core(this);
session.addListener(this);
}
public void connect () {
session.connect(false);
}
@Override
public void packetReceived (Session session, Packet packet) {
if (packet instanceof ClientboundLoginPacket) loggedIn = true;
}
@Override
public void disconnected(DisconnectedEvent event) {
loggedIn = false;
}
}

View file

@ -0,0 +1,46 @@
package me.chayapak1.testingbot.base;
import me.chayapak1.testingbot.Utilities;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.ServerboundChatCommandPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.ServerboundChatPacket;
import java.time.Instant;
import java.util.BitSet;
public class Chat {
private final BaseBot bot;
public Chat (BaseBot bot) {
this.bot = bot;
}
public void send (String message) {
if (message.startsWith("/")) {
bot.session.send(
new ServerboundChatCommandPacket(
message.substring(1, Math.min(message.length(), 256 + 1))
)
);
} else {
bot.session.send(
new ServerboundChatPacket(
Utilities.stripIllegalCharacters(message).substring(0, Math.min(message.length(), 256)),
Instant.now().toEpochMilli(),
0L,
null,
0,
new BitSet()
)
);
}
}
public void tellraw (Component component) {
bot.core.run(
"minecraft:tellraw @a " +
GsonComponentSerializer.gson().serialize(component)
);
}
}

View file

@ -1,39 +1,33 @@
package me.chayapak1.testingbot;
package me.chayapak1.testingbot.base;
import org.cloudburstmc.math.vector.Vector3i;
import org.geysermc.mcprotocollib.network.Session;
import org.geysermc.mcprotocollib.network.event.session.SessionAdapter;
import org.geysermc.mcprotocollib.network.packet.Packet;
import org.geysermc.mcprotocollib.protocol.data.game.level.block.CommandBlockMode;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundLoginPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.entity.player.ClientboundPlayerPositionPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.ServerboundChatCommandPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.inventory.ServerboundSetCommandBlockPacket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
public class Core {
private static final Logger log = LoggerFactory.getLogger(Core.class);
private final Session session;
private final BaseBot bot;
private Vector3i from;
private Vector3i to;
private Vector3i block = null;
private boolean loggedIn = false;
private final List<String> queue = Collections.synchronizedList(new ArrayList<>());
private final List<String> queue = new ArrayList<>();
private final List<Listener> listeners = new ArrayList<>();
public Core (Session session) {
this.session = session;
public Core (BaseBot bot) {
this.bot = bot;
session.addListener(new SessionAdapter() {
this.bot.session.addListener(new SessionAdapter() {
@Override
public void packetReceived (Session session, Packet packet) {
if (packet instanceof ClientboundPlayerPositionPacket t_packet) Core.this.packetReceived(t_packet);
@ -45,23 +39,23 @@ public class Core {
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if (!loggedIn) return;
if (!bot.loggedIn) return;
processCoreQueue();
processCoreQueue();
}
}, 0, 15);
}
private void processCoreQueue () {
if (queue.size() > 25) queue.clear();
if (queue.size() > 50) queue.clear();
if (queue.isEmpty()) return;
final String command = queue.getFirst();
queue.removeFirst();
forceRun(command);
queue.removeFirst();
}
public void clearQueue () {
@ -69,24 +63,22 @@ public class Core {
}
private void packetReceived (ClientboundPlayerPositionPacket packet) {
if (!loggedIn) loggedIn = true;
from = Vector3i.from(
packet.getPosition().getFloorX() - 1,
packet.getPosition().getFloorX() - 4,
-63,
packet.getPosition().getFloorZ() - 1
packet.getPosition().getFloorZ() - 4
);
to = Vector3i.from(
packet.getPosition().getFloorX() + 1,
packet.getPosition().getFloorX() + 3,
-63,
packet.getPosition().getFloorZ() + 1
packet.getPosition().getFloorZ() + 3
);
block = Vector3i.from(from);
final String command = String.format(
"fill %d %d %d %d %d %d command_block",
"/fill %d %d %d %d %d %d command_block",
from.getX(),
from.getY(),
@ -97,19 +89,21 @@ public class Core {
to.getZ()
);
session.send(new ServerboundChatCommandPacket(command));
bot.chat.send(command);
for (Listener listener : listeners) listener.refilled();
}
public void run (String command) {
if (!loggedIn || command.length() > 32767) return;
if (!bot.loggedIn || command.length() > 32767) return;
queue.add(command);
}
public void forceRun (String command) {
if (!loggedIn || command.length() > 32767) return;
if (!bot.loggedIn || command.length() > 32767) return;
session.send(new ServerboundSetCommandBlockPacket(
bot.session.send(new ServerboundSetCommandBlockPacket(
block,
command,
CommandBlockMode.AUTO,
@ -142,4 +136,10 @@ public class Core {
block = Vector3i.from(x, y, z);
}
public void addListener (Listener listener) { listeners.add(listener); }
public static class Listener {
public void refilled () {}
}
}

View file

@ -0,0 +1,232 @@
package me.chayapak1.testingbot.bots;
import me.chayapak1.testingbot.ComponentParser;
import me.chayapak1.testingbot.SNBTParser;
import me.chayapak1.testingbot.base.BaseBot;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.geysermc.mcprotocollib.auth.GameProfile;
import org.geysermc.mcprotocollib.network.Session;
import org.geysermc.mcprotocollib.network.event.session.DisconnectedEvent;
import org.geysermc.mcprotocollib.network.event.session.SessionAdapter;
import org.geysermc.mcprotocollib.network.packet.Packet;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundLoginPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundPlayerChatPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundSystemChatPacket;
import org.geysermc.mcprotocollib.protocol.packet.login.clientbound.ClientboundLoginFinishedPacket;
import java.util.*;
// didn't really refactor due to laziness
// expect a lot of trash code here
public class BotsBot {
private final BaseBot bot;
public BotsBot () {
this.bot = new BaseBot("botty bot");
final GameProfile[] profile = new GameProfile[1];
final List<String> playersSecrets = new ArrayList<>();
final List<String> dataSecrets = new ArrayList<>();
final Map<String, Object> playerSNBTs = new HashMap<>();
final SNBTParser parser = new SNBTParser();
final Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if (!bot.loggedIn) return;
final String secret = UUID.randomUUID().toString();
playersSecrets.add(secret);
bot.core.run(
String.format(
"minecraft:tellraw @a[name=\"%s\"] %s",
profile[0].getName().replace("\\", "\\\\").replace("\"", "\\\""),
GsonComponentSerializer.gson().serialize(
Component.text(secret)
.append(
Component.selector("@a")
.separator(Component.empty())
)
)
)
);
}
}, 0, 2000);
bot.session.addListener(new SessionAdapter() {
@Override
public void packetReceived(Session session, Packet packet) {
try {
packetReceived(packet);
} catch (Exception e) {
e.printStackTrace();
}
}
private void packetReceived (Packet packet) {
if (packet instanceof ClientboundLoginFinishedPacket t_packet) {
profile[0] = t_packet.getProfile();
} else if (packet instanceof ClientboundLoginPacket t_packet) {
System.out.println("logged in, entity id " + t_packet.getEntityId());
} else if (packet instanceof ClientboundPlayerChatPacket t_packet) {
final String message = t_packet.getContent();
if (!message.equalsIgnoreCase("bot list")) return;
final List<String> bots = new ArrayList<>();
for (Map.Entry<String, Object> entry : playerSNBTs.entrySet()) {
if (!(entry.getValue() instanceof Map<?, ?> compound)) continue;
final Object objectPos = compound.get("Pos");
if (!(objectPos instanceof List<?> pos) || !(pos.get(0) instanceof Double)) continue;
final Double[] posArray = pos.toArray(Double[]::new);
final Object objectRotation = compound.get("Rotation");
if (!(objectRotation instanceof List<?> rotation) || !(rotation.get(0) instanceof Float)) continue;
final Float[] rotationArray = rotation.toArray(Float[]::new);
if (
((posArray[0] - Math.floor(posArray[0])) != 0.5) &&
((posArray[1] - Math.floor(posArray[1])) != 0.0) &&
((posArray[2] - Math.floor(posArray[2])) != 0.5) &&
((rotationArray[0] - Math.floor(rotationArray[0])) != 0.0) &&
((rotationArray[1] - Math.floor(rotationArray[1])) != 0.0)
) continue;
bots.add(entry.getKey());
}
final Component component = Component
.text("Bot list:")
.color(NamedTextColor.GREEN)
.append(Component.newline())
.append(
Component.join(
JoinConfiguration.newlines(),
bots.stream().map(
bot -> Component.text(bot).color(NamedTextColor.AQUA)
).toList()
)
);
bot.chat.tellraw(component);
} else if (packet instanceof ClientboundSystemChatPacket t_packet) {
if (!(t_packet.getContent() instanceof TextComponent textComponent)) return;
final String targetSecret = textComponent.content();
if (playersSecrets.contains(targetSecret)) {
playersSecrets.remove(targetSecret);
final List<Component> children = t_packet.getContent().children();
final Component selectorComponent = children.getFirst();
if (selectorComponent == null) return;
final List<Component> selectorChildren = selectorComponent.children();
final List<String> players = new ArrayList<>();
for (Component child : selectorChildren) {
if (
!(child instanceof TextComponent childText) ||
child.hoverEvent() == null ||
child.clickEvent() == null
) continue;
players.add(childText.content());
}
for (Map.Entry<String, Object> entry : playerSNBTs.entrySet()) {
if (!players.contains(entry.getKey())) playerSNBTs.remove(entry.getKey());
}
int i = 50;
for (String player : players) {
final String secret = UUID.randomUUID().toString();
dataSecrets.add(secret);
timer.schedule(new TimerTask() {
@Override
public void run() {
bot.core.run(
String.format(
"minecraft:tellraw @a[name=\"%s\"] %s",
profile[0].getName().replace("\\", "\\\\").replace("\"", "\\\""),
GsonComponentSerializer.gson().serialize(
Component.text(secret)
.append(
Component.entityNBT(
"",
String.format(
"@a[limit=1,name=\"%s\"]",
player.replace("\\", "\\\\").replace("\"", "\\\"")
)
)
)
.append(Component.text(player))
)
)
);
}
}, i);
i += 50;
}
} else if (dataSecrets.contains(targetSecret)) {
dataSecrets.remove(targetSecret);
final List<Component> children = t_packet.getContent().children();
if (children.size() != 2) return;
final Component componentData = children.getFirst();
if (!(componentData instanceof TextComponent textComponentData)) return;
final Component playerNameComponent = children.getLast();
if (!(playerNameComponent instanceof TextComponent playerNameTextComponent)) return;
final String playerName = playerNameTextComponent.content();
final String data = textComponentData.content();
timer.schedule(new TimerTask() {
@Override
public void run() {
final Object parsed = parser.parse(data);
playerSNBTs.put(playerName, parsed);
}
}, 1);
}
}
}
@Override
public void disconnected(DisconnectedEvent event) {
if (event.getCause() != null) event.getCause().printStackTrace();
else System.out.println(ComponentParser.parse(event.getReason()));
}
});
bot.connect();
}
}

View file

@ -0,0 +1,58 @@
package me.chayapak1.testingbot.bots;
import me.chayapak1.testingbot.ComponentParser;
import me.chayapak1.testingbot.base.BaseBot;
import net.kyori.adventure.text.Component;
import org.geysermc.mcprotocollib.network.Session;
import org.geysermc.mcprotocollib.network.event.session.DisconnectedEvent;
import org.geysermc.mcprotocollib.network.event.session.PacketErrorEvent;
import org.geysermc.mcprotocollib.network.event.session.SessionAdapter;
import org.geysermc.mcprotocollib.network.packet.Packet;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundDisguisedChatPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundLoginPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundPlayerChatPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundSystemChatPacket;
import org.geysermc.mcprotocollib.protocol.packet.login.clientbound.ClientboundLoginFinishedPacket;
// this is the first ever bot made in this testing-bot project
public class ChatParserBot {
public ChatParserBot () {
final BaseBot bot = new BaseBot("some bot");
bot.session.addListener(new SessionAdapter() {
@Override
public void packetReceived(Session session, Packet packet) {
if (packet instanceof ClientboundLoginFinishedPacket) {
System.out.println("login finished");
} else if (packet instanceof ClientboundLoginPacket t_packet) {
bot.chat.send("Hello! I have logged in. My entity ID is " + t_packet.getEntityId());
} else if (packet instanceof ClientboundSystemChatPacket t_packet) {
parseAndPrint(t_packet.getContent());
} else if (packet instanceof ClientboundPlayerChatPacket t_packet) {
parseAndPrint(t_packet.getUnsignedContent());
} else if (packet instanceof ClientboundDisguisedChatPacket t_packet) {
parseAndPrint(t_packet.getMessage());
}
}
@Override
public void packetError(PacketErrorEvent event) {
if (event.getCause() != null) event.getCause().printStackTrace();
event.setSuppress(true);
}
@Override
public void disconnected(DisconnectedEvent event) {
if (event.getCause() != null) event.getCause().printStackTrace();
System.out.println("disconnect " + ComponentParser.parse(event.getReason()));
}
});
bot.connect();
}
public void parseAndPrint (Component component) {
System.out.println(ComponentParser.parse(component));
}
}

View file

@ -0,0 +1,94 @@
package me.chayapak1.testingbot.bots;
import me.chayapak1.testingbot.base.BaseBot;
import org.cloudburstmc.math.vector.Vector3i;
import org.geysermc.mcprotocollib.network.Session;
import org.geysermc.mcprotocollib.network.event.session.SessionAdapter;
import org.geysermc.mcprotocollib.network.packet.Packet;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.level.ClientboundTagQueryPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.level.ServerboundBlockEntityTagQueryPacket;
import java.util.HashMap;
import java.util.Map;
// originally this was written in javascript, but it was really slow
// so i ported it to java, which is why the username is "spy but java"
public class CoreSpyBot {
private final BaseBot bot;
// core size
private final int x = Integer.parseInt(System.getProperty("x", "15"));
private final int y = Integer.parseInt(System.getProperty("y", "3"));
private final int z = Integer.parseInt(System.getProperty("z", "15"));
// position
private final int positionX = Integer.parseInt(System.getProperty("x", "0"));
private final int positionY = Integer.parseInt(System.getProperty("y", "0"));
private final int positionZ = Integer.parseInt(System.getProperty("z", "0"));
public CoreSpyBot () {
this.bot = new BaseBot("spy but java");
final int[] transactionId = {0};
final Map<Integer, Vector3i> transactionsMap = new HashMap<>();
new Thread(() -> {
try {
while (true) {
if (!bot.loggedIn) continue;
for (int y = 0; y < this.y; y++) {
for (int z = 0; z < this.z; z++) {
for (int x = 0; x < this.x; x++) {
final int id = transactionId[0]++;
final Vector3i location = Vector3i.from(x + positionX, y + positionY, z + positionZ);
transactionsMap.put(id, location);
System.out.println(location);
bot.session.send(new ServerboundBlockEntityTagQueryPacket(
id,
location
));
// try {
// Thread.sleep(50);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}).start();
bot.session.addListener(new SessionAdapter() {
@Override
public void packetReceived(Session session, Packet packet) {
if (packet instanceof ClientboundTagQueryPacket t_packet) {
final Vector3i location = transactionsMap.get(t_packet.getTransactionId());
if (location == null) return;
transactionsMap.remove(t_packet.getTransactionId());
if (t_packet.getNbt() == null) return;
final String command = t_packet.getNbt().getString("Command");
if (command == null || command.isEmpty()) return;
System.out.printf("block %s executed %s%n", location, command);
}
}
});
bot.connect();
}
}

View file

@ -0,0 +1,27 @@
package me.chayapak1.testingbot.bots;
import me.chayapak1.testingbot.base.BaseBot;
import me.chayapak1.testingbot.base.Core;
public class CoreTestBot extends Core.Listener {
private final BaseBot bot;
public CoreTestBot () {
this.bot = new BaseBot("core");
bot.core.addListener(this);
bot.connect();
}
public void refilled () {
try {
bot.core.run("say testing core Works.");
for (int i = 0; i < 40; i++) {
bot.core.run("say " + i);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,24 @@
package me.chayapak1.testingbot.bots;
import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.jse.JsePlatform;
import java.util.Timer;
import java.util.TimerTask;
// not really a bot itself
public class LuaBot {
public LuaBot () {
final Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
}
}, 0, 30 * 1000);
final Globals globals = JsePlatform.standardGlobals();
final LuaValue chunk = globals.loadfile("main.lua");
chunk.invoke();
}
}

View file

@ -0,0 +1,130 @@
package me.chayapak1.testingbot.bots;
import me.chayapak1.testingbot.Utilities;
import me.chayapak1.testingbot.base.BaseBot;
import me.chayapak1.testingbot.base.Core;
import net.kyori.adventure.text.Component;
import org.geysermc.mcprotocollib.network.Session;
import org.geysermc.mcprotocollib.network.event.session.SessionAdapter;
import org.geysermc.mcprotocollib.network.packet.Packet;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundPlayerChatPacket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
// requires qemu-system-x86_64 in /usr/bin/
// image.iso = cdrom, you can just `touch image.iso` if you don't want it
// disk.img = hard disk image (raw format), `qemu-img create -f raw disk.img 10G`
public class VMBot extends SessionAdapter {
private final BaseBot bot;
private boolean started = false;
private Process process;
public VMBot () {
this.bot = new BaseBot("vm");
bot.session.addListener(this);
bot.core.addListener(new Core.Listener() {
@Override
public void refilled() {
VMBot.this.coreRefilled();
}
});
bot.connect();
}
private void coreRefilled () {
if (started) return;
started = true;
new Thread(() -> {
try {
process = new ProcessBuilder(
"/usr/bin/qemu-system-x86_64",
"-nographic",
"-enable-kvm",
"-cdrom",
"image.iso",
"-m",
"512M",
"-serial",
"mon:stdio",
"-net",
"user",
"-nic",
"user",
"-drive",
"format=raw,file=disk.img",
"-display",
"none",
"-netdev",
"user,id=net0",
"-device",
"rtl8139,netdev=net0"
).start();
final BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String output;
while ((output = reader.readLine()) != null) {
if (output.isBlank()) continue;
System.out.println(output);
bot.chat.tellraw(
Component.text(
output
// INSANE regex.
.replaceAll("[\\u001B\\u009B][\\[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?(?:\\u0007|\\u001B\\u005C|\\u009C))|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))", "")
)
);
}
} catch (IOException e) {
bot.chat.tellraw(Utilities.getErrorComponent(e));
}
}).start();
}
@Override
public void packetReceived (Session session, Packet packet) {
if (packet instanceof ClientboundPlayerChatPacket t_packet) packetReceived(t_packet);
}
public void packetReceived (ClientboundPlayerChatPacket packet) {
if (process == null) return;
final String content = packet.getContent();
try {
final OutputStream output = process.getOutputStream();
if (content.trim().equalsIgnoreCase("sendc")) {
// ALSO clear the core queue
bot.core.clearQueue();
output.write("\u0003".getBytes());
output.flush();
return;
}
if (!content.toLowerCase().startsWith("send")) return;
final String input = content.substring("send".length());
output.write((input.stripLeading() + "\n").getBytes());
output.flush();
} catch (IOException e) {
bot.chat.tellraw(Utilities.getErrorComponent(e));
}
}
}