Skip to content

Commit 870f11f

Browse files
author
EternalBlueFlame
committed
switch track fixes
Switches now check for redstone in a 3x3 area. Small performance improvements to all TC rails.
1 parent 3cfa262 commit 870f11f

2 files changed

Lines changed: 94 additions & 64 deletions

File tree

src/main/java/train/common/blocks/BlockTCRail.java

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import cpw.mods.fml.relauncher.Side;
44
import cpw.mods.fml.relauncher.SideOnly;
5+
import ebf.tim.utility.CommonUtil;
56
import net.minecraft.block.Block;
67
import net.minecraft.block.material.Material;
78
import net.minecraft.client.renderer.texture.IIconRegister;
@@ -121,28 +122,20 @@ public void breakBlock(World world, int i, int j, int k, Block par5, int par6) {
121122
@Override
122123
public void onNeighborBlockChange(World world, int i, int j, int k, Block par5) {
123124
TileEntity tile = world.getTileEntity(i, j, k);
124-
if (tile == null || !(tile instanceof TileTCRail))
125-
return;
126-
127-
TileTCRail tileEntity = (TileTCRail) world.getTileEntity(i, j, k);
128-
if (tileEntity != null && tileEntity.isLinkedToRail) {
129-
if (world.isAirBlock(tileEntity.linkedX, tileEntity.linkedY, tileEntity.linkedZ)) {
130-
// NOTE: func_147480_a = destroyBlock
131-
world.removeTileEntity(i, j, k);
132-
world.func_147480_a(i, j, k, false);
133-
}
134-
}
135-
if (!World.doesBlockHaveSolidTopSurface(world, i, j - 1, k) && world.getBlock(i, j-1, k) != TCBlocks.bridgePillar) {
136-
// NOTE: func_147480_a = destroyBlock
137-
world.func_147480_a(i, j, k, false);
138-
world.removeTileEntity(i, j, k);
139-
}
140-
if (tileEntity != null && !world.isRemote) {
141-
boolean flag = world.isBlockIndirectlyGettingPowered(i, j, k);
142-
if (tileEntity.getSwitchState() != flag) {
143-
tileEntity.changeSwitchState(world, tileEntity, i, j, k);
144-
}
145-
}
125+
if (tile instanceof TileTCRail) {
126+
if (((TileTCRail)tile).isLinkedToRail) {
127+
if (world.isAirBlock(((TileTCRail)tile).linkedX, ((TileTCRail)tile).linkedY, ((TileTCRail)tile).linkedZ)) {
128+
// NOTE: func_147480_a = destroyBlock
129+
world.removeTileEntity(i, j, k);
130+
world.func_147480_a(i, j, k, false);
131+
}
132+
}
133+
if (!World.doesBlockHaveSolidTopSurface(world, i, j - 1, k) && world.getBlock(i, j - 1, k) != TCBlocks.bridgePillar) {
134+
// NOTE: func_147480_a = destroyBlock
135+
world.func_147480_a(i, j, k, false);
136+
world.removeTileEntity(i, j, k);
137+
}
138+
}
146139
}
147140

148141
@Override

src/main/java/train/common/tile/TileTCRail.java

Lines changed: 79 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import cpw.mods.fml.relauncher.Side;
44
import cpw.mods.fml.relauncher.SideOnly;
5+
import ebf.tim.utility.CommonUtil;
6+
import ebf.tim.utility.DebugUtil;
57
import net.minecraft.block.Block;
68
import net.minecraft.entity.item.EntityMinecart;
79
import net.minecraft.entity.player.EntityPlayer;
@@ -16,6 +18,8 @@
1618
import org.apache.logging.log4j.Level;
1719
import train.common.Traincraft;
1820
import train.common.api.TrackRecord;
21+
import train.common.blocks.BlockTCRail;
22+
import train.common.blocks.BlockTCRailGag;
1923
import train.common.core.handlers.ConfigHandler;
2024
import train.common.items.TCRailTypes;
2125
import 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

Comments
 (0)