aboutsummaryrefslogtreecommitdiff
path: root/spark-common/src/main/java/me/lucko/spark/common/tick
diff options
context:
space:
mode:
authorLuck <git@lucko.me>2021-03-23 00:52:44 +0000
committerLuck <git@lucko.me>2021-03-23 00:52:44 +0000
commit0fd7d30f2a9027c2bd9df3215759c6d91d110acc (patch)
treebdf02fa8b7c6dfc7dc6db5eaaabeb1e17189d420 /spark-common/src/main/java/me/lucko/spark/common/tick
parent9766754d28fcbca1ccbeefc11ef7a88a4e3d7946 (diff)
downloadspark-0fd7d30f2a9027c2bd9df3215759c6d91d110acc.tar.gz
spark-0fd7d30f2a9027c2bd9df3215759c6d91d110acc.tar.bz2
spark-0fd7d30f2a9027c2bd9df3215759c6d91d110acc.zip
Refactor and tidy up, more consistent code style
Diffstat (limited to 'spark-common/src/main/java/me/lucko/spark/common/tick')
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/tick/AbstractTickHook.java53
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/tick/AbstractTickReporter.java45
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/tick/TickHook.java64
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/tick/TickReporter.java57
4 files changed, 219 insertions, 0 deletions
diff --git a/spark-common/src/main/java/me/lucko/spark/common/tick/AbstractTickHook.java b/spark-common/src/main/java/me/lucko/spark/common/tick/AbstractTickHook.java
new file mode 100644
index 0000000..a6e8745
--- /dev/null
+++ b/spark-common/src/main/java/me/lucko/spark/common/tick/AbstractTickHook.java
@@ -0,0 +1,53 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package me.lucko.spark.common.tick;
+
+import java.util.HashSet;
+import java.util.Set;
+
+public abstract class AbstractTickHook implements TickHook {
+
+ private final Set<Callback> tasks = new HashSet<>();
+ private int tick = 0;
+
+ protected void onTick() {
+ for (Callback r : this.tasks) {
+ r.onTick(this.tick);
+ }
+ this.tick++;
+ }
+
+ @Override
+ public int getCurrentTick() {
+ return this.tick;
+ }
+
+ @Override
+ public void addCallback(Callback runnable) {
+ this.tasks.add(runnable);
+ }
+
+ @Override
+ public void removeCallback(Callback runnable) {
+ this.tasks.remove(runnable);
+ }
+
+}
diff --git a/spark-common/src/main/java/me/lucko/spark/common/tick/AbstractTickReporter.java b/spark-common/src/main/java/me/lucko/spark/common/tick/AbstractTickReporter.java
new file mode 100644
index 0000000..74a814d
--- /dev/null
+++ b/spark-common/src/main/java/me/lucko/spark/common/tick/AbstractTickReporter.java
@@ -0,0 +1,45 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package me.lucko.spark.common.tick;
+
+import java.util.HashSet;
+import java.util.Set;
+
+public abstract class AbstractTickReporter implements TickReporter {
+ private final Set<Callback> tasks = new HashSet<>();
+
+ protected void onTick(double duration) {
+ for (Callback r : this.tasks) {
+ r.onTick(duration);
+ }
+ }
+
+ @Override
+ public void addCallback(Callback runnable) {
+ this.tasks.add(runnable);
+ }
+
+ @Override
+ public void removeCallback(Callback runnable) {
+ this.tasks.remove(runnable);
+ }
+
+}
diff --git a/spark-common/src/main/java/me/lucko/spark/common/tick/TickHook.java b/spark-common/src/main/java/me/lucko/spark/common/tick/TickHook.java
new file mode 100644
index 0000000..e673258
--- /dev/null
+++ b/spark-common/src/main/java/me/lucko/spark/common/tick/TickHook.java
@@ -0,0 +1,64 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package me.lucko.spark.common.tick;
+
+/**
+ * A hook with the game's "tick loop".
+ */
+public interface TickHook extends AutoCloseable {
+
+ /**
+ * Starts the hook
+ */
+ void start();
+
+ /**
+ * Stops the hook
+ */
+ @Override
+ void close();
+
+ /**
+ * Gets the current tick number
+ *
+ * @return the current tick
+ */
+ int getCurrentTick();
+
+ /**
+ * Adds a callback to be called each time the tick increments
+ *
+ * @param runnable the task
+ */
+ void addCallback(Callback runnable);
+
+ /**
+ * Removes a callback
+ *
+ * @param runnable the callback
+ */
+ void removeCallback(Callback runnable);
+
+ interface Callback {
+ void onTick(int currentTick);
+ }
+
+}
diff --git a/spark-common/src/main/java/me/lucko/spark/common/tick/TickReporter.java b/spark-common/src/main/java/me/lucko/spark/common/tick/TickReporter.java
new file mode 100644
index 0000000..b575f59
--- /dev/null
+++ b/spark-common/src/main/java/me/lucko/spark/common/tick/TickReporter.java
@@ -0,0 +1,57 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package me.lucko.spark.common.tick;
+
+/**
+ * A reporting callback for the game's "tick loop".
+ */
+public interface TickReporter extends AutoCloseable {
+
+ /**
+ * Starts the reporter
+ */
+ void start();
+
+ /**
+ * Stops the reporter
+ */
+ @Override
+ void close();
+
+ /**
+ * Adds a callback to be called each time the tick increments
+ *
+ * @param runnable the callback
+ */
+ void addCallback(Callback runnable);
+
+ /**
+ * Removes a callback
+ *
+ * @param runnable callback
+ */
+ void removeCallback(Callback runnable);
+
+ interface Callback {
+ void onTick(double duration);
+ }
+
+}