| 1 | package net.minecraft.client.multiplayer.prediction;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
| 4 | import it.unimi.dsi.fastutil.longs.Long2ObjectMap.Entry;
|
| 5 | import it.unimi.dsi.fastutil.objects.ObjectIterator;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import net.minecraft.client.multiplayer.ClientLevel;
|
| 9 | import net.minecraft.client.player.LocalPlayer;
|
| 10 | import net.minecraft.core.BlockPos;
|
| 11 | import net.minecraft.world.level.block.state.BlockState;
|
| 12 | import net.minecraft.world.phys.Vec3;
|
| 13 |
|
| 14 | @Environment(EnvType.CLIENT)
|
| 15 | public class BlockStatePredictionHandler implements AutoCloseable {
|
| 16 | private final Long2ObjectOpenHashMap<BlockStatePredictionHandler.ServerVerifiedState> serverVerifiedStates = new Long2ObjectOpenHashMap<>();
|
| 17 | private int currentSequenceNr;
|
| 18 | private boolean isPredicting;
|
| 19 | private int lastTeleportSequence = -1;
|
| 20 |
|
| 21 | public void retainKnownServerState(final BlockPos pos, final BlockState state, final LocalPlayer player) {
|
| 22 | this.serverVerifiedStates
|
| 23 | .compute(
|
| 24 | pos.asLong(),
|
| 25 | (key, serverVerifiedState) -> serverVerifiedState != null
|
| 26 | ? serverVerifiedState.setSequence(this.currentSequenceNr)
|
| 27 | : new BlockStatePredictionHandler.ServerVerifiedState(this.currentSequenceNr, state, player.position())
|
| 28 | );
|
| 29 | }
|
| 30 |
|
| 31 | public boolean updateKnownServerState(final BlockPos pos, final BlockState blockState) {
|
| 32 | BlockStatePredictionHandler.ServerVerifiedState serverVerifiedState = this.serverVerifiedStates.get(pos.asLong());
|
| 33 | if (serverVerifiedState == null) {
|
| 34 | return false;
|
| 35 | }
|
| 36 |
|
| 37 | serverVerifiedState.setBlockState(blockState);
|
| 38 | return true;
|
| 39 | }
|
| 40 |
|
| 41 | public void endPredictionsUpTo(final int sequence, final ClientLevel clientLevel) {
|
| 42 | ObjectIterator<Entry<BlockStatePredictionHandler.ServerVerifiedState>> stateIterator = this.serverVerifiedStates.long2ObjectEntrySet().iterator();
|
| 43 |
|
| 44 | while (stateIterator.hasNext()) {
|
| 45 | Entry<BlockStatePredictionHandler.ServerVerifiedState> next = stateIterator.next();
|
| 46 | BlockStatePredictionHandler.ServerVerifiedState serverVerifiedState = next.getValue();
|
| 47 | if (serverVerifiedState.sequence <= sequence) {
|
| 48 | BlockPos pos = BlockPos.of(next.getLongKey());
|
| 49 | stateIterator.remove();
|
| 50 | clientLevel.syncBlockState(pos, serverVerifiedState.blockState, this.lastTeleportSequence < sequence ? serverVerifiedState.playerPos : null);
|
| 51 | }
|
| 52 | }
|
| 53 | }
|
| 54 |
|
| 55 | public BlockStatePredictionHandler startPredicting() {
|
| 56 | this.currentSequenceNr++;
|
| 57 | this.isPredicting = true;
|
| 58 | return this;
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | public void close() {
|
| 63 | this.isPredicting = false;
|
| 64 | }
|
| 65 |
|
| 66 | public int currentSequence() {
|
| 67 | return this.currentSequenceNr;
|
| 68 | }
|
| 69 |
|
| 70 | public void onTeleport() {
|
| 71 | this.lastTeleportSequence = this.currentSequenceNr;
|
| 72 | }
|
| 73 |
|
| 74 | public boolean isPredicting() {
|
| 75 | return this.isPredicting;
|
| 76 | }
|
| 77 |
|
| 78 | @Environment(EnvType.CLIENT)
|
| 79 | private static class ServerVerifiedState {
|
| 80 | private final Vec3 playerPos;
|
| 81 | private int sequence;
|
| 82 | private BlockState blockState;
|
| 83 |
|
| 84 | private ServerVerifiedState(final int sequence, final BlockState blockState, final Vec3 playerPos) {
|
| 85 | this.sequence = sequence;
|
| 86 | this.blockState = blockState;
|
| 87 | this.playerPos = playerPos;
|
| 88 | }
|
| 89 |
|
| 90 | private BlockStatePredictionHandler.ServerVerifiedState setSequence(final int sequence) {
|
| 91 | this.sequence = sequence;
|
| 92 | return this;
|
| 93 | }
|
| 94 |
|
| 95 | private void setBlockState(final BlockState blockState) {
|
| 96 | this.blockState = blockState;
|
| 97 | }
|
| 98 | }
|
| 99 | }
|
| 100 |
|