From 5d6206ffc532c7300d26c16dd29af9893edf29f5 Mon Sep 17 00:00:00 2001 From: Sollace Date: Wed, 10 Feb 2021 18:33:21 +0200 Subject: [PATCH] Switch to the new networking api --- .../unicopia/network/Channel.java | 56 +++++++++---------- .../unicopia/network/MsgBlockDestruction.java | 4 +- .../network/MsgOtherPlayerCapabilities.java | 4 +- .../unicopia/network/MsgPlayerAbility.java | 6 +- .../network/MsgPlayerCapabilities.java | 10 ++-- .../network/MsgRequestCapabilities.java | 10 ++-- .../unicopia/network/MsgSpawnProjectile.java | 6 +- 7 files changed, 46 insertions(+), 50 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/network/Channel.java b/src/main/java/com/minelittlepony/unicopia/network/Channel.java index d483347e..0f8c7e1b 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/Channel.java +++ b/src/main/java/com/minelittlepony/unicopia/network/Channel.java @@ -4,14 +4,15 @@ import java.util.function.Function; import io.netty.buffer.Unpooled; import net.fabricmc.api.EnvType; -import net.fabricmc.fabric.api.network.ClientSidePacketRegistry; -import net.fabricmc.fabric.api.network.PacketContext; -import net.fabricmc.fabric.api.network.ServerSidePacketRegistry; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; import net.fabricmc.loader.api.FabricLoader; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.util.Identifier; +import net.minecraft.util.thread.ThreadExecutor; import net.minecraft.world.World; import net.minecraft.network.PacketByteBuf; +import net.minecraft.server.network.ServerPlayerEntity; public interface Channel { @@ -27,20 +28,26 @@ public interface Channel { static void bootstrap() { } static SPacketType clientToServer(Identifier id, Function factory) { - ServerSidePacketRegistry.INSTANCE.register(id, (context, buffer) -> factory.apply(buffer).handleOnMain(context)); - return () -> id; - } - - static MPacketType serverToClients(Identifier id, Function factory) { - if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) { - ClientSidePacketRegistry.INSTANCE.register(id, (context, buffer) -> factory.apply(buffer).handleOnMain(context)); - } + ServerPlayNetworking.registerGlobalReceiver(id, (server, player, handler, buffer, responder) -> { + factory.apply(buffer).handleOnMain(server, player); + }); return () -> id; } static CPacketType serverToClient(Identifier id, Function factory) { if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) { - ClientSidePacketRegistry.INSTANCE.register(id, (context, buffer) -> factory.apply(buffer).handleOnMain(context)); + ClientPlayNetworking.registerGlobalReceiver(id, (client, ignore1, buffer, ignore2) -> { + factory.apply(buffer).handleOnMain(client, client.player); + }); + } + return () -> id; + } + + static MPacketType serverToClients(Identifier id, Function factory) { + if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) { + ClientPlayNetworking.registerGlobalReceiver(id, (client, ignore1, buffer, ignore2) -> { + factory.apply(buffer).handleOnMain(client, client.player); + }); } return () -> id; } @@ -51,25 +58,21 @@ public interface Channel { default void send(World world, T packet) { world.getPlayers().forEach(player -> { if (player != null) { - ServerSidePacketRegistry.INSTANCE.sendToPlayer(player, getId(), packet.toBuffer()); + ServerPlayNetworking.send((ServerPlayerEntity)player, getId(), packet.toBuffer()); } }); } - - default net.minecraft.network.Packet toPacket(T packet) { - return ServerSidePacketRegistry.INSTANCE.toPacket(getId(), packet.toBuffer()); - } } interface CPacketType { Identifier getId(); default void send(PlayerEntity recipient, T packet) { - ServerSidePacketRegistry.INSTANCE.sendToPlayer(recipient, getId(), packet.toBuffer()); + ServerPlayNetworking.send((ServerPlayerEntity)recipient, getId(), packet.toBuffer()); } default net.minecraft.network.Packet toPacket(T packet) { - return ServerSidePacketRegistry.INSTANCE.toPacket(getId(), packet.toBuffer()); + return ServerPlayNetworking.createS2CPacket(getId(), packet.toBuffer()); } } @@ -80,24 +83,17 @@ public interface Channel { if (FabricLoader.getInstance().getEnvironmentType() != EnvType.CLIENT) { throw new RuntimeException("Client packet send called by the server"); } - ClientSidePacketRegistry.INSTANCE.sendToServer(getId(), packet.toBuffer()); - } - - default net.minecraft.network.Packet toPacket(T packet) { - if (FabricLoader.getInstance().getEnvironmentType() != EnvType.CLIENT) { - throw new RuntimeException("Client packet send called by the server"); - } - return ClientSidePacketRegistry.INSTANCE.toPacket(getId(), packet.toBuffer()); + ClientPlayNetworking.send(getId(), packet.toBuffer()); } } interface Packet { - void handle(PacketContext context); + void handle(PlayerEntity sender); void toBuffer(PacketByteBuf buffer); - default void handleOnMain(PacketContext context) { - context.getTaskQueue().execute(() -> handle(context)); + default void handleOnMain(ThreadExecutor server, PlayerEntity player) { + server.execute(() -> handle(player)); } default PacketByteBuf toBuffer() { diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgBlockDestruction.java b/src/main/java/com/minelittlepony/unicopia/network/MsgBlockDestruction.java index a18c3c60..c82df842 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgBlockDestruction.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgBlockDestruction.java @@ -4,9 +4,9 @@ import com.minelittlepony.unicopia.client.ClientBlockDestructionManager; import it.unimi.dsi.fastutil.longs.Long2ObjectMap; import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; -import net.fabricmc.fabric.api.network.PacketContext; import net.minecraft.network.PacketByteBuf; import net.minecraft.client.MinecraftClient; +import net.minecraft.entity.player.PlayerEntity; public class MsgBlockDestruction implements Channel.Packet { @@ -34,7 +34,7 @@ public class MsgBlockDestruction implements Channel.Packet { } @Override - public void handle(PacketContext context) { + public void handle(PlayerEntity sender) { ClientBlockDestructionManager destr = ((ClientBlockDestructionManager.Source)MinecraftClient.getInstance().worldRenderer).getDestructionManager(); destructions.forEach((i, d) -> { diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgOtherPlayerCapabilities.java b/src/main/java/com/minelittlepony/unicopia/network/MsgOtherPlayerCapabilities.java index e98c2ecc..937ec069 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgOtherPlayerCapabilities.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgOtherPlayerCapabilities.java @@ -2,8 +2,8 @@ package com.minelittlepony.unicopia.network; import com.minelittlepony.unicopia.entity.player.Pony; -import net.fabricmc.fabric.api.network.PacketContext; import net.minecraft.client.MinecraftClient; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.network.PacketByteBuf; public class MsgOtherPlayerCapabilities extends MsgPlayerCapabilities { @@ -19,7 +19,7 @@ public class MsgOtherPlayerCapabilities extends MsgPlayerCapabilities { } @Override - protected Pony getRecipient(PacketContext context) { + protected Pony getRecipient(PlayerEntity sender) { return Pony.of(MinecraftClient.getInstance().world.getPlayerByUuid(playerId)); } } diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerAbility.java b/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerAbility.java index d256a3a2..6275147c 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerAbility.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerAbility.java @@ -5,8 +5,8 @@ import com.minelittlepony.unicopia.ability.data.Hit; import com.minelittlepony.unicopia.entity.player.Pony; import com.minelittlepony.unicopia.ability.Abilities; -import net.fabricmc.fabric.api.network.PacketContext; import net.minecraft.util.Identifier; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.network.PacketByteBuf; public class MsgPlayerAbility implements Channel.Packet { @@ -33,8 +33,8 @@ public class MsgPlayerAbility implements Channel.Packet { } @Override - public void handle(PacketContext context) { - Pony player = Pony.of(context.getPlayer()); + public void handle(PlayerEntity sender) { + Pony player = Pony.of(sender); if (player == null) { return; } diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerCapabilities.java b/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerCapabilities.java index ba1b23ca..b4a40028 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerCapabilities.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgPlayerCapabilities.java @@ -11,8 +11,8 @@ import com.minelittlepony.unicopia.entity.player.Pony; import io.netty.buffer.ByteBufInputStream; import io.netty.buffer.ByteBufOutputStream; -import net.fabricmc.fabric.api.network.PacketContext; import net.minecraft.network.PacketByteBuf; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.NbtIo; @@ -51,8 +51,8 @@ public class MsgPlayerCapabilities implements Channel.Packet { } @Override - public void handle(PacketContext context) { - Pony player = getRecipient(context); + public void handle(PlayerEntity sender) { + Pony player = getRecipient(sender); if (player == null) { Unicopia.LOGGER.warn("Skipping capabilities for unknown player " + playerId.toString()); } @@ -63,7 +63,7 @@ public class MsgPlayerCapabilities implements Channel.Packet { } } - protected Pony getRecipient(PacketContext context) { - return Pony.of(context.getPlayer()); + protected Pony getRecipient(PlayerEntity sender) { + return Pony.of(sender); } } diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgRequestCapabilities.java b/src/main/java/com/minelittlepony/unicopia/network/MsgRequestCapabilities.java index bee1f5d0..16fb02fa 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgRequestCapabilities.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgRequestCapabilities.java @@ -4,7 +4,7 @@ import com.minelittlepony.unicopia.Race; import com.minelittlepony.unicopia.WorldTribeManager; import com.minelittlepony.unicopia.entity.player.Pony; -import net.fabricmc.fabric.api.network.PacketContext; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.network.PacketByteBuf; import net.minecraft.server.world.ServerWorld; @@ -26,15 +26,15 @@ public class MsgRequestCapabilities implements Channel.Packet { } @Override - public void handle(PacketContext context) { - Pony player = Pony.of(context.getPlayer()); + public void handle(PlayerEntity sender) { + Pony player = Pony.of(sender); Race worldDefaultRace = WorldTribeManager.forWorld((ServerWorld)player.getWorld()).getDefaultRace(); if (player.getSpecies().isDefault() || (player.getSpecies() == worldDefaultRace && !player.isSpeciesPersisted())) { - player.setSpecies(clientPreferredRace.isPermitted(context.getPlayer()) ? clientPreferredRace : worldDefaultRace); + player.setSpecies(clientPreferredRace.isPermitted(sender) ? clientPreferredRace : worldDefaultRace); } - Channel.SERVER_PLAYER_CAPABILITIES.send(context.getPlayer(), new MsgPlayerCapabilities(true, player)); + Channel.SERVER_PLAYER_CAPABILITIES.send(sender, new MsgPlayerCapabilities(true, player)); } } diff --git a/src/main/java/com/minelittlepony/unicopia/network/MsgSpawnProjectile.java b/src/main/java/com/minelittlepony/unicopia/network/MsgSpawnProjectile.java index f6fcb5ba..912820ae 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/MsgSpawnProjectile.java +++ b/src/main/java/com/minelittlepony/unicopia/network/MsgSpawnProjectile.java @@ -5,9 +5,9 @@ import java.util.Optional; import com.minelittlepony.unicopia.Owned; -import net.fabricmc.fabric.api.network.PacketContext; import net.minecraft.client.world.ClientWorld; import net.minecraft.entity.Entity; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.network.PacketByteBuf; import net.minecraft.network.packet.s2c.play.EntitySpawnS2CPacket; import net.minecraft.world.World; @@ -35,8 +35,8 @@ public class MsgSpawnProjectile extends EntitySpawnS2CPacket implements Channel. @SuppressWarnings("unchecked") @Override - public void handle(PacketContext context) { - World world = context.getPlayer().world; + public void handle(PlayerEntity sender) { + World world = sender.world; Entity entity = getEntityTypeId().create(world); entity.updateTrackedPosition(getX(), getY(), getZ());