Skip to content

Commit

Permalink
Add back pufferfish async mob spawning
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreeam-qwq committed Jul 18, 2024
1 parent 5d0794b commit ed3cd2c
Show file tree
Hide file tree
Showing 79 changed files with 243 additions and 115 deletions.
153 changes: 153 additions & 0 deletions patches/server/0004-Pufferfish-Utils.patch
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,156 @@ index 0000000000000000000000000000000000000000..53aab67aea0a28c004c6106aa775443c
+ setLevel(Level.ALL);
+ }
+}
diff --git a/src/main/java/gg/pufferfish/pufferfish/util/AsyncExecutor.java b/src/main/java/gg/pufferfish/pufferfish/util/AsyncExecutor.java
new file mode 100644
index 0000000000000000000000000000000000000000..a62d22dc4ae2cc82cf6763e8b0ce6d4611782a57
--- /dev/null
+++ b/src/main/java/gg/pufferfish/pufferfish/util/AsyncExecutor.java
@@ -0,0 +1,73 @@
+package gg.pufferfish.pufferfish.util;
+
+import com.google.common.collect.Queues;
+import gg.pufferfish.pufferfish.PufferfishLogger;
+
+import java.util.Queue;
+import java.util.concurrent.locks.Condition;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+import java.util.logging.Level;
+
+public class AsyncExecutor implements Runnable {
+
+ private final Queue<Runnable> jobs = Queues.newArrayDeque();
+ private final Lock mutex = new ReentrantLock();
+ private final Condition cond = mutex.newCondition();
+ private final Thread thread;
+ private volatile boolean killswitch = false;
+
+ public AsyncExecutor(String threadName) {
+ this.thread = new Thread(this, threadName);
+ }
+
+ public void start() {
+ thread.start();
+ }
+
+ public void kill() {
+ killswitch = true;
+ cond.signalAll();
+ }
+
+ public void submit(Runnable runnable) {
+ mutex.lock();
+ try {
+ jobs.offer(runnable);
+ cond.signalAll();
+ } finally {
+ mutex.unlock();
+ }
+ }
+
+ @Override
+ public void run() {
+ while (!killswitch) {
+ try {
+ Runnable runnable = takeRunnable();
+ if (runnable != null) {
+ runnable.run();
+ }
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ } catch (Exception e) {
+ PufferfishLogger.LOGGER.log(Level.SEVERE, e, () -> "Failed to execute async job for thread " + thread.getName());
+ }
+ }
+ }
+
+ private Runnable takeRunnable() throws InterruptedException {
+ mutex.lock();
+ try {
+ while (jobs.isEmpty() && !killswitch) {
+ cond.await();
+ }
+
+ if (jobs.isEmpty()) return null; // We've set killswitch
+
+ return jobs.remove();
+ } finally {
+ mutex.unlock();
+ }
+ }
+}
diff --git a/src/main/java/gg/pufferfish/pufferfish/util/IterableWrapper.java b/src/main/java/gg/pufferfish/pufferfish/util/IterableWrapper.java
new file mode 100644
index 0000000000000000000000000000000000000000..8b4a51ae41e99d41a1095333c2036efcc9a38973
--- /dev/null
+++ b/src/main/java/gg/pufferfish/pufferfish/util/IterableWrapper.java
@@ -0,0 +1,20 @@
+package gg.pufferfish.pufferfish.util;
+
+import java.util.Iterator;
+
+import org.jetbrains.annotations.NotNull;
+
+public class IterableWrapper<T> implements Iterable<T> {
+
+ private final Iterator<T> iterator;
+
+ public IterableWrapper(Iterator<T> iterator) {
+ this.iterator = iterator;
+ }
+
+ @NotNull
+ @Override
+ public Iterator<T> iterator() {
+ return iterator;
+ }
+}
diff --git a/src/main/java/gg/pufferfish/pufferfish/util/Long2ObjectOpenHashMapWrapper.java b/src/main/java/gg/pufferfish/pufferfish/util/Long2ObjectOpenHashMapWrapper.java
new file mode 100644
index 0000000000000000000000000000000000000000..5578acce073cea8a60619e634b3862624c8a1ae8
--- /dev/null
+++ b/src/main/java/gg/pufferfish/pufferfish/util/Long2ObjectOpenHashMapWrapper.java
@@ -0,0 +1,42 @@
+package gg.pufferfish.pufferfish.util;
+
+import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
+
+import java.util.Map;
+
+import org.jetbrains.annotations.Nullable;
+
+public class Long2ObjectOpenHashMapWrapper<V> extends Long2ObjectOpenHashMap<V> {
+
+ private final Map<Long, V> backingMap;
+
+ public Long2ObjectOpenHashMapWrapper(Map<Long, V> map) {
+ backingMap = map;
+ }
+
+ @Override
+ public V put(Long key, V value) {
+ return backingMap.put(key, value);
+ }
+
+ @Override
+ public V get(Object key) {
+ return backingMap.get(key);
+ }
+
+ @Override
+ public V remove(Object key) {
+ return backingMap.remove(key);
+ }
+
+ @Nullable
+ @Override
+ public V putIfAbsent(Long key, V value) {
+ return backingMap.putIfAbsent(key, value);
+ }
+
+ @Override
+ public int size() {
+ return backingMap.size();
+ }
+}
Loading

0 comments on commit ed3cd2c

Please sign in to comment.