Skip to content

Commit e0b5188

Browse files
committed
improve logging when data packet has data left
1 parent d0184c8 commit e0b5188

File tree

4 files changed

+54
-20
lines changed

4 files changed

+54
-20
lines changed

src/main/java/gregtech/api/capability/GregtechDataCodes.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package gregtech.api.capability;
22

3+
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
4+
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
5+
6+
import java.lang.reflect.Field;
7+
38
public class GregtechDataCodes {
49

510
private static int nextId = 0;
@@ -180,4 +185,20 @@ public static int assignId() {
180185
// ME Parts
181186
public static final int UPDATE_AUTO_PULL = assignId();
182187
public static final int UPDATE_ONLINE_STATUS = assignId();
188+
189+
// Everything below MUST be last in the class!
190+
public static final Int2ObjectMap<String> NAMES = new Int2ObjectArrayMap<>();
191+
192+
static {
193+
try {
194+
for (Field field : GregtechDataCodes.class.getFields()) {
195+
if (field.getType() != Integer.TYPE) continue;
196+
NAMES.put(field.getInt(null), field.getName());
197+
}
198+
} catch (IllegalAccessException ignored) {}
199+
}
200+
201+
public static String getNameFor(int id) {
202+
return NAMES.getOrDefault(id, "Unknown DataCode: " + id);
203+
}
183204
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,9 @@ public void receiveCustomData(int dataId, @NotNull PacketBuffer buf) {
10961096
if (cover != null) {
10971097
cover.readCustomData(internalId, buf);
10981098
}
1099+
if (buf.readableBytes() != 0)
1100+
ISyncedTileEntity.handleUnreadPacket(internalId, buf, this);
1101+
buf.clear(); // clear data so holder doesn't log error
10991102
} else if (dataId == UPDATE_SOUND_MUFFLED) {
11001103
this.muffled = buf.readBoolean();
11011104
if (muffled) {

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,10 @@ public final void onDataPacket(@NotNull NetworkManager net, @NotNull SPacketUpda
8080
NBTTagCompound entryTag = (NBTTagCompound) entryBase;
8181
for (String discriminatorKey : entryTag.getKeySet()) {
8282
ByteBuf backedBuffer = Unpooled.copiedBuffer(entryTag.getByteArray(discriminatorKey));
83-
receiveCustomData(Integer.parseInt(discriminatorKey), new PacketBuffer(backedBuffer));
83+
int dataId = Integer.parseInt(discriminatorKey);
84+
receiveCustomData(dataId, new PacketBuffer(backedBuffer));
8485
if (backedBuffer.readableBytes() != 0) {
85-
String className = null;
86-
if (this instanceof IGregTechTileEntity gtte) {
87-
MetaTileEntity mte = gtte.getMetaTileEntity();
88-
if (mte != null) className = mte.getClass().getName();
89-
}
90-
if (className == null) {
91-
className = this.getClass().getName();
92-
}
93-
GTLog.logger.error(
94-
"Class {} failed to finish reading receiveCustomData with discriminator {} and {} bytes remaining",
95-
className, discriminatorKey, backedBuffer.readableBytes());
86+
ISyncedTileEntity.handleUnreadPacket(dataId, backedBuffer, this);
9687
}
9788
}
9889
}

src/main/java/gregtech/api/metatileentity/interfaces/ISyncedTileEntity.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package gregtech.api.metatileentity.interfaces;
22

3+
import gregtech.api.capability.GregtechDataCodes;
4+
import gregtech.api.metatileentity.MetaTileEntity;
5+
import gregtech.api.util.GTLog;
6+
37
import net.minecraft.network.PacketBuffer;
8+
import net.minecraft.tileentity.TileEntity;
49

10+
import io.netty.buffer.ByteBuf;
511
import org.jetbrains.annotations.NotNull;
612

713
import java.util.function.Consumer;
@@ -25,7 +31,7 @@ public interface ISyncedTileEntity {
2531
* <p>
2632
* This method is called <strong>Server-Side</strong>.
2733
* <p>
28-
* Equivalent to {@link net.minecraft.tileentity.TileEntity#getUpdateTag}.
34+
* Equivalent to {@link TileEntity#getUpdateTag}.
2935
*
3036
* @param buf the buffer to write data to
3137
*/
@@ -43,7 +49,7 @@ public interface ISyncedTileEntity {
4349
* <p>
4450
* This method is called <strong>Client-Side</strong>.
4551
* <p>
46-
* Equivalent to {@link net.minecraft.tileentity.TileEntity#handleUpdateTag}.
52+
* Equivalent to {@link TileEntity#handleUpdateTag}.
4753
*
4854
* @param buf the buffer to read data from
4955
*/
@@ -62,11 +68,11 @@ public interface ISyncedTileEntity {
6268
* <p>
6369
* This method is called <strong>Server-Side</strong>.
6470
* <p>
65-
* Equivalent to {@link net.minecraft.tileentity.TileEntity#getUpdatePacket}
71+
* Equivalent to {@link TileEntity#getUpdatePacket}
6672
*
6773
* @param discriminator the discriminator determining the packet sent.
6874
* @param dataWriter a consumer which writes packet data to a buffer.
69-
* @see gregtech.api.capability.GregtechDataCodes
75+
* @see GregtechDataCodes
7076
*/
7177
void writeCustomData(int discriminator, @NotNull Consumer<@NotNull PacketBuffer> dataWriter);
7278

@@ -82,10 +88,10 @@ public interface ISyncedTileEntity {
8288
* <p>
8389
* This method is called <strong>Server-Side</strong>.
8490
* <p>
85-
* Equivalent to {@link net.minecraft.tileentity.TileEntity#getUpdatePacket}
91+
* Equivalent to {@link TileEntity#getUpdatePacket}
8692
*
8793
* @param discriminator the discriminator determining the packet sent.
88-
* @see gregtech.api.capability.GregtechDataCodes
94+
* @see GregtechDataCodes
8995
*/
9096
default void writeCustomData(int discriminator) {
9197
writeCustomData(discriminator, NO_OP);
@@ -103,11 +109,24 @@ default void writeCustomData(int discriminator) {
103109
* <p>
104110
* This method is called <strong>Client-Side</strong>.
105111
* <p>
106-
* Equivalent to {@link net.minecraft.tileentity.TileEntity#onDataPacket}
112+
* Equivalent to {@link TileEntity#onDataPacket}
107113
*
108114
* @param discriminator the discriminator determining the packet sent.
109115
* @param buf the buffer containing the packet data.
110-
* @see gregtech.api.capability.GregtechDataCodes
116+
* @see GregtechDataCodes
111117
*/
112118
void receiveCustomData(int discriminator, @NotNull PacketBuffer buf);
119+
120+
static void handleUnreadPacket(int discriminator, @NotNull ByteBuf buf, ISyncedTileEntity syncedTile) {
121+
String className = null;
122+
if (syncedTile instanceof IGregTechTileEntity gtte) {
123+
MetaTileEntity mte = gtte.getMetaTileEntity();
124+
if (mte != null) className = mte.getClass().getSimpleName();
125+
} else {
126+
className = syncedTile.getClass().getSimpleName();
127+
}
128+
GTLog.logger.error(
129+
"Class {} failed to finish reading receiveCustomData with discriminator {} and {} bytes remaining",
130+
className, GregtechDataCodes.getNameFor(discriminator), buf.readableBytes());
131+
}
113132
}

0 commit comments

Comments
 (0)