2020-07-13 15:22:54 +02:00
--- a/net/minecraft/advancements/critereon/SimpleCriterionTrigger.java
+++ b/net/minecraft/advancements/critereon/SimpleCriterionTrigger.java
2024-12-13 17:38:42 +01:00
@@ -15,41 +_,41 @@
2020-07-13 15:22:54 +02:00
import net.minecraft.world.level.storage.loot.LootContext;
public abstract class SimpleCriterionTrigger<T extends SimpleCriterionTrigger.SimpleInstance> implements CriterionTrigger<T> {
- private final Map<PlayerAdvancements, Set<CriterionTrigger.Listener<T>>> players = Maps.newIdentityHashMap();
+ // private final Map<PlayerAdvancements, Set<CriterionTrigger.Listener<T>>> players = Maps.newIdentityHashMap(); // Paper - fix AdvancementDataPlayer leak; moved into AdvancementDataPlayer to fix memory leak
@Override
2024-12-13 17:38:42 +01:00
public final void addPlayerListener(PlayerAdvancements playerAdvancements, CriterionTrigger.Listener<T> listener) {
- this.players.computeIfAbsent(playerAdvancements, advancements -> Sets.newHashSet()).add(listener);
+ playerAdvancements.criterionData.computeIfAbsent(this, managerx -> Sets.newHashSet()).add(listener); // Paper - fix AdvancementDataPlayer leak
2020-07-13 15:22:54 +02:00
}
@Override
2024-12-13 17:38:42 +01:00
public final void removePlayerListener(PlayerAdvancements playerAdvancements, CriterionTrigger.Listener<T> listener) {
- Set<CriterionTrigger.Listener<T>> set = this.players.get(playerAdvancements);
+ Set<CriterionTrigger.Listener<T>> set = (Set) playerAdvancements.criterionData.get(this); // Paper - fix AdvancementDataPlayer leak
2020-07-13 15:22:54 +02:00
if (set != null) {
2024-12-13 17:38:42 +01:00
set.remove(listener);
2020-07-13 15:22:54 +02:00
if (set.isEmpty()) {
2024-12-13 17:38:42 +01:00
- this.players.remove(playerAdvancements);
+ playerAdvancements.criterionData.remove(this); // Paper - fix AdvancementDataPlayer leak
2020-07-13 15:22:54 +02:00
}
}
}
@Override
2024-12-13 17:38:42 +01:00
public final void removePlayerListeners(PlayerAdvancements playerAdvancements) {
- this.players.remove(playerAdvancements);
+ playerAdvancements.criterionData.remove(this); // Paper - fix AdvancementDataPlayer leak
2020-07-13 15:22:54 +02:00
}
2024-12-13 17:38:42 +01:00
protected void trigger(ServerPlayer player, Predicate<T> testTrigger) {
PlayerAdvancements advancements = player.getAdvancements();
- Set<CriterionTrigger.Listener<T>> set = this.players.get(advancements);
+ Set<CriterionTrigger.Listener<T>> set = (Set) advancements.criterionData.get(this); // Paper - fix AdvancementDataPlayer leak
2020-07-13 15:22:54 +02:00
if (set != null && !set.isEmpty()) {
2023-11-21 16:16:39 +01:00
- LootContext lootContext = EntityPredicate.createContext(player, player);
+ LootContext lootContext = null; // EntityPredicate.createContext(player, player); // Paper - Perf: lazily create LootContext for criterions
2020-07-13 15:22:54 +02:00
List<CriterionTrigger.Listener<T>> list = null;
2023-11-21 16:16:39 +01:00
for (CriterionTrigger.Listener<T> listener : set) {
T simpleInstance = listener.trigger();
2024-12-13 17:38:42 +01:00
if (testTrigger.test(simpleInstance)) {
2023-11-21 16:16:39 +01:00
Optional<ContextAwarePredicate> optional = simpleInstance.player();
- if (optional.isEmpty() || optional.get().matches(lootContext)) {
+ if (optional.isEmpty() || optional.get().matches(lootContext = (lootContext == null ? EntityPredicate.createContext(player, player) : lootContext))) { // Paper - Perf: lazily create LootContext for criterions
if (list == null) {
list = Lists.newArrayList();
}