mirror of
https://github.com/Sollace/Unicopia.git
synced 2024-11-30 08:18:00 +01:00
Switch to the new networking api
This commit is contained in:
parent
62ca1de596
commit
5d6206ffc5
7 changed files with 46 additions and 50 deletions
|
@ -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 <T extends Packet> SPacketType<T> clientToServer(Identifier id, Function<PacketByteBuf, T> factory) {
|
||||
ServerSidePacketRegistry.INSTANCE.register(id, (context, buffer) -> factory.apply(buffer).handleOnMain(context));
|
||||
return () -> id;
|
||||
}
|
||||
|
||||
static <T extends Packet> MPacketType<T> serverToClients(Identifier id, Function<PacketByteBuf, T> 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 <T extends Packet> CPacketType<T> serverToClient(Identifier id, Function<PacketByteBuf, T> 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 <T extends Packet> MPacketType<T> serverToClients(Identifier id, Function<PacketByteBuf, T> 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<T extends Packet> {
|
||||
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() {
|
||||
|
|
|
@ -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) -> {
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<T extends Hit> implements Channel.Packet {
|
||||
|
@ -33,8 +33,8 @@ public class MsgPlayerAbility<T extends Hit> 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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
|
|
Loading…
Reference in a new issue