-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
TickCallable.java
100 lines (89 loc) · 2.86 KB
/
TickCallable.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
package me.nallar.tickthreading.minecraft.tickcallables;
import java.util.concurrent.Callable;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import me.nallar.tickthreading.minecraft.TickManager;
import net.minecraft.src.World;
public abstract class TickCallable<T> implements Callable<T> {
public volatile Lock thisLock = new ReentrantLock();
public volatile Lock xPlusLock = null;
public volatile Lock xMinusLock = null;
public volatile Lock zPlusLock = null;
public volatile Lock zMinusLock = null;
final World world;
final TickManager manager;
public final int hashCode;
public final int regionX;
public final int regionZ;
public TickCallable(World world, TickManager manager, int regionX, int regionZ) {
super();
this.world = world;
this.manager = manager;
this.hashCode = TickManager.getHashCodeFromRegionCoords(regionX, regionZ);
this.regionX = regionX;
this.regionZ = regionZ;
setupLocks();
}
private void setupLocks() {
TickCallable tickCallable = getCallable(regionX + 1, regionZ);
if (tickCallable != null) {
tickCallable.xMinusLock = thisLock;
this.xPlusLock = tickCallable.thisLock;
}
tickCallable = getCallable(regionX - 1, regionZ);
if (tickCallable != null) {
tickCallable.xPlusLock = thisLock;
this.xMinusLock = tickCallable.thisLock;
}
tickCallable = getCallable(regionX, regionZ + 1);
if (tickCallable != null) {
tickCallable.zMinusLock = thisLock;
this.zPlusLock = tickCallable.thisLock;
}
tickCallable = getCallable(regionX, regionZ - 1);
if (tickCallable != null) {
tickCallable.zPlusLock = thisLock;
this.zMinusLock = tickCallable.thisLock;
}
}
public void die() {
thisLock = null;
TickCallable tickCallable = getCallable(regionX + 1, regionZ);
if (tickCallable != null) {
tickCallable.xMinusLock = null;
}
tickCallable = getCallable(regionX - 1, regionZ);
if (tickCallable != null) {
tickCallable.xPlusLock = null;
}
tickCallable = getCallable(regionX, regionZ + 1);
if (tickCallable != null) {
tickCallable.zMinusLock = null;
}
tickCallable = getCallable(regionX, regionZ - 1);
if (tickCallable != null) {
tickCallable.zPlusLock = null;
}
}
@Override
public String toString() {
return "rX: " + regionX + ", rZ: " + regionZ + ", hashCode: " + hashCode;
}
@Override
public int hashCode() {
return hashCode;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof TickCallable)) {
return false;
}
TickCallable otherTickCallable = (TickCallable) other;
return otherTickCallable.hashCode == this.hashCode && this.getClass().isInstance(other);
}
protected abstract TickCallable getCallable(int regionX, int regionZ);
public abstract boolean isEmpty();
}