aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/me/xmrvizzy/skyblocker/utils/scheduler
diff options
context:
space:
mode:
authorYasin <a.piri@hotmail.de>2023-10-09 12:58:02 +0200
committerYasin <a.piri@hotmail.de>2023-10-09 12:58:02 +0200
commitbd3f0329d0e391bd84b5f9e3ff207d9dd9815853 (patch)
tree2fd1d1ef625f57acc2e4916c967d8d2393844798 /src/main/java/me/xmrvizzy/skyblocker/utils/scheduler
parent2315b90da8117f28f66348927afdb621ee4fc815 (diff)
downloadSkyblocker-bd3f0329d0e391bd84b5f9e3ff207d9dd9815853.tar.gz
Skyblocker-bd3f0329d0e391bd84b5f9e3ff207d9dd9815853.tar.bz2
Skyblocker-bd3f0329d0e391bd84b5f9e3ff207d9dd9815853.zip
new pr because fixing merge conflict would take too long
Diffstat (limited to 'src/main/java/me/xmrvizzy/skyblocker/utils/scheduler')
-rw-r--r--src/main/java/me/xmrvizzy/skyblocker/utils/scheduler/MessageScheduler.java66
-rw-r--r--src/main/java/me/xmrvizzy/skyblocker/utils/scheduler/Scheduler.java140
2 files changed, 0 insertions, 206 deletions
diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/scheduler/MessageScheduler.java b/src/main/java/me/xmrvizzy/skyblocker/utils/scheduler/MessageScheduler.java
deleted file mode 100644
index b8ffa548..00000000
--- a/src/main/java/me/xmrvizzy/skyblocker/utils/scheduler/MessageScheduler.java
+++ /dev/null
@@ -1,66 +0,0 @@
-package me.xmrvizzy.skyblocker.utils.scheduler;
-
-import net.minecraft.client.MinecraftClient;
-
-/**
- * A scheduler for sending chat messages or commands. Use the instance in {@link #INSTANCE}. Do not instantiate this class.
- */
-public class MessageScheduler extends Scheduler {
- /**
- * The minimum delay that the server will accept between chat messages.
- */
- private static final int MIN_DELAY = 200;
- public static final MessageScheduler INSTANCE = new MessageScheduler();
- /**
- * The timestamp of the last message send,
- */
- private long lastMessage = 0;
-
- protected MessageScheduler() {
- }
-
- /**
- * Sends a chat message or command after the minimum cooldown. Prefer this method to send messages or commands to the server.
- *
- * @param message the message to send
- */
- public void sendMessageAfterCooldown(String message) {
- if (lastMessage + MIN_DELAY < System.currentTimeMillis()) {
- sendMessage(message);
- lastMessage = System.currentTimeMillis();
- } else {
- queueMessage(message, 0);
- }
- }
-
- private void sendMessage(String message) {
- if (MinecraftClient.getInstance().player != null) {
- if (message.startsWith("/")) {
- MinecraftClient.getInstance().player.networkHandler.sendCommand(message.substring(1));
- } else {
- MinecraftClient.getInstance().inGameHud.getChatHud().addToMessageHistory(message);
- MinecraftClient.getInstance().player.networkHandler.sendChatMessage(message);
- }
- }
- }
-
- /**
- * Queues a chat message or command to send in {@code delay} ticks. Use this method to send messages or commands a set time in the future. The minimum cooldown is still respected.
- *
- * @param message the message to send
- * @param delay the delay before sending the message in ticks
- */
- public void queueMessage(String message, int delay) {
- schedule(() -> sendMessage(message), delay);
- }
-
- @Override
- protected boolean runTask(Runnable task) {
- if (lastMessage + MIN_DELAY < System.currentTimeMillis()) {
- task.run();
- lastMessage = System.currentTimeMillis();
- return true;
- }
- return false;
- }
-}
diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/scheduler/Scheduler.java b/src/main/java/me/xmrvizzy/skyblocker/utils/scheduler/Scheduler.java
deleted file mode 100644
index 700bdce3..00000000
--- a/src/main/java/me/xmrvizzy/skyblocker/utils/scheduler/Scheduler.java
+++ /dev/null
@@ -1,140 +0,0 @@
-package me.xmrvizzy.skyblocker.utils.scheduler;
-
-import com.mojang.brigadier.Command;
-import it.unimi.dsi.fastutil.ints.AbstractInt2ObjectMap;
-import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
-import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
-import net.minecraft.client.MinecraftClient;
-import net.minecraft.client.gui.screen.Screen;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.function.Supplier;
-
-/**
- * A scheduler for running tasks at a later time. Tasks will be run synchronously on the main client thread. Use the instance stored in {@link #INSTANCE}. Do not instantiate this class.
- */
-public class Scheduler {
- private static final Logger LOGGER = LoggerFactory.getLogger(Scheduler.class);
- public static final Scheduler INSTANCE = new Scheduler();
- private int currentTick = 0;
- private final AbstractInt2ObjectMap<List<ScheduledTask>> tasks = new Int2ObjectOpenHashMap<>();
-
- protected Scheduler() {
- }
-
- /**
- * Schedules a task to run after a delay.
- *
- * @param task the task to run
- * @param delay the delay in ticks
- */
- public void schedule(Runnable task, int delay) {
- if (delay >= 0) {
- addTask(new ScheduledTask(task), currentTick + delay);
- } else {
- LOGGER.warn("Scheduled a task with negative delay");
- }
- }
-
- /**
- * Schedules a task to run every period ticks.
- *
- * @param task the task to run
- * @param period the period in ticks
- */
- public void scheduleCyclic(Runnable task, int period) {
- if (period > 0) {
- addTask(new CyclicTask(task, period), currentTick);
- } else {
- LOGGER.error("Attempted to schedule a cyclic task with period lower than 1");
- }
- }
-
- public static Command<FabricClientCommandSource> queueOpenScreenCommand(Supplier<Screen> screenSupplier) {
- return context -> INSTANCE.queueOpenScreen(screenSupplier);
- }
-
- /**
- * Schedules a screen to open in the next tick. Used in commands to avoid screen immediately closing after the command is executed.
- *
- * @param screenSupplier the supplier of the screen to open
- * @see #queueOpenScreenCommand(Supplier)
- */
- public int queueOpenScreen(Supplier<Screen> screenSupplier) {
- MinecraftClient.getInstance().send(() -> MinecraftClient.getInstance().setScreen(screenSupplier.get()));
- return Command.SINGLE_SUCCESS;
- }
-
- public void tick() {
- if (tasks.containsKey(currentTick)) {
- List<ScheduledTask> currentTickTasks = tasks.get(currentTick);
- //noinspection ForLoopReplaceableByForEach (or else we get a ConcurrentModificationException)
- for (int i = 0; i < currentTickTasks.size(); i++) {
- ScheduledTask task = currentTickTasks.get(i);
- if (!runTask(task)) {
- tasks.computeIfAbsent(currentTick + 1, key -> new ArrayList<>()).add(task);
- }
- }
- tasks.remove(currentTick);
- }
- currentTick += 1;
- }
-
- /**
- * Runs the task if able.
- *
- * @param task the task to run
- * @return {@code true} if the task is run, and {@link false} if task is not run.
- */
- protected boolean runTask(Runnable task) {
- task.run();
- return true;
- }
-
- private void addTask(ScheduledTask scheduledTask, int schedule) {
- if (tasks.containsKey(schedule)) {
- tasks.get(schedule).add(scheduledTask);
- } else {
- List<ScheduledTask> list = new ArrayList<>();
- list.add(scheduledTask);
- tasks.put(schedule, list);
- }
- }
-
- /**
- * A task that runs every period ticks. More specifically, this task reschedules itself to run again after period ticks every time it runs.
- */
- protected class CyclicTask extends ScheduledTask {
- private final int period;
-
- CyclicTask(Runnable inner, int period) {
- super(inner);
- this.period = period;
- }
-
- @Override
- public void run() {
- super.run();
- addTask(this, currentTick + period);
- }
- }
-
- /**
- * A task that runs at a specific tick, relative to {@link #currentTick}.
- */
- protected static class ScheduledTask implements Runnable {
- private final Runnable inner;
-
- public ScheduledTask(Runnable inner) {
- this.inner = inner;
- }
-
- @Override
- public void run() {
- inner.run();
- }
- }
-}