Skip to content

Commit

Permalink
Fix packet reading issue with extra entity data packet
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeltumn committed Jul 15, 2024
1 parent 83f3c85 commit 3f3aae5
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
6 changes: 6 additions & 0 deletions fabric/src/main/java/com/noxcrew/noxesium/NoxesiumMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import com.noxcrew.noxesium.api.protocol.ProtocolVersion;
import com.noxcrew.noxesium.config.NoxesiumConfig;
import com.noxcrew.noxesium.feature.TeamGlowHotkeys;
import com.noxcrew.noxesium.feature.entity.ExtraEntityData;
import com.noxcrew.noxesium.feature.entity.ExtraEntityDataModule;
import com.noxcrew.noxesium.feature.entity.QibBehaviorModule;
import com.noxcrew.noxesium.feature.model.CustomServerCreativeItems;
import com.noxcrew.noxesium.feature.rule.ServerRuleModule;
import com.noxcrew.noxesium.feature.rule.ServerRules;
import com.noxcrew.noxesium.feature.skull.SkullFontModule;
import com.noxcrew.noxesium.feature.sounds.NoxesiumSoundModule;
import com.noxcrew.noxesium.feature.ui.NoxesiumReloadListener;
Expand Down Expand Up @@ -171,6 +173,10 @@ public void onInitializeClient() {

// Register the resource listener
ResourceManagerHelper.get(PackType.CLIENT_RESOURCES).registerReloadListener(new NoxesiumReloadListener());

// Trigger registration of all server and entity rules
Object ignored = ServerRules.DISABLE_SPIN_ATTACK_COLLISIONS;
ignored = ExtraEntityData.DISABLE_BUBBLES;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.noxcrew.noxesium.mixin;
package com.noxcrew.noxesium.mixin.general;

import com.noxcrew.noxesium.NoxesiumMod;
import io.netty.channel.ChannelHandlerContext;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.noxcrew.noxesium.mixin.general;

import com.noxcrew.noxesium.NoxesiumMod;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import net.minecraft.network.PacketDecoder;
import net.minecraft.network.PacketListener;
import net.minecraft.network.ProtocolInfo;
import net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket;
import net.minecraft.network.protocol.common.CommonPacketTypes;
import org.slf4j.Logger;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.List;

/**
* Hooks into packets having additional bytes and prints out the packet that was read.
*/
@Mixin(PacketDecoder.class)
public class PacketDecoderMixin<T extends PacketListener> {

@Shadow
@Final
private static Logger LOGGER;

@Shadow
@Final
private ProtocolInfo<T> protocolInfo;

@Inject(method = "decode", at = @At(value = "INVOKE", target = "Ljava/io/IOException;<init>(Ljava/lang/String;)V"))
public void onExceptionCaught(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list, CallbackInfo ci) {
if (NoxesiumMod.getInstance().getConfig().printPacketExceptions) {
// First rewind the buffer and re-create the packet
var oldReaderIndex = byteBuf.readerIndex();
byteBuf.setIndex(0, byteBuf.writerIndex());
var packet = protocolInfo.codec().decode(byteBuf);

// Secondly we print out the lingering bytes
var out = new byte[byteBuf.readableBytes()];
byteBuf.readBytes(out);
LOGGER.error("Received packet with lingering bytes, packet type is {}, full received packet is {} and the following bytes were left over {}",
// If it's a custom payload we change the type to match the exact type
packet.type() == CommonPacketTypes.CLIENTBOUND_CUSTOM_PAYLOAD ? ((ClientboundCustomPayloadPacket) packet).payload().type() : packet.type(),
packet,
new String(out)
);

// Set the buffer back
byteBuf.setIndex(oldReaderIndex, byteBuf.writerIndex());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static List<Object> readValues(RuleIndexProvider provider, FriendlyByteBu
for (var index : indices) {
// If we don't know one rule the whole packet is useless
var rule = provider.getIndex(index);
if (rule == null) return result;
if (rule == null) throw new UnsupportedOperationException("Invalid rule index " + index);
var data = rule.read(buf);
result.add(data);
}
Expand Down
3 changes: 2 additions & 1 deletion fabric/src/main/resources/noxesium.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@
"defaultRequire": 1
},
"mixins": [
"ConnectionMixin",
"entity.EntityMixin",
"entity.GuardianMixin",
"entity.InteractionMixin",
"general.ConnectionMixin",
"general.PacketDecoderMixin",
"model.ItemDisplayParametersMixin"
]
}

0 comments on commit 3f3aae5

Please sign in to comment.