Fixed pony skin data not being reliably sent to servers

This commit is contained in:
Sollace 2023-09-28 14:38:39 +01:00
parent 9f280f79eb
commit 26aa16d684
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB
4 changed files with 45 additions and 34 deletions

View file

@ -8,7 +8,6 @@ import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
@ -29,7 +28,6 @@ public class Channel {
public static void bootstrap() { public static void bootstrap() {
ClientLoginConnectionEvents.INIT.register((handler, client) -> { ClientLoginConnectionEvents.INIT.register((handler, client) -> {
registered = false; registered = false;
LOGGER.info("Resetting registered flag");
}); });
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> { ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
LOGGER.info("Sending consent packet to " + handler.getPlayer().getName().getString()); LOGGER.info("Sending consent packet to " + handler.getPlayer().getName().getString());
@ -38,12 +36,14 @@ public class Channel {
}); });
ClientPlayNetworking.registerGlobalReceiver(REQUEST_PONY_DATA, (client, handler, ignored, sender) -> { ClientPlayNetworking.registerGlobalReceiver(REQUEST_PONY_DATA, (client, handler, ignored, sender) -> {
registered = true;
if (client.player != null) { if (client.player != null) {
Pony pony = Pony.getManager().getPony(client.player); Pony pony = Pony.getManager().getPony(client.player);
registered = true;
LOGGER.info("Server has just consented"); LOGGER.info("Server has just consented");
sender.sendPacket(CLIENT_PONY_DATA, MsgPonyData.write(pony.metadata(), PacketByteBufs.create())); sender.sendPacket(CLIENT_PONY_DATA, MsgPonyData.write(pony.metadata(), PacketByteBufs.create()));
} else {
LOGGER.info("Server has just consented but the client player was not set");
} }
}); });
ServerPlayNetworking.registerGlobalReceiver(CLIENT_PONY_DATA, (server, player, ignore, buffer, ignore2) -> { ServerPlayNetworking.registerGlobalReceiver(CLIENT_PONY_DATA, (server, player, ignore, buffer, ignore2) -> {
@ -54,20 +54,19 @@ public class Channel {
}); });
} }
public static void broadcastPonyData(PonyData packet) { public static boolean broadcastPonyData(PonyData packet) {
if (FabricLoader.getInstance().getEnvironmentType() != EnvType.CLIENT) { if (FabricLoader.getInstance().getEnvironmentType() != EnvType.CLIENT) {
throw new RuntimeException("Client packet send called by the server"); throw new RuntimeException("Client packet send called by the server");
} }
if (!registered) { if (!registered) {
if (MinecraftClient.getInstance().isInSingleplayer() || MinecraftClient.getInstance().isIntegratedServerRunning()) { LOGGER.info("Skipping network packet as the server has not consented");
LOGGER.info("Sending pony skin data over as we are either in single-player or lan"); return false;
} else { } else {
LOGGER.info("Skipping network packet as the server has not consented"); LOGGER.info("Sending pony data to server for player");
return;
}
} }
ClientPlayNetworking.send(CLIENT_PONY_DATA, MsgPonyData.write(packet, PacketByteBufs.create())); ClientPlayNetworking.send(CLIENT_PONY_DATA, MsgPonyData.write(packet, PacketByteBufs.create()));
return true;
} }
} }

View file

@ -4,7 +4,6 @@ import net.minecraft.client.world.ClientWorld;
import com.minelittlepony.api.model.PreviewModel; import com.minelittlepony.api.model.PreviewModel;
import com.minelittlepony.api.pony.*; import com.minelittlepony.api.pony.*;
import com.minelittlepony.client.render.EquineRenderManager;
import com.minelittlepony.hdskins.client.gui.player.*; import com.minelittlepony.hdskins.client.gui.player.*;
import com.minelittlepony.hdskins.client.gui.player.skins.PlayerSkins; import com.minelittlepony.hdskins.client.gui.player.skins.PlayerSkins;
@ -13,18 +12,13 @@ import java.util.UUID;
/** /**
* Dummy model used for the skin uploading screen. * Dummy model used for the skin uploading screen.
*/ */
class DummyPony extends DummyPlayer implements PreviewModel, PonyManager.ForcedPony, EquineRenderManager.RegistrationHandler { class DummyPony extends DummyPlayer implements PreviewModel, PonyManager.ForcedPony {
public DummyPony(ClientWorld world, PlayerSkins<?> textures) { public DummyPony(ClientWorld world, PlayerSkins<?> textures) {
super(world, textures); super(world, textures);
setUuid(UUID.randomUUID()); // uuid must be random so animations aren't linked between the two previews setUuid(UUID.randomUUID()); // uuid must be random so animations aren't linked between the two previews
} }
@Override
public boolean shouldUpdateRegistration(Pony pony) {
return false;
}
@Override @Override
public boolean forceSeapony() { public boolean forceSeapony() {
return getTextures().getPosture().getActiveSkinType() == MineLPHDSkins.seaponySkinType; return getTextures().getPosture().getActiveSkinType() == MineLPHDSkins.seaponySkinType;

View file

@ -9,7 +9,6 @@ import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityDimensions; import net.minecraft.entity.EntityDimensions;
import net.minecraft.entity.EntityPose; import net.minecraft.entity.EntityPose;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Inject;
@ -20,8 +19,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
abstract class MixinClientPlayerEntity extends AbstractClientPlayerEntity implements EquineRenderManager.RegistrationHandler { abstract class MixinClientPlayerEntity extends AbstractClientPlayerEntity implements EquineRenderManager.RegistrationHandler {
public MixinClientPlayerEntity() { super(null, null); } public MixinClientPlayerEntity() { super(null, null); }
@Nullable private final EquineRenderManager.SyncedPony syncedPony = new EquineRenderManager.SyncedPony();
private Pony pony;
@Inject(method = "startRiding(Lnet/minecraft/entity/Entity;Z)Z", at = @At("RETURN")) @Inject(method = "startRiding(Lnet/minecraft/entity/Entity;Z)Z", at = @At("RETURN"))
private void onStartRiding(Entity entity, boolean bl, CallbackInfoReturnable<Boolean> info) { private void onStartRiding(Entity entity, boolean bl, CallbackInfoReturnable<Boolean> info) {
@ -34,12 +32,8 @@ abstract class MixinClientPlayerEntity extends AbstractClientPlayerEntity implem
} }
@Override @Override
public boolean shouldUpdateRegistration(Pony pony) { public EquineRenderManager.SyncedPony getSyncedPony() {
if (this.pony != pony && (this.pony == null || this.pony.metadata().compareTo(pony.metadata()) != 0)) { return syncedPony;
this.pony = pony.immutableCopy();
return true;
}
return false;
} }
@Override @Override

View file

@ -22,6 +22,8 @@ import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity; import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import org.jetbrains.annotations.Nullable;
public class EquineRenderManager<T extends LivingEntity, M extends EntityModel<T> & PonyModel<T>> { public class EquineRenderManager<T extends LivingEntity, M extends EntityModel<T> & PonyModel<T>> {
private ModelWrapper<T, M> playerModel; private ModelWrapper<T, M> playerModel;
@ -120,16 +122,31 @@ public class EquineRenderManager<T extends LivingEntity, M extends EntityModel<T
Pony pony = renderer.getEntityPony(entity); Pony pony = renderer.getEntityPony(entity);
playerModel.applyMetadata(pony.metadata()); playerModel.applyMetadata(pony.metadata());
if (pony.hasMetadata() && entity instanceof RegistrationHandler && ((RegistrationHandler)entity).shouldUpdateRegistration(pony)) { if (entity instanceof PlayerEntity player && entity instanceof RegistrationHandler handler) {
entity.calculateDimensions(); SyncedPony synced = handler.getSyncedPony();
boolean changed = pony.compareTo(synced.lastRenderedPony) != 0;
PlayerEntity clientPlayer = MinecraftClient.getInstance().player; if (changed) {
if (clientPlayer != null) { synced.lastRenderedPony = pony;
if (Objects.equals(entity, clientPlayer) || Objects.equals(((PlayerEntity)entity).getGameProfile(), clientPlayer.getGameProfile())) { player.calculateDimensions();
Channel.broadcastPonyData(pony.metadata()); }
if (!(player instanceof PreviewModel)) {
@Nullable
PlayerEntity clientPlayer = MinecraftClient.getInstance().player;
if (pony.compareTo(synced.lastTransmittedPony) != 0) {
if (clientPlayer != null && (Objects.equals(player, clientPlayer) || Objects.equals(player.getGameProfile(), clientPlayer.getGameProfile()))) {
if (Channel.broadcastPonyData(pony.metadata())) {
synced.lastTransmittedPony = pony;
}
}
}
if (changed) {
PonyDataCallback.EVENT.invoker().onPonyDataAvailable(player, pony.metadata(), EnvType.CLIENT);
} }
} }
PonyDataCallback.EVENT.invoker().onPonyDataAvailable((PlayerEntity)entity, pony.metadata(), EnvType.CLIENT);
} }
getModel().updateLivingState(entity, pony, mode); getModel().updateLivingState(entity, pony, mode);
@ -169,6 +186,13 @@ public class EquineRenderManager<T extends LivingEntity, M extends EntityModel<T
} }
public interface RegistrationHandler { public interface RegistrationHandler {
boolean shouldUpdateRegistration(Pony pony); SyncedPony getSyncedPony();
}
public static class SyncedPony {
@Nullable
private Pony lastRenderedPony;
@Nullable
private Pony lastTransmittedPony;
} }
} }