7
7
import li .cil .oc .api .machine .Arguments ;
8
8
import li .cil .oc .api .machine .Callback ;
9
9
import li .cil .oc .api .machine .Context ;
10
- import mods .railcraft .api .carts .CartTools ;
11
10
import mods .railcraft .common .carts .EntityLocomotiveElectric ;
12
11
import mods .railcraft .common .items .ItemTicket ;
13
12
import mods .railcraft .common .items .ItemTicketGold ;
14
- import mods .railcraft .common .util .misc .ChunkManager ;
15
13
import mods .railcraft .common .util .misc .MiscTools ;
16
- import net .minecraft .entity .item .EntityMinecart ;
17
14
import net .minecraft .item .ItemStack ;
18
15
import net .minecraft .nbt .NBTTagCompound ;
19
- import net . minecraft . world . ChunkCache ;
16
+ import pl . asie . computronics . integration . railcraft . LocomotiveManager ;
20
17
import pl .asie .computronics .reference .Config ;
21
18
import pl .asie .computronics .reference .Mods ;
22
19
29
26
public class TileLocomotiveRelay extends TileEntityPeripheralBase {
30
27
31
28
private WeakReference <EntityLocomotiveElectric > locomotive ;
32
- private boolean isInitialized = false , isBound = false ;
33
- // private int prevLocoticksExisted ;
29
+ // private boolean isInitialized = false;
30
+ private boolean isBound = false ;
34
31
35
32
private UUID uuid ;
36
33
@@ -39,20 +36,13 @@ public TileLocomotiveRelay() {
39
36
}
40
37
41
38
public void setLocomotive (EntityLocomotiveElectric loco ) {
42
- // this.locomotive = new WeakReference<EntityLocomotiveElectric>(loco);
39
+ this .locomotive = new WeakReference <EntityLocomotiveElectric >(loco );
43
40
this .isBound = true ;
44
- this .uuid = loco .getUniqueID ();
41
+ this .uuid = loco .getPersistentID ();
45
42
}
46
43
47
44
public EntityLocomotiveElectric getLocomotive () {
48
- //return this.locomotive != null ? this.locomotive.get() : null;
49
- if (uuid != null ) {
50
- EntityMinecart cart = CartTools .getLinkageManager (worldObj ).getCartFromUUID (uuid );
51
- if (cart != null && cart instanceof EntityLocomotiveElectric ) {
52
- return (EntityLocomotiveElectric ) cart ;
53
- }
54
- }
55
- return null ;
45
+ return this .locomotive != null ? this .locomotive .get () : null ;
56
46
}
57
47
58
48
public boolean isBound () {
@@ -72,61 +62,34 @@ public boolean unbind() {
72
62
@ Override
73
63
public void updateEntity () {
74
64
super .updateEntity ();
65
+
75
66
if (worldObj .isRemote ) {
76
67
return ;
77
68
}
78
- if (!isInitialized && isBound && uuid != null ) {
79
- this .tryFindLocomotive (this .uuid );
80
- isInitialized = true ;
81
- }
82
69
83
- EntityLocomotiveElectric locomotive = getLocomotive ();
84
-
85
- boolean b = true , c = true ;
86
-
87
- if (locomotive != null && locomotive .dimension == worldObj .provider .dimensionId ) {
88
- b = locomotive .worldObj .getChunkProvider ().chunkExists (locomotive .chunkCoordX , locomotive .chunkCoordZ );
89
- if (!b ) {
90
- return ;
91
- }
92
- c = locomotive .worldObj .getChunkFromChunkCoords (locomotive .chunkCoordX , locomotive .chunkCoordZ ).isChunkLoaded ;
93
- }
94
- if (locomotive != null && (locomotive .isDead || !isBound
95
- || !locomotive .worldObj .getChunkProvider ().chunkExists (locomotive .chunkCoordX , locomotive .chunkCoordZ ))) {
96
- this .locomotive = null ;
97
- //locomotive = null;
70
+ if (!isBound ) {
98
71
return ;
99
72
}
100
73
101
- if (isBound && uuid == null ) {
74
+ if (uuid == null ) {
102
75
isBound = false ;
103
76
}
104
77
105
- if (locomotive != null && locomotive .dimension != worldObj .provider .dimensionId ) {
106
- return ;
107
- }
108
-
109
- /*if(locomotive != null) {
110
- prevLocoticksExisted = locomotive.ticksExisted;
111
- }*/
112
-
113
- if (locomotive != null || !isBound ) {
114
- return ;
115
- }
116
- // Only check every second
117
- if (worldObj .getTotalWorldTime () % 20 == 0 ) {
78
+ if (uuid != null ) {
118
79
this .tryFindLocomotive (this .uuid );
119
80
}
120
81
}
121
82
122
83
private void tryFindLocomotive (UUID uuid ) {
123
- if (getLocomotive () != null ) {
124
- return ;
125
- }
126
84
if (uuid != null ) {
127
- EntityMinecart cart = CartTools .getLinkageManager (worldObj ).getCartFromUUID (uuid );
128
- if (cart != null && cart instanceof EntityLocomotiveElectric ) {
129
- this .setLocomotive ((EntityLocomotiveElectric ) cart );
85
+ EntityLocomotiveElectric cart = LocomotiveManager .instance ().getCartFromUUID (uuid );
86
+ if (cart != null ) {
87
+ EntityLocomotiveElectric oldLoco = getLocomotive ();
88
+ if (oldLoco == null || cart != oldLoco ) {
89
+ this .setLocomotive (cart );
90
+ }
91
+ } else {
92
+ this .locomotive = null ;
130
93
}
131
94
}
132
95
}
@@ -143,9 +106,8 @@ public void readFromNBT(NBTTagCompound nbt) {
143
106
@ Override
144
107
public void writeToNBT (NBTTagCompound nbt ) {
145
108
super .writeToNBT (nbt );
146
- EntityLocomotiveElectric locomotive = getLocomotive ();
147
- if (isBound && locomotive != null ) {
148
- MiscTools .writeUUID (nbt , "locomotive" , locomotive .getPersistentID ());
109
+ if (isBound && this .uuid != null ) {
110
+ MiscTools .writeUUID (nbt , "locomotive" , this .uuid );
149
111
}
150
112
nbt .setBoolean ("bound" , isBound );
151
113
}
@@ -175,7 +137,7 @@ private String cannotAccessLocomotive() {
175
137
if (locomotive .dimension != this .worldObj .provider .dimensionId ) {
176
138
return "relay and locomotive are in different dimensions" ;
177
139
}
178
- if (!( locomotive .getDistance (xCoord , yCoord , zCoord ) <= Config .LOCOMOTIVE_RELAY_RANGE ) ) {
140
+ if (locomotive .getDistanceSq (xCoord , yCoord , zCoord ) > Config .LOCOMOTIVE_RELAY_RANGE * Config . LOCOMOTIVE_RELAY_RANGE ) {
179
141
return "locomotive is too far away" ;
180
142
}
181
143
if (locomotive .isSecure ()) {
0 commit comments