Skip to content

Commit

Permalink
track data codes and object
Browse files Browse the repository at this point in the history
merge data methods together
  • Loading branch information
ghzdude committed Jan 23, 2025
1 parent 32c6d59 commit 32f2aa1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 33 deletions.
8 changes: 2 additions & 6 deletions src/main/java/gregtech/api/metatileentity/MetaTileEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -1083,10 +1083,8 @@ public void receiveCustomData(int dataId, @NotNull PacketBuffer buf) {
if (trait == null) {
GTLog.logger.warn("Could not find MTETrait for id: {} at position {}.", traitNetworkId, getPos());
} else {
ISyncedTileEntity.addCode(internalId, trait);
trait.receiveCustomData(internalId, buf);

// this should be fine, as nothing else is read after this
ISyncedTileEntity.checkCustomData(internalId, buf, trait);
}
} else if (dataId == COVER_ATTACHED_MTE) {
CoverSaveHandler.readCoverPlacement(buf, this);
Expand All @@ -1102,10 +1100,8 @@ public void receiveCustomData(int dataId, @NotNull PacketBuffer buf) {
Cover cover = getCoverAtSide(coverSide);
int internalId = buf.readVarInt();
if (cover != null) {
ISyncedTileEntity.addCode(internalId, cover);
cover.readCustomData(internalId, buf);

// this should be fine, as nothing else is read after this
ISyncedTileEntity.checkCustomData(internalId, buf, cover);
}
} else if (dataId == UPDATE_SOUND_MUFFLED) {
this.muffled = buf.readBoolean();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gregtech.api.metatileentity;

import gregtech.api.block.BlockStateTileEntity;
import gregtech.api.metatileentity.interfaces.IGregTechTileEntity;
import gregtech.api.metatileentity.interfaces.ISyncedTileEntity;
import gregtech.api.network.PacketDataList;

Expand Down Expand Up @@ -80,12 +79,9 @@ public final void onDataPacket(@NotNull NetworkManager net, @NotNull SPacketUpda
for (String discriminatorKey : entryTag.getKeySet()) {
ByteBuf backedBuffer = Unpooled.copiedBuffer(entryTag.getByteArray(discriminatorKey));
int dataId = Integer.parseInt(discriminatorKey);
ISyncedTileEntity.addCode(dataId, this);
receiveCustomData(dataId, new PacketBuffer(backedBuffer));

MetaTileEntity mte = null;
if (this instanceof IGregTechTileEntity gtte)
mte = gtte.getMetaTileEntity();
ISyncedTileEntity.checkCustomData(dataId, backedBuffer, mte == null ? this : mte);
ISyncedTileEntity.checkData(backedBuffer);
}
}
}
Expand All @@ -105,11 +101,8 @@ public final void handleUpdateTag(@NotNull NBTTagCompound tag) {
super.readFromNBT(tag); // deserializes Forge data and capabilities
byte[] updateData = tag.getByteArray("d");
ByteBuf backedBuffer = Unpooled.copiedBuffer(updateData);
ISyncedTileEntity.track(this);
receiveInitialSyncData(new PacketBuffer(backedBuffer));

MetaTileEntity mte = null;
if (this instanceof IGregTechTileEntity gtte)
mte = gtte.getMetaTileEntity();
ISyncedTileEntity.checkInitialData(backedBuffer, mte == null ? this : mte);
ISyncedTileEntity.checkData(backedBuffer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import net.minecraft.util.math.BlockPos;

import io.netty.buffer.ByteBuf;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
import org.jetbrains.annotations.NotNull;

import java.util.function.Consumer;
Expand All @@ -20,6 +22,8 @@
public interface ISyncedTileEntity {

Consumer<PacketBuffer> NO_OP = buf -> {};
IntList datacodes = new IntArrayList();
ThreadLocal<Object> tracked = ThreadLocal.withInitial(() -> null);

/**
* Used to sync data from Server -> Client.
Expand Down Expand Up @@ -119,26 +123,25 @@ default void writeCustomData(int discriminator) {
*/
void receiveCustomData(int discriminator, @NotNull PacketBuffer buf);

static void checkCustomData(int discriminator, @NotNull ByteBuf buf, Object obj) {
if (buf.readableBytes() == 0) return;

GTLog.logger.error(
"Class {} failed to finish reading receiveCustomData with discriminator {} and {} bytes remaining",
stringify(obj), GregtechDataCodes.getNameFor(discriminator), buf.readableBytes());

buf.clear(); // clear to prevent further logging
}

static void checkInitialData(@NotNull ByteBuf buf, Object obj) {
if (buf.readableBytes() == 0) return;

GTLog.logger.error("Class {} failed to finish reading initialSyncData with {} bytes remaining",
stringify(obj), buf.readableBytes());
static void checkData(@NotNull ByteBuf buf) {
if (buf.readableBytes() != 0) {
if (datacodes.isEmpty()) {
GTLog.logger.error("Class {} failed to finish reading initialSyncData with {} bytes remaining",
stringify(tracked.get()), buf.readableBytes());
} else {
GTLog.logger.error(
"Class {} failed to finish reading receiveCustomData at code path [{}] with {} bytes remaining",
stringify(tracked.get()), getCodePath(), buf.readableBytes());
}
}

buf.clear(); // clear to prevent further logging
reset();
}

static String stringify(Object obj) {
if (obj instanceof IGregTechTileEntity gtte && gtte.getMetaTileEntity() != null)
obj = gtte.getMetaTileEntity();

StringBuilder builder = new StringBuilder(obj.getClass().getSimpleName());

BlockPos pos = null;
Expand All @@ -158,4 +161,27 @@ static String stringify(Object obj) {

return builder.toString();
}

static void addCode(int code, Object trackedObject) {
datacodes.add(code);
track(trackedObject);
}

static void track(Object trackedObject) {
tracked.set(trackedObject);
}

static String getCodePath() {
var builder = new StringBuilder();
for (int i = 0; i < datacodes.size(); i++) {
builder.append(GregtechDataCodes.getNameFor(datacodes.get(i)));
if (i < datacodes.size() - 1) builder.append(" > ");
}
return builder.toString();
}

static void reset() {
datacodes.clear();
tracked.remove();
}
}

0 comments on commit 32f2aa1

Please sign in to comment.