Hopefully more stable version of the original packet fix

This commit is contained in:
embeddedt 2023-05-14 19:59:39 -04:00
parent 0f2764b79d
commit b21ee9a7e7
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
2 changed files with 21 additions and 18 deletions

View File

@ -11,23 +11,13 @@ import org.spongepowered.asm.mixin.injection.Redirect;
@Mixin(ClientPacketListener.class)
@ClientOnlyMixin
public class ClientPlayNetHandlerMixin implements IClientNetHandler {
private FriendlyByteBuf savedCopy = null;
public class ClientPlayNetHandlerMixin {
/**
* @author embeddedt
* @reason Release the packet buffer at the end. Needed in f
* @reason allow the other function to track use of the buffer
*/
@Redirect(method = "handleCustomPayload", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/protocol/game/ClientboundCustomPayloadPacket;getData()Lnet/minecraft/network/FriendlyByteBuf;"))
private FriendlyByteBuf saveCopyForRelease(ClientboundCustomPayloadPacket instance) {
FriendlyByteBuf copy = instance.getData();
savedCopy = copy;
return copy;
}
@Override
public FriendlyByteBuf getCopiedCustomBuffer() {
FriendlyByteBuf copy = savedCopy;
savedCopy = null;
return copy;
return ((IClientNetHandler)instance).getCopiedCustomBuffer();
}
}

View File

@ -15,9 +15,13 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(ClientboundCustomPayloadPacket.class)
@ClientOnlyMixin
public class SCustomPayloadPlayPacketMixin {
public abstract class SCustomPayloadPlayPacketMixin implements IClientNetHandler {
@Shadow private FriendlyByteBuf data;
@Shadow public abstract FriendlyByteBuf getData();
private FriendlyByteBuf usedByteBuf = null;
private boolean needsRelease;
@Inject(method = "<init>(Lnet/minecraft/resources/ResourceLocation;Lnet/minecraft/network/FriendlyByteBuf;)V", at = @At("RETURN"))
@ -30,16 +34,25 @@ public class SCustomPayloadPlayPacketMixin {
this.needsRelease = true;
}
@Override
public FriendlyByteBuf getCopiedCustomBuffer() {
FriendlyByteBuf buf = this.getData();
usedByteBuf = buf;
return buf;
}
@Redirect(method = "handle(Lnet/minecraft/network/protocol/game/ClientGamePacketListener;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/protocol/game/ClientGamePacketListener;handleCustomPayload(Lnet/minecraft/network/protocol/game/ClientboundCustomPayloadPacket;)V"))
private void handleAndFree(ClientGamePacketListener instance, ClientboundCustomPayloadPacket sCustomPayloadPlayPacket) {
usedByteBuf = null;
try {
instance.handleCustomPayload(sCustomPayloadPlayPacket);
} finally {
FriendlyByteBuf copied = ((IClientNetHandler)instance).getCopiedCustomBuffer();
if(copied != null)
copied.release();
FriendlyByteBuf buf = usedByteBuf;
if(buf != null && buf.refCnt() > 0) {
buf.release();
}
}
if(this.needsRelease)
if(this.needsRelease && this.data.refCnt() > 0)
this.data.release();
}
}