1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-25 22:53:22 +00:00

Check for duplicate ids when registering channels

Should prevent #1130 occurring again. Possibly worth submitting a PR to
Forge for this too.
This commit is contained in:
Jonathan Coates 2022-07-08 08:27:37 +01:00
parent 954254e7e4
commit be3a960273
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06

View File

@ -9,6 +9,8 @@
import dan200.computercraft.api.ComputerCraftAPI;
import dan200.computercraft.shared.network.client.*;
import dan200.computercraft.shared.network.server.*;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSet;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.network.PacketBuffer;
@ -26,7 +28,8 @@
public final class NetworkHandler
{
public static SimpleChannel network;
private static SimpleChannel network;
private static final IntSet usedIds = new IntOpenHashSet();
private NetworkHandler()
{
@ -100,6 +103,7 @@ public static void sendToAllTracking( NetworkMessage packet, Chunk chunk )
*/
private static <T extends NetworkMessage> void registerMainThread( int id, NetworkDirection direction, Class<T> type, Function<PacketBuffer, T> decoder )
{
if( !usedIds.add( id ) ) throw new IllegalStateException( "Duplicate message kind for for id " + id );
network.messageBuilder( type, id, direction )
.encoder( NetworkMessage::toBytes )
.decoder( decoder )