mirror of
https://github.com/Winds-Studio/Leaf.git
synced 2025-12-29 20:09:17 +00:00
186 lines
7.8 KiB
Diff
186 lines
7.8 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: wangxyper <wangxyper@163.com>
|
|
Date: Mon, 9 Jan 2023 13:15:09 +0800
|
|
Subject: [PATCH] Hearse: Fix some problems in GoalSelector and ShufflingList
|
|
|
|
Original license: MIT
|
|
Original project: https://github.com/NaturalCodeClub/HearseRewrite
|
|
|
|
diff --git a/src/main/java/com/destroystokyo/paper/util/set/OptimizedSmallEnumSet.java b/src/main/java/com/destroystokyo/paper/util/set/OptimizedSmallEnumSet.java
|
|
index b3329c6fcd6758a781a51f5ba8f5052ac1c77b49..adb02cba6cdb62752f847136000c6f7ca857bd5a 100644
|
|
--- a/src/main/java/com/destroystokyo/paper/util/set/OptimizedSmallEnumSet.java
|
|
+++ b/src/main/java/com/destroystokyo/paper/util/set/OptimizedSmallEnumSet.java
|
|
@@ -2,9 +2,6 @@ package com.destroystokyo.paper.util.set;
|
|
|
|
import java.util.Collection;
|
|
|
|
-/**
|
|
- * @author Spottedleaf <Spottedleaf@users.noreply.github.com>
|
|
- */
|
|
public final class OptimizedSmallEnumSet<E extends Enum<E>> {
|
|
|
|
private final Class<E> enumClass;
|
|
@@ -20,7 +17,7 @@ public final class OptimizedSmallEnumSet<E extends Enum<E>> {
|
|
this.enumClass = clazz;
|
|
}
|
|
|
|
- public boolean addUnchecked(final E element) {
|
|
+ public synchronized boolean addUnchecked(final E element) {
|
|
final int ordinal = element.ordinal();
|
|
final long key = 1L << ordinal;
|
|
|
|
@@ -30,7 +27,7 @@ public final class OptimizedSmallEnumSet<E extends Enum<E>> {
|
|
return (prev & key) == 0;
|
|
}
|
|
|
|
- public boolean removeUnchecked(final E element) {
|
|
+ public synchronized boolean removeUnchecked(final E element) {
|
|
final int ordinal = element.ordinal();
|
|
final long key = 1L << ordinal;
|
|
|
|
@@ -40,15 +37,15 @@ public final class OptimizedSmallEnumSet<E extends Enum<E>> {
|
|
return (prev & key) != 0;
|
|
}
|
|
|
|
- public void clear() {
|
|
+ public synchronized void clear() {
|
|
this.backingSet = 0L;
|
|
}
|
|
|
|
- public int size() {
|
|
+ public synchronized int size() {
|
|
return Long.bitCount(this.backingSet);
|
|
}
|
|
|
|
- public void addAllUnchecked(final Collection<E> enums) {
|
|
+ public synchronized void addAllUnchecked(final Collection<E> enums) {
|
|
for (final E element : enums) {
|
|
if (element == null) {
|
|
throw new NullPointerException("Null element");
|
|
@@ -57,15 +54,15 @@ public final class OptimizedSmallEnumSet<E extends Enum<E>> {
|
|
}
|
|
}
|
|
|
|
- public long getBackingSet() {
|
|
+ public synchronized long getBackingSet() {
|
|
return this.backingSet;
|
|
}
|
|
|
|
- public boolean hasCommonElements(final OptimizedSmallEnumSet<E> other) {
|
|
+ public synchronized boolean hasCommonElements(final OptimizedSmallEnumSet<E> other) {
|
|
return (other.backingSet & this.backingSet) != 0;
|
|
}
|
|
|
|
- public boolean hasElement(final E element) {
|
|
+ public synchronized boolean hasElement(final E element) {
|
|
return (this.backingSet & (1L << element.ordinal())) != 0;
|
|
}
|
|
}
|
|
diff --git a/src/main/java/net/minecraft/world/entity/ai/behavior/ShufflingList.java b/src/main/java/net/minecraft/world/entity/ai/behavior/ShufflingList.java
|
|
index fe3ab3d388f0481fb0db06b7f730f868dbf8e8a5..ac006bacbe8715e5c272c69afd1edab45a6511e8 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/ai/behavior/ShufflingList.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/ai/behavior/ShufflingList.java
|
|
@@ -25,7 +25,7 @@ public class ShufflingList<U> implements Iterable<U> {
|
|
public ShufflingList(boolean isUnsafe) {
|
|
this.isUnsafe = isUnsafe;
|
|
// Paper end
|
|
- this.entries = Lists.newArrayList();
|
|
+ this.entries = Lists.newCopyOnWriteArrayList();
|
|
}
|
|
|
|
private ShufflingList(List<ShufflingList.WeightedEntry<U>> list) {
|
|
@@ -35,7 +35,7 @@ public class ShufflingList<U> implements Iterable<U> {
|
|
private ShufflingList(List<ShufflingList.WeightedEntry<U>> list, boolean isUnsafe) {
|
|
this.isUnsafe = isUnsafe;
|
|
// Paper end
|
|
- this.entries = Lists.newArrayList(list);
|
|
+ this.entries = Lists.newCopyOnWriteArrayList(list);
|
|
}
|
|
|
|
public static <U> Codec<ShufflingList<U>> codec(Codec<U> codec) {
|
|
@@ -44,12 +44,12 @@ public class ShufflingList<U> implements Iterable<U> {
|
|
});
|
|
}
|
|
|
|
- public ShufflingList<U> add(U data, int weight) {
|
|
+ public synchronized ShufflingList<U> add(U data, int weight) {
|
|
this.entries.add(new ShufflingList.WeightedEntry<>(data, weight));
|
|
return this;
|
|
}
|
|
|
|
- public ShufflingList<U> shuffle() {
|
|
+ public synchronized ShufflingList<U> shuffle() {
|
|
// Paper start - make concurrent safe, work off a clone of the list
|
|
List<ShufflingList.WeightedEntry<U>> list = this.isUnsafe ? Lists.newArrayList(this.entries) : this.entries;
|
|
list.forEach(entry -> entry.setRandom(this.random.nextFloat()));
|
|
@@ -58,17 +58,17 @@ public class ShufflingList<U> implements Iterable<U> {
|
|
// Paper end
|
|
}
|
|
|
|
- public Stream<U> stream() {
|
|
+ public synchronized Stream<U> stream() {
|
|
return this.entries.stream().map(ShufflingList.WeightedEntry::getData);
|
|
}
|
|
|
|
@Override
|
|
- public Iterator<U> iterator() {
|
|
+ public synchronized Iterator<U> iterator() {
|
|
return Iterators.transform(this.entries.iterator(), ShufflingList.WeightedEntry::getData);
|
|
}
|
|
|
|
@Override
|
|
- public String toString() {
|
|
+ public synchronized String toString() {
|
|
return "ShufflingList[" + this.entries + "]";
|
|
}
|
|
|
|
@@ -90,16 +90,16 @@ public class ShufflingList<U> implements Iterable<U> {
|
|
this.randWeight = -Math.pow((double)random, (double)(1.0F / (float)this.weight));
|
|
}
|
|
|
|
- public T getData() {
|
|
+ public synchronized T getData() {
|
|
return this.data;
|
|
}
|
|
|
|
- public int getWeight() {
|
|
+ public synchronized int getWeight() {
|
|
return this.weight;
|
|
}
|
|
|
|
@Override
|
|
- public String toString() {
|
|
+ public synchronized String toString() {
|
|
return this.weight + ":" + this.data;
|
|
}
|
|
|
|
diff --git a/src/main/java/net/minecraft/world/entity/ai/goal/GoalSelector.java b/src/main/java/net/minecraft/world/entity/ai/goal/GoalSelector.java
|
|
index 02978315bc2b828cc603ce7478408f3f82c249c2..96d37e0845df9b22cf60f9835787789d0d0e4a79 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/ai/goal/GoalSelector.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/ai/goal/GoalSelector.java
|
|
@@ -3,11 +3,8 @@ package net.minecraft.world.entity.ai.goal;
|
|
import com.google.common.annotations.VisibleForTesting;
|
|
import com.google.common.collect.Sets;
|
|
import com.mojang.logging.LogUtils;
|
|
-import java.util.EnumMap;
|
|
-import java.util.EnumSet;
|
|
-import java.util.Iterator;
|
|
-import java.util.Map;
|
|
-import java.util.Set;
|
|
+
|
|
+import java.util.*;
|
|
import java.util.function.Predicate;
|
|
import java.util.function.Supplier;
|
|
import java.util.stream.Stream;
|
|
@@ -27,8 +24,8 @@ public class GoalSelector {
|
|
return false;
|
|
}
|
|
};
|
|
- private final Map<Goal.Flag, WrappedGoal> lockedFlags = new EnumMap<>(Goal.Flag.class);
|
|
- private final Set<WrappedGoal> availableGoals = Sets.newLinkedHashSet();
|
|
+ private final Map<Goal.Flag, WrappedGoal> lockedFlags = Collections.synchronizedMap(new EnumMap<>(Goal.Flag.class));
|
|
+ private final Set<WrappedGoal> availableGoals = Sets.newCopyOnWriteArraySet();
|
|
private final Supplier<ProfilerFiller> profiler;
|
|
private final EnumSet<Goal.Flag> disabledFlags = EnumSet.noneOf(Goal.Flag.class); // Paper unused, but dummy to prevent plugins from crashing as hard. Theyll need to support paper in a special case if this is super important, but really doesn't seem like it would be.
|
|
private final com.destroystokyo.paper.util.set.OptimizedSmallEnumSet<net.minecraft.world.entity.ai.goal.Goal.Flag> goalTypes = new com.destroystokyo.paper.util.set.OptimizedSmallEnumSet<>(Goal.Flag.class); // Paper - remove streams from pathfindergoalselector
|