mirror of
https://github.com/PaperMC/Paper.git
synced 2024-12-30 16:19:03 +01:00
153 lines
7.2 KiB
Diff
153 lines
7.2 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Aikar <aikar@aikar.co>
|
|
Date: Mon, 6 Jul 2020 18:36:41 -0400
|
|
Subject: [PATCH] Fix Concurrency issue in WeightedList
|
|
|
|
if multiple threads from worldgen sort at same time, it will crash.
|
|
So make a copy of the list for sorting purposes.
|
|
|
|
diff --git a/src/main/java/net/minecraft/world/entity/ai/behavior/GateBehavior.java b/src/main/java/net/minecraft/world/entity/ai/behavior/GateBehavior.java
|
|
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/ai/behavior/GateBehavior.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/ai/behavior/GateBehavior.java
|
|
@@ -0,0 +0,0 @@ public class GateBehavior<E extends LivingEntity> extends Behavior<E> {
|
|
private final Set<MemoryModuleType<?>> exitErasedMemories;
|
|
private final GateBehavior.OrderPolicy orderPolicy;
|
|
private final GateBehavior.RunningPolicy runningPolicy;
|
|
- private final WeightedList<Behavior<? super E>> behaviors = new WeightedList<>();
|
|
+ private final WeightedList<Behavior<? super E>> behaviors = new WeightedList<>(false); // Paper - don't use a clone
|
|
|
|
public GateBehavior(Map<MemoryModuleType<?>, MemoryStatus> requiredMemoryState, Set<MemoryModuleType<?>> memoriesToForgetWhenStopped, GateBehavior.OrderPolicy order, GateBehavior.RunningPolicy runMode, List<Pair<Behavior<? super E>, Integer>> tasks) {
|
|
super(requiredMemoryState);
|
|
@@ -0,0 +0,0 @@ public class GateBehavior<E extends LivingEntity> extends Behavior<E> {
|
|
}).forEach((behavior) -> {
|
|
behavior.g(world, entity, time);
|
|
});
|
|
- Set set = this.exitErasedMemories;
|
|
Brain behaviorcontroller = entity.getBrain();
|
|
|
|
- set.forEach(behaviorcontroller::removeMemory);
|
|
+ this.exitErasedMemories.forEach(behaviorcontroller::eraseMemory); // Paper - decomp fix
|
|
}
|
|
|
|
@Override
|
|
@@ -0,0 +0,0 @@ public class GateBehavior<E extends LivingEntity> extends Behavior<E> {
|
|
static enum OrderPolicy {
|
|
|
|
ORDERED((weightedlist) -> {
|
|
- }), SHUFFLED(WeightedList::a);
|
|
+ }), SHUFFLED(WeightedList::shuffle);
|
|
|
|
private final Consumer<WeightedList<?>> consumer;
|
|
|
|
- private OrderPolicy(Consumer consumer) {
|
|
+ private OrderPolicy(Consumer<WeightedList<?>> consumer) { // Paper - decomp fix
|
|
this.consumer = consumer;
|
|
}
|
|
|
|
diff --git a/src/main/java/net/minecraft/world/entity/ai/behavior/WeightedList.java b/src/main/java/net/minecraft/world/entity/ai/behavior/WeightedList.java
|
|
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/ai/behavior/WeightedList.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/ai/behavior/WeightedList.java
|
|
@@ -0,0 +0,0 @@ import com.mojang.serialization.Codec;
|
|
import com.mojang.serialization.DataResult;
|
|
import com.mojang.serialization.Dynamic;
|
|
import com.mojang.serialization.DynamicOps;
|
|
-import com.mojang.serialization.OptionalDynamic;
|
|
+
|
|
import java.util.Comparator;
|
|
import java.util.List;
|
|
import java.util.Random;
|
|
@@ -0,0 +0,0 @@ import java.util.stream.Stream;
|
|
|
|
public class WeightedList<U> {
|
|
|
|
- protected final List<WeightedList.WeightedEntry<U>> entries;
|
|
+ protected final List<WeightedList.WeightedEntry<U>> list; // Paper - decompile conflict
|
|
private final Random random;
|
|
+ private final boolean isUnsafe; // Paper
|
|
|
|
- public WeightedList() {
|
|
- this(Lists.newArrayList());
|
|
+ // Paper start - add useClone option
|
|
+ public WeightedList() { this(true); }
|
|
+ public WeightedList(boolean isUnsafe) {
|
|
+ this(Lists.newArrayList(), isUnsafe);
|
|
}
|
|
|
|
- private WeightedList(List<WeightedList.WeightedEntry<U>> entries) {
|
|
+ private WeightedList(List<WeightedList.WeightedEntry<U>> entries) { this(entries, true); }
|
|
+ private WeightedList(List<WeightedList.WeightedEntry<U>> list, boolean isUnsafe) {
|
|
+ this.isUnsafe = isUnsafe;
|
|
+ // Paper end
|
|
this.random = new Random();
|
|
- this.entries = Lists.newArrayList(entries);
|
|
+ this.list = Lists.newArrayList(list); // Paper - decompile conflict
|
|
}
|
|
|
|
public static <U> Codec<WeightedList<U>> codec(Codec<U> codec) {
|
|
- return WeightedList.entries.a(codec).listOf().xmap(WeightedList::new, (weightedlist) -> {
|
|
- return weightedlist.a;
|
|
+ return WeightedList.WeightedEntry.codec(codec).listOf().xmap(WeightedList::new, (weightedlist) -> { // Paper - decompile conflict
|
|
+ return weightedlist.list; // Paper - decompile conflict
|
|
});
|
|
}
|
|
|
|
public WeightedList<U> add(U item, int weight) {
|
|
- this.entries.add(new WeightedList.WeightedEntry<>(item, weight));
|
|
+ this.list.add(new WeightedList.WeightedEntry<>(item, weight)); // Paper - decompile conflict
|
|
return this;
|
|
}
|
|
|
|
@@ -0,0 +0,0 @@ public class WeightedList<U> {
|
|
}
|
|
|
|
public WeightedList<U> shuffle(Random random) {
|
|
- this.entries.forEach((weightedlist_a) -> {
|
|
- weightedlist_a.setRandom(random.nextFloat());
|
|
- });
|
|
- this.entries.sort(Comparator.comparingDouble((object) -> {
|
|
- return ((WeightedList.WeightedEntry) object).getRandWeight();
|
|
- }));
|
|
- return this;
|
|
+ // Paper start - make concurrent safe, work off a clone of the list
|
|
+ List<WeightedList.WeightedEntry<U>> list = isUnsafe ? new java.util.ArrayList<WeightedList.WeightedEntry<U>>(this.list) : this.list;
|
|
+ list.forEach((weightedlist_a) -> weightedlist_a.setRandom(random.nextFloat()));
|
|
+ list.sort(Comparator.comparingDouble(WeightedEntry::getRandWeight));
|
|
+ return isUnsafe ? new WeightedList<>(list, isUnsafe) : this;
|
|
+ // Paper end
|
|
}
|
|
|
|
public boolean isEmpty() {
|
|
- return this.entries.isEmpty();
|
|
+ return this.list.isEmpty(); // Paper - decompile conflict
|
|
}
|
|
|
|
public Stream<U> stream() {
|
|
- return this.entries.stream().map(WeightedList.entries::a);
|
|
+ return this.list.stream().map(WeightedList.WeightedEntry::getData); // Paper - decompile conflict
|
|
}
|
|
|
|
public U getOne(Random random) {
|
|
@@ -0,0 +0,0 @@ public class WeightedList<U> {
|
|
}
|
|
|
|
public String toString() {
|
|
- return "WeightedList[" + this.entries + "]";
|
|
+ return "WeightedList[" + this.list + "]"; // Paper - decompile conflict
|
|
}
|
|
|
|
public static class WeightedEntry<T> {
|
|
@@ -0,0 +0,0 @@ public class WeightedList<U> {
|
|
return new Codec<WeightedList.WeightedEntry<E>>() {
|
|
public <T> DataResult<Pair<WeightedList.WeightedEntry<E>, T>> decode(DynamicOps<T> dynamicops, T t0) {
|
|
Dynamic<T> dynamic = new Dynamic(dynamicops, t0);
|
|
- OptionalDynamic optionaldynamic = dynamic.get("data");
|
|
- Codec codec1 = codec;
|
|
-
|
|
- codec.getClass();
|
|
- return optionaldynamic.flatMap(codec1::parse).map((object) -> {
|
|
+ return dynamic.get("data").flatMap(codec::parse).map((object) -> { // Paper - decompile error
|
|
return new WeightedList.WeightedEntry<>(object, dynamic.get("weight").asInt(1));
|
|
}).map((weightedlist_a) -> {
|
|
return Pair.of(weightedlist_a, dynamicops.empty());
|