-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathMixinEventFactoryEventImpl.java
More file actions
63 lines (55 loc) · 2.37 KB
/
Copy pathMixinEventFactoryEventImpl.java
File metadata and controls
63 lines (55 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.player2.playerengine.mixins;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.function.Function;
/**
* Fixes ConcurrentModificationException in Architectury EventFactory.
*
* Architectury API issue #653: EventFactory uses a plain ArrayList for event
* listeners, which is not thread-safe. When a mod registers a listener during
* event dispatch (e.g., during the first server tick), the iterator throws
* ConcurrentModificationException.
*
* This mixin replaces the ArrayList with a SnapshotArrayList subclass that
* returns snapshot-based iterators, making concurrent registration during
* iteration safe. We must extend ArrayList because the field type is ArrayList,
* not List.
*/
@Mixin(targets = "dev.architectury.event.EventFactory$EventImpl", remap = false)
public class MixinEventFactoryEventImpl<T> {
@Shadow
private ArrayList<T> listeners;
/**
* An ArrayList subclass whose iterators operate on a snapshot of the backing
* array at the time of iterator creation. This means concurrent add/remove
* during iteration won't throw ConcurrentModificationException.
*/
public static class SnapshotArrayList<E> extends ArrayList<E> {
@Override
public Iterator<E> iterator() {
// Return an iterator over a snapshot copy
return new ArrayList<>(this).iterator();
}
@Override
public ListIterator<E> listIterator() {
return new ArrayList<>(this).listIterator();
}
@Override
public ListIterator<E> listIterator(int index) {
return new ArrayList<>(this).listIterator(index);
}
}
@Inject(method = "<init>", at = @At("RETURN"))
private void playerengine$replaceListenersWithSnapshotList(Function<?, ?> function, CallbackInfo ci) {
// Replace the plain ArrayList with our SnapshotArrayList.
// The snapshot iterator pattern means that iteration (event dispatch) sees
// a frozen copy, while registration can safely modify the real list.
this.listeners = new SnapshotArrayList<>();
}
}