Handle some additional cases for non-standard modded entities

This commit is contained in:
Moulberry committed 2024-11-14 21:10:23 +08:00
1 parent 51986d4af5
commit 2c38d27e5d
5 files changed
+64 -15

No files matched your search

@@ -24,6 +24,7 @@ import com.moulberry.flashback.keyframe.types.SpeedKeyframeType;
import com.moulberry.flashback.keyframe.types.TimeOfDayKeyframeType;
import com.moulberry.flashback.keyframe.types.TimelapseKeyframeType;
import com.moulberry.flashback.packet.FlashbackAccurateEntityPosition;
import com.moulberry.flashback.packet.FlashbackClearEntities;
import com.moulberry.flashback.packet.FlashbackClearParticles;
import com.moulberry.flashback.packet.FinishedServerTick;
import com.moulberry.flashback.packet.FlashbackForceClientTick;
@@ -165,6 +166,7 @@ public class Flashback implements ModInitializer, ClientModInitializer {
PayloadTypeRegistry.playS2C().register(FlashbackForceClientTick.TYPE, StreamCodec.unit(FlashbackForceClientTick.INSTANCE));
PayloadTypeRegistry.playS2C().register(FlashbackClearParticles.TYPE, StreamCodec.unit(FlashbackClearParticles.INSTANCE));
PayloadTypeRegistry.playS2C().register(FlashbackClearEntities.TYPE, StreamCodec.unit(FlashbackClearEntities.INSTANCE));
PayloadTypeRegistry.playS2C().register(FlashbackInstantlyLerp.TYPE, StreamCodec.unit(FlashbackInstantlyLerp.INSTANCE));
PayloadTypeRegistry.playS2C().register(FlashbackRemoteSelectHotbarSlot.TYPE, FlashbackRemoteSelectHotbarSlot.STREAM_CODEC);
PayloadTypeRegistry.playS2C().register(FlashbackRemoteExperience.TYPE, FlashbackRemoteExperience.STREAM_CODEC);
@@ -243,6 +245,16 @@ public class Flashback implements ModInitializer, ClientModInitializer {
}
});
ClientPlayNetworking.registerGlobalReceiver(FlashbackClearEntities.TYPE, (payload, context) -> {
if (Flashback.isInReplay()) {
for (Entity entity : Minecraft.getInstance().level.entitiesForRendering()) {
if (entity != null && !(entity instanceof Player)) {
entity.discard();
}
}
}
});
ClientPlayNetworking.registerGlobalReceiver(FlashbackInstantlyLerp.TYPE, (payload, context) -> {
if (Flashback.isInReplay()) {
for (Entity entity : Minecraft.getInstance().level.entitiesForRendering()) {
@@ -14,9 +14,6 @@ import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
public class ReplayReader {
private final FriendlyByteBuf friendlyByteBuf;
@@ -0,0 +1,15 @@
package com.moulberry.flashback.packet;
import com.moulberry.flashback.Flashback;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
public class FlashbackClearEntities implements CustomPacketPayload {
public static final Type<FlashbackClearEntities> TYPE = new Type<>(Flashback.createResourceLocation("clear_entities"));
public static final FlashbackClearEntities INSTANCE = new FlashbackClearEntities();
@Override
public Type<? extends CustomPacketPayload> type() {
return TYPE;
}
}
@@ -14,6 +14,8 @@ import com.moulberry.flashback.packet.FlashbackRemoteSetSlot;
import io.netty.channel.embedded.EmbeddedChannel;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.client.multiplayer.PlayerInfo;
import net.minecraft.core.BlockPos;
@@ -111,11 +113,11 @@ public class ReplayGamePacketHandler implements ClientGamePacketListener {
private void forward(Entity entity, Packet<?> packet) {
if (entity == null) {
return;
this.forward(packet);
} else {
ServerChunkCache serverChunkCache = this.level().getChunkSource();
serverChunkCache.broadcast(entity, packet);
}
ServerChunkCache serverChunkCache = this.level().getChunkSource();
serverChunkCache.broadcast(entity, packet);
}
private void setBlockState(ServerLevel level, BlockPos blockPos, BlockState blockState) {
@@ -473,11 +475,20 @@ public class ReplayGamePacketHandler implements ClientGamePacketListener {
@Override
public void handleEntityLinkPacket(ClientboundSetEntityLinkPacket clientboundSetEntityLinkPacket) {
Entity source = this.level().getEntity(clientboundSetEntityLinkPacket.getSourceId());
if (source == null) {
forward(clientboundSetEntityLinkPacket);
return;
}
if (source instanceof Leashable leashable) {
if (clientboundSetEntityLinkPacket.getDestId() == 0) {
leashable.setLeashedTo(null, true);
} else {
Entity dest = this.level().getEntity(clientboundSetEntityLinkPacket.getDestId());
if (dest == null) {
forward(clientboundSetEntityLinkPacket);
return;
}
leashable.setLeashedTo(dest, true);
}
}
@@ -887,22 +898,28 @@ public class ReplayGamePacketHandler implements ClientGamePacketListener {
@Override
public void handleRemoveEntities(ClientboundRemoveEntitiesPacket clientboundRemoveEntitiesPacket) {
IntList forwardRemoveUnknown = new IntArrayList();
clientboundRemoveEntitiesPacket.getEntityIds().forEach(i -> {
Entity entity = this.level().getEntity(i);
if (entity != null) {
if (entity instanceof ServerPlayer serverPlayer) {
serverPlayer.connection.disconnect(Component.empty());
} else {
entity.discard();
}
if (entity == null) {
forwardRemoveUnknown.add(i);
} else if (entity instanceof ServerPlayer serverPlayer) {
serverPlayer.connection.disconnect(Component.empty());
} else {
entity.discard();
}
});
if (!forwardRemoveUnknown.isEmpty()) {
forward(new ClientboundRemoveEntitiesPacket(forwardRemoveUnknown));
}
}
@Override
public void handleRemoveMobEffect(ClientboundRemoveMobEffectPacket clientboundRemoveMobEffectPacket) {
Entity entity = clientboundRemoveMobEffectPacket.getEntity(this.level());
if (entity instanceof LivingEntity livingEntity) {
Entity entity = this.getEntityOrPending(clientboundRemoveMobEffectPacket.entityId());
if (entity == null) {
forward(clientboundRemoveMobEffectPacket);
} else if (entity instanceof LivingEntity livingEntity) {
livingEntity.removeEffect(clientboundRemoveMobEffectPacket.effect());
}
}
@@ -1141,6 +1158,7 @@ public class ReplayGamePacketHandler implements ClientGamePacketListener {
public void handleSoundEntityEvent(ClientboundSoundEntityPacket clientboundSoundEntityPacket) {
Entity entity = this.level().getEntity(clientboundSoundEntityPacket.getId());
if (entity == null) {
forward(clientboundSoundEntityPacket);
return;
}
@@ -10,6 +10,7 @@ import com.moulberry.flashback.ext.LevelChunkExt;
import com.moulberry.flashback.TempFolderProvider;
import com.moulberry.flashback.keyframe.handler.ReplayServerKeyframeHandler;
import com.moulberry.flashback.packet.FlashbackAccurateEntityPosition;
import com.moulberry.flashback.packet.FlashbackClearEntities;
import com.moulberry.flashback.packet.FlashbackClearParticles;
import com.moulberry.flashback.packet.FlashbackForceClientTick;
import com.moulberry.flashback.packet.FlashbackInstantlyLerp;
@@ -727,6 +728,12 @@ public class ReplayServer extends IntegratedServer {
}
}
serverLevel.setDayTime(0);
for (ServerPlayer player : serverLevel.players()) {
if (player instanceof ReplayPlayer replayPlayer) {
ServerPlayNetworking.send(replayPlayer, FlashbackClearEntities.INSTANCE);
}
}
}
@Override