Skip to content

Commit d739019

Browse files
AnsonYeunggithub-actions[bot]Dream-Master
authored
Fix spam of block update (#7068)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Martin Robertz <dream-master@gmx.net>
1 parent f94dd70 commit d739019

5 files changed

Lines changed: 70 additions & 32 deletions

File tree

src/main/java/gregtech/api/metatileentity/BaseMetaPipeEntity.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -796,15 +796,12 @@ public boolean onRightclick(final EntityPlayer aPlayer, final ForgeDirection sid
796796
// logic handled internally
797797
sendSoundToPlayers(SoundResource.IC2_TOOLS_BATTERY_USE, 1.0F, -1);
798798
} else if (GTModHandler.useSolderingIron(tCurrentItem, aPlayer)) {
799-
mMetaTileEntity.markDirty();
800-
mStrongRedstone ^= wrenchingSide.flag;
801799
GTUtility.sendChatTrans(
802800
aPlayer,
803-
(mStrongRedstone & wrenchingSide.flag) != 0 ? "GT5U.chat.machine.redstone_output_set.strong"
801+
toggleStrongRedstone(wrenchingSide) ? "GT5U.chat.machine.redstone_output_set.strong"
804802
: "GT5U.chat.machine.redstone_output_set.weak",
805803
new ChatComponentTranslation(GTUtility.getUnlocalizedSideName(wrenchingSide)));
806804
sendSoundToPlayers(SoundResource.IC2_TOOLS_BATTERY_USE, 3.0F, -1);
807-
issueBlockUpdate();
808805
}
809806
doEnetUpdate();
810807
return true;

src/main/java/gregtech/api/metatileentity/BaseMetaTileEntity.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,15 +1548,12 @@ public boolean onRightclick(final EntityPlayer aPlayer, final ForgeDirection sid
15481548
// logic handled internally
15491549
sendSoundToPlayers(SoundResource.IC2_TOOLS_BATTERY_USE, 1.0F, -1);
15501550
} else if (GTModHandler.useSolderingIron(tCurrentItem, aPlayer)) {
1551-
mStrongRedstone ^= wrenchingSide.flag;
15521551
GTUtility.sendChatTrans(
15531552
aPlayer,
1554-
(mStrongRedstone & wrenchingSide.flag) != 0
1555-
? "GT5U.chat.machine.redstone_output_set.strong"
1553+
toggleStrongRedstone(wrenchingSide) ? "GT5U.chat.machine.redstone_output_set.strong"
15561554
: "GT5U.chat.machine.redstone_output_set.weak",
15571555
new ChatComponentTranslation(GTUtility.getUnlocalizedSideName(wrenchingSide)));
15581556
sendSoundToPlayers(SoundResource.IC2_TOOLS_BATTERY_USE, 3.0F, -1);
1559-
issueBlockUpdate();
15601557
}
15611558
if (tCurrentItem.stackSize == 0) ForgeEventFactory.onPlayerDestroyItem(aPlayer, tCurrentItem);
15621559
doEnetUpdate();

src/main/java/gregtech/api/metatileentity/BaseTileEntity.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ public final int getOffsetZ(ForgeDirection side, int multiplier) {
161161
return zCoord + side.offsetZ * multiplier;
162162
}
163163

164+
abstract boolean isTickDisabled();
165+
164166
@Override
165167
public final boolean isServerSide() {
166168
if (worldObj == null) {

src/main/java/gregtech/api/metatileentity/CommonBaseMetaTileEntity.java

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,18 @@
4545
public abstract class CommonBaseMetaTileEntity extends CoverableTileEntity
4646
implements IGregTechTileEntity, IInterfaceNameProvider {
4747

48-
protected boolean mNeedsBlockUpdate = true, mNeedsUpdate = true, mNeedsTileUpdate = false,
49-
mInventoryChanged = false, mTickDisabled = false;
48+
// mNeedsUpdate: Client only, mark the block for rerender
49+
// mNeedsTileUpdate: Server only, mark the block for sync using `S35PacketUpdateTileEntity`
50+
// mInventoryChanged: whether the inventory had changed in the previous tick, currently not all code set this
51+
// mTickDisabled: whether this block is currently or pending to be unregistered from loaded tile entity list.
52+
protected boolean mNeedsUpdate = true, mNeedsTileUpdate = false, mInventoryChanged = false, mTickDisabled = false;
5053

5154
private boolean mIgnoreNextUnload = false;
5255

5356
protected int oldX = 0, oldY = 0, oldZ = 0;
54-
protected byte oldStrongRedstone = 0, oldRedstoneData = 63, oldUpdateData = 0;
57+
// oldRedstoneData: bitmask of redstone output for all 6 sides, apparently used for client rendering
58+
// oldUpdateData: One byte of data that is supplied by MTE and synced with client
59+
protected byte oldRedstoneData = 63, oldUpdateData = 0;
5560

5661
private byte mColor = 0;
5762

@@ -75,7 +80,8 @@ protected boolean createNewMetatileEntity(short aID) {
7580
return false;
7681
}
7782

78-
public boolean isTickDisabled() {
83+
@Override
84+
public final boolean isTickDisabled() {
7985
return mTickDisabled;
8086
}
8187

@@ -471,20 +477,6 @@ public void issueTextureUpdate() {
471477
mNeedsUpdate = true;
472478
}
473479

474-
@Override
475-
public void issueBlockUpdate() {
476-
mNeedsBlockUpdate = true;
477-
if (mTickDisabled) {
478-
doBlockUpdateServer();
479-
}
480-
}
481-
482-
public final void doBlockUpdateServer() {
483-
updateNeighbours(mStrongRedstone, oldStrongRedstone);
484-
oldStrongRedstone = mStrongRedstone;
485-
mNeedsBlockUpdate = false;
486-
}
487-
488480
@Override
489481
public void issueTileUpdate() {
490482
mNeedsTileUpdate = true;

src/main/java/gregtech/api/metatileentity/CoverableTileEntity.java

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,15 @@ public abstract class CoverableTileEntity extends BaseTileEntity implements ICov
7474
CoverRegistry.NO_COVER, CoverRegistry.NO_COVER, CoverRegistry.NO_COVER, CoverRegistry.NO_COVER };
7575
private byte validCoversMask;
7676

77+
// Whether block update is needed because of redstone changes.
78+
protected boolean mNeedsBlockUpdate = true;
79+
// The actual redstone signal per side, direct modifiers of this are required to issue block update for change.
80+
// Use `setOutputRedstoneSignal` to modify the redstone output and issue block update on the server if changed.
7781
protected final byte[] mSidedRedstone = new byte[] { 0, 0, 0, 0, 0, 0 };
78-
protected byte mStrongRedstone = 0;
79-
protected byte oldStrongRedstone = 0;
82+
// Use `setStrongRedstone` to automatically send block update on change
83+
private byte mStrongRedstone = 0;
84+
// To only issue update to sides that has strong redstone set either previously or currently
85+
private byte oldStrongRedstone = 0;
8086

8187
protected short mID = 0;
8288
public long mTickTimer = 0;
@@ -330,6 +336,9 @@ protected void onBaseTEDestroyed() {
330336
}
331337
}
332338

339+
/**
340+
* Used on client side, no need to issue block update
341+
*/
333342
protected void setRedstoneOutput(int packedRedstoneValue) {
334343
mSidedRedstone[0] = (byte) ((packedRedstoneValue & 1) == 1 ? 15 : 0);
335344
mSidedRedstone[1] = (byte) ((packedRedstoneValue & 2) == 2 ? 15 : 0);
@@ -349,13 +358,54 @@ public final byte getSidedRedstoneMask() {
349358
return redstone;
350359
}
351360

361+
public final byte getStrongRedstone() {
362+
return mStrongRedstone;
363+
}
364+
365+
/**
366+
* Sets a new value for strong redstone. If the value is changed, block update is issued automatically.
367+
*/
368+
public final void setStrongRedstone(byte newStrongRedstone) {
369+
if (newStrongRedstone != mStrongRedstone) {
370+
mStrongRedstone = newStrongRedstone;
371+
markDirty();
372+
issueBlockUpdate();
373+
}
374+
}
375+
376+
/**
377+
* Toggles the specified side for strong redstone
378+
*
379+
* @return whether the side is emitting strong redstone
380+
*/
381+
public final boolean toggleStrongRedstone(ForgeDirection side) {
382+
mStrongRedstone ^= (byte) side.flag;
383+
markDirty();
384+
issueBlockUpdate();
385+
return (mStrongRedstone & side.flag) != 0;
386+
}
387+
388+
@Override
389+
public void issueBlockUpdate() {
390+
mNeedsBlockUpdate = true;
391+
if (isTickDisabled()) {
392+
doBlockUpdateServer();
393+
}
394+
}
395+
396+
public final void doBlockUpdateServer() {
397+
updateNeighbours(mStrongRedstone, oldStrongRedstone);
398+
oldStrongRedstone = mStrongRedstone;
399+
mNeedsBlockUpdate = false;
400+
}
401+
352402
@Override
353403
public void setOutputRedstoneSignal(ForgeDirection side, byte strength) {
354404
final byte cappedStrength = (byte) Math.min(Math.max(0, strength), 15);
355405
if (side == ForgeDirection.UNKNOWN) return;
356406

357407
final int ordinalSide = side.ordinal();
358-
if (mSidedRedstone[ordinalSide] != cappedStrength || (mStrongRedstone & (1 << ordinalSide)) > 0) {
408+
if (mSidedRedstone[ordinalSide] != cappedStrength) {
359409
mSidedRedstone[ordinalSide] = cappedStrength;
360410
issueBlockUpdate();
361411
scheduleTexturePacket();
@@ -364,16 +414,16 @@ public void setOutputRedstoneSignal(ForgeDirection side, byte strength) {
364414

365415
@Override
366416
public void setStrongOutputRedstoneSignal(ForgeDirection side, byte strength) {
367-
mStrongRedstone |= (byte) side.flag;
417+
setStrongRedstone((byte) (mStrongRedstone | side.flag));
368418
setOutputRedstoneSignal(side, strength);
369419
}
370420

371421
@Override
372422
public void setRedstoneOutputStrength(ForgeDirection side, boolean isStrong) {
373423
if (isStrong) {
374-
mStrongRedstone |= (byte) side.flag;
424+
setStrongRedstone((byte) (mStrongRedstone | side.flag));
375425
} else {
376-
mStrongRedstone &= ~(byte) side.flag;
426+
setStrongRedstone((byte) (mStrongRedstone & (~side.flag)));
377427
}
378428
setOutputRedstoneSignal(side, mSidedRedstone[side.ordinal()]);
379429
}

0 commit comments

Comments
 (0)