Skip to content

Commit

Permalink
Fix Laser Pipe Client Desync + Small Rework (#2245)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghzdude authored Dec 3, 2023
1 parent 5429ae5 commit a0fa877
Showing 1 changed file with 38 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,32 +156,29 @@ public boolean isActive() {
* @param duration how long the pipe should be active for
*/
public void setActive(boolean active, int duration) {
boolean stateChanged = false;
if (this.isActive && !active) {
this.isActive = false;
stateChanged = true;
} else if (!this.isActive && active) {
this.isActive = true;
stateChanged = true;
activeDuration = duration;
TaskScheduler.scheduleTask(getWorld(), () -> {
if (++this.ticksActive % activeDuration == 0) {
this.ticksActive = 0;
setActive(false, -1);
return false;
}
return true;
});
} else if (this.isActive) {
if (this.isActive != active) {
this.isActive = active;
notifyBlockUpdate();
markDirty();
writeCustomData(GregtechDataCodes.PIPE_LASER_ACTIVE, buf -> buf.writeBoolean(this.isActive));
if (active && duration != this.activeDuration) {
TaskScheduler.scheduleTask(getWorld(), this::queueDisconnect);
}
}

this.activeDuration = duration;
if (duration > 0 && active) {
this.ticksActive = 0;
this.activeDuration = duration;
}
}

if (stateChanged) {
writeCustomData(GregtechDataCodes.PIPE_LASER_ACTIVE, buf -> buf.writeBoolean(this.isActive));
notifyBlockUpdate();
markDirty();
public boolean queueDisconnect() {
if (++this.ticksActive % activeDuration == 0) {
this.ticksActive = 0;
setActive(false, -1);
return false;
}
return true;
}

@Override
Expand All @@ -193,6 +190,25 @@ public void receiveCustomData(int discriminator, PacketBuffer buf) {
}
}

@Override
public void writeInitialSyncData(PacketBuffer buf) {
super.writeInitialSyncData(buf);
buf.writeBoolean(this.isActive);

// schedule a disconnect on world load, gotta set the duration to something
if (isActive) {
activeDuration = 100;
TaskScheduler.scheduleTask(getWorld(), this::queueDisconnect);
}
}

@Override
public void receiveInitialSyncData(PacketBuffer buf) {
super.receiveInitialSyncData(buf);
this.isActive = buf.readBoolean();
scheduleChunkForRenderUpdate();
}

@NotNull
@Override
public NBTTagCompound writeToNBT(@NotNull NBTTagCompound compound) {
Expand Down

0 comments on commit a0fa877

Please sign in to comment.