9
0
mirror of https://github.com/Samsuik/Sakura.git synced 2025-12-22 16:29:16 +00:00
Files
SakuraMC/patches/server/0002-Sakura-Utils.patch
Samsuik ca5ff29a0d Updated Upstream (Paper)
Upstream has released updates that appear to apply and compile correctly

Paper Changes:
PaperMC/Paper@691d452 Fix bundled spark permission check (#11355)
PaperMC/Paper@012c527 Update Velocity natives (#11347)
PaperMC/Paper@953e6e9 Fire BlockExpEvent on grindstone use (#11346)
PaperMC/Paper@10f5879 Change condition check order of entity tracking Y (#11348)
PaperMC/Paper@805a974 Improve console completion with brig suggestions (#9251)
PaperMC/Paper@e0021b1 Fix allowSpiderWorldBorderClimbing world config (#11321)
PaperMC/Paper@3db4758 Check dead flag in isAlive() (#11330)
PaperMC/Paper@21f125f Revert velocity natives to 3.1.2 (#11368)
PaperMC/Paper@0e82527 Fix NPE while trying to respawn an already disconnected player (#11353)
PaperMC/Paper@5d91bef Fix shulkerbox loot table replenish (#11366)
PaperMC/Paper@a8e6a93 Deprecate for removal all OldEnum-related methods (#11371)
PaperMC/Paper@925c3b9 Add FeatureFlag API (#8952)
PaperMC/Paper@426f992 Enchantment is data-driven, so not FeatureDependant (#11377)
PaperMC/Paper@1ba1be7 Update Velocity natives again
PaperMC/Paper@7632de5 Tag Lifecycle Events (#10993)
PaperMC/Paper@b09eaf2 Add Item serialization as json api (#11235)
PaperMC/Paper@971a7a5 Add Decorated Pot Cracked API (#11365)
PaperMC/Paper@61fe23c deprecate isEnabledByFeature in Item/BlockType
PaperMC/Paper@e945cfe Fix PaperServerListPingEvent#getPlayerSample not being populated or used (#11387)
PaperMC/Paper@4ff58c4 Update spark
PaperMC/Paper@d1a72ea Updated Upstream (Bukkit/CraftBukkit/Spigot) (#11405)
PaperMC/Paper@0a53f1d Set default drop behavior for player deaths (#11380)
PaperMC/Paper@951e7dd Fix TrialSpawner forgetting assigned mob when placed by player (#11381)
PaperMC/Paper@13a2395 Fix enable-player-collisions playing sounds when set to false (#11390)
PaperMC/Paper@1348e44 Prevent NPE when serializing unresolved profile (#11407)
PaperMC/Paper@2aaf436 Validate slot in PlayerInventory#setSlot (#11399)
PaperMC/Paper@5c82955 Only mark decorations dirty if a removal actually occurs (#11413)
PaperMC/Paper@c5a1066 Remove wall-time / unused skip tick protection (#11412)
2024-09-22 20:38:14 +01:00

252 lines
8.2 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Samsuik <40902469+Samsuik@users.noreply.github.com>
Date: Tue, 23 May 2023 23:07:20 +0100
Subject: [PATCH] Sakura Utils
diff --git a/src/main/java/me/samsuik/sakura/utils/collections/FixedSizeCustomObjectTable.java b/src/main/java/me/samsuik/sakura/utils/collections/FixedSizeCustomObjectTable.java
new file mode 100644
index 0000000000000000000000000000000000000000..e97f3cc00945f79026af894685d6104dfc920a35
--- /dev/null
+++ b/src/main/java/me/samsuik/sakura/utils/collections/FixedSizeCustomObjectTable.java
@@ -0,0 +1,54 @@
+package me.samsuik.sakura.utils.collections;
+
+import it.unimi.dsi.fastutil.HashCommon;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.function.ToIntFunction;
+
+public final class FixedSizeCustomObjectTable<T> {
+ private final ToIntFunction<T> keyFunction;
+ private final T[] contents;
+ private final int mask;
+
+ public FixedSizeCustomObjectTable(int size, @NotNull ToIntFunction<T> keyFunction) {
+ if (size < 0) {
+ throw new IllegalArgumentException("Table size cannot be negative");
+ } else {
+ int n = HashCommon.nextPowerOfTwo(size - 1);
+ this.keyFunction = keyFunction;
+ this.contents = (T[]) new Object[n];
+ this.mask = (n - 1);
+ }
+ }
+
+ private int key(T value) {
+ return this.keyFunction.applyAsInt(value);
+ }
+
+ public @Nullable T get(T value) {
+ return this.get(this.key(value));
+ }
+
+ public @Nullable T get(int key) {
+ return this.contents[key & this.mask];
+ }
+
+ public void write(int key, T value) {
+ this.contents[key & this.mask] = value;
+ }
+
+ public @Nullable T getAndWrite(T value) {
+ int key = this.key(value);
+ T found = this.get(key);
+ this.write(key, value);
+ return found;
+ }
+
+ public void clear() {
+ int size = this.contents.length;
+ for (int i = 0; i < size; ++i) {
+ this.contents[i] = null;
+ }
+ }
+}
diff --git a/src/main/java/me/samsuik/sakura/utils/collections/OrderedComparatorList.java b/src/main/java/me/samsuik/sakura/utils/collections/OrderedComparatorList.java
new file mode 100644
index 0000000000000000000000000000000000000000..edbbf9dea7c6659c2053407dc55b5a2a1cfcb0dd
--- /dev/null
+++ b/src/main/java/me/samsuik/sakura/utils/collections/OrderedComparatorList.java
@@ -0,0 +1,49 @@
+package me.samsuik.sakura.utils.collections;
+
+import it.unimi.dsi.fastutil.objects.ObjectArrayList;
+
+import java.util.Arrays;
+import java.util.Comparator;
+
+public final class OrderedComparatorList<T> extends ObjectArrayList<T> {
+ private final Comparator<T> comparator;
+ private boolean binarySearch = true;
+
+ public OrderedComparatorList(int capacity, Comparator<T> comparator) {
+ super(capacity);
+ this.comparator = Comparator.nullsLast(comparator);
+ }
+
+ public OrderedComparatorList(Comparator<T> comparator) {
+ this(DEFAULT_INITIAL_CAPACITY, comparator);
+ }
+
+ private void validateBounds(int index, T t, boolean up) {
+ if (index != 0 && this.comparator.compare(get(index - 1), t) > 0) {
+ this.binarySearch = false;
+ } else if (up && index < size() - 1 && this.comparator.compare(get(index + 1), t) < 0) {
+ this.binarySearch = false;
+ }
+ }
+
+ @Override
+ public boolean add(T t) {
+ this.validateBounds(size(), t, false);
+ return super.add(t);
+ }
+
+ @Override
+ public void add(int index, T t) {
+ this.validateBounds(index, t, true);
+ super.add(index, t);
+ }
+
+ @Override
+ public int indexOf(final Object k) {
+ if (this.binarySearch) {
+ return Math.max(Arrays.binarySearch(this.a, (T) k, this.comparator), -1);
+ } else {
+ return super.indexOf(k);
+ }
+ }
+}
diff --git a/src/main/java/me/samsuik/sakura/utils/collections/TrackedEntityChunkMap.java b/src/main/java/me/samsuik/sakura/utils/collections/TrackedEntityChunkMap.java
new file mode 100644
index 0000000000000000000000000000000000000000..d112b559eaff82c32e943edf34c616565cb1e189
--- /dev/null
+++ b/src/main/java/me/samsuik/sakura/utils/collections/TrackedEntityChunkMap.java
@@ -0,0 +1,32 @@
+package me.samsuik.sakura.utils.collections;
+
+import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
+import it.unimi.dsi.fastutil.objects.ObjectArrayList;
+import it.unimi.dsi.fastutil.objects.ObjectCollection;
+import net.minecraft.server.level.ChunkMap;
+
+public final class TrackedEntityChunkMap extends Int2ObjectOpenHashMap<ChunkMap.TrackedEntity> {
+ private final ObjectArrayList<ChunkMap.TrackedEntity> entityList = new UnorderedIndexedList<>();
+
+ @Override
+ public ChunkMap.TrackedEntity put(int k, ChunkMap.TrackedEntity trackedEntity) {
+ ChunkMap.TrackedEntity tracked = super.put(k, trackedEntity);
+ // bad plugins may replace entries
+ if (tracked != null)
+ this.entityList.remove(trackedEntity);
+ this.entityList.add(trackedEntity);
+ return tracked;
+ }
+
+ @Override
+ public ChunkMap.TrackedEntity remove(int k) {
+ ChunkMap.TrackedEntity tracked = super.remove(k);
+ this.entityList.remove(tracked);
+ return tracked;
+ }
+
+ @Override
+ public ObjectCollection<ChunkMap.TrackedEntity> values() {
+ return this.entityList;
+ }
+}
diff --git a/src/main/java/me/samsuik/sakura/utils/collections/UnorderedIndexedList.java b/src/main/java/me/samsuik/sakura/utils/collections/UnorderedIndexedList.java
new file mode 100644
index 0000000000000000000000000000000000000000..131dc69cdd2c121975199324022f942194d345c8
--- /dev/null
+++ b/src/main/java/me/samsuik/sakura/utils/collections/UnorderedIndexedList.java
@@ -0,0 +1,61 @@
+package me.samsuik.sakura.utils.collections;
+
+import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
+import it.unimi.dsi.fastutil.objects.ObjectArrayList;
+
+public final class UnorderedIndexedList<T> extends ObjectArrayList<T> {
+ private final Int2IntOpenHashMap elementToIndex;
+
+ public UnorderedIndexedList() {
+ this(DEFAULT_INITIAL_CAPACITY);
+ }
+
+ public UnorderedIndexedList(int capacity) {
+ super(capacity);
+ this.elementToIndex = new Int2IntOpenHashMap();
+ this.elementToIndex.defaultReturnValue(-1);
+ }
+
+ @Override
+ public boolean add(final T t) {
+ this.elementToIndex.put(t.hashCode(), size());
+ return super.add(t);
+ }
+
+ @Override
+ public T remove(final int index) {
+ final int tail = size() - 1;
+ final T at = a[index];
+
+ if (index != tail) {
+ final T tailObj = a[tail];
+ if (tailObj != null)
+ this.elementToIndex.put(tailObj.hashCode(), index);
+ this.a[index] = tailObj;
+ }
+
+ if (at != null)
+ this.elementToIndex.remove(at.hashCode());
+ this.a[tail] = null;
+ this.size = tail;
+ return at;
+ }
+
+ @Override
+ public void clear() {
+ this.elementToIndex.clear();
+ super.clear();
+ }
+
+ @Override
+ public int indexOf(final Object k) {
+ if (k == null) return -1;
+ // entities uses their id as a hashcode
+ return this.elementToIndex.get(k.hashCode());
+ }
+
+ @Override
+ public void add(final int index, final T t) {
+ throw new UnsupportedOperationException();
+ }
+}
diff --git a/src/main/java/me/samsuik/sakura/utils/objects/Expiry.java b/src/main/java/me/samsuik/sakura/utils/objects/Expiry.java
new file mode 100644
index 0000000000000000000000000000000000000000..97a54930416951645e35f7bd7ec11325d1ba4d53
--- /dev/null
+++ b/src/main/java/me/samsuik/sakura/utils/objects/Expiry.java
@@ -0,0 +1,19 @@
+package me.samsuik.sakura.utils.objects;
+
+public final class Expiry {
+ private int expireAt;
+ private final int inc;
+
+ public Expiry(int tick, int inc) {
+ this.expireAt = tick + inc;
+ this.inc = inc;
+ }
+
+ public void refresh(int tick) {
+ this.expireAt = tick + this.inc;
+ }
+
+ public boolean isExpired(int tick) {
+ return tick >= this.expireAt;
+ }
+}