22
33import cpw .mods .fml .relauncher .Side ;
44import cpw .mods .fml .relauncher .SideOnly ;
5+ import ebf .tim .utility .CommonUtil ;
6+ import ebf .tim .utility .DebugUtil ;
57import net .minecraft .block .Block ;
68import net .minecraft .entity .item .EntityMinecart ;
79import net .minecraft .entity .player .EntityPlayer ;
1618import org .apache .logging .log4j .Level ;
1719import train .common .Traincraft ;
1820import train .common .api .TrackRecord ;
21+ import train .common .blocks .BlockTCRail ;
22+ import train .common .blocks .BlockTCRailGag ;
1923import train .common .core .handlers .ConfigHandler ;
2024import train .common .items .TCRailTypes ;
2125import train .common .library .BlockIDs ;
@@ -51,7 +55,6 @@ public class TileTCRail extends TileEntity {
5155 public EntityPlayer lastPlayerToInteract = null ;
5256 private int updateTicks ;
5357 public Item idDrop ;
54- private int isLeftFlag = -5 ;
5558
5659 public TileTCRail () {
5760 if (this .worldObj != null )
@@ -79,6 +82,11 @@ public void setFacing(int facing) {
7982 public void setType (String type ) {
8083 worldObj .markBlockForUpdate (xCoord , yCoord , zCoord );
8184 this .type = type ;
85+ for (EnumTracks rail : EnumTracks .values ()) {
86+ if (rail .getLabel ().equals (type )) {
87+ track = rail ;
88+ }
89+ }
8290 }
8391
8492 public String getType () {
@@ -125,9 +133,11 @@ public TCRailTypes.RailTypes getRailType(){
125133
126134 @ Deprecated
127135 public EnumTracks getTrack (){
128- for (EnumTracks rail : EnumTracks .values ()){
129- if (rail .getLabel ().equals (getType ())){
130- track = rail ;
136+ if (track ==null ) {
137+ for (EnumTracks rail : EnumTracks .values ()) {
138+ if (rail .getLabel ().equals (getType ())) {
139+ track = rail ;
140+ }
131141 }
132142 }
133143 return track ;
@@ -163,6 +173,7 @@ public void printInfo() {
163173 System .out .println (TCRailTypes .isStraightTrack (this ));
164174 }
165175
176+ private byte checkBlockXZ =0 ;
166177 @ Override
167178 public void updateEntity () {
168179 if (worldObj .isRemote || !TCRailTypes .isSwitchTrack (this )) {
@@ -171,57 +182,84 @@ public void updateEntity() {
171182 }
172183
173184 if (updateTicks % 11 == 0 || updateTicks ==1 ) {
174- TileEntity tile1 = null ;
175-
176- switch (worldObj .getBlockMetadata (xCoord , yCoord , zCoord )) {
185+ boolean flag = worldObj .isBlockIndirectlyGettingPowered (xCoord , yCoord , zCoord );
177186
178- case 0 : {
179- tile1 = worldObj .getTileEntity (xCoord , yCoord , zCoord - 1 );
180- break ;
181- }
182- case 1 : {
183- tile1 = worldObj .getTileEntity (xCoord + 1 , yCoord , zCoord );
184- break ;
185- }
186- case 2 : {
187- tile1 = worldObj .getTileEntity (xCoord , yCoord , zCoord + 1 );
188- break ;
189- }
190- case 3 : {
191- tile1 = worldObj .getTileEntity (xCoord - 1 , yCoord , zCoord );
192- break ;
187+ if (checkBlockXZ ==0 ){
188+ if (CommonUtil .getBlockAt (worldObj ,xCoord ,yCoord ,zCoord +1 ) instanceof BlockTCRail ){
189+ checkBlockXZ =1 ;
190+ } else {
191+ checkBlockXZ =2 ;
193192 }
194193 }
195- if (tile1 instanceof TileTCRail && TCRailTypes .isSwitchTrack ((TileTCRail ) tile1 )) {
196-
197- TileTCRail tileSwitch = (TileTCRail ) tile1 ;
198- if (tileSwitch .switchActive != worldObj .isBlockIndirectlyGettingPowered (tileSwitch .xCoord , tileSwitch .yCoord , tileSwitch .zCoord )) {
199- tileSwitch .changeSwitchState (worldObj , tileSwitch , tile1 .xCoord , tile1 .yCoord , tile1 .zCoord );
194+ if (!flag ){
195+ if (checkBlockXZ ==1 ){
196+ flag = worldObj .isBlockIndirectlyGettingPowered (xCoord , yCoord , zCoord +1 );
197+ if (!flag ){
198+ flag = worldObj .isBlockIndirectlyGettingPowered (xCoord , yCoord , zCoord -1 );
199+ }
200+ } else if (checkBlockXZ ==2 ){
201+ flag = worldObj .isBlockIndirectlyGettingPowered (xCoord +1 , yCoord , zCoord );
202+ if (!flag ){
203+ flag = worldObj .isBlockIndirectlyGettingPowered (xCoord -1 , yCoord , zCoord );
204+ }
200205 }
201206 }
207+ if (getSwitchState () != flag ) {
208+ setSwitchState (flag );
209+ }
202210 }
203211
204212 updateTicks ++;
205-
206- if (!getSwitchState () && updateTicks % 10 ==0 ) {
207-
208- /* Right-handed switch types create a value of 1, left-handed switch types a value of type -1. If neither cases match, value is set to 0. */
209- if (isLeftFlag == -5 ) {
210- if (type .contains ("SWITCH" ) && type .contains ("RIGHT" )) {
211- isLeftFlag = 1 ;
212- } else if (type .contains ("SWITCH" ) && type .contains ("LEFT" )) {
213- isLeftFlag = -1 ;
214- } else {
215- isLeftFlag = 0 ;
216- }
217- }
218- }
219213 }
220214
221215
222216
223217 public void setSwitchState (boolean state ) {
224218 this .switchActive = state ;
219+ TileEntity te1 ;
220+ int a = 0 ;
221+ int b = 0 ;
222+ int c = 0 ;
223+ switch (getBlockMetadata ()) {
224+ case 0 :
225+ c = 1 ;
226+ break ;
227+ case 1 :
228+ a = -1 ;
229+ break ;
230+ case 2 :
231+ c = -1 ;
232+ break ;
233+ case 3 :
234+ a = 1 ;
235+ break ;
236+ default :
237+ Traincraft .tcLog .log (Level .WARN , "Unsupported block meta for switch state." );
238+ return ;
239+ }
240+ int offsetX = a ;
241+ int offsetY = b ;
242+ int offsetZ = c ;
243+ while (Math .abs (offsetX ) < getTrack ().getSwitchSize () && Math .abs (offsetY ) < getTrack ().getSwitchSize () && Math .abs (offsetZ ) < getTrack ().getSwitchSize ()) {
244+ te1 = worldObj .getTileEntity (xCoord + offsetX , yCoord + offsetY , zCoord + offsetZ );
245+ if (te1 instanceof TileTCRail ) {
246+ if (getSwitchState ()) {
247+ if (getType ().contains ("SWITCH" ) && getType ().contains ("LEFT" )) {
248+ ((TileTCRail ) te1 ).setType (EnumTracks .MEDIUM_LEFT_TURN .getLabel ());
249+ ((TileTCRail ) te1 ).switchActive =true ;
250+ } else if (getType ().contains ("SWITCH" ) && getType ().contains ("RIGHT" )) {
251+ ((TileTCRail ) te1 ).setType (EnumTracks .MEDIUM_RIGHT_TURN .getLabel ());
252+ ((TileTCRail ) te1 ).switchActive =true ;
253+ }
254+ } else {
255+ ((TileTCRail ) te1 ).setType (EnumTracks .SMALL_STRAIGHT .getLabel ());
256+ ((TileTCRail ) te1 ).switchActive =false ;
257+ }
258+ }
259+ offsetX += a ;
260+ offsetY += b ;
261+ offsetZ += c ;
262+ }
225263
226264 this .markDirty ();
227265 this .worldObj .markBlockForUpdate (this .xCoord , this .yCoord , this .zCoord );
@@ -336,7 +374,6 @@ public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt){
336374
337375 public void changeSwitchState (World world , TileTCRail tileEntity , int i , int j , int k ) {
338376 if (tileEntity .getType () != null && (tileEntity .getType ().contains ("SWITCH" ))) {
339- tileEntity .setSwitchState (!tileEntity .getSwitchState ());
340377 TileEntity te1 ;
341378 int a = 0 ;
342379 int b = 0 ;
0 commit comments