refactor: optimize the bot by at least a little bit
This commit is contained in:
@@ -1 +1 @@
|
||||
1676
|
||||
1684
|
||||
@@ -26,6 +26,7 @@ import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.Serverbound
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -39,7 +40,7 @@ public class ChatPlugin extends Bot.Listener {
|
||||
|
||||
private final List<ChatParser> chatParsers;
|
||||
|
||||
private final List<String> queue = Collections.synchronizedList(new ArrayList<>());
|
||||
private final ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<>();
|
||||
|
||||
public final int queueDelay;
|
||||
|
||||
@@ -239,9 +240,9 @@ public class ChatPlugin extends Bot.Listener {
|
||||
private void sendChatTick () {
|
||||
if (queue.size() > 100) queue.clear(); // detects spam, like spamming *echo for example
|
||||
|
||||
if (queue.isEmpty()) return;
|
||||
final String message = queue.poll();
|
||||
|
||||
final String message = queue.getFirst();
|
||||
if (message == null) return;
|
||||
|
||||
if (message.startsWith("/")) {
|
||||
String removedMessage = message.substring(1);
|
||||
@@ -263,8 +264,6 @@ public class ChatPlugin extends Bot.Listener {
|
||||
} else {
|
||||
sendChatInstantly(message);
|
||||
}
|
||||
|
||||
queue.remove(0);
|
||||
}
|
||||
|
||||
public void sendCommandInstantly (String command) {
|
||||
|
||||
@@ -27,6 +27,7 @@ import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.player.Serv
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -50,7 +51,7 @@ public class CorePlugin extends PositionPlugin.Listener {
|
||||
|
||||
public Vector3i block = null;
|
||||
|
||||
public final List<String> placeBlockQueue = Collections.synchronizedList(new ArrayList<>());
|
||||
public final ConcurrentLinkedQueue<String> placeBlockQueue = new ConcurrentLinkedQueue<>();
|
||||
|
||||
private int commandsPerSecond = 0;
|
||||
|
||||
@@ -105,17 +106,16 @@ public class CorePlugin extends PositionPlugin.Listener {
|
||||
@Override
|
||||
public void onTick() {
|
||||
try {
|
||||
final List<String> clonedQueue = new ArrayList<>(placeBlockQueue);
|
||||
|
||||
if (clonedQueue.isEmpty()) return;
|
||||
|
||||
if (clonedQueue.size() > 500) {
|
||||
if (placeBlockQueue.size() > 300) {
|
||||
placeBlockQueue.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
forceRunPlaceBlock(clonedQueue.getFirst());
|
||||
placeBlockQueue.removeFirst();
|
||||
final String command = placeBlockQueue.poll();
|
||||
|
||||
if (command == null) return;
|
||||
|
||||
forceRunPlaceBlock(command);
|
||||
} catch (Exception e) {
|
||||
bot.logger.error(e);
|
||||
}
|
||||
|
||||
@@ -25,12 +25,7 @@ public class FilterManagerPlugin extends PlayersPlugin.Listener {
|
||||
|
||||
bot.players.addListener(this);
|
||||
|
||||
bot.tick.addListener(new TickPlugin.Listener() {
|
||||
@Override
|
||||
public void onAlwaysTick() {
|
||||
FilterManagerPlugin.this.onAlwaysTick();
|
||||
}
|
||||
});
|
||||
bot.executor.scheduleAtFixedRate(this::removeLeftPlayers, 0, 1, TimeUnit.SECONDS);
|
||||
|
||||
bot.addListener(new Bot.Listener() {
|
||||
@Override
|
||||
@@ -58,8 +53,8 @@ public class FilterManagerPlugin extends PlayersPlugin.Listener {
|
||||
bot.executor.scheduleAtFixedRate(this::kick, 0, 10, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
private void onAlwaysTick () {
|
||||
// remove from list if player is not on the server
|
||||
private void removeLeftPlayers () {
|
||||
// remove from list if player is not on the server anymore
|
||||
list.entrySet().removeIf(entry -> bot.players.getEntry(entry.getKey().profile.getId()) == null);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,10 +5,10 @@ import me.chayapak1.chomens_bot.data.team.Team;
|
||||
import me.chayapak1.chomens_bot.util.UUIDUtilities;
|
||||
import org.geysermc.mcprotocollib.network.event.session.ConnectedEvent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
// the name might sound confusing but it just adds the bot into its own team
|
||||
public class TeamJoinerPlugin extends TickPlugin.Listener {
|
||||
public class TeamJoinerPlugin {
|
||||
public final String teamName;
|
||||
|
||||
private final Bot bot;
|
||||
@@ -24,16 +24,17 @@ public class TeamJoinerPlugin extends TickPlugin.Listener {
|
||||
}
|
||||
});
|
||||
|
||||
bot.tick.addListener(this);
|
||||
bot.executor.scheduleAtFixedRate(this::check, 0, 500, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
private void connected () {
|
||||
addTeam();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTick () {
|
||||
public void check () {
|
||||
try {
|
||||
if (!bot.loggedIn) return;
|
||||
|
||||
final Team team = bot.team.findTeamByName(teamName);
|
||||
|
||||
if (team == null) {
|
||||
@@ -41,14 +42,15 @@ public class TeamJoinerPlugin extends TickPlugin.Listener {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!team.players.contains(bot.username)) joinTeam();
|
||||
|
||||
for (String player : new ArrayList<>(team.players)) {
|
||||
if (!player.equals(bot.username)) {
|
||||
excludeOthers();
|
||||
break;
|
||||
}
|
||||
if (!team.players.contains(bot.username)) {
|
||||
joinTeam();
|
||||
return;
|
||||
}
|
||||
|
||||
// checks if ONLY the bot is in the team, and not anyone else
|
||||
if (team.players.size() == 1 && team.players.getFirst().equals(bot.username)) return;
|
||||
|
||||
excludeOthers();
|
||||
} catch (Exception e) {
|
||||
bot.logger.error(e);
|
||||
}
|
||||
|
||||
@@ -10,13 +10,9 @@ import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.scoreboard.
|
||||
import java.util.*;
|
||||
|
||||
public class TeamPlugin extends Bot.Listener {
|
||||
private final Bot bot;
|
||||
|
||||
public final List<Team> teams = Collections.synchronizedList(new ArrayList<>());
|
||||
|
||||
public TeamPlugin (Bot bot) {
|
||||
this.bot = bot;
|
||||
|
||||
bot.addListener(this);
|
||||
}
|
||||
|
||||
@@ -47,63 +43,59 @@ public class TeamPlugin extends Bot.Listener {
|
||||
}
|
||||
|
||||
public void packetReceived(ClientboundSetPlayerTeamPacket packet) {
|
||||
try {
|
||||
switch (packet.getAction()) {
|
||||
case CREATE -> {
|
||||
final Team team = new Team(
|
||||
packet.getTeamName(),
|
||||
new ArrayList<>(),
|
||||
packet.getDisplayName(),
|
||||
packet.isFriendlyFire(),
|
||||
packet.isSeeFriendlyInvisibles(),
|
||||
packet.getNameTagVisibility(),
|
||||
packet.getCollisionRule(),
|
||||
packet.getColor(),
|
||||
packet.getPrefix(),
|
||||
packet.getSuffix()
|
||||
);
|
||||
switch (packet.getAction()) {
|
||||
case CREATE -> {
|
||||
final Team team = new Team(
|
||||
packet.getTeamName(),
|
||||
new ArrayList<>(),
|
||||
packet.getDisplayName(),
|
||||
packet.isFriendlyFire(),
|
||||
packet.isSeeFriendlyInvisibles(),
|
||||
packet.getNameTagVisibility(),
|
||||
packet.getCollisionRule(),
|
||||
packet.getColor(),
|
||||
packet.getPrefix(),
|
||||
packet.getSuffix()
|
||||
);
|
||||
|
||||
teams.add(team);
|
||||
}
|
||||
case REMOVE -> {
|
||||
final Team team = findTeamByName(packet.getTeamName());
|
||||
|
||||
if (team == null) return;
|
||||
|
||||
teams.remove(team);
|
||||
}
|
||||
case UPDATE -> {
|
||||
final Team team = findTeamByName(packet.getTeamName());
|
||||
|
||||
if (team == null) return;
|
||||
|
||||
team.teamName = packet.getTeamName();
|
||||
team.displayName = packet.getDisplayName();
|
||||
team.friendlyFire = packet.isFriendlyFire();
|
||||
team.seeFriendlyInvisibles = packet.isSeeFriendlyInvisibles();
|
||||
team.nametagVisibility = packet.getNameTagVisibility();
|
||||
team.collisionRule = packet.getCollisionRule();
|
||||
team.color = packet.getColor();
|
||||
team.prefix = packet.getPrefix();
|
||||
team.suffix = packet.getSuffix();
|
||||
}
|
||||
case ADD_PLAYER -> {
|
||||
final Team team = findTeamByName(packet.getTeamName());
|
||||
|
||||
if (team == null) return;
|
||||
|
||||
team.players.addAll(Arrays.asList(packet.getPlayers()));
|
||||
}
|
||||
case REMOVE_PLAYER -> {
|
||||
final Team team = findTeamByName(packet.getTeamName());
|
||||
|
||||
if (team == null) return;
|
||||
|
||||
team.players.removeAll(Arrays.asList(packet.getPlayers()));
|
||||
}
|
||||
teams.add(team);
|
||||
}
|
||||
case REMOVE -> {
|
||||
final Team team = findTeamByName(packet.getTeamName());
|
||||
|
||||
if (team == null) return;
|
||||
|
||||
teams.remove(team);
|
||||
}
|
||||
case UPDATE -> {
|
||||
final Team team = findTeamByName(packet.getTeamName());
|
||||
|
||||
if (team == null) return;
|
||||
|
||||
team.teamName = packet.getTeamName();
|
||||
team.displayName = packet.getDisplayName();
|
||||
team.friendlyFire = packet.isFriendlyFire();
|
||||
team.seeFriendlyInvisibles = packet.isSeeFriendlyInvisibles();
|
||||
team.nametagVisibility = packet.getNameTagVisibility();
|
||||
team.collisionRule = packet.getCollisionRule();
|
||||
team.color = packet.getColor();
|
||||
team.prefix = packet.getPrefix();
|
||||
team.suffix = packet.getSuffix();
|
||||
}
|
||||
case ADD_PLAYER -> {
|
||||
final Team team = findTeamByName(packet.getTeamName());
|
||||
|
||||
if (team == null) return;
|
||||
|
||||
team.players.addAll(Arrays.asList(packet.getPlayers()));
|
||||
}
|
||||
case REMOVE_PLAYER -> {
|
||||
final Team team = findTeamByName(packet.getTeamName());
|
||||
|
||||
if (team == null) return;
|
||||
|
||||
team.players.removeAll(Arrays.asList(packet.getPlayers()));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
bot.logger.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user