Skip to content

Commit

Permalink
make forwarding set
Browse files Browse the repository at this point in the history
  • Loading branch information
Machine-Maker committed Oct 20, 2024
1 parent 4195155 commit e3769d5
Showing 1 changed file with 27 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,66 @@ Subject: [PATCH] Create Limited set for Entity

diff --git a/src/main/java/io/papermc/paper/util/SizeLimitedSet.java b/src/main/java/io/papermc/paper/util/SizeLimitedSet.java
new file mode 100644
index 0000000000000000000000000000000000000000..e5a1b373a87e789fb728a7dec9054fae3196e50f
index 0000000000000000000000000000000000000000..0ce9c117447ae07563c576944d2fc3f255d49cf1
--- /dev/null
+++ b/src/main/java/io/papermc/paper/util/SizeLimitedSet.java
@@ -0,0 +1,37 @@
@@ -0,0 +1,46 @@
+package io.papermc.paper.util;
+
+import org.jspecify.annotations.NullMarked;
+import com.google.common.collect.ForwardingSet;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+import org.jspecify.annotations.NullMarked;
+import org.jspecify.annotations.Nullable;
+
+@NullMarked
+public class SizeLimitedSet<T> extends HashSet<T> {
+ private final int maxTags;
+public class SizeLimitedSet<E> extends ForwardingSet<E> {
+
+ private final Set<E> delegate;
+ private final int maxSize;
+
+ public SizeLimitedSet(int maxTags) {
+ this.maxTags = maxTags;
+ public SizeLimitedSet(final Set<E> delegate, final int maxSize) {
+ this.delegate = delegate;
+ this.maxSize = maxSize;
+ }
+
+ @Override
+ public boolean add(T tag) {
+ // Enforce the max tag limit
+ if (this.size() >= this.maxTags) {
+ public boolean add(final E element) {
+ if (this.size() >= this.maxSize) {
+ return false;
+ }
+ return super.add(tag);
+ return super.add(element);
+ }
+
+ @Override
+ public boolean addAll(Collection<? extends T> tags) {
+ public boolean addAll(final Collection<? extends @Nullable E> collection) {
+ boolean edited = false;
+
+ for (T tag : tags) {
+ if (this.size() >= this.maxTags) {
+ for (final E element : collection) {
+ if (this.size() >= this.maxSize) {
+ break;
+ }
+
+ edited |= this.add(tag);
+ edited |= super.add(element);
+ }
+ return edited;
+ }
+
+ @Override
+ protected Set<E> delegate() {
+ return this.delegate;
+ }
+}
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
index 4b54d0ea31062972e68ee8fafe3cfaf68f65a5cd..5c6555445b0713c6e1f53b8daed10dc4fd3d0fc4 100644
index 4b54d0ea31062972e68ee8fafe3cfaf68f65a5cd..2711cd3b13686efdb3227cf681ddcf98ee82fd06 100644
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
@@ -566,7 +566,7 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
this.packetPositionCodec = new VecDeltaCodec();
this.uuid = Mth.createInsecureUUID(this.random);
this.stringUUID = this.uuid.toString();
- this.tags = Sets.newHashSet();
+ this.tags = new io.papermc.paper.util.SizeLimitedSet(MAX_ENTITY_TAG_COUNT); // Paper - Create Limited set for Entity
+ this.tags = new io.papermc.paper.util.SizeLimitedSet<>(new it.unimi.dsi.fastutil.objects.ObjectOpenHashSet<>(), MAX_ENTITY_TAG_COUNT);
this.pistonDeltas = new double[]{0.0D, 0.0D, 0.0D};
this.mainSupportingBlockPos = Optional.empty();
this.onGroundNoBlocks = false;
Expand Down

0 comments on commit e3769d5

Please sign in to comment.