Skip to content

Commit

Permalink
Merge pull request #680 from fixrtm/fix-ride-opposite-bogie
Browse files Browse the repository at this point in the history
Fix riding to bogie of moving train
  • Loading branch information
anatawa12 committed Jul 1, 2023
2 parents 8c5aca9 + 425b2bb commit 69b2686
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG-SNAPSHOTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ The changelog for 2.0.23 and earlier is generated by [anatawa12's fork of `auto-
- Unnecessary ABI breaking changes `#676`
- we don't need to remove original signature of `RenderElectricalWiring.renderAllWire`
- Directory based ModelPacks not working `#677`
- Getting onto bogie of moving train may drive train opposite direction `#680`
- This also reverts `#633` because it's broken for long train

### Security

Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ Thanks to prepare-changelog.sh, we have some macros.
### Removed

### Fixed
- Sitting in the driver's seat on the opposite side drives train in reverse `#633`
- Vec3 DataMap will cause crash `#652`
- World is accessed from Netty IO Thread `#654`
- syncVehicleState(Door, data) does not work as the setter of getVehicleState(Door) `#660`
Expand All @@ -49,6 +48,7 @@ Thanks to prepare-changelog.sh, we have some macros.
- Unnecessary ABI breaking changes `#676`
- we don't need to remove original signature of `RenderElectricalWiring.renderAllWire`
- Directory based ModelPacks not working `#677`
- Getting onto bogie of moving train may drive train opposite direction `#680`

### Security

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
--- a/jp/ngt/rtm/entity/train/EntityTrainBase.java
+++ b/jp/ngt/rtm/entity/train/EntityTrainBase.java
@@ -54,28 +54,29 @@
@@ -54,28 +54,30 @@
import net.minecraftforge.fml.relauncher.SideOnly;

public abstract class EntityTrainBase extends EntityVehicleBase<ModelSetTrain> implements IChunkLoader {
private static final DataParameter<Integer> BOGIE_ID0 = EntityDataManager.createKey(EntityTrainBase.class, DataSerializers.VARINT);
private static final DataParameter<Integer> BOGIE_ID1 = EntityDataManager.createKey(EntityTrainBase.class, DataSerializers.VARINT);
+ private static final DataParameter<Float> TRAIN_SPEED = EntityDataManager.createKey(EntityTrainBase.class, DataSerializers.FLOAT);
+ private static final DataParameter<Boolean> CAB_DIRECTION = EntityDataManager.createKey(EntityTrainBase.class, DataSerializers.BOOLEAN);
public static final short MAX_AIR_COUNT = 2880;
public static final short MIN_AIR_COUNT = 2480;
public static final float TRAIN_WIDTH = 2.75F;
Expand All @@ -32,19 +33,37 @@
super(world);
this.setSize(2.75F, 1.1875F);
this.noClip = true;
@@ -92,10 +93,11 @@
@@ -92,18 +94,21 @@

protected void entityInit() {
super.entityInit();
this.getDataManager().register(BOGIE_ID0, 0);
this.getDataManager().register(BOGIE_ID1, 0);
+ this.getDataManager().register(TRAIN_SPEED, 0.0f);
+ this.getDataManager().register(CAB_DIRECTION, false);
}

protected void writeEntityToNBT(NBTTagCompound nbt) {
super.writeEntityToNBT(nbt);
NBTTagCompound nbttagcompound = new NBTTagCompound();
@@ -126,12 +128,15 @@
this.writeFormationData(nbttagcompound);
nbt.setTag("FormationEntry", nbttagcompound);
nbt.setInteger("trainDir", this.getTrainDirection());
+ nbt.setBoolean("cabDir", this.getDataManager().get(CAB_DIRECTION));
}

private void writeFormationData(NBTTagCompound nbt) {
if (this.formation != null) {
FormationEntry formationentry = this.formation.getEntry(this);
@@ -119,19 +124,23 @@
protected void readEntityFromNBT(NBTTagCompound nbt) {
super.readEntityFromNBT(nbt);
NBTTagCompound nbttagcompound = nbt.getCompoundTag("FormationEntry");
this.readFormationData(nbttagcompound);
this.setTrainDirection(nbt.getInteger("trainDir"));
+ this.getDataManager().set(CAB_DIRECTION, nbt.getBoolean("cabDir"));
}

private void readFormationData(NBTTagCompound nbt) {
long i = nbt.getLong("FormationId");
byte b0 = nbt.getByte("EntryPos");
Expand All @@ -61,7 +80,7 @@
formation.setTrain(this, b0, b1);
}

@@ -293,25 +298,25 @@
@@ -293,25 +302,25 @@
float f = this.getSpeed();
int i = this.getNotch();
Random random = this.world.rand;
Expand Down Expand Up @@ -91,7 +110,7 @@
double d6 = (random.nextDouble() * 2.0D - 1.0D) * d5;
double d7 = (random.nextDouble() * 2.0D - 1.0D) * d5;
this.world.spawnParticle(enumparticletypes, d2, d3, d4, d6, 0.25D, d7, new int[0]);
@@ -441,25 +446,34 @@
@@ -441,25 +450,34 @@
}
}

Expand Down Expand Up @@ -129,7 +148,44 @@
this.setSpeed(f);
}

@@ -655,11 +669,11 @@
@@ -506,11 +524,11 @@
public Vec3 getRiderPos(Entity passenger) {
return super.getRiderPos(passenger).add(0.0D, this.getMountedYOffset(), 0.0D);
}

protected int getRiderPosIndex() {
- return this.getTrainDirection();
+ return this.getCabDirection();
}

public double getMountedYOffset() {
return (double)(this.height + 1.1875F - 0.93F);
}
@@ -593,15 +611,20 @@
}

}

private void mountEntityToTrain(Entity entity, int direction) {
- if (this.getTrainDirection() != direction) {
- this.setSpeed(-this.getSpeed());
+ if (this.isControlCar()) {
+ this.setTrainDirection(direction);
+ if (this.formation != null && this.formation.size() > 1) {
+ byte data = this.getVehicleState(TrainState.TrainStateType.Role);
+ byte newData = this.getCabDirection() == this.getTrainDirection() ? data : (byte) (data ^ 2);
+ this.setTrainStateData_NoSync(TrainState.TrainStateType.Role, newData);
+ }
}

- this.setTrainDirection(direction);
+ setCabDirection(direction);
entity.startRiding(this);
}

protected void removePassengerFromVehicle(Entity passenger) {
Entity entity = this.getBogie(this.getTrainDirection());
@@ -655,11 +678,11 @@
if (!this.world.isRemote && par2.getTrain() != null) {
int i = par1.getBogieId();
int j = par2.getBogieId();
Expand All @@ -142,7 +198,7 @@
entityplayer = (EntityPlayer)this.getFirstPassenger();
} else if (par2.getTrain().getFirstPassenger() instanceof EntityPlayer) {
entityplayer = (EntityPlayer)par2.getTrain().getFirstPassenger();
@@ -672,38 +686,38 @@
@@ -672,38 +695,38 @@
}

}
Expand Down Expand Up @@ -186,7 +242,20 @@
public boolean existBogies() {
return this.getBogie(0) != null && this.getBogie(1) != null;
}
@@ -760,10 +774,11 @@
@@ -755,15 +778,24 @@

public void setFormation(Formation par1) {
this.formation = par1;
}

+ public int getCabDirection() {
+ return this.getDataManager().get(CAB_DIRECTION) ? 1 : 0;
+ }
+
+ private void setCabDirection(int direction) {
+ this.getDataManager().set(CAB_DIRECTION, direction != 0);
+ }
+
public int getTrainDirection() {
return this.getVehicleState(TrainState.TrainStateType.Direction);
}
Expand All @@ -198,7 +267,7 @@
} else {
this.formation.setTrainDirection((byte)par1, this);
}
@@ -793,33 +808,46 @@
@@ -793,33 +825,46 @@
public boolean addNotch(Entity driver, int par2) {
if (par2 != 0) {
int i = this.getNotch();
Expand Down Expand Up @@ -250,7 +319,7 @@
}

public void setSignal(int par1) {
@@ -883,10 +911,11 @@
@@ -883,10 +928,11 @@
this.releaseTicket();
}

Expand All @@ -262,7 +331,7 @@
public boolean isChunkLoaderEnable() {
return this.getVehicleState(TrainState.TrainStateType.ChunkLoader) > 0;
}
@@ -903,11 +932,10 @@
@@ -903,11 +949,10 @@
private boolean requestTicket() {
Ticket ticket = RTMChunkManager.INSTANCE.getNewTicket(this.world, Type.ENTITY);
if (ticket != null) {
Expand All @@ -274,7 +343,7 @@
return true;
} else {
NGTLog.debug("[RTM] Failed to get ticket (Chunk Loader)");
@@ -931,20 +959,26 @@
@@ -931,20 +976,26 @@
if (!this.world.isRemote) {
if (this.ticket == null && !this.requestTicket()) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
break;
}
}
@@ -241,10 +247,11 @@
@@ -241,18 +247,23 @@
}

public void setSpeed(float par1) {
Expand All @@ -102,7 +102,19 @@

this.speed = par1;
}
@@ -265,35 +272,44 @@
}

public void setTrainDirection(byte par1, EntityTrainBase par2) {
+ if (par2.getTrainDirection() != par1) {
+ this.setSpeed(-par2.getSpeed());
+ }
+
FormationEntry formationentry = this.getEntry(par2);
if (formationentry != null) {
this.direction = (byte)(par1 ^ formationentry.dir);
byte b0 = 0;

@@ -265,35 +276,45 @@

}
}
Expand All @@ -116,7 +128,8 @@
+ if (formationentry == null) continue;
+ if (data == TrainState.Role_Front.data || data == TrainState.Role_Back.data) {
this.controlCar = par2;
+ par2.setTrainDirection(par2.getTrainDirection());
+ par2.setTrainStateData_NoSync(type, (par2.getCabDirection() == par2.getTrainDirection()) ? data : (byte) (data ^ 2));
+ par2.setTrainDirection(par2.getCabDirection());
}

if (par2.equals(formationentry.train)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
if (axisalignedbb == null) {
axisalignedbb = new AxisAlignedBB(-1.5D, 0.0D, -2.0D, 1.5D, 3.0D, 2.0D);
}
@@ -532,21 +531,36 @@
@@ -532,21 +531,33 @@
public void setTrainStateData(int id, byte data) {
this.setVehicleState(TrainState.getStateType(id), data);
}
Expand All @@ -25,9 +25,6 @@
public void setVehicleState(TrainState.TrainStateType type, byte data) {
- this.getResourceState().getDataMap().setInt(type.toString(), data, 3);
+ this.getResourceState().getDataMap().setInt(type.toString(), type.clap(data, this), 3);
+ if (type == TrainState.TrainStateType.Direction && data != getVehicleState(TrainState.TrainStateType.Direction)) {
+ setSpeed(-getSpeed());
+ }
}

@SideOnly(Side.CLIENT)
Expand Down

0 comments on commit 69b2686

Please sign in to comment.