Skip to content

Commit

Permalink
Create Limited set for Entity
Browse files Browse the repository at this point in the history
  • Loading branch information
Intybyte committed Sep 30, 2024
1 parent ba3c29b commit 83d23c8
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions patches/server/1065-Create-Limited-set-for-Entity.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Intybyte <[email protected]>
Date: Mon, 30 Sep 2024 17:16:36 +0200
Subject: [PATCH] Create Limited set for Entity


diff --git a/src/main/java/io/papermc/paper/entity/PaperLimitedSet.java b/src/main/java/io/papermc/paper/entity/PaperLimitedSet.java
new file mode 100644
index 0000000000000000000000000000000000000000..456c8cdb38d29a73a96dc44f7e8e4ce799cbab38
--- /dev/null
+++ b/src/main/java/io/papermc/paper/entity/PaperLimitedSet.java
@@ -0,0 +1,40 @@
+package io.papermc.paper.entity;
+
+import org.jetbrains.annotations.NotNull;
+import java.util.Collection;
+import java.util.HashSet;
+
+public class PaperLimitedSet extends HashSet<String> {
+ private final int maxTags;
+
+ public PaperLimitedSet(int maxTags) {
+ this.maxTags = maxTags;
+ }
+
+ @Override
+ public boolean add(String tag) {
+ // Enforce the max tag limit
+ if (this.size() >= maxTags) {
+ return false;
+ }
+ return this.add(tag);
+ }
+
+ @Override
+ public boolean addAll(@NotNull Collection<? extends String> tags) {
+ int currentSize = this.size();
+ boolean edited = false;
+
+ for (String tag : tags) {
+ if (currentSize >= maxTags) {
+ break;
+ }
+
+ boolean added = this.add(tag);
+ if (added) currentSize++;
+
+ edited = added || edited;
+ }
+ return edited;
+ }
+}
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
index 4b54d0ea31062972e68ee8fafe3cfaf68f65a5cd..f696033cb1d937274ccbb9c04e1c755c13d6f7e1 100644
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
@@ -3,9 +3,9 @@ package net.minecraft.world.entity;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;
import com.google.common.collect.Lists;
-import com.google.common.collect.Sets;
import com.google.common.collect.UnmodifiableIterator;
import com.mojang.logging.LogUtils;
+import io.papermc.paper.entity.PaperLimitedSet;
import it.unimi.dsi.fastutil.doubles.DoubleList;
import it.unimi.dsi.fastutil.doubles.DoubleListIterator;
import it.unimi.dsi.fastutil.floats.FloatArraySet;
@@ -134,7 +134,6 @@ import net.minecraft.world.scores.Team;
import org.slf4j.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Location;
-import org.bukkit.Server;
import org.bukkit.block.BlockFace;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Hanging;
@@ -566,7 +565,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 PaperLimitedSet(1024);
this.pistonDeltas = new double[]{0.0D, 0.0D, 0.0D};
this.mainSupportingBlockPos = Optional.empty();
this.onGroundNoBlocks = false;
@@ -654,7 +653,7 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
}

public boolean addTag(String tag) {
- return this.tags.size() >= 1024 ? false : this.tags.add(tag);
+ return this.tags.add(tag);
}

public boolean removeTag(String tag) {

0 comments on commit 83d23c8

Please sign in to comment.