Skip to content

Commit 40d6d9b

Browse files
[~] Refactored json parser.
[~] Improved Json parsing performance. [#] Fixed Typo. [+] Added "packet-warning" option in config. [-] Removed useless classes. [~] Updated README.md. [~] Improved Logging system.
1 parent a78adb5 commit 40d6d9b

18 files changed

+195
-155
lines changed

README.md

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
2-
## Langrage: [简体中文](README_CN.md) / `English`
1+
## Language: [简体中文](README_CN.md) / `English`
32
# DolphinBot-Reloaded
43

54
<p align="center">
@@ -16,6 +15,9 @@
1615
<p align="center">
1716
<a href="https://github.com/NeonAngelThreads/DolphinBot/releases">
1817
<img src="https://img.shields.io/github/v/release/NeonAngelThreads/DolphinBot" alt="Release"/>
18+
</a>
19+
<a href="https://github.com/NeonAngelThreads/DolphinBot/releases">
20+
<img src="https://img.shields.io/github/downloads/neonangelthreads/dolphinbot/total" alt="downloads"/>
1921
</a>
2022
<br>
2123
<a href="https://github.com/NeonAngelThreads/DolphinBot/commits/master/">
@@ -28,10 +30,9 @@
2830
<a href="https://github.com/NeonAngelThreads/DolphinBot/tree/master/src/main">
2931
<img src="https://img.shields.io/github/languages/code-size/NeonAngelThreads/DolphinBot" alt="GitHub code size"/>
3032
</a>
31-
<br>
32-
<a href="https://github.com/NeonAngelThreads/DolphinBot/releases">
33-
<img src="https://img.shields.io/github/downloads/neonangelthreads/dolphinbot/total" alt="downloads"/>
34-
</a>
33+
<a href="https://app.codacy.com/gh/NeonAngelThreads/DolphinBot/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade">
34+
<img src="https://app.codacy.com/project/badge/Grade/ec90e9d4d7ef4023b124c4609b41a961"/>
35+
</a>
3536
<p align="center">
3637
<a href="https://github.com/NeonAngelThreads/DolphinBot/blob/master/PluginDocs.md">📖Docs</a>
3738
·
@@ -167,8 +168,8 @@ In this section, you will understand below how-tos:
167168
In the profile config file, you can create `profiles` field in `bot.profiles.json` to specify multiple bot profiles to log to a server.
168169
169170
> [!NOTE]
170-
> Some servers may prohibit multiple bots started on same IP, the proxy settings is aimed to help you to run multiple bots
171-
from different network environments or requiring distinct egress IPs.
171+
> Some servers may prohibit multiple bots started on same IP, the proxy settings is aimed to help you to run multiple bots
172+
> from different network environments or requiring distinct egress IPs.
172173
173174
To configure proxy settings for each bot, you need to edit `proxy` field. An example shown below:
174175
@@ -285,15 +286,18 @@ each profile name, should be split with ";".
285286
"server": "2b2t.xin",
286287
"port": 25565,
287288
"auto-reconnecting": true,
288-
"enable-skin-recorder": true,
289-
290-
"packet-filter-delay": 3000,
289+
"packet-filter-delay": 0,
291290
"msg-send-delay": 3000,
292291
"max-chunk-view": 12,
293-
294-
"connect-timing-out": 2000,
295-
"reconnect-delay": 3000,
296-
"enable-packet-debug": false
292+
"connect-timing-out": 2000,
293+
"reconnect-delay": 1000,
294+
"debug-settings": {
295+
"enable-packet-debug": false,
296+
"packet-warning": true
297+
},
298+
"other": {
299+
"enable-skin-recorder": true
300+
}
297301
}
298302
```
299303
### Config Options:
@@ -310,6 +314,7 @@ each profile name, should be split with ";".
310314
| `reconnect-delay` | Min delay(millis) for cooling down when reconnect a server. |
311315
| `msg-send-delay` | The delay of sending in-game messages. |
312316
| `enable-packet-debug` | Whether enable packet debugger. |
317+
| `packet-warning` | Showing packet errors or not. |
313318
314319
## Hot Swapping Plugins In-Game
315320
Dolphin bot supports you to **hot-reload** and **hot-load** (**hot injection**) plugins in server, without quit the entire client and reconnecting to server.

src/main/java/org/angellock/impl/AbstractRobot.java

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,18 @@
2222
import org.angellock.impl.commands.CommandSpec;
2323
import org.angellock.impl.events.IConnectListener;
2424
import org.angellock.impl.events.IDisconnectListener;
25-
import org.angellock.impl.events.handlers.*;
25+
import org.angellock.impl.events.handlers.ChatCommandHandler;
26+
import org.angellock.impl.events.handlers.KeepAliveHandler;
27+
import org.angellock.impl.events.handlers.PlayerEmergeHandler;
28+
import org.angellock.impl.events.handlers.ServerChatCommandHandler;
2629
import org.angellock.impl.events.packets.EntityMovePacket;
2730
import org.angellock.impl.events.packets.PlayerPositionPacket;
2831
import org.angellock.impl.events.packets.debugger.PacketDebugger;
29-
import org.angellock.impl.ingame.IPlayer;
3032
import org.angellock.impl.ingame.Player;
3133
import org.angellock.impl.ingame.PlayerTracker;
32-
import org.angellock.impl.managers.ProfileObject;
3334
import org.angellock.impl.managers.BotManager;
3435
import org.angellock.impl.managers.ConfigManager;
36+
import org.angellock.impl.managers.ProfileObject;
3537
import org.angellock.impl.managers.TerminalCommandManager;
3638
import org.angellock.impl.plugin.Plugin;
3739
import org.angellock.impl.plugin.PluginManager;
@@ -49,19 +51,21 @@
4951
import org.slf4j.Logger;
5052
import org.slf4j.LoggerFactory;
5153

52-
import java.util.*;
54+
import java.util.ArrayList;
55+
import java.util.List;
56+
import java.util.Map;
57+
import java.util.UUID;
5358
import java.util.concurrent.Executors;
5459
import java.util.concurrent.ScheduledExecutorService;
5560
import java.util.concurrent.TimeUnit;
5661

57-
public abstract class AbstractRobot implements ISendable, SessionProvider, IOptionalProcedures, IPlayer {
62+
public abstract class AbstractRobot implements ISendable, SessionProvider, IOptionalProcedures {
5863
protected TcpClientSession serverSession;
5964
protected static final Logger log = LoggerFactory.getLogger(ConsoleTokens.colorizeText("&aDolphinBot"));
6065
private final ScheduledExecutorService reconnectScheduler = Executors.newScheduledThreadPool(1);
61-
protected final Random randomizer = new Random();
6266
protected final PluginManager pluginManager;
6367
protected MinecraftProtocol minecraftProtocol;
64-
protected ConfigManager config;
68+
protected ConfigManager globalConfig;
6569
protected long connectDuration;
6670
protected boolean isByPassedVerification = true;
6771
protected GameMode serverGamemode = GameMode.ADVENTURE;
@@ -75,21 +79,12 @@ public abstract class AbstractRobot implements ISendable, SessionProvider, IOpti
7579
protected final CommandSpec commands = new CommandSpec(this);
7680

7781
public AbstractRobot(ConfigManager configManager, PluginManager pluginManager){
78-
this.config = configManager;
79-
String playerName = this.config.getConfigValue("username");
80-
String serverAddress = this.config.getConfigValue("server");
81-
int serverPort = Integer.parseInt(this.config.getConfigValue("port"));
82-
this.connectDuration = Long.parseLong(this.config.getConfigValue("reconnect-delay"));
83-
this.infoHelper.setPassword(this.config.getConfigValue("password"));
82+
this.globalConfig = configManager;
83+
this.infoHelper.setName(this.globalConfig.getConfigValue("username"));
84+
this.infoHelper.setPassword(this.globalConfig.getConfigValue("password"));
8485

8586
this.pluginManager = pluginManager;
8687

87-
this.infoHelper.setServer(serverAddress);
88-
this.infoHelper.setName(playerName);
89-
this.infoHelper.setPort(serverPort);
90-
this.infoHelper.setTIME_OUT(Integer.parseInt(this.config.getConfigValue("connect-timing-out")));
91-
this.infoHelper.setReconnectionDelay(Integer.parseInt(this.config.getConfigValue("reconnect-delay")));
92-
9388
}
9489

9590
public AbstractRobot enableProxy(ProxyInfo proxyInfo){
@@ -137,8 +132,8 @@ public TerminalCommandManager getCommandManager() {
137132
return commandManager;
138133
}
139134

140-
public ConfigManager config() {
141-
return this.config;
135+
public DolphinConfig config() {
136+
return this.globalConfig.config();
142137
}
143138

144139
public String getPassword(){
@@ -152,9 +147,9 @@ public ChatMessageManager getMessageManager() {
152147
public void connect(){
153148
onPreLogin();
154149
if (this.proxyInfo != null) {
155-
this.serverSession = new TcpClientSession(this.infoHelper.getServer(), this.infoHelper.getPort(), minecraftProtocol, this.proxyInfo);
150+
this.serverSession = new TcpClientSession(this.config().getServer(), this.config().getPort(), minecraftProtocol, this.proxyInfo);
156151
} else {
157-
this.serverSession = new TcpClientSession(this.infoHelper.getServer(), this.infoHelper.getPort(), minecraftProtocol);
152+
this.serverSession = new TcpClientSession(this.config().getServer(), this.config().getPort(), minecraftProtocol);
158153
}
159154

160155
this.messageManager = new ChatMessageManager(this);
@@ -176,7 +171,7 @@ public void connect(){
176171
this.serverSession.addListener(new PlayerEmergeHandler());
177172
this.serverSession.addListener(new PlayerPositionPacket(this));
178173
this.serverSession.addListener(new KeepAliveHandler());
179-
if (this.config.isDebugMode()){
174+
if (this.config().getDebugSettings().isEnablePacketDebug()) {
180175
this.serverSession.addListener(new PacketDebugger());
181176
}
182177

@@ -224,7 +219,7 @@ public void connect(){
224219

225220
public void scheduleReconnect() {
226221
try {
227-
Thread.sleep(this.infoHelper.getReconnectionDelay());
222+
Thread.sleep(this.config().getReconnectDelay());
228223
} catch (InterruptedException e) {
229224
throw new RuntimeException(e);
230225
}
@@ -253,9 +248,6 @@ public PluginManager getPluginManager() {
253248
return pluginManager;
254249
}
255250

256-
public Random getRandomizer() {
257-
return randomizer;
258-
}
259251
public long getConnectTime() {
260252
return connectDuration;
261253
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.angellock.impl;
2+
3+
import lombok.Data;
4+
import lombok.ToString;
5+
6+
import java.util.Map;
7+
8+
@Data
9+
@ToString
10+
public class DolphinConfig {
11+
String server;
12+
int port;
13+
boolean autoReconnect;
14+
long packetFilterDelay;
15+
long msgSendDelay;
16+
int maxChunkView;
17+
long connectTimingOut;
18+
long reconnectDelay;
19+
DebugSettings debugSettings;
20+
OtherSettings other;
21+
22+
@Data
23+
@ToString
24+
public static class DebugSettings {
25+
boolean enablePacketDebug;
26+
}
27+
28+
@Data
29+
@ToString
30+
public static class OtherSettings {
31+
boolean enableSkinRecorder;
32+
}
33+
34+
public DolphinConfig mergeCommandOptions(Map<String, Object> commandLines) {
35+
for (Map.Entry<String, Object> opt : commandLines.entrySet()) {
36+
String value = (String) opt.getValue();
37+
switch (opt.getKey().toLowerCase()) {
38+
case "server" -> setServer(value);
39+
case "port" -> setPort(Integer.parseInt(value));
40+
case "auto-reconnect", "reconnect" -> setAutoReconnect(true);
41+
case "max-chunk-view" -> setMaxChunkView(Integer.parseInt(value));
42+
case "debug" -> this.debugSettings.setEnablePacketDebug(true);
43+
case "enable-skin-recorder" -> this.other.setEnableSkinRecorder(true);
44+
}
45+
}
46+
return this;
47+
}
48+
}

src/main/java/org/angellock/impl/RobotPlayer.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,28 @@
1616

1717
package org.angellock.impl;
1818

19-
import org.angellock.impl.commands.CommandBuilder;
20-
import org.angellock.impl.commands.CommandResponse;
2119
import org.angellock.impl.ingame.IPlayer;
2220
import org.angellock.impl.managers.ConfigManager;
2321
import org.angellock.impl.plugin.PluginManager;
2422
import org.angellock.impl.util.ConsoleTokens;
2523
import org.angellock.impl.util.math.Position;
26-
import org.cloudburstmc.math.immutable.vector.ImmutableVector3d;
27-
import org.cloudburstmc.math.vector.Vector3d;
2824
import org.cloudburstmc.math.vector.Vector3i;
2925
import org.geysermc.mcprotocollib.protocol.data.game.entity.object.Direction;
3026
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.Hand;
31-
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.InteractAction;
32-
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.PlayerAction;
33-
import org.geysermc.mcprotocollib.protocol.data.game.level.particle.positionsource.BlockPositionSource;
34-
import org.geysermc.mcprotocollib.protocol.data.game.level.particle.positionsource.PositionSource;
35-
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.inventory.ServerboundPlaceRecipePacket;
36-
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.player.*;
27+
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.player.ServerboundSwingPacket;
28+
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.player.ServerboundUseItemOnPacket;
3729

3830
import java.util.Optional;
39-
import java.util.Vector;
4031

41-
public class RobotPlayer extends AbstractRobot {
32+
public class RobotPlayer extends AbstractRobot implements IPlayer {
4233
private long connectTime;
4334
private long lastMsgTime = 0L;
4435
private final long msgDelay;
4536

4637
public RobotPlayer(ConfigManager configManager, PluginManager pluginManager) {
4738
super(configManager, pluginManager);
4839

49-
this.msgDelay = Long.parseLong(Optional.ofNullable(this.config.getConfigValue("msg-send-delay")).orElse("3000"));
40+
this.msgDelay = Long.parseLong(Optional.ofNullable(this.globalConfig.getConfigValue("msg-send-delay")).orElse("3000"));
5041
}
5142

5243
@Override
@@ -69,7 +60,7 @@ public void onQuit(String reason) {
6960
long millis = System.currentTimeMillis() - this.connectTime;
7061
log.info(ConsoleTokens.colorizeText("[{}] &7Session Duration: &f{}ms"), this.getProfileName(), millis);
7162
log.info(ConsoleTokens.colorizeText("&l&4Disconnected from the server! &6Reason: &d&n{}"), reason);
72-
if (this.config.getConfigValue("auto-reconnecting").equals("true")){
63+
if (this.globalConfig.getConfigValue("auto-reconnecting").equals("true")) {
7364
log.info(ConsoleTokens.colorizeText("&9Trying to reconnect to the server..."));
7465

7566
this.getPluginManager().disableAllPlugins(this);
@@ -88,7 +79,7 @@ public void onKicked() {
8879
@Override
8980
public void onPreLogin() {
9081
this.connectTime = System.currentTimeMillis();
91-
log.info(ConsoleTokens.colorizeText("&l&bAttempt to join to the server &3{}:{}. &bWaiting for server establishing the connection..."), this.infoHelper.getServer(), this.infoHelper.getPort());
82+
log.info(ConsoleTokens.colorizeText("&l&bAttempt to join to the server &3{}:{}. &bWaiting for server establishing the connection..."), this.config().getServer(), this.config().getPort());
9283
}
9384

9485
@Override

src/main/java/org/angellock/impl/Start.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ private static void getTerminal(AbstractRobot dolphinBot) {
116116
if (exit){
117117
System.exit(0);
118118
} else {
119-
log.warn("To exit the DolphinBot, press Ctrl + C again.");
119+
log.warn("To exit DolphinBot, press Ctrl + C again.");
120120
exit = true;
121121
}
122122
} catch (Throwable e) {

src/main/java/org/angellock/impl/commands/executors/RespawnExecutor.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,15 @@
1919
import org.angellock.impl.AbstractRobot;
2020
import org.angellock.impl.commands.CommandResponse;
2121
import org.angellock.impl.commands.ICommandAction;
22-
import org.angellock.impl.commands.ICommandCompleter;
2322
import org.angellock.impl.managers.BotManager;
24-
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.player.ServerboundPlayerAbilitiesPacket;
25-
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.player.ServerboundPlayerActionPacket;
26-
import org.geysermc.mcprotocollib.protocol.packet.status.clientbound.ClientboundStatusResponsePacket;
27-
import org.geysermc.mcprotocollib.protocol.packet.status.serverbound.ServerboundStatusRequestPacket;
2823

2924
public class RespawnExecutor implements ICommandAction {
3025
@Override
3126
public void onCommand(CommandResponse responseEntity) {
3227
String botName = responseEntity.getCommandList()[1];
3328
if (botName == null) {
3429
for (AbstractRobot bot: BotManager.bots().values()){
35-
bot.sendPacket(new ServerboundStatusRequestPacket());
30+
//bot.sendPacket(new ServerStatusInfo(RESPAWN));
3631
}
3732
}
3833
}

src/main/java/org/angellock/impl/events/AbstractEventProcessor.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public abstract class AbstractEventProcessor<T extends MinecraftPacket> extends
2020
protected long time_elapse = System.currentTimeMillis();
2121
private final long DELAY;
2222
protected List<IActions<T>> actionList = new ArrayList<>();
23+
24+
private final boolean showWarn = true;//ConfigManager.getCoreSettings().getDebugSettings().isEnablePacketDebug();
2325
protected IActions<T> preAction = (T) -> {
2426
};
2527

@@ -61,8 +63,10 @@ public void packetReceived(Session session, Packet packet){
6163

6264
@Override
6365
public void packetError(PacketErrorEvent event) {
64-
log.warn(ConsoleTokens.colorizeText("&eA packet error was detected: &7At event &6" + event));
65-
log.error(ConsoleTokens.colorizeText("&7" + event.getCause().toString()));
66+
if (this.showWarn) {
67+
log.warn(ConsoleTokens.colorizeText("&eA packet error was detected: &7At event &6" + event));
68+
log.error(ConsoleTokens.colorizeText("&7" + event.getCause().toString()));
69+
}
6670
event.setSuppress(true);
6771
}
6872

src/main/java/org/angellock/impl/extensions/BaseDefaultPlugin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void onEnable(AbstractRobot robotEntity) {
8787
getTerminalCommands().registerCommand(new TerminalCommand("load", new LoadCommandExecutor()), new LoadPluginCompleter());
8888
getTerminalCommands().registerCommand(new TerminalCommand("respawn", new RespawnExecutor()));
8989

90-
if (robotEntity.config().isDebugMode()) {
90+
if (robotEntity.config().getDebugSettings().isEnablePacketDebug()) {
9191
getEvents().registerEvents(new PlayerListener(), this);
9292
}
9393
robotEntity.getRegisteredCommands().register(new CommandBuilder().withName("reload").allowedUsers(robotEntity.getInfoHelper().getOwners()).build((response) -> {
@@ -173,7 +173,7 @@ public String getLogMsg(GameProfile player){
173173
}
174174

175175
public void joinGame(AbstractRobot player){
176-
player.sendPacket(new ServerboundClientInformationPacket("en-us", player.config().getChunkLoad(), ChatVisibility.FULL, true, new ArrayList<>(), HandPreference.LEFT_HAND, true, true));
176+
player.sendPacket(new ServerboundClientInformationPacket("en-us", player.config().getMaxChunkView(), ChatVisibility.FULL, true, new ArrayList<>(), HandPreference.LEFT_HAND, true, true));
177177

178178
player.sendPacket(new ServerboundSetCarriedItemPacket(1));
179179
player.sendPacket(new ServerboundUseItemPacket(

src/main/java/org/angellock/impl/extensions/PlayerVerificationPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public void onEnable(AbstractRobot entityBot) {
9999
public void execute() {
100100
int var = 0;
101101
try {
102-
var = TimingUtil.getRandomDelay(entityBot.getRandomizer(), var);
102+
var = TimingUtil.getRandomDelay(TimingUtil.getRandomizer(), var);
103103
Thread.sleep(500L*(1+var));
104104

105105
if (entityBot.getSession().isConnected()){

0 commit comments

Comments
 (0)