Skip to content

Commit

Permalink
Reworked chat sending to better obey the chat message delay at all times
Browse files Browse the repository at this point in the history
  • Loading branch information
DustinRepo committed Mar 15, 2022
1 parent 0d056b4 commit 848d400
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/me/dustin/chatbot/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public void setClientConnection(ClientConnection clientConnection) {
}

public void sendChat(String message) {
getClientConnection().sendPacket(new ServerBoundChatPacket((ChatBot.getConfig().isGreenText() && !message.startsWith("/") ? ">" : "") + message));
getClientConnection().getClientPlayer().chat(message);
}
}
5 changes: 0 additions & 5 deletions src/me/dustin/chatbot/command/CommandManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public class CommandManager {

private final ArrayList<Command> commands = new ArrayList<>();
private final ClientConnection clientConnection;
private final StopWatch stopWatch = new StopWatch();

public CommandManager(ClientConnection clientConnection) {
this.clientConnection = clientConnection;
Expand Down Expand Up @@ -56,9 +55,6 @@ public void init() {
if (!string.startsWith(ChatBot.getConfig().getCommandPrefix()) || (sender != null && GeneralHelper.matchUUIDs(sender.toString(), getClientConnection().getSession().getUuid()))) {
return;
}
if (!stopWatch.hasPassed(ChatBot.getConfig().getMessageDelay())) {
return;
}
try {
String cmd = string.split(" ")[0].replace(ChatBot.getConfig().getCommandPrefix(), "");
String input;
Expand All @@ -71,7 +67,6 @@ public void init() {
if (command.getName().equalsIgnoreCase(cmd) || command.getAlias().contains(cmd.toLowerCase())) {
try {
command.run(input, sender);
stopWatch.reset();
return;
} catch (Exception e) {
e.printStackTrace();
Expand Down
13 changes: 13 additions & 0 deletions src/me/dustin/chatbot/network/player/ClientPlayer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package me.dustin.chatbot.network.player;

import me.dustin.chatbot.ChatBot;
import me.dustin.chatbot.helper.StopWatch;
import me.dustin.chatbot.network.ClientConnection;
import me.dustin.chatbot.network.packet.c2s.play.ServerBoundChatPacket;

import java.util.UUID;

Expand All @@ -13,6 +16,8 @@ public class ClientPlayer {
private double x,y,z;
private float yaw, pitch;

private final StopWatch messageStopwatch = new StopWatch();

public ClientPlayer(String name, UUID uuid, ClientConnection clientConnection) {
this.name = name;
this.uuid = uuid;
Expand All @@ -22,6 +27,14 @@ public ClientPlayer(String name, UUID uuid, ClientConnection clientConnection) {
public void tick() {
}

public void chat(String message) {
if (!messageStopwatch.hasPassed(ChatBot.getConfig().getMessageDelay())) {
return;
}
getClientConnection().sendPacket(new ServerBoundChatPacket((ChatBot.getConfig().isGreenText() && !message.startsWith("/") ? ">" : "") + message));
messageStopwatch.reset();
}

public ClientConnection getClientConnection() {
return clientConnection;
}
Expand Down
6 changes: 1 addition & 5 deletions src/me/dustin/chatbot/process/ChatBotProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
public abstract class ChatBotProcess {

private final ClientConnection clientConnection;
private final StopWatch messageStopWatch = new StopWatch();

public ChatBotProcess(ClientConnection clientConnection) {
this.clientConnection = clientConnection;
Expand All @@ -23,9 +22,6 @@ public ClientConnection getClientConnection() {
}

public void sendChat(String message) {
if (!messageStopWatch.hasPassed(ChatBot.getConfig().getMessageDelay()))
return;
messageStopWatch.reset();
getClientConnection().sendPacket(new ServerBoundChatPacket((ChatBot.getConfig().isGreenText() && !message.startsWith("/") ? ">" : "") + message));
getClientConnection().getClientPlayer().chat(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void tick() {
int size = announcements.size();
Random random = new Random();
int select = random.nextInt(size);
getClientConnection().sendPacket(new ServerBoundChatPacket((ChatBot.getConfig().isGreenText() ? ">" : "") + announcements.get(select).replace("{PREFIX}", ChatBot.getConfig().getCommandPrefix())));
getClientConnection().getClientPlayer().chat(announcements.get(select).replace("{PREFIX}", ChatBot.getConfig().getCommandPrefix()));
stopWatch.reset();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/me/dustin/chatbot/process/impl/Chat2b2tProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void init() {
return;
String body = GeneralHelper.strip(event.getChatMessagePacket().getMessage().getBody().toLowerCase());
if (stopWatch.hasPassed(ChatBot.getConfig().getMessageDelay()) && (body.contains("2b2t") || body.contains("2b") || body.contains("2builders2tools") || body.contains("2 builders 2 tools") || body.contains("oldest anarchy server in minecraft")) && !GeneralHelper.matchUUIDs(event.getChatMessagePacket().getSender().toString(), getClientConnection().getSession().getUuid())) {
getClientConnection().sendPacket(new ServerBoundChatPacket((ChatBot.getConfig().isGreenText() ? ">" : "") + "Congrats. We only made it " + chatCount + " messages before someone mentioned 2b2t."));
getClientConnection().getClientPlayer().chat("Congrats. We only made it " + chatCount + " messages before someone mentioned 2b2t.");
chatCount = 0;
stopWatch.reset();
} else {
Expand Down

0 comments on commit 848d400

Please sign in to comment.