Skip to content

Commit

Permalink
Fix PacketHandlerFabric crashing in SMP
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Sep 28, 2024
1 parent 97d2cdb commit e7abbb2
Showing 1 changed file with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.cyclops.cyclopscore.network;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.PlayerLookup;
Expand Down Expand Up @@ -27,13 +29,7 @@ public PacketHandlerFabric(ModBaseFabric<?> mod) {
public <P extends PacketBase> void register(Class<P> clazz, CustomPacketPayload.Type<P> type, StreamCodec<? super RegistryFriendlyByteBuf, P> codec) {
PayloadTypeRegistry.playS2C().register(type, codec);
if (this.mod.getModHelpers().getMinecraftHelpers().isClientSide()) {
ClientPlayNetworking.registerGlobalReceiver(type, (packet, ctx) -> {
if (packet.isAsync()) {
handlePacketClient(ctx, packet);
} else {
ctx.client().execute(() -> handlePacketClient(ctx, packet));
}
});
registerClientSide(clazz, type, codec);
}

PayloadTypeRegistry.playC2S().register(type, codec);
Expand All @@ -46,14 +42,26 @@ public <P extends PacketBase> void register(Class<P> clazz, CustomPacketPayload.
});
}

public void handlePacketClient(ClientPlayNetworking.Context context, PacketBase<?> packet) {
packet.actionClient(context.player().level(), context.player());
}

public void handlePacketServer(ServerPlayNetworking.Context context, PacketBase<?> packet) {
packet.actionServer(context.player().level(), context.player());
}

@Environment(EnvType.CLIENT)
protected <P extends PacketBase> void registerClientSide(Class<P> clazz, CustomPacketPayload.Type<P> type, StreamCodec<? super RegistryFriendlyByteBuf, P> codec) {
ClientPlayNetworking.registerGlobalReceiver(type, (packet, ctx) -> {
if (packet.isAsync()) {
handlePacketClient(ctx, packet);
} else {
ctx.client().execute(() -> handlePacketClient(ctx, packet));
}
});
}

@Environment(EnvType.CLIENT)
public void handlePacketClient(ClientPlayNetworking.Context context, PacketBase<?> packet) {
packet.actionClient(context.player().level(), context.player());
}

@Override
public void sendToServer(PacketBase packet) {
ClientPlayNetworking.send(packet);
Expand Down

0 comments on commit e7abbb2

Please sign in to comment.