refactor: second ticker in TickPlugin
This commit is contained in:
@@ -28,11 +28,10 @@ import java.security.spec.InvalidKeySpecException;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
// This is inspired from the ChomeNS Bot Proxy which is in the JavaScript version of ChomeNS Bot.
|
||||
public class ChomeNSModIntegrationPlugin implements ChatPlugin.Listener, PlayersPlugin.Listener {
|
||||
public class ChomeNSModIntegrationPlugin implements ChatPlugin.Listener, PlayersPlugin.Listener, TickPlugin.Listener {
|
||||
private static final String ID = "chomens_mod";
|
||||
|
||||
public static final List<Class<? extends Packet>> SERVERBOUND_PACKETS = new ArrayList<>();
|
||||
@@ -143,8 +142,12 @@ public class ChomeNSModIntegrationPlugin implements ChatPlugin.Listener, Players
|
||||
|
||||
bot.chat.addListener(this);
|
||||
bot.players.addListener(this);
|
||||
bot.tick.addListener(this);
|
||||
}
|
||||
|
||||
bot.executor.scheduleAtFixedRate(this::tryHandshaking, 1, 1, TimeUnit.SECONDS);
|
||||
@Override
|
||||
public void onSecondTick () {
|
||||
tryHandshaking();
|
||||
}
|
||||
|
||||
public byte[] decrypt (byte[] data) throws Exception {
|
||||
|
||||
@@ -14,9 +14,8 @@ import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class CommandHandlerPlugin {
|
||||
public class CommandHandlerPlugin implements TickPlugin.Listener {
|
||||
public static final List<Command> commands = new ArrayList<>();
|
||||
|
||||
static {
|
||||
@@ -61,6 +60,10 @@ public class CommandHandlerPlugin {
|
||||
registerCommand(new RestartCommand());
|
||||
}
|
||||
|
||||
public static void registerCommand (Command command) {
|
||||
commands.add(command);
|
||||
}
|
||||
|
||||
public boolean disabled = false;
|
||||
|
||||
private final Bot bot;
|
||||
@@ -70,11 +73,12 @@ public class CommandHandlerPlugin {
|
||||
public CommandHandlerPlugin (Bot bot) {
|
||||
this.bot = bot;
|
||||
|
||||
bot.executor.scheduleAtFixedRate(() -> commandPerSecond = 0, 0, 1, TimeUnit.SECONDS);
|
||||
bot.tick.addListener(this);
|
||||
}
|
||||
|
||||
public static void registerCommand (Command command) {
|
||||
commands.add(command);
|
||||
@Override
|
||||
public void onSecondTick () {
|
||||
commandPerSecond = 0;
|
||||
}
|
||||
|
||||
// BETTER QUALITY than the js version
|
||||
|
||||
@@ -90,17 +90,6 @@ public class CorePlugin
|
||||
);
|
||||
}
|
||||
|
||||
bot.executor.scheduleAtFixedRate(() -> {
|
||||
checkCoreTick();
|
||||
|
||||
resizeTick();
|
||||
|
||||
if (!shouldRefill) return;
|
||||
|
||||
refill(false);
|
||||
shouldRefill = false;
|
||||
}, 0, 1, TimeUnit.SECONDS);
|
||||
|
||||
bot.addListener(this);
|
||||
bot.world.addListener(this);
|
||||
bot.tick.addListener(this);
|
||||
@@ -126,6 +115,18 @@ public class CorePlugin
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSecondTick () {
|
||||
checkCoreTick();
|
||||
|
||||
resizeTick();
|
||||
|
||||
if (!shouldRefill) return;
|
||||
|
||||
refill(false);
|
||||
shouldRefill = false;
|
||||
}
|
||||
|
||||
public boolean hasRateLimit () {
|
||||
return bot.options.coreRateLimit.limit > 0;
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class FilterManagerPlugin
|
||||
extends Bot.Listener
|
||||
implements PlayersPlugin.Listener, ChatPlugin.Listener, CommandSpyPlugin.Listener
|
||||
implements PlayersPlugin.Listener, ChatPlugin.Listener, CommandSpyPlugin.Listener, TickPlugin.Listener
|
||||
{
|
||||
private final Bot bot;
|
||||
|
||||
@@ -26,16 +26,20 @@ public class FilterManagerPlugin
|
||||
public FilterManagerPlugin (Bot bot) {
|
||||
this.bot = bot;
|
||||
|
||||
bot.players.addListener(this);
|
||||
|
||||
bot.executor.scheduleAtFixedRate(this::removeLeftPlayers, 0, 1, TimeUnit.SECONDS);
|
||||
bot.executor.scheduleAtFixedRate(this::kick, 0, 10, TimeUnit.SECONDS);
|
||||
|
||||
bot.addListener(this);
|
||||
bot.players.addListener(this);
|
||||
bot.tick.addListener(this);
|
||||
bot.chat.addListener(this);
|
||||
bot.commandSpy.addListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSecondTick () {
|
||||
removeLeftPlayers();
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
@@ -22,7 +22,7 @@ import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class PlayersPlugin extends Bot.Listener {
|
||||
public class PlayersPlugin extends Bot.Listener implements TickPlugin.Listener {
|
||||
private final Bot bot;
|
||||
|
||||
public final List<PlayerEntry> list = new ArrayList<>();
|
||||
@@ -34,10 +34,15 @@ public class PlayersPlugin extends Bot.Listener {
|
||||
public PlayersPlugin (Bot bot) {
|
||||
this.bot = bot;
|
||||
|
||||
bot.addListener(this);
|
||||
|
||||
bot.executor.scheduleAtFixedRate(this::queryPlayersIP, 0, 1, TimeUnit.SECONDS);
|
||||
bot.executor.scheduleAtFixedRate(this::onLastKnownNameTick, 0, 5, TimeUnit.SECONDS);
|
||||
|
||||
bot.addListener(this);
|
||||
bot.tick.addListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSecondTick () {
|
||||
queryPlayersIP();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -18,7 +18,6 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
// some part of the code used to be in a test plugin but i thought it would be useful in the future so i moved it here
|
||||
public class PositionPlugin extends Bot.Listener implements TickPlugin.Listener {
|
||||
@@ -37,28 +36,29 @@ public class PositionPlugin extends Bot.Listener implements TickPlugin.Listener
|
||||
public PositionPlugin (Bot bot) {
|
||||
this.bot = bot;
|
||||
|
||||
// notchian clients also does this, sends the position packet every second
|
||||
bot.executor.scheduleAtFixedRate(() -> {
|
||||
if (!bot.loggedIn || isGoingDownFromHeightLimit) return;
|
||||
|
||||
bot.session.send(new ServerboundMovePlayerPosPacket(
|
||||
false,
|
||||
false,
|
||||
position.getX(),
|
||||
position.getY(),
|
||||
position.getZ()
|
||||
));
|
||||
}, 0, 1, TimeUnit.SECONDS);
|
||||
|
||||
bot.addListener(this);
|
||||
bot.tick.addListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTick() {
|
||||
public void onTick () {
|
||||
handleHeightLimit();
|
||||
}
|
||||
|
||||
// notchian clients also does this, where it sends the current position to the server every second
|
||||
@Override
|
||||
public void onSecondTick () {
|
||||
if (isGoingDownFromHeightLimit) return;
|
||||
|
||||
bot.session.send(new ServerboundMovePlayerPosPacket(
|
||||
false,
|
||||
false,
|
||||
position.getX(),
|
||||
position.getY(),
|
||||
position.getZ()
|
||||
));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void packetReceived (Session session, Packet packet) {
|
||||
if (packet instanceof ClientboundPlayerPositionPacket t_packet) packetReceived(t_packet);
|
||||
|
||||
@@ -26,7 +26,7 @@ import java.util.Arrays;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class SelfCarePlugin extends Bot.Listener implements ChatPlugin.Listener, PositionPlugin.Listener {
|
||||
public class SelfCarePlugin extends Bot.Listener implements ChatPlugin.Listener, PositionPlugin.Listener, TickPlugin.Listener {
|
||||
private final Bot bot;
|
||||
|
||||
private ScheduledFuture<?> checkTask;
|
||||
@@ -51,11 +51,15 @@ public class SelfCarePlugin extends Bot.Listener implements ChatPlugin.Listener,
|
||||
public SelfCarePlugin (Bot bot) {
|
||||
this.bot = bot;
|
||||
|
||||
bot.executor.scheduleAtFixedRate(() -> positionPacketsPerSecond = 0, 0, 1, TimeUnit.SECONDS);
|
||||
|
||||
bot.addListener(this);
|
||||
bot.chat.addListener(this);
|
||||
bot.position.addListener(this);
|
||||
bot.tick.addListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSecondTick () {
|
||||
positionPacketsPerSecond = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -15,6 +15,7 @@ public class TickPlugin {
|
||||
this.bot = bot;
|
||||
|
||||
bot.executor.scheduleAtFixedRate(this::tick, 0, 50, TimeUnit.MILLISECONDS);
|
||||
bot.executor.scheduleAtFixedRate(this::tickSecond, 0, 1, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
private void tick () {
|
||||
@@ -39,6 +40,19 @@ public class TickPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
private void tickSecond () {
|
||||
if (!bot.loggedIn) return;
|
||||
|
||||
for (Listener listener : listeners) {
|
||||
try {
|
||||
listener.onSecondTick();
|
||||
} catch (Exception e) {
|
||||
bot.logger.error("Caught exception in a second tick listener!");
|
||||
bot.logger.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void addListener (Listener listener) {
|
||||
listeners.add(listener);
|
||||
}
|
||||
@@ -46,5 +60,6 @@ public class TickPlugin {
|
||||
public interface Listener {
|
||||
default void onTick () {}
|
||||
default void onAlwaysTick () {}
|
||||
default void onSecondTick () {}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user