This commit is contained in:
Moulberry committed 2024-09-01 18:08:55 +08:00
1 parent fcf1b4168f
commit 2fbe1f5480
46 files changed
+1846 -132

No files matched your search

@@ -7,12 +7,15 @@ import com.moulberry.flashback.exception.UnsupportedPacketException;
import com.moulberry.flashback.ext.LevelChunkExt;
import com.moulberry.flashback.ext.ServerLevelExt;
import com.moulberry.flashback.ext.ThreadedLevelLightEngineExt;
import com.moulberry.flashback.packet.FlashbackRemoteExperience;
import com.moulberry.flashback.packet.FlashbackRemoteFoodData;
import com.moulberry.flashback.packet.FlashbackRemoteSelectHotbarSlot;
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 net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.client.multiplayer.PlayerInfo;
import net.minecraft.client.player.KeyboardInput;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.Holder;
@@ -48,12 +51,10 @@ import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
import net.minecraft.world.entity.ai.attributes.AttributeMap;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.decoration.HangingEntity;
import net.minecraft.world.entity.decoration.ItemFrame;
import net.minecraft.world.entity.decoration.Painting;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.npc.Villager;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.GameRules;
import net.minecraft.world.level.GameType;
@@ -221,6 +222,12 @@ public class ReplayGamePacketHandler implements ClientGamePacketListener {
}
}
for (ReplayPlayer replayViewer : this.replayServer.getReplayViewers()) {
if (Objects.equals(replayViewer.lastFirstPersonDataUUID, addEntityPacket.getUUID())) {
replayViewer.lastFirstPersonDataUUID = null;
}
}
Connection connection = new Connection(PacketFlow.SERVERBOUND);
EmbeddedChannel embeddedChannel = new EmbeddedChannel(connection);
serverPlayer.recreateFromPacket(addEntityPacket);
@@ -408,7 +415,23 @@ public class ReplayGamePacketHandler implements ClientGamePacketListener {
@Override
public void handleContainerSetSlot(ClientboundContainerSetSlotPacket clientboundContainerSetSlotPacket) {
throw new UnsupportedPacketException(clientboundContainerSetSlotPacket);
Entity entity = this.level().getEntity(this.localPlayerId);
if (!(entity instanceof Player player)) {
return;
}
if (clientboundContainerSetSlotPacket.getContainerId() == -2) {
int slot = clientboundContainerSetSlotPacket.getSlot();
ItemStack itemStack = clientboundContainerSetSlotPacket.getItem();
player.getInventory().setItem(slot, itemStack);
for (ReplayPlayer replayViewer : this.replayServer.getReplayViewers()) {
if (Objects.equals(replayViewer.lastFirstPersonDataUUID, player.getUUID())) {
replayViewer.lastFirstPersonHotbarItems[slot] = itemStack.copy();
ServerPlayNetworking.send(replayViewer, new FlashbackRemoteSetSlot(player.getId(), slot, itemStack.copy()));
}
}
}
}
@Override
@@ -896,7 +919,29 @@ public class ReplayGamePacketHandler implements ClientGamePacketListener {
@Override
public void handleSetCarriedItem(ClientboundSetCarriedItemPacket clientboundSetCarriedItemPacket) {
throw new UnsupportedPacketException(clientboundSetCarriedItemPacket);
if (!Inventory.isHotbarSlot(clientboundSetCarriedItemPacket.getSlot())) {
return;
}
Entity entity = this.level().getEntity(this.localPlayerId);
if (!(entity instanceof Player player)) {
return;
}
Inventory inventory = player.getInventory();
if (inventory.selected == clientboundSetCarriedItemPacket.getSlot()) {
return;
}
inventory.selected = clientboundSetCarriedItemPacket.getSlot();
for (ReplayPlayer replayViewer : this.replayServer.getReplayViewers()) {
if (Objects.equals(replayViewer.lastFirstPersonDataUUID, player.getUUID())) {
replayViewer.lastFirstPersonSelectedSlot = inventory.selected;
ServerPlayNetworking.send(replayViewer, new FlashbackRemoteSelectHotbarSlot(player.getId(), inventory.selected));
}
}
}
@Override
@@ -952,6 +997,16 @@ public class ReplayGamePacketHandler implements ClientGamePacketListener {
player.experienceProgress = clientboundSetExperiencePacket.getExperienceProgress();
player.totalExperience = clientboundSetExperiencePacket.getTotalExperience();
player.experienceLevel = clientboundSetExperiencePacket.getExperienceLevel();
for (ReplayPlayer replayViewer : this.replayServer.getReplayViewers()) {
if (Objects.equals(replayViewer.lastFirstPersonDataUUID, player.getUUID())) {
replayViewer.lastFirstPersonExperienceProgress = player.experienceProgress;
replayViewer.lastFirstPersonTotalExperience = player.totalExperience;
replayViewer.lastFirstPersonExperienceLevel = player.experienceLevel;
ServerPlayNetworking.send(replayViewer, new FlashbackRemoteExperience(player.getId(), player.experienceProgress,
player.totalExperience, player.experienceLevel));
}
}
}
}
@@ -960,8 +1015,20 @@ public class ReplayGamePacketHandler implements ClientGamePacketListener {
Entity entity = this.level().getEntity(this.localPlayerId);
if (entity instanceof Player player) {
player.setHealth(clientboundSetHealthPacket.getHealth());
player.getFoodData().setFoodLevel(clientboundSetHealthPacket.getFood());
player.getFoodData().setSaturation(clientboundSetHealthPacket.getSaturation());
int food = clientboundSetHealthPacket.getFood();
float saturation = clientboundSetHealthPacket.getSaturation();
player.getFoodData().setFoodLevel(food);
player.getFoodData().setSaturation(saturation);
for (ReplayPlayer replayViewer : this.replayServer.getReplayViewers()) {
if (Objects.equals(replayViewer.lastFirstPersonDataUUID, player.getUUID())) {
replayViewer.lastFirstPersonFoodLevel = food;
replayViewer.lastFirstPersonSaturationLevel = saturation;
ServerPlayNetworking.send(replayViewer, new FlashbackRemoteFoodData(player.getId(), food, saturation));
}
}
}
}
@@ -1414,7 +1481,6 @@ public class ReplayGamePacketHandler implements ClientGamePacketListener {
@Override
public void handleCustomPayload(ClientboundCustomPayloadPacket clientboundCustomPayloadPacket) {
// todo: handle some custom packets here?
forward(clientboundCustomPayloadPacket);
}
@@ -9,6 +9,7 @@ import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.stats.Stat;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.RecipeHolder;
import net.minecraft.world.level.biome.BiomeManager;
@@ -18,6 +19,16 @@ import java.util.UUID;
public class ReplayPlayer extends ServerPlayer {
public boolean followLocalPlayerNextTick = false;
public UUID spectatingUuid = null;
public int spectatingUuidTickCount = 0;
public UUID lastFirstPersonDataUUID = null;
public int lastFirstPersonSelectedSlot = -1;
public ItemStack[] lastFirstPersonHotbarItems = new ItemStack[9];
public float lastFirstPersonExperienceProgress = 0.0f;
public int lastFirstPersonTotalExperience = 0;
public int lastFirstPersonExperienceLevel = 0;
public int lastFirstPersonFoodLevel = 0;
public float lastFirstPersonSaturationLevel = 0;
public ReplayPlayer(MinecraftServer minecraftServer, ServerLevel serverLevel, GameProfile gameProfile, ClientInformation clientInformation) {
super(minecraftServer, serverLevel, gameProfile, clientInformation);
@@ -4,12 +4,18 @@ import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.mojang.authlib.GameProfile;
import com.moulberry.flashback.Flashback;
import com.moulberry.flashback.action.PositionAndAngle;
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.FlashbackClearParticles;
import com.moulberry.flashback.packet.FlashbackForceClientTick;
import com.moulberry.flashback.packet.FlashbackInstantlyLerp;
import com.moulberry.flashback.packet.FlashbackRemoteExperience;
import com.moulberry.flashback.packet.FlashbackRemoteFoodData;
import com.moulberry.flashback.packet.FlashbackRemoteSelectHotbarSlot;
import com.moulberry.flashback.packet.FlashbackRemoteSetSlot;
import com.moulberry.flashback.state.EditorState;
import com.moulberry.flashback.state.EditorStateManager;
import com.moulberry.flashback.ext.MinecraftExt;
@@ -30,6 +36,7 @@ import net.minecraft.Util;
import net.minecraft.client.Minecraft;
import net.minecraft.client.server.IntegratedServer;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.codec.StreamCodec;
@@ -53,8 +60,11 @@ import net.minecraft.server.players.PlayerList;
import net.minecraft.world.BossEvent;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.flag.FeatureFlagSet;
import net.minecraft.world.food.FoodData;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.WorldDataConfiguration;
@@ -75,9 +85,11 @@ import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.TreeMap;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BooleanSupplier;
@@ -108,7 +120,7 @@ public class ReplayServer extends IntegratedServer {
public boolean followLocalPlayerNextTickIfWrongDimension = false;
public boolean isProcessingSnapshot = false;
private boolean processedSnapshot = false;
private volatile boolean fastForwarding = false;
public volatile boolean fastForwarding = false;
private int printFailedDecodePacketCount = 8;
@@ -409,6 +421,18 @@ public class ReplayServer extends IntegratedServer {
}
}
public float getPartialServerTick() {
if (this.replayPaused || this.isPaused()) {
return 1.0f;
} else {
long currentNanos = Util.getNanos();
long nanosPerTick = this.tickRateManager().nanosecondsPerTick();
double partial = (currentNanos - this.lastTickTimeNanos) / (double) nanosPerTick;
return Math.max(0, Math.min(1, (float) partial));
}
}
public int getTotalReplayTicks() {
return this.totalTicks;
}
@@ -546,6 +570,13 @@ public class ReplayServer extends IntegratedServer {
this.gamePacketHandler.handleCreateLocalPlayer(friendlyByteBuf);
}
public void handleAccuratePlayerPosition(RegistryFriendlyByteBuf friendlyByteBuf) {
var packet = FlashbackAccurateEntityPosition.STREAM_CODEC.decode(friendlyByteBuf);
for (ReplayPlayer replayViewer : this.replayViewers) {
ServerPlayNetworking.send(replayViewer, packet);
}
}
public void handleMoveEntities(RegistryFriendlyByteBuf friendlyByteBuf) {
this.gamePacketHandler.flushPendingEntities();
this.configurationPacketHandler.flushPendingConfiguration();
@@ -622,7 +653,8 @@ public class ReplayServer extends IntegratedServer {
public void clearLevel(ServerLevel serverLevel) {
for (ServerPlayer player : new ArrayList<>(serverLevel.players())) {
if (player instanceof ReplayPlayer) {
if (player instanceof ReplayPlayer replayPlayer) {
replayPlayer.lastFirstPersonDataUUID = null;
continue;
}
player.connection.disconnect(Component.empty());
@@ -664,15 +696,30 @@ public class ReplayServer extends IntegratedServer {
replayReader.handleSnapshot(this);
}
// this.accurateEntityPositions = this.pendingAccurateEntityPositions;
// this.pendingAccurateEntityPositions = new ConcurrentHashMap<>();
this.lastReplayTick = this.currentTick;
this.lastTickTimeNanos = this.nextTickTimeNanos - this.tickRateManager().nanosecondsPerTick();
// Update list of replay viewers
this.replayViewers.clear();
for (ServerPlayer player : this.getPlayerList().getPlayers()) {
if (player instanceof ReplayPlayer replayPlayer) {
Entity cameraEntity = replayPlayer.getCamera();
replayPlayer.spectatingUuid = cameraEntity == null || cameraEntity == replayPlayer ? null : cameraEntity.getUUID();
if (replayPlayer.isShiftKeyDown()) {
replayPlayer.spectatingUuid = null;
replayPlayer.spectatingUuidTickCount = 0;
} else {
Entity cameraEntity = replayPlayer.getCamera();
if (cameraEntity != null && cameraEntity != replayPlayer) {
replayPlayer.spectatingUuid = cameraEntity.getUUID();
replayPlayer.spectatingUuidTickCount = 20;
} else if (replayPlayer.spectatingUuidTickCount > 0) {
replayPlayer.spectatingUuidTickCount -= 1;
} else {
replayPlayer.spectatingUuid = null;
}
}
this.replayViewers.add(replayPlayer);
}
}
@@ -724,6 +771,80 @@ public class ReplayServer extends IntegratedServer {
this.tryFollowLocalPlayer();
// Update first person data
for (ReplayPlayer replayViewer : this.replayViewers) {
// Ensure replay viewers are still spectating
if (replayViewer.spectatingUuid != null) {
Entity camera = replayViewer.getCamera();
if (camera == null || camera == replayViewer || camera.isRemoved()) {
Entity targetEntity = replayViewer.serverLevel().getEntity(replayViewer.spectatingUuid);
if (targetEntity != null && !targetEntity.isRemoved()) {
replayViewer.setCamera(targetEntity);
}
}
}
Entity camera = replayViewer.getCamera();
if (camera != replayViewer && camera instanceof Player playerCamera) {
Inventory inventory = playerCamera.getInventory();
if (!Objects.equals(replayViewer.lastFirstPersonDataUUID, playerCamera.getUUID())) {
replayViewer.lastFirstPersonDataUUID = playerCamera.getUUID();
replayViewer.lastFirstPersonExperienceProgress = playerCamera.experienceProgress;
replayViewer.lastFirstPersonTotalExperience = playerCamera.totalExperience;
replayViewer.lastFirstPersonExperienceLevel = playerCamera.experienceLevel;
ServerPlayNetworking.send(replayViewer, new FlashbackRemoteExperience(playerCamera.getId(), playerCamera.experienceProgress,
playerCamera.totalExperience, playerCamera.experienceLevel));
FoodData foodData = playerCamera.getFoodData();
replayViewer.lastFirstPersonFoodLevel = foodData.getFoodLevel();
replayViewer.lastFirstPersonSaturationLevel = foodData.getSaturationLevel();
ServerPlayNetworking.send(replayViewer, new FlashbackRemoteFoodData(playerCamera.getId(), foodData.getFoodLevel(), foodData.getSaturationLevel()));
replayViewer.lastFirstPersonSelectedSlot = inventory.selected;
ServerPlayNetworking.send(replayViewer, new FlashbackRemoteSelectHotbarSlot(playerCamera.getId(), inventory.selected));
for (int i = 0; i < 9; i++) {
ItemStack hotbarItem = inventory.getItem(i);
replayViewer.lastFirstPersonHotbarItems[i] = hotbarItem.copy();
ServerPlayNetworking.send(replayViewer, new FlashbackRemoteSetSlot(playerCamera.getId(), i, hotbarItem.copy()));
}
} else {
if (replayViewer.lastFirstPersonExperienceProgress != playerCamera.experienceProgress ||
replayViewer.lastFirstPersonTotalExperience != playerCamera.totalExperience ||
replayViewer.lastFirstPersonExperienceLevel != playerCamera.experienceLevel) {
replayViewer.lastFirstPersonExperienceProgress = playerCamera.experienceProgress;
replayViewer.lastFirstPersonTotalExperience = playerCamera.totalExperience;
replayViewer.lastFirstPersonExperienceLevel = playerCamera.experienceLevel;
ServerPlayNetworking.send(replayViewer, new FlashbackRemoteExperience(playerCamera.getId(), playerCamera.experienceProgress,
playerCamera.totalExperience, playerCamera.experienceLevel));
}
FoodData foodData = playerCamera.getFoodData();
if (replayViewer.lastFirstPersonFoodLevel != foodData.getFoodLevel() || replayViewer.lastFirstPersonSaturationLevel != foodData.getSaturationLevel()) {
replayViewer.lastFirstPersonFoodLevel = foodData.getFoodLevel();
replayViewer.lastFirstPersonSaturationLevel = foodData.getSaturationLevel();
ServerPlayNetworking.send(replayViewer, new FlashbackRemoteFoodData(playerCamera.getId(), foodData.getFoodLevel(), foodData.getSaturationLevel()));
}
if (replayViewer.lastFirstPersonSelectedSlot != inventory.selected) {
replayViewer.lastFirstPersonSelectedSlot = inventory.selected;
ServerPlayNetworking.send(replayViewer, new FlashbackRemoteSelectHotbarSlot(playerCamera.getId(), inventory.selected));
}
for (int i = 0; i < 9; i++) {
ItemStack hotbarItem = inventory.getItem(i);
if (!ItemStack.matches(replayViewer.lastFirstPersonHotbarItems[i], hotbarItem)) {
replayViewer.lastFirstPersonHotbarItems[i] = hotbarItem.copy();
ServerPlayNetworking.send(replayViewer, new FlashbackRemoteSetSlot(playerCamera.getId(), i, hotbarItem.copy()));
}
}
}
} else {
replayViewer.lastFirstPersonDataUUID = null;
}
}
// Update resourcepacks
if (this.remotePacks.isEmpty()) {
if (!this.oldRemotePacks.isEmpty()) {
@@ -808,21 +929,6 @@ public class ReplayServer extends IntegratedServer {
replayViewer.connection.chunkSender.sendNextChunks(replayViewer);
ServerPlayNetworking.send(replayViewer, FlashbackInstantlyLerp.INSTANCE);
ServerPlayNetworking.send(replayViewer, FlashbackClearParticles.INSTANCE);
// Ensure replay viewers are still spectating
if (replayViewer.spectatingUuid != null) {
Entity camera = replayViewer.getCamera();
if (camera == null || camera == replayViewer || camera.isRemoved()) {
Entity targetEntity = replayViewer.serverLevel().getEntity(replayViewer.spectatingUuid);
if (targetEntity != null && !targetEntity.isRemoved()) {
replayViewer.setCamera(targetEntity);
} else {
replayViewer.spectatingUuid = null;
}
} else {
replayViewer.spectatingUuid = null;
}
}
}
for (ServerLevel level : this.getAllLevels()) {
for (ServerPlayer player : level.players()) {