Skip to content

Commit 9737a08

Browse files
[+] Added Logging file recording.
[+] Exposed currentState getter to plugin system. [#] Moved the login state machine to the bot class. [#] Changed Plugin onEnable(AbstractRobot) to onEnable(RobotPlayer). [#] Moved the event loop logic to RobotPlayer.mainTickingEventLoop.
1 parent 62ad3e6 commit 9737a08

33 files changed

+171
-118
lines changed

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

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ public abstract class AbstractRobot implements ISendable, SessionProvider, IOpti
7777
@Setter
7878
protected @Getter GameMode serverGamemode = GameMode.ADVENTURE;
7979
@Getter
80-
private ChatMessageManager messageManager;
81-
@Getter
8280
private BotManager botManager = BotManager.getInstance();
8381
protected ProxyInfo proxyInfo;
8482
protected @Getter ProfileObject infoHelper = new ProfileObject();
@@ -159,7 +157,6 @@ public void connect(){
159157
this.serverSession = new TcpClientSession(serverIP, serverPort, minecraftProtocol);
160158
}
161159

162-
this.messageManager = new ChatMessageManager(this);
163160
this.serverSession.addListener((IConnectListener) event -> onJoin());
164161
this.serverSession.addListener(new DisconnectReasonHandler(this));
165162
this.serverSession.addListener(new ServerChatCommandHandler(this.commands));
@@ -172,45 +169,12 @@ public void connect(){
172169
this.serverSession.connect(true, false);
173170

174171
this.connectDuration = System.currentTimeMillis();
175-
try {
176-
boolean connect = true;
177-
boolean shouldWait = false;
178-
179-
while (true) {
180-
try {
181-
Thread.sleep(20L);
182-
if (!this.serverSession.isConnected()){
183-
this.connectDuration = System.currentTimeMillis();
184-
break;
185-
} else if (connect) {
186-
if (System.currentTimeMillis() - this.connectDuration > 100L){
187-
this.pluginManager.loadAllPlugins(this);
188-
connect = false;
189-
}
190-
} else if (!shouldWait) {
191-
if (this.messageManager.pollMessage()) {
192-
shouldWait = true;
193-
}
194-
} else if (canSendMessages()) {
195-
shouldWait = false;
196-
}
197-
}
198-
catch (InterruptedException e){
199-
continue;
200-
} catch (IllegalArgumentException e) {
201-
TranslatableUtil.warnTranslatableOf(EnumSystemEvents.PACKET_ERROR, e);
202-
} catch (Exception e) {
203-
TranslatableUtil.warnTranslatableOf(EnumSystemEvents.PLUGIN_ERROR, e);
204-
}
205-
}
206-
} finally {
207-
this.serverSession.disconnect("");
208-
if (BotManager.getBotByProfileName(getProfileName()) != null){
209-
scheduleReconnect();
210-
}
211-
}
172+
173+
this.mainTickingEventLoop();
212174
}
213175

176+
public abstract void mainTickingEventLoop();
177+
214178
public abstract boolean canSendMessages();
215179

216180
public void scheduleReconnect() {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ public class ChatMessageManager{
3434
protected static final Logger log = LoggerFactory.getLogger(ConsoleTokens.colorizeText("&7ChatMessageManager"));
3535
@Getter
3636
private final Queue<String> chatMessageQueue = new ArrayDeque<>();
37-
private final AbstractRobot instance;
37+
private final RobotPlayer instance;
3838

39-
public ChatMessageManager(AbstractRobot bot) {
39+
public ChatMessageManager(RobotPlayer bot) {
4040
this.instance = bot;
4141
}
4242

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

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
package org.angellock.impl;
1818

19+
import lombok.Getter;
1920
import lombok.Setter;
21+
import org.angellock.impl.api.state.LoginState;
22+
import org.angellock.impl.api.state.LoginStateMachine;
2023
import org.angellock.impl.events.bukkit.AbstractEvent;
2124
import org.angellock.impl.ingame.IPlayer;
2225
import org.angellock.impl.managers.BotManager;
@@ -38,18 +41,63 @@ public class RobotPlayer extends AbstractRobot implements IPlayer {
3841
private long lastMsgTime = 0L;
3942
private final long msgDelay;
4043
private volatile @Setter boolean shouldReconnect = true;
44+
@Getter
45+
private ChatMessageManager messageManager;
46+
@Getter
47+
private final LoginStateMachine loginStateMachine = new LoginStateMachine(LoginState.DISCONNECTED);
4148
protected @Setter Position loginPos = new Position();
4249

4350
public RobotPlayer(ConfigManager configManager, PluginManager pluginManager) {
4451
super(configManager, pluginManager);
45-
52+
this.messageManager = new ChatMessageManager(this);
4653
this.msgDelay = Long.parseLong(Optional
4754
.ofNullable(
4855
this.globalConfig.getConfigValue("msg-send-delay"))
4956
.orElse("3000")
5057
);
5158
}
5259

60+
@Override
61+
public void mainTickingEventLoop() {
62+
try {
63+
boolean connect = true;
64+
boolean shouldWait = false;
65+
66+
while (true) {
67+
try {
68+
Thread.sleep(20L);
69+
if (!this.serverSession.isConnected()){
70+
this.connectDuration = System.currentTimeMillis();
71+
break;
72+
} else if (connect) {
73+
if (System.currentTimeMillis() - this.connectDuration > 100L){
74+
this.pluginManager.loadAllPlugins(this);
75+
connect = false;
76+
}
77+
} else if (!shouldWait) {
78+
if (this.getMessageManager().pollMessage()) {
79+
shouldWait = true;
80+
}
81+
} else if (canSendMessages()) {
82+
shouldWait = false;
83+
}
84+
}
85+
catch (InterruptedException e){
86+
continue;
87+
} catch (IllegalArgumentException e) {
88+
TranslatableUtil.warnTranslatableOf(EnumSystemEvents.PACKET_ERROR, e);
89+
} catch (Exception e) {
90+
TranslatableUtil.warnTranslatableOf(EnumSystemEvents.PLUGIN_ERROR, e);
91+
}
92+
}
93+
} finally {
94+
this.serverSession.disconnect("");
95+
if (BotManager.getBotByProfileName(getProfileName()) != null){
96+
scheduleReconnect();
97+
}
98+
}
99+
}
100+
53101
@Override
54102
public boolean canSendMessages() {
55103
long t = System.currentTimeMillis();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public static void main(String[] args) {
118118

119119
}
120120

121-
private static void getTerminal(AbstractRobot dolphinBot) {
121+
private static void getTerminal(RobotPlayer dolphinBot) {
122122
LineReader reader = AnsiEscapes.getReader();
123123
terminalInput = new Thread(() -> {
124124
while (true) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
package org.angellock.impl.api.state;
1818

1919
import it.unimi.dsi.fastutil.Pair;
20+
import lombok.Getter;
2021
import org.angellock.impl.EnumSystemEvents;
2122
import org.angellock.impl.util.TranslatableUtil;
2223
import org.angellock.impl.util.reason.IReason;
2324
import org.slf4j.Logger;
2425
import org.slf4j.LoggerFactory;
2526

2627
public class LoginStateMachine extends StateMachine<String> {
28+
@Getter
2729
private LoginState currentState;
2830
private final LoginState initialState;
2931
private String message;

src/main/java/org/angellock/impl/api/state/StateAction.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
package org.angellock.impl.api.state;
1818

1919
import org.angellock.impl.AbstractRobot;
20+
import org.angellock.impl.RobotPlayer;
2021

2122
public abstract class StateAction {
22-
protected AbstractRobot entityBot;
23-
public StateAction(AbstractRobot botInstance) {
23+
protected RobotPlayer entityBot;
24+
public StateAction(RobotPlayer botInstance) {
2425
this.entityBot = botInstance;
2526
}
2627
public abstract void execute();

src/main/java/org/angellock/impl/commands/AbstractCommand.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import lombok.Getter;
2020
import lombok.Setter;
2121
import org.angellock.impl.AbstractRobot;
22+
import org.angellock.impl.RobotPlayer;
2223

2324
import java.util.ArrayList;
2425
import java.util.List;
@@ -52,5 +53,5 @@ public boolean isAnAliasOf(String alias) {
5253
return this.aliases.contains(alias);
5354
}
5455

55-
public abstract boolean activate(CommandResponse entity, AbstractRobot bot);
56+
public abstract boolean activate(CommandResponse entity, RobotPlayer bot);
5657
}

src/main/java/org/angellock/impl/commands/Command.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.angellock.impl.commands;
1818

1919
import org.angellock.impl.AbstractRobot;
20+
import org.angellock.impl.RobotPlayer;
2021

2122
import java.util.ArrayList;
2223
import java.util.List;
@@ -39,7 +40,7 @@ public void setAction(ICommandAction action){
3940
}
4041

4142
@Override
42-
public boolean activate(CommandResponse entity, AbstractRobot bot) {
43+
public boolean activate(CommandResponse entity, RobotPlayer bot) {
4344
if (users.contains(entity.getSender()) || users.isEmpty()) {
4445
this.action.onCommand(entity, bot);
4546
return true;

src/main/java/org/angellock/impl/commands/CommandSpec.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import lombok.Getter;
2020
import org.angellock.impl.AbstractRobot;
2121
import org.angellock.impl.EnumSystemEvents;
22+
import org.angellock.impl.RobotPlayer;
2223
import org.angellock.impl.util.ConsoleTokens;
2324
import org.angellock.impl.util.TranslatableUtil;
2425
import org.jetbrains.annotations.Nullable;
@@ -32,10 +33,10 @@
3233
public class CommandSpec {
3334
protected static final Logger log = LoggerFactory.getLogger(ConsoleTokens.colorizeText("&9&lDolphinCommandExecutor"));
3435
private final @Getter Map<String, Command> registeredCommands = new HashMap<>();
35-
private final AbstractRobot bot;
36+
private final RobotPlayer bot;
3637

3738
public CommandSpec(AbstractRobot bot) {
38-
this.bot = bot;
39+
this.bot = (RobotPlayer) bot;
3940
}
4041

4142
public void register(Command command){
@@ -49,7 +50,7 @@ public void register(Command command){
4950

5051
public void executeCommand(CommandResponse response) {
5152
if (response != null) {
52-
log.info(TranslatableUtil.getFormattedMessage(EnumSystemEvents.CHAT_COMMAND_DETECTED, Arrays.toString(response.getCommandList()), response.getSender()));
53+
log.info(bot.getBotLabel(), TranslatableUtil.getFormattedMessage(EnumSystemEvents.CHAT_COMMAND_DETECTED, Arrays.toString(response.getCommandList()), response.getSender()));
5354
Command cmd = this.getCommand(response.getCommandList()[0]);
5455
if (cmd != null) {
5556
boolean success = cmd.activate(response, bot);

src/main/java/org/angellock/impl/commands/ICommandAction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
package org.angellock.impl.commands;
1818

1919
import org.angellock.impl.AbstractRobot;
20+
import org.angellock.impl.RobotPlayer;
2021

2122
public interface ICommandAction {
22-
void onCommand(CommandResponse responseEntity, AbstractRobot bot);
23+
void onCommand(CommandResponse responseEntity, RobotPlayer bot);
2324
}

0 commit comments

Comments
 (0)