9
0
mirror of https://github.com/Samsuik/Sakura.git synced 2025-12-21 15:59:26 +00:00
Files
SakuraMC/patches/api/0006-Local-Value-Storage-API.patch
2024-12-05 22:23:07 +00:00

324 lines
11 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Samsuik <40902469+Samsuik@users.noreply.github.com>
Date: Tue, 21 Nov 2023 23:22:35 +0000
Subject: [PATCH] Local Value Storage API
diff --git a/src/main/java/me/samsuik/sakura/local/LocalRegion.java b/src/main/java/me/samsuik/sakura/local/LocalRegion.java
new file mode 100644
index 0000000000000000000000000000000000000000..e9c29d22dc734854244c2e91f8b186abe0e34443
--- /dev/null
+++ b/src/main/java/me/samsuik/sakura/local/LocalRegion.java
@@ -0,0 +1,46 @@
+package me.samsuik.sakura.local;
+
+import io.papermc.paper.math.Position;
+import org.bukkit.util.BoundingBox;
+import org.bukkit.util.Vector;
+import org.jspecify.annotations.NullMarked;
+
+@NullMarked
+public record LocalRegion(int minX, int minZ, int maxX, int maxZ) {
+ public static LocalRegion from(BoundingBox boundingBox) {
+ return of(boundingBox.getMin(), boundingBox.getMax());
+ }
+
+ public static LocalRegion of(Vector min, Vector max) {
+ return of(min.getBlockX(), min.getBlockZ(), max.getBlockX(), max.getBlockZ());
+ }
+
+ public static LocalRegion of(Position min, Position max) {
+ return of(min.blockX(), min.blockZ(), max.blockX(), max.blockZ());
+ }
+
+ public static LocalRegion of(int minX, int minZ, int maxX, int maxZ) {
+ return new LocalRegion(
+ Math.min(minX, maxX), Math.min(minZ, maxZ),
+ Math.max(minX, maxX), Math.max(minZ, maxZ)
+ );
+ }
+
+ public static LocalRegion at(int x, int z, int radius) {
+ return new LocalRegion(x-radius, z-radius, x+radius, z+radius);
+ }
+
+ public boolean intersects(LocalRegion region) {
+ return (this.minX < region.minX() && this.maxX > region.minX() || this.maxX > region.maxX() && this.minX < region.maxX())
+ && (this.minZ < region.minZ() && this.maxZ > region.minZ() || this.maxZ > region.maxZ() && this.minZ < region.maxZ());
+ }
+
+ public boolean contains(LocalRegion region) {
+ return this.minX < region.minX() && this.maxX > region.maxX()
+ && this.maxZ < region.minZ() && this.maxZ > region.maxZ();
+ }
+
+ public boolean contains(int x, int z) {
+ return this.minX <= x && this.maxX >= x && this.minZ <= z && this.maxZ >= z;
+ }
+}
diff --git a/src/main/java/me/samsuik/sakura/local/LocalValueKey.java b/src/main/java/me/samsuik/sakura/local/LocalValueKey.java
new file mode 100644
index 0000000000000000000000000000000000000000..4a1540d4f6125bd2a8a4f6b7e645a3eb30e35837
--- /dev/null
+++ b/src/main/java/me/samsuik/sakura/local/LocalValueKey.java
@@ -0,0 +1,23 @@
+package me.samsuik.sakura.local;
+
+import org.bukkit.NamespacedKey;
+import org.jspecify.annotations.NullMarked;
+
+import java.util.function.Supplier;
+
+@NullMarked
+public record LocalValueKey<T>(NamespacedKey key, Supplier<T> defaultSupplier) {
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || this.getClass() != o.getClass()) return false;
+
+ LocalValueKey<?> that = (LocalValueKey<?>) o;
+ return this.key.equals(that.key);
+ }
+
+ @Override
+ public int hashCode() {
+ return this.key.hashCode();
+ }
+}
diff --git a/src/main/java/me/samsuik/sakura/local/LocalValueKeys.java b/src/main/java/me/samsuik/sakura/local/LocalValueKeys.java
new file mode 100644
index 0000000000000000000000000000000000000000..54c3693c97d5049882d7c10e14fc79ef6c76b1fc
--- /dev/null
+++ b/src/main/java/me/samsuik/sakura/local/LocalValueKeys.java
@@ -0,0 +1,25 @@
+package me.samsuik.sakura.local;
+
+import me.samsuik.sakura.physics.PhysicsVersion;
+import me.samsuik.sakura.redstone.RedstoneImplementation;
+import org.bukkit.Material;
+import org.bukkit.NamespacedKey;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.function.Supplier;
+
+public final class LocalValueKeys {
+ private static final String NAMESPACE = "sakura";
+
+ public static final LocalValueKey<PhysicsVersion> PHYSICS_VERSION = create("physics-version", () -> PhysicsVersion.LATEST);
+ public static final LocalValueKey<Map<Material, Map.Entry<Integer, Float>>> DURABLE_MATERIALS = create("durable-materials", HashMap::new);
+ public static final LocalValueKey<RedstoneImplementation> REDSTONE_IMPLEMENTATION = create("redstone-implementation", () -> RedstoneImplementation.VANILLA);
+ public static final LocalValueKey<Boolean> CONSISTENT_EXPLOSION_RADIUS = create("consistent-radius", () -> false);
+ public static final LocalValueKey<Boolean> REDSTONE_CACHE = create("redstone-cache", () -> false);
+ public static final LocalValueKey<Integer> LAVA_FLOW_SPEED = create("lava-flow-speed", () -> -1);
+
+ private static <T> LocalValueKey<T> create(String key, Supplier<T> supplier) {
+ return new LocalValueKey<>(new NamespacedKey(NAMESPACE, key), supplier);
+ }
+}
diff --git a/src/main/java/me/samsuik/sakura/local/storage/LocalStorageHandler.java b/src/main/java/me/samsuik/sakura/local/storage/LocalStorageHandler.java
new file mode 100644
index 0000000000000000000000000000000000000000..220a12eddf3aeaeb7adfd6404b5225e392439ca8
--- /dev/null
+++ b/src/main/java/me/samsuik/sakura/local/storage/LocalStorageHandler.java
@@ -0,0 +1,22 @@
+package me.samsuik.sakura.local.storage;
+
+import me.samsuik.sakura.local.LocalRegion;
+import org.bukkit.Location;
+
+import java.util.List;
+
+public interface LocalStorageHandler {
+ LocalRegion locate(Location location, int searchDistance);
+
+ LocalRegion locate(int x, int z, int searchDistance);
+
+ LocalValueStorage get(LocalRegion region);
+
+ boolean has(LocalRegion region);
+
+ void put(LocalRegion region, LocalValueStorage storage);
+
+ void remove(LocalRegion region);
+
+ List<LocalRegion> regions();
+}
diff --git a/src/main/java/me/samsuik/sakura/local/storage/LocalValueStorage.java b/src/main/java/me/samsuik/sakura/local/storage/LocalValueStorage.java
new file mode 100644
index 0000000000000000000000000000000000000000..cbaaa24b504f3a2478c9db4e3b8ef0095b6e2c80
--- /dev/null
+++ b/src/main/java/me/samsuik/sakura/local/storage/LocalValueStorage.java
@@ -0,0 +1,50 @@
+package me.samsuik.sakura.local.storage;
+
+import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
+import me.samsuik.sakura.local.LocalValueKey;
+import org.jspecify.annotations.NullMarked;
+import org.jspecify.annotations.Nullable;
+
+import java.util.Map;
+import java.util.Optional;
+
+@NullMarked
+@SuppressWarnings("unchecked")
+public final class LocalValueStorage {
+ private final Map<LocalValueKey<?>, Object> map = new Object2ObjectOpenHashMap<>();
+
+ public <T> void set(LocalValueKey<T> key, T insert) {
+ this.map.put(key, insert);
+ }
+
+ public void remove(LocalValueKey<?> key) {
+ this.map.remove(key);
+ }
+
+ public <T> Optional<T> get(LocalValueKey<T> key) {
+ T value = (T) this.map.get(key);
+ return Optional.ofNullable(value);
+ }
+
+ public <T> T getOrDefault(LocalValueKey<T> key, T def) {
+ return (T) this.map.getOrDefault(key, def);
+ }
+
+ public boolean exists(LocalValueKey<?> key) {
+ return this.map.containsKey(key);
+ }
+
+ @Nullable
+ public <T> T value(LocalValueKey<T> key) {
+ return (T) this.map.get(key);
+ }
+
+ public <T> T value(LocalValueKey<T> key, boolean returnDefault) {
+ T val = (T) this.map.get(key);
+ if (!returnDefault || val != null)
+ return val;
+ // update value
+ this.set(key, val = key.defaultSupplier().get());
+ return val;
+ }
+}
diff --git a/src/main/java/me/samsuik/sakura/physics/PhysicsVersion.java b/src/main/java/me/samsuik/sakura/physics/PhysicsVersion.java
new file mode 100644
index 0000000000000000000000000000000000000000..685fefb4ac36153f038b28452e3b29ded52cff07
--- /dev/null
+++ b/src/main/java/me/samsuik/sakura/physics/PhysicsVersion.java
@@ -0,0 +1,74 @@
+package me.samsuik.sakura.physics;
+
+import org.jspecify.annotations.NullMarked;
+
+@NullMarked
+public enum PhysicsVersion {
+ LEGACY("legacy", 1_0_0), // replicates patched 1.8.8 paper mechanics
+ v1_8_2("1.8.2", 1_8_2), // vanilla mechanics
+ v1_9("1.9", 1_9_0),
+ v1_10("1.10", 1_10_0),
+ v1_11("1.11", 1_11_0),
+ v1_12("1.12", 1_12_0),
+ v1_13("1.13", 1_13_0),
+ v1_14("1.14", 1_14_0),
+ v1_16("1.16", 1_16_0),
+ v1_17("1.17", 1_17_0),
+ v1_18_2("1.18.2", 1_18_2),
+ v1_19_3("1.19.3", 1_19_3),
+ v1_20("1.20", 1_20_0),
+ v1_21_2("1.21.2", 1_21_2),
+ LATEST("latest", 9_99_9); // latest version
+
+ private final String friendlyName;
+ private final int version;
+
+ PhysicsVersion(String friendlyName, int version) {
+ this.friendlyName = friendlyName;
+ this.version = version;
+ }
+
+ public boolean isLegacy() {
+ return this == LEGACY;
+ }
+
+ public boolean afterOrEqual(int version) {
+ return this.version >= version;
+ }
+
+ public boolean before(int version) {
+ return this.version < version;
+ }
+
+ public boolean is(int version) {
+ return this.version == version;
+ }
+
+ public boolean isWithin(int min, int max) {
+ return this.version >= min && this.version <= max;
+ }
+
+ public int getVersion() {
+ return this.version;
+ }
+
+ public String getFriendlyName() {
+ return this.friendlyName;
+ }
+
+ public static PhysicsVersion from(String string) {
+ int parsedVersion = Integer.MIN_VALUE;
+ try {
+ String versionString = string.replace(".", "");
+ parsedVersion = Integer.parseInt(versionString);
+ } catch (NumberFormatException nfe) {
+ // ignored
+ }
+ for (PhysicsVersion ver : values()) {
+ if (ver.name().equalsIgnoreCase(string) || ver.getFriendlyName().equalsIgnoreCase(string) || ver.is(parsedVersion)) {
+ return ver;
+ }
+ }
+ return LATEST;
+ }
+}
diff --git a/src/main/java/me/samsuik/sakura/redstone/RedstoneImplementation.java b/src/main/java/me/samsuik/sakura/redstone/RedstoneImplementation.java
new file mode 100644
index 0000000000000000000000000000000000000000..bfb5e9ec99dfdd6f4664ddb8191496706bc0d2a1
--- /dev/null
+++ b/src/main/java/me/samsuik/sakura/redstone/RedstoneImplementation.java
@@ -0,0 +1,20 @@
+package me.samsuik.sakura.redstone;
+
+import org.jspecify.annotations.NullMarked;
+
+@NullMarked
+public enum RedstoneImplementation {
+ VANILLA("vanilla"),
+ EIGENCRAFT("eigencraft"),
+ ALTERNATE_CURRENT("alternate-current");
+
+ private final String friendlyName;
+
+ RedstoneImplementation(String friendlyName) {
+ this.friendlyName = friendlyName;
+ }
+
+ public String getFriendlyName() {
+ return this.friendlyName;
+ }
+}
diff --git a/src/main/java/org/bukkit/World.java b/src/main/java/org/bukkit/World.java
index bef54a6c8290e09cbaac20b03dde8dfb902c96b0..93236dc21f2373983bca1fcfb470690395e7406c 100644
--- a/src/main/java/org/bukkit/World.java
+++ b/src/main/java/org/bukkit/World.java
@@ -205,6 +205,10 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient
return new Location(this, x, y, z);
}
// Paper end
+ // Sakura start
+ @NotNull
+ me.samsuik.sakura.local.storage.LocalStorageHandler getStorageHandler();
+ // Sakura end
/**
* Gets the highest non-empty (impassable) block at the given coordinates.