mirror of
https://github.com/Sollace/Unicopia.git
synced 2025-02-01 19:46:42 +01:00
Fixed network error sending capabilities to the wrong players
This commit is contained in:
parent
a525a9127c
commit
804403ce25
4 changed files with 64 additions and 27 deletions
|
@ -22,7 +22,7 @@ import com.minelittlepony.unicopia.item.toxin.Toxicity;
|
||||||
import com.minelittlepony.unicopia.item.toxin.Toxin;
|
import com.minelittlepony.unicopia.item.toxin.Toxin;
|
||||||
import com.minelittlepony.unicopia.network.Channel;
|
import com.minelittlepony.unicopia.network.Channel;
|
||||||
import com.minelittlepony.unicopia.network.EffectSync;
|
import com.minelittlepony.unicopia.network.EffectSync;
|
||||||
import com.minelittlepony.unicopia.network.MsgPlayerCapabilities;
|
import com.minelittlepony.unicopia.network.MsgOtherPlayerCapabilities;
|
||||||
import com.minelittlepony.unicopia.network.MsgRequestCapabilities;
|
import com.minelittlepony.unicopia.network.MsgRequestCapabilities;
|
||||||
import com.minelittlepony.unicopia.network.Transmittable;
|
import com.minelittlepony.unicopia.network.Transmittable;
|
||||||
import com.minelittlepony.unicopia.util.Copieable;
|
import com.minelittlepony.unicopia.util.Copieable;
|
||||||
|
@ -149,7 +149,9 @@ public class Pony implements Caster<PlayerEntity>, Equine<PlayerEntity>, Transmi
|
||||||
dirty = false;
|
dirty = false;
|
||||||
|
|
||||||
if (entity instanceof ServerPlayerEntity) {
|
if (entity instanceof ServerPlayerEntity) {
|
||||||
Channel.BROADCAST_CAPABILITIES.send(new MsgPlayerCapabilities(full, this));
|
MsgOtherPlayerCapabilities packet = new MsgOtherPlayerCapabilities(full, this);
|
||||||
|
Channel.SERVER_PLAYER_CAPABILITIES.send(entity, packet);
|
||||||
|
Channel.SERVER_OTHER_PLAYER_CAPABILITIES.send(entity, packet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,13 +13,9 @@ import net.minecraft.network.PacketByteBuf;
|
||||||
public interface Channel {
|
public interface Channel {
|
||||||
|
|
||||||
SPacketType<MsgPlayerAbility<?>> CLIENT_PLAYER_ABILITY = clientToServer(new Identifier("unicopia", "player_ability"), MsgPlayerAbility::new);
|
SPacketType<MsgPlayerAbility<?>> CLIENT_PLAYER_ABILITY = clientToServer(new Identifier("unicopia", "player_ability"), MsgPlayerAbility::new);
|
||||||
|
|
||||||
SPacketType<MsgRequestCapabilities> CLIENT_REQUEST_CAPABILITIES = clientToServer(new Identifier("unicopia", "request_capabilities"), MsgRequestCapabilities::new);
|
SPacketType<MsgRequestCapabilities> CLIENT_REQUEST_CAPABILITIES = clientToServer(new Identifier("unicopia", "request_capabilities"), MsgRequestCapabilities::new);
|
||||||
|
|
||||||
CPacketType<MsgPlayerCapabilities> SERVER_PLAYER_CAPABILITIES = serverToClient(new Identifier("unicopia", "player_capabilities"), MsgPlayerCapabilities::new);
|
CPacketType<MsgPlayerCapabilities> SERVER_PLAYER_CAPABILITIES = serverToClient(new Identifier("unicopia", "player_capabilities"), MsgPlayerCapabilities::new);
|
||||||
|
MPacketType<MsgOtherPlayerCapabilities> SERVER_OTHER_PLAYER_CAPABILITIES = serverToClients(new Identifier("unicopia", "other_player_capabilities"), MsgOtherPlayerCapabilities::new);
|
||||||
SPacketType<MsgPlayerCapabilities> BROADCAST_CAPABILITIES = broadcast(SERVER_PLAYER_CAPABILITIES, MsgPlayerCapabilities::new);
|
|
||||||
|
|
||||||
CPacketType<MsgSpawnProjectile> SERVER_SPAWN_PROJECTILE = serverToClient(new Identifier("unicopia", "projectile_entity"), MsgSpawnProjectile::new);
|
CPacketType<MsgSpawnProjectile> SERVER_SPAWN_PROJECTILE = serverToClient(new Identifier("unicopia", "projectile_entity"), MsgSpawnProjectile::new);
|
||||||
|
|
||||||
static void bootstrap() { }
|
static void bootstrap() { }
|
||||||
|
@ -29,19 +25,8 @@ public interface Channel {
|
||||||
return () -> id;
|
return () -> id;
|
||||||
}
|
}
|
||||||
|
|
||||||
static <T extends Packet> SPacketType<T> broadcast(CPacketType<T> redirect, Function<PacketByteBuf, T> factory) {
|
static <T extends Packet> MPacketType<T> serverToClients(Identifier id, Function<PacketByteBuf, T> factory) {
|
||||||
Identifier id = new Identifier(redirect.getId().getNamespace(), "broadcast_" + redirect.getId().getPath());
|
ClientSidePacketRegistry.INSTANCE.register(id, (context, buffer) -> factory.apply(buffer).handleOnMain(context));
|
||||||
ServerSidePacketRegistry.INSTANCE.register(id, (context, buffer) -> {
|
|
||||||
PlayerEntity sender = context.getPlayer();
|
|
||||||
|
|
||||||
T p = factory.apply(buffer);
|
|
||||||
p.handleOnMain(context);
|
|
||||||
sender.world.getPlayers().forEach(player -> {
|
|
||||||
if (player != null) {
|
|
||||||
redirect.send(player, p);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
return () -> id;
|
return () -> id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,6 +35,22 @@ public interface Channel {
|
||||||
return () -> id;
|
return () -> id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface MPacketType<T extends Packet> {
|
||||||
|
Identifier getId();
|
||||||
|
|
||||||
|
default void send(PlayerEntity sender, T packet) {
|
||||||
|
sender.world.getPlayers().forEach(player -> {
|
||||||
|
if (player != null) {
|
||||||
|
ServerSidePacketRegistry.INSTANCE.sendToPlayer(player, getId(), packet.toBuffer());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
default net.minecraft.network.Packet<?> toPacket(T packet) {
|
||||||
|
return ServerSidePacketRegistry.INSTANCE.toPacket(getId(), packet.toBuffer());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
interface CPacketType<T extends Packet> {
|
interface CPacketType<T extends Packet> {
|
||||||
Identifier getId();
|
Identifier getId();
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
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.network.PacketByteBuf;
|
||||||
|
|
||||||
|
public class MsgOtherPlayerCapabilities extends MsgPlayerCapabilities {
|
||||||
|
|
||||||
|
MsgOtherPlayerCapabilities(PacketByteBuf buffer) {
|
||||||
|
super(buffer);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public MsgOtherPlayerCapabilities(boolean full, Pony player) {
|
||||||
|
super(full, player);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Pony getRecipient(PacketContext context) {
|
||||||
|
return Pony.of(MinecraftClient.getInstance().world.getPlayerByUuid(playerId));
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,24 +3,29 @@ package com.minelittlepony.unicopia.network;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
import com.minelittlepony.unicopia.Race;
|
import com.minelittlepony.unicopia.Race;
|
||||||
|
import com.minelittlepony.unicopia.Unicopia;
|
||||||
import com.minelittlepony.unicopia.entity.player.Pony;
|
import com.minelittlepony.unicopia.entity.player.Pony;
|
||||||
|
|
||||||
import io.netty.buffer.ByteBufInputStream;
|
import io.netty.buffer.ByteBufInputStream;
|
||||||
import io.netty.buffer.ByteBufOutputStream;
|
import io.netty.buffer.ByteBufOutputStream;
|
||||||
import net.fabricmc.fabric.api.network.PacketContext;
|
import net.fabricmc.fabric.api.network.PacketContext;
|
||||||
import net.minecraft.entity.player.PlayerEntity;
|
|
||||||
import net.minecraft.network.PacketByteBuf;
|
import net.minecraft.network.PacketByteBuf;
|
||||||
import net.minecraft.nbt.CompoundTag;
|
import net.minecraft.nbt.CompoundTag;
|
||||||
import net.minecraft.nbt.NbtIo;
|
import net.minecraft.nbt.NbtIo;
|
||||||
|
|
||||||
public class MsgPlayerCapabilities implements Channel.Packet {
|
public class MsgPlayerCapabilities implements Channel.Packet {
|
||||||
|
|
||||||
|
protected final UUID playerId;
|
||||||
|
|
||||||
private final Race newRace;
|
private final Race newRace;
|
||||||
|
|
||||||
private final CompoundTag compoundTag;
|
private final CompoundTag compoundTag;
|
||||||
|
|
||||||
MsgPlayerCapabilities(PacketByteBuf buffer) {
|
MsgPlayerCapabilities(PacketByteBuf buffer) {
|
||||||
|
playerId = buffer.readUuid();
|
||||||
newRace = Race.values()[buffer.readInt()];
|
newRace = Race.values()[buffer.readInt()];
|
||||||
try (InputStream in = new ByteBufInputStream(buffer)) {
|
try (InputStream in = new ByteBufInputStream(buffer)) {
|
||||||
compoundTag = NbtIo.readCompressed(in);
|
compoundTag = NbtIo.readCompressed(in);
|
||||||
|
@ -29,18 +34,15 @@ public class MsgPlayerCapabilities implements Channel.Packet {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public MsgPlayerCapabilities(Race race, PlayerEntity player) {
|
|
||||||
newRace = race;
|
|
||||||
compoundTag = new CompoundTag();
|
|
||||||
}
|
|
||||||
|
|
||||||
public MsgPlayerCapabilities(boolean full, Pony player) {
|
public MsgPlayerCapabilities(boolean full, Pony player) {
|
||||||
|
playerId = player.getOwner().getUuid();
|
||||||
newRace = player.getSpecies();
|
newRace = player.getSpecies();
|
||||||
compoundTag = full ? player.toNBT() : new CompoundTag();
|
compoundTag = full ? player.toNBT() : new CompoundTag();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void toBuffer(PacketByteBuf buffer) {
|
public void toBuffer(PacketByteBuf buffer) {
|
||||||
|
buffer.writeUuid(playerId);
|
||||||
buffer.writeInt(newRace.ordinal());
|
buffer.writeInt(newRace.ordinal());
|
||||||
try (OutputStream out = new ByteBufOutputStream(buffer)) {
|
try (OutputStream out = new ByteBufOutputStream(buffer)) {
|
||||||
NbtIo.writeCompressed(compoundTag, out);
|
NbtIo.writeCompressed(compoundTag, out);
|
||||||
|
@ -50,11 +52,18 @@ public class MsgPlayerCapabilities implements Channel.Packet {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handle(PacketContext context) {
|
public void handle(PacketContext context) {
|
||||||
Pony player = Pony.of(context.getPlayer());
|
Pony player = getRecipient(context);
|
||||||
|
if (player == null) {
|
||||||
|
Unicopia.LOGGER.warn("Skipping capabilities for unknown player " + playerId.toString());
|
||||||
|
}
|
||||||
if (compoundTag.isEmpty()) {
|
if (compoundTag.isEmpty()) {
|
||||||
player.setSpecies(newRace);
|
player.setSpecies(newRace);
|
||||||
} else {
|
} else {
|
||||||
player.fromNBT(compoundTag);
|
player.fromNBT(compoundTag);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected Pony getRecipient(PacketContext context) {
|
||||||
|
return Pony.of(context.getPlayer());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue