Prevent disconnection on decode failure

This commit is contained in:
s809
2024-06-10 23:55:33 +05:00
parent ea47e11ecf
commit 004103551f
2 changed files with 23 additions and 5 deletions
@@ -1,5 +1,7 @@
package com.seibel.distanthorizons.common;
import com.seibel.distanthorizons.core.config.Config;
import com.seibel.distanthorizons.core.logging.ConfigBasedLogger;
import com.seibel.distanthorizons.core.network.messages.PluginMessageRegistry;
import com.seibel.distanthorizons.core.network.plugin.PluginChannelMessage;
import com.seibel.distanthorizons.core.network.protocol.INetworkObject;
@@ -9,12 +11,16 @@ import com.seibel.distanthorizons.coreapi.ModInfo;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import org.apache.logging.log4j.LogManager;
import org.jetbrains.annotations.Nullable;
import java.util.Objects;
public abstract class AbstractPluginPacketSender implements IPluginPacketSender
{
private static final ConfigBasedLogger LOGGER = new ConfigBasedLogger(LogManager.getLogger(),
() -> Config.Client.Advanced.Logging.logNetworkEvent.get());
public static final ResourceLocation PLUGIN_CHANNEL_RESOURCE = new ResourceLocation(ModInfo.RESOURCE_NAMESPACE, ModInfo.PLUGIN_CHANNEL_PATH);
public static final ResourceLocation WRAPPER_PACKET_RESOURCE = new ResourceLocation(ModInfo.RESOURCE_NAMESPACE, ModInfo.WRAPPER_PACKET_PATH);
@@ -31,13 +37,21 @@ public abstract class AbstractPluginPacketSender implements IPluginPacketSender
@Nullable
public static PluginChannelMessage decodeMessage(FriendlyByteBuf in)
{
if (in.readShort() != ModInfo.PROTOCOL_VERSION)
try
{
if (in.readShort() != ModInfo.PROTOCOL_VERSION)
{
return null;
}
PluginChannelMessage message = PluginMessageRegistry.INSTANCE.createMessage(in.readUnsignedShort());
return INetworkObject.decodeToInstance(message, in);
}
catch (Exception e)
{
LOGGER.error("Failed to decode message", e);
return null;
}
PluginChannelMessage message = PluginMessageRegistry.INSTANCE.createMessage(in.readUnsignedShort());
return INetworkObject.decodeToInstance(message, in);
}
public static void encodeMessage(FriendlyByteBuf out, PluginChannelMessage message)
@@ -33,7 +33,11 @@ public class NeoforgePluginPacketSender extends AbstractPluginPacketSender
.map(player -> player instanceof ServerPlayer ? (ServerPlayer) player : null)
.map(ServerPlayerWrapper::getWrapper)
.orElse(null);
packetConsumer.accept(serverPlayer, payload.message());
if (payload.message() != null)
{
packetConsumer.accept(serverPlayer, payload.message());
}
});
}