-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
EntityList.java
192 lines (160 loc) · 4.56 KB
/
EntityList.java
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package nallar.tickthreading.minecraft.entitylist;
import nallar.collections.ConcurrentUnsafeIterableArrayList;
import nallar.tickthreading.Log;
import nallar.tickthreading.minecraft.TickManager;
import net.minecraft.world.World;
import java.lang.reflect.Field;
import java.util.*;
/*
* Used to override World.loadedTile/EntityList.
* */
public abstract class EntityList<T> extends ArrayList<T> {
public final TickManager manager;
public final ConcurrentUnsafeIterableArrayList<T> innerList;
EntityList(World world, Field overriddenField, TickManager manager, ConcurrentUnsafeIterableArrayList<T> innerList) {
this.manager = manager;
this.innerList = innerList;
overriddenField.setAccessible(true);
try {
ArrayList worldList = (ArrayList) overriddenField.get(world);
if (worldList.getClass() != ArrayList.class) {
Log.severe("Another mod has replaced an entity list with " + Log.toString(worldList));
}
} catch (Throwable t) {
Log.severe("Failed to get " + overriddenField.getName() + " in world " + world.getName());
}
try {
//This should hopefully avoid leaving the world in a bad state if something goes wrong.
this.addAll((Collection<? extends T>) overriddenField.get(world));
overriddenField.set(world, this);
} catch (Exception e) {
throw new RuntimeException("Failed to override " + overriddenField.getName() + " in world " + world.getName(), e);
}
}
@Override
public abstract boolean add(T t);
@Override
public abstract boolean remove(Object o);
@Override
public boolean addAll(Collection<? extends T> c) {
boolean changed = false;
for (T t : c) {
changed |= add(t);
}
return changed;
}
@Override
public boolean removeAll(Collection c) {
boolean changed = false;
for (Object t : c) {
changed |= remove(t);
}
return changed;
}
@Override
public Iterator<T> iterator() {
if (!Thread.holdsLock(this)) {
return innerList.unsafeIterator();
}
return innerList.iterator();
}
@Override
public ListIterator<T> listIterator() {
if (!Thread.holdsLock(this)) {
throw new IllegalStateException("Must synchronize to iterate over EntityList.");
}
return innerList.listIterator();
}
@Override
public ListIterator<T> listIterator(int index) {
if (!Thread.holdsLock(this)) {
throw new IllegalStateException("Must synchronize to iterate over EntityList.");
}
return innerList.listIterator(index);
}
@Override
public boolean contains(Object o) {
return innerList.contains(o);
}
@Override
public void trimToSize() {
innerList.trimToSize();
}
@Override
public void ensureCapacity(final int minCapacity) {
innerList.ensureCapacity(minCapacity);
}
@Override
public boolean isEmpty() {
return innerList.isEmpty();
}
@Override
public int indexOf(final Object o) {
return innerList.indexOf(o);
}
@Override
public int lastIndexOf(final Object o) {
return innerList.lastIndexOf(o);
}
@SuppressWarnings("CloneDoesntCallSuperClone")
@Override
public Object clone() {
return innerList.clone();
}
@Override
public Object[] toArray() {
return innerList.toArray();
}
@Override
public <T1> T1[] toArray(final T1[] a) {
return innerList.toArray(a);
}
@Override
public T set(final int index, final T element) {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public void add(final int index, final T element) {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public T remove(final int index) {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public void clear() {
innerList.clear();
}
@Override
public boolean addAll(final int index, final Collection<? extends T> c) {
throw new UnsupportedOperationException("Not implemented");
}
@Override
protected void removeRange(final int fromIndex, final int toIndex) {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public List<T> subList(final int fromIndex, final int toIndex) {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public int size() {
return innerList.size();
}
@Override
public T get(int index) {
return innerList.get(index);
}
@Override
public boolean containsAll(Collection<?> c) {
return innerList.containsAll(c);
}
@Override
public boolean retainAll(Collection<?> c) {
return innerList.retainAll(c);
}
@Override
public String toString() {
return innerList.toString();
}
}