mirror of
https://github.com/Winds-Studio/Leaf.git
synced 2025-12-22 16:39:22 +00:00
293 lines
10 KiB
Diff
293 lines
10 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: violetc <58360096+s-yh-china@users.noreply.github.com>
|
|
Date: Tue, 13 Sep 2022 16:59:31 +0800
|
|
Subject: [PATCH] Leaves: Server Utils
|
|
|
|
Original license: GPLv3
|
|
Original project: https://github.com/LeavesMC/Leaves
|
|
|
|
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
|
|
index 17eb6e690f358bd660b364c8dbe2a08e2370bcd1..6d2b435e3ba70c98b0d85938c02b38870cfffe4f 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/Entity.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
|
|
@@ -420,6 +420,7 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
|
|
public boolean freezeLocked = false; // Paper - Freeze Tick Lock API
|
|
public boolean collidingWithWorldBorder; // Paper
|
|
public @Nullable Boolean immuneToFire = null; // Purpur - Fire immune API
|
|
+ private CompoundTag leavesData = new CompoundTag(); // Leaves - Leaves ex data
|
|
|
|
public void setOrigin(@javax.annotation.Nonnull Location location) {
|
|
this.origin = location.toVector();
|
|
@@ -2536,6 +2537,7 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
|
|
nbt.putBoolean("Purpur.FireImmune", immuneToFire);
|
|
}
|
|
// Purpur end
|
|
+ nbt.put("Leaves.Data", leavesData); // Leaves - leaves ex data
|
|
return nbt;
|
|
} catch (Throwable throwable) {
|
|
CrashReport crashreport = CrashReport.forThrowable(throwable, "Saving entity NBT");
|
|
@@ -2709,6 +2711,11 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
|
|
immuneToFire = nbt.getBoolean("Purpur.FireImmune");
|
|
}
|
|
// Purpur end
|
|
+ // Leaves start - leaves ex data
|
|
+ if (nbt.contains("Leaves.Data")) {
|
|
+ leavesData = nbt.getCompound("Leaves.Data");
|
|
+ }
|
|
+ // Leaves end - leaves ex data
|
|
|
|
} catch (Throwable throwable) {
|
|
CrashReport crashreport = CrashReport.forThrowable(throwable, "Loading entity NBT");
|
|
@@ -5038,4 +5045,10 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
|
|
return false;
|
|
}
|
|
// Purpur end
|
|
+
|
|
+ // Leaves start - leaves ex data
|
|
+ public CompoundTag getLeavesData() {
|
|
+ return leavesData;
|
|
+ }
|
|
+ // Leaves end - leaves ex data
|
|
}
|
|
diff --git a/src/main/java/top/leavesmc/leaves/LeavesLogger.java b/src/main/java/top/leavesmc/leaves/LeavesLogger.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..890b91a95719f18a75bc2c2176ef5cb9f2bf4274
|
|
--- /dev/null
|
|
+++ b/src/main/java/top/leavesmc/leaves/LeavesLogger.java
|
|
@@ -0,0 +1,16 @@
|
|
+package top.leavesmc.leaves;
|
|
+
|
|
+import org.bukkit.Bukkit;
|
|
+
|
|
+import java.util.logging.Level;
|
|
+import java.util.logging.Logger;
|
|
+
|
|
+public class LeavesLogger extends Logger {
|
|
+ public static final LeavesLogger LOGGER = new LeavesLogger();
|
|
+
|
|
+ private LeavesLogger() {
|
|
+ super("Leaves", null);
|
|
+ setParent(Bukkit.getLogger());
|
|
+ setLevel(Level.ALL);
|
|
+ }
|
|
+}
|
|
diff --git a/src/main/java/top/leavesmc/leaves/util/AsyncExecutor.java b/src/main/java/top/leavesmc/leaves/util/AsyncExecutor.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..0c03f3f051eff7abaacfaa8adb992811f934b9c8
|
|
--- /dev/null
|
|
+++ b/src/main/java/top/leavesmc/leaves/util/AsyncExecutor.java
|
|
@@ -0,0 +1,76 @@
|
|
+package top.leavesmc.leaves.util;
|
|
+
|
|
+import com.google.common.collect.Queues;
|
|
+import top.leavesmc.leaves.LeavesLogger;
|
|
+
|
|
+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;
|
|
+
|
|
+// Powered by Pufferfish(https://github.com/pufferfish-gg/Pufferfish)
|
|
+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) {
|
|
+ LeavesLogger.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;
|
|
+ }
|
|
+
|
|
+ return jobs.remove();
|
|
+ } finally {
|
|
+ mutex.unlock();
|
|
+ }
|
|
+ }
|
|
+}
|
|
diff --git a/src/main/java/top/leavesmc/leaves/util/AsyncPlayerAreaMap.java b/src/main/java/top/leavesmc/leaves/util/AsyncPlayerAreaMap.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..4ccc1b3b29e9d62526b2d7c56ef06db77704bf80
|
|
--- /dev/null
|
|
+++ b/src/main/java/top/leavesmc/leaves/util/AsyncPlayerAreaMap.java
|
|
@@ -0,0 +1,32 @@
|
|
+package top.leavesmc.leaves.util;
|
|
+
|
|
+import com.destroystokyo.paper.util.misc.PlayerAreaMap;
|
|
+import com.destroystokyo.paper.util.misc.PooledLinkedHashSets;
|
|
+import net.minecraft.server.level.ServerPlayer;
|
|
+
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
+
|
|
+// Powered by Pufferfish(https://github.com/pufferfish-gg/Pufferfish)
|
|
+public class AsyncPlayerAreaMap extends PlayerAreaMap {
|
|
+
|
|
+ public AsyncPlayerAreaMap() {
|
|
+ super();
|
|
+ this.areaMap = new Long2ObjectOpenHashMapWrapper<>(new ConcurrentHashMap<>(1024, 0.7f));
|
|
+ }
|
|
+
|
|
+ public AsyncPlayerAreaMap(final PooledLinkedHashSets<ServerPlayer> pooledHashSets) {
|
|
+ super(pooledHashSets);
|
|
+ this.areaMap = new Long2ObjectOpenHashMapWrapper<>(new ConcurrentHashMap<>(1024, 0.7f));
|
|
+ }
|
|
+
|
|
+ public AsyncPlayerAreaMap(final PooledLinkedHashSets<ServerPlayer> pooledHashSets, final ChangeCallback<ServerPlayer> addCallback,
|
|
+ final ChangeCallback<ServerPlayer> removeCallback) {
|
|
+ this(pooledHashSets, addCallback, removeCallback, null);
|
|
+ }
|
|
+
|
|
+ public AsyncPlayerAreaMap(final PooledLinkedHashSets<ServerPlayer> pooledHashSets, final ChangeCallback<ServerPlayer> addCallback,
|
|
+ final ChangeCallback<ServerPlayer> removeCallback, final ChangeSourceCallback<ServerPlayer> changeSourceCallback) {
|
|
+ super(pooledHashSets, addCallback, removeCallback, changeSourceCallback);
|
|
+ this.areaMap = new Long2ObjectOpenHashMapWrapper<>(new ConcurrentHashMap<>(1024, 0.7f));
|
|
+ }
|
|
+}
|
|
diff --git a/src/main/java/top/leavesmc/leaves/util/IterableWrapper.java b/src/main/java/top/leavesmc/leaves/util/IterableWrapper.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..0dc58b78c84a825ec7025534cd0fc7be9c4610ad
|
|
--- /dev/null
|
|
+++ b/src/main/java/top/leavesmc/leaves/util/IterableWrapper.java
|
|
@@ -0,0 +1,21 @@
|
|
+package top.leavesmc.leaves.util;
|
|
+
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+
|
|
+import java.util.Iterator;
|
|
+
|
|
+// Powered by Pufferfish(https://github.com/pufferfish-gg/Pufferfish)
|
|
+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/top/leavesmc/leaves/util/Long2ObjectOpenHashMapWrapper.java b/src/main/java/top/leavesmc/leaves/util/Long2ObjectOpenHashMapWrapper.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..b684d8f576acbc5de8d06b0ff779c257198bc32d
|
|
--- /dev/null
|
|
+++ b/src/main/java/top/leavesmc/leaves/util/Long2ObjectOpenHashMapWrapper.java
|
|
@@ -0,0 +1,41 @@
|
|
+package top.leavesmc.leaves.util;
|
|
+
|
|
+import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
|
+import org.jetbrains.annotations.Nullable;
|
|
+
|
|
+import java.util.Map;
|
|
+
|
|
+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();
|
|
+ }
|
|
+}
|
|
diff --git a/src/main/java/top/leavesmc/leaves/util/ProtocolUtils.java b/src/main/java/top/leavesmc/leaves/util/ProtocolUtils.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..a29da205e542edb0400b8c5db76dc97cff015b8c
|
|
--- /dev/null
|
|
+++ b/src/main/java/top/leavesmc/leaves/util/ProtocolUtils.java
|
|
@@ -0,0 +1,19 @@
|
|
+package top.leavesmc.leaves.util;
|
|
+
|
|
+import net.minecraft.network.FriendlyByteBuf;
|
|
+import net.minecraft.network.protocol.game.ClientboundCustomPayloadPacket;
|
|
+import net.minecraft.network.protocol.game.ServerboundCustomPayloadPacket;
|
|
+import net.minecraft.resources.ResourceLocation;
|
|
+import net.minecraft.server.level.ServerPlayer;
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+
|
|
+public class ProtocolUtils {
|
|
+
|
|
+ public static void sendPayloadPacket(@NotNull ServerPlayer player, ResourceLocation channel, FriendlyByteBuf data) {
|
|
+ player.connection.send(new ClientboundCustomPayloadPacket(channel, data));
|
|
+ }
|
|
+
|
|
+ public static boolean isNamespacePacket(@NotNull ServerboundCustomPayloadPacket packet, String namespace) {
|
|
+ return packet.identifier.getNamespace().equals(namespace);
|
|
+ }
|
|
+}
|