Skip to content

Commit 80030d9

Browse files
Merge pull request #8 from NeonAngelThreads/1.3.1
[+] Removed redundant code in human verification module. [+] Added Message dispatching API for connecting bots each other. [+] Updated Plugin document for message dispatching feature. [#] Split handler code into individual handler classes. [#] Moved state actions into individual state action. [#] Improved login StateMachine logic. [#] Improved package architecture.
2 parents 2adbf72 + 4d03bca commit 80030d9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+850
-302
lines changed

PluginDocs.md

Lines changed: 80 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,16 @@ Define what state should transit to, and what action(s) should be done in this n
111111

112112
These methods aiming to build the chain calling style for simplicity.
113113
**Here is an example:**
114+
114115
```java
115116

116-
import org.angellock.impl.state.LoginStateMachine;
117+
import org.angellock.impl.api.state.LoginStateMachine;
117118

118119
@Override
119120
public void onEnable(AbstractRobot entityBot) {
120-
121+
121122
LoginStateMachine loginStateMachine = new LoginStateMachine();
122-
123+
123124
loginStateMachine
124125
.source(LoginState.IDLE).whenReceive("请登录").goal(LoginState.LOGIN, loginAction) // from IDLE transit to LOGIN
125126
.source(LoginState.LOGIN).whenReceive("已成功登录").goal(LoginState.JOIN, joinAction) // From LOGIN state transit to JOIN
@@ -142,21 +143,21 @@ in order to make the state machine running correctly on next time join to the se
142143

143144
### How to build an advanced login process?
144145
If you want to add multiple branches to a state node to build a more powerful and advanced login process,
145-
you can simply add `.and()` branch statement in that state node. **For example:**
146+
you can simply add `.and()` branch statement in that state node. **For example:**
146147

147148
```java
148149

149-
import org.angellock.impl.state.LoginStateMachine;
150+
import org.angellock.impl.api.state.LoginStateMachine;
150151

151152
@Override
152153
public void onEnable(AbstractRobot entityBot) {
153-
154+
154155
LoginStateMachine loginStateMachine = new LoginStateMachine();
155156

156157
stateMachine
157158
.source(LoginState.IDLE).whenReceive("请进行机器人验证").goal(LoginState.VERIFY, verifyAction)
158-
.and() // branch
159-
.whenReceive("请登录").goal(LoginState.LOGIN, loginAction)
159+
.and() // branch
160+
.whenReceive("请登录").goal(LoginState.LOGIN, loginAction)
160161
.source(LoginState.VERIFY).whenReceive("机器人验证已完毕").goal(LoginState.REGISTER, registerAction)
161162
.source(LoginState.REGISTER).whenReceive("已成功注册").goal(LoginState.JOIN, joinAction)
162163
.source(LoginState.LOGIN).whenReceive("已成功登录").goal(LoginState.JOIN, joinAction)
@@ -309,13 +310,15 @@ public class ExamplePlugin extends AbstractPlugin implements IListener {
309310

310311
- Available Event Listeners:
311312

312-
| Event Listener | Only Triggered When |
313-
|--------------------|-----------------------------------------------------------|
314-
| PlayerMoveEvent | Triggered when a nearby player moves |
315-
| EntityEmergedEvent | An entity generated, player emerged, or projectile thrown |
316-
| PlayerChatEvent | When a player send a in-game message. |
317-
- Please Note That:
318-
- Every single handling method should annotate with `@EventHandler`.
313+
| Event Listener | Only Triggered When |
314+
|----------------------|-----------------------------------------------------------|
315+
| PlayerMoveEvent | Triggered when a nearby player moves |
316+
| EntityEmergedEvent | An entity generated, player emerged, or projectile thrown |
317+
| PlayerChatEvent | When a player send a in-game message. |
318+
| MessageDispatchEvent | Customized Message payloads send from other bot by user |
319+
320+
> [!NOTE]
321+
> - Every single handling method should annotate with `@EventHandler`.
319322
- All handling method should be public.
320323
- Remember to register the all listener classes.
321324

@@ -415,9 +418,69 @@ public class ExamplePlugin extends AbstractPlugin implements IListener {
415418
```
416419
For each plugin has individual command system, and every command system is base on `SystemChatPacket` and `PlayerChatPacket` listening.
417420
The command sender and command list is serialized by `CommandSerializer`.
418-
- ### 4.3. Message Manager:
421+
- ### 4.3.Chat Message Manager:
419422
In-game chat messages sends is managed by `MessageManager`.
420-
For each bot individual has a message manager.
423+
For each bot has a message manager.
424+
Message manager aimed to send in-game chat message and chat commands.
425+
-
426+
You can use `.sendCommand(String stringCommand)` to send command.
427+
- Each unsent message and command is stored in a Queue, these message and command will be sent in a specified delay (you can change it in config file.).
428+
## Dolphin Message API:
429+
Message APIs are aimed to help you to connect one or more bot between bots, that is, you can notify notifications to a bot on some situation, once a bot received the message, you can specify what action should be done by the bot.
430+
431+
1. To use the dolphin message API, you need to register an event listener containing a method to handle the incoming message.
432+
2. Register it on you custom plugin.
433+
3. Then, you need to broadcast the message payload content from a bot.
434+
435+
**Here is a simple example:**
436+
**Step 1:**
437+
Defining a message listener on a receiver bot:
438+
ExampleMessageListener.java
439+
```java
440+
import org.angellock.impl.events.IListener;
441+
import org.angellock.impl.events.dolphin.MessageBroadcastEvent;
442+
443+
public class ExampleMessageListener implements IListener {
444+
@EventHandler
445+
public void onMessage(MessageBroadcastEvent event){
446+
log.info("message payload: {}", event.getMessage());
447+
// Do something...
448+
}
449+
}
450+
```
451+
Step 2:
452+
453+
```java
454+
import org.angellock.impl.AbstractRobot;
455+
import org.angellock.impl.plugin.AbstractPlugin;
456+
457+
public class MessagePlugin extends AbstractPlugin {
458+
@Override
459+
public void onEnable(AbstractRobot robot){
460+
getEvents().registerListeners(new ExampleMessageListener(), this);
461+
}
462+
}
463+
```
464+
Step 3:
465+
Defining a message sender plugin in sender bot.
466+
467+
```java
468+
import org.angellock.impl.api.message.BotMessage;
469+
import org.angellock.impl.plugin.AbstractPlugin;
470+
471+
public class SendMsgPlugin extends AbstractPlugin {
472+
@Override
473+
public void onEnable(AbstractRobot robot) {
474+
if (...){ // when the sending condition is meet
475+
BotManager.getBotByName("bot#1") // you can specify on which bot you want to send to. (you define on config file)
476+
.callHandleableEvent(new MessageBroadcastEvent("something you want to send"));
477+
478+
// alternately, you can use DispatchMessage() to distribute messages to all bots.
479+
entityBot.getBotManager().distributeMessage("something.");
480+
}
481+
}
482+
}
483+
```
421484
## Player Events:
422485
DolphinAPI also implemented player event system, allowing you to detect and predict online players on server.
423486
- `Player` class:

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public void connect(){
156156
this.serverSession.addListener(new ServerChatCommandHandler(this.commands));
157157
this.serverSession.addListener(new ChatCommandHandler(this.commands));
158158
this.serverSession.addListener(new EntityMovePacket());
159-
this.serverSession.addListener(new PlayerEmergeHandler());
159+
this.serverSession.addListener(new PlayerEmergeHandler(this));
160160
this.serverSession.addListener(new PlayerPositionPacket((RobotPlayer) this));
161161
if (this.config().getDebugSettings().isEnablePacketDebug()) { this.serverSession.addListener(new PacketDebugger()); }
162162
this.serverSession.setFlag(BuiltinFlags.READ_TIMEOUT, -1);
@@ -194,7 +194,7 @@ public void connect(){
194194
}
195195
}
196196
} finally {
197-
this.serverSession.disconnect("Connection was Interrupted");
197+
this.serverSession.disconnect("");
198198
scheduleReconnect();
199199
}
200200
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,7 @@ private void sendMessagePacket(String message){
7676
}
7777
}
7878

79+
public void sendCommand(String stringCommand){
80+
this.instance.sendPacket(new ServerboundChatCommandPacket(stringCommand));
81+
}
7982
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.angellock.impl;
1818

1919
import lombok.Setter;
20+
import org.angellock.impl.events.annotations.EventHandler;
21+
import org.angellock.impl.events.bukkit.AbstractEvent;
2022
import org.angellock.impl.ingame.IPlayer;
2123
import org.angellock.impl.managers.ConfigManager;
2224
import org.angellock.impl.plugin.PluginManager;
@@ -73,6 +75,10 @@ public void onQuit(String reason) {
7375
TranslatableUtil.infoTranslatableOf(EnumSystemEvents.DOLPHIN_TIMING_RESET);
7476
}
7577

78+
public void callHandleableEvent(AbstractEvent event){
79+
this.getPluginManager().event().broadcastEvent(event);
80+
}
81+
7682
@Override
7783
public void onKicked() {
7884
return;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* DolphinBot - https://github.com/NeonAngelThreads/DolphinBot
3+
* Copyright (C) 2025 NeonAngelThreads (https://github.com/NeonAngelThreads)
4+
*
5+
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
6+
* License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any
7+
* later version.
8+
*
9+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
10+
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
11+
* License for more details. You should have received a copy of the GNU General Public License along with this
12+
* program. If not, see <https://www.gnu.org/licenses/>.
13+
*
14+
* https://space.bilibili.com/386644641
15+
*/
16+
17+
package org.angellock.impl.api.message;
18+
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
22+
public abstract class BotMessage implements INotify{
23+
private final Map<String, Object> messageMap;
24+
25+
public BotMessage(Map<String, Object> messageMap) {
26+
this.messageMap = messageMap;
27+
}
28+
public BotMessage addItem(String key, Object value){
29+
this.messageMap.put(key, value);
30+
return this;
31+
}
32+
public Map<String, Object> message() {
33+
return this.messageMap;
34+
}
35+
}

src/main/java/org/angellock/impl/IEventCallable.java renamed to src/main/java/org/angellock/impl/api/message/INotify.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@
99
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
1010
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
1111
* License for more details. You should have received a copy of the GNU General Public License along with this
12-
* program. If not, see <https://www.gnu.org/licenses/>.
12+
* program. If not, see <https://www.gnu.org/licenses/>.
1313
*
1414
* https://space.bilibili.com/386644641
1515
*/
1616

17-
package org.angellock.impl;
17+
package org.angellock.impl.api.message;
1818

19-
import org.angellock.impl.events.bukkit.Event;
20-
21-
public interface IEventCallable<T> {
22-
T callEvent(Event event);
19+
public interface INotify{
20+
void onAccept();
21+
void onDismiss();
2322
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* DolphinBot - https://github.com/NeonAngelThreads/DolphinBot
3+
* Copyright (C) 2025 NeonAngelThreads (https://github.com/NeonAngelThreads)
4+
*
5+
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
6+
* License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any
7+
* later version.
8+
*
9+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
10+
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
11+
* License for more details. You should have received a copy of the GNU General Public License along with this
12+
* program. If not, see <https://www.gnu.org/licenses/>.
13+
*
14+
* https://space.bilibili.com/386644641
15+
*/
16+
17+
package org.angellock.impl.api.message;
18+
19+
public abstract class Notification implements INotify{
20+
public String message;
21+
public Notification(String message) {
22+
this.message = message;
23+
}
24+
public String notification() {
25+
return this.message;
26+
}
27+
}

src/main/java/org/angellock/impl/state/LoginState.java renamed to src/main/java/org/angellock/impl/api/state/LoginState.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
1010
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
1111
* License for more details. You should have received a copy of the GNU General Public License along with this
12-
* program. If not, see <https://www.gnu.org/licenses/>.
12+
* program. If not, see <https://www.gnu.org/licenses/>.
1313
*
1414
* https://space.bilibili.com/386644641
1515
*/
1616

17-
package org.angellock.impl.state;
17+
package org.angellock.impl.api.state;
1818

1919
public enum LoginState {
2020

@@ -26,18 +26,18 @@ public enum LoginState {
2626
JOIN(5);
2727

2828
private final int stateValue;
29-
private Action action;
29+
private StateAction action;
3030

3131
private LoginState(int stateValue) {
3232
this.stateValue = stateValue;
3333
}
3434

35-
public LoginState withAction(Action action) {
35+
public LoginState withAction(StateAction action) {
3636
this.action = action;
3737
return this;
3838
}
3939

40-
public Action getAction() {
40+
public StateAction getAction() {
4141
return action;
4242
}
4343

src/main/java/org/angellock/impl/state/LoginStateMachine.java renamed to src/main/java/org/angellock/impl/api/state/LoginStateMachine.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
1010
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
1111
* License for more details. You should have received a copy of the GNU General Public License along with this
12-
* program. If not, see <https://www.gnu.org/licenses/>.
12+
* program. If not, see <https://www.gnu.org/licenses/>.
1313
*
1414
* https://space.bilibili.com/386644641
1515
*/
1616

17-
package org.angellock.impl.state;
17+
package org.angellock.impl.api.state;
1818

1919
import it.unimi.dsi.fastutil.Pair;
2020
import org.angellock.impl.EnumSystemEvents;
@@ -26,7 +26,7 @@
2626
public class LoginStateMachine extends StateMachine<String> {
2727

2828
private static final Logger log = LoggerFactory.getLogger(LoginStateMachine.class);
29-
public LoginState currentState;
29+
private LoginState currentState;
3030
private final LoginState initialState;
3131
private String message;
3232
private IReason resetCondition;
@@ -67,7 +67,7 @@ public LoginStateMachine whenReceive(String message) {
6767
return this;
6868
}
6969

70-
public LoginStateMachine goal(LoginState nextState, Action action) {
70+
public LoginStateMachine goal(LoginState nextState, StateAction action) {
7171
this.msgCache.add(this.message);
7272
this.transitionMap.put(Pair.of(this.currentState.name(), this.message), nextState.withAction(action));
7373
return this;
@@ -89,7 +89,7 @@ public boolean check(String input) {
8989
if (nextState != null) {
9090
TranslatableUtil.infoTranslatableOf(EnumSystemEvents.LOGIN_STATEMACHINE_TRANSIT, this.currentState);
9191
this.currentState = nextState;
92-
Action action = this.currentState.getAction();
92+
StateAction action = this.currentState.getAction();
9393
if (action != null) {
9494
action.execute();
9595
return true;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* DolphinBot - https://github.com/NeonAngelThreads/DolphinBot
3+
* Copyright (C) 2025 NeonAngelThreads (https://github.com/NeonAngelThreads)
4+
*
5+
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
6+
* License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any
7+
* later version.
8+
*
9+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
10+
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
11+
* License for more details. You should have received a copy of the GNU General Public License along with this
12+
* program. If not, see <https://www.gnu.org/licenses/>.
13+
*
14+
* https://space.bilibili.com/386644641
15+
*/
16+
17+
package org.angellock.impl.api.state;
18+
19+
import org.angellock.impl.AbstractRobot;
20+
21+
public abstract class StateAction {
22+
protected AbstractRobot entityBot;
23+
public StateAction(AbstractRobot botInstance) {
24+
this.entityBot = botInstance;
25+
}
26+
public abstract void execute();
27+
}

0 commit comments

Comments
 (0)