Skip to content

Commit

Permalink
Move block entity syncing to vanilla's system
Browse files Browse the repository at this point in the history
  • Loading branch information
kb-1000 committed Jul 29, 2024
1 parent 52c5b3e commit 7cbaa5a
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 173 deletions.
2 changes: 0 additions & 2 deletions projects/pswg/src/main/java/com/parzivail/pswg/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import com.parzivail.pswg.mixin.DimensionEffectsAccessor;
import com.parzivail.pswg.mixin.MinecraftClientAccessor;
import com.parzivail.pswg.network.OpenEntityInventoryS2CPacket;
import com.parzivail.util.block.BlockEntityClientSerializable;
import com.parzivail.util.client.TextUtil;
import com.parzivail.util.client.model.DynamicBakedModel;
import com.parzivail.util.client.model.ModelRegistry;
Expand Down Expand Up @@ -381,7 +380,6 @@ public void onInitializeClient()
}
});

ClientPlayNetworking.registerGlobalReceiver(SwgPackets.S2C.SyncBlockToClient, BlockEntityClientSerializable::handle);
ClientPlayNetworking.registerGlobalReceiver(SwgPackets.S2C.PreciseEntityVelocityUpdate, PreciseEntityVelocityUpdateS2CPacket::handle);
ClientPlayNetworking.registerGlobalReceiver(SwgPackets.S2C.PreciseEntitySpawn, PreciseEntitySpawnS2CPacket::handle);
ClientPlayNetworking.registerGlobalReceiver(SwgPackets.S2C.OpenEntityInventory, OpenEntityInventoryS2CPacket::handle);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package com.parzivail.pswg.blockentity;

import com.parzivail.pswg.container.SwgBlocks;
import com.parzivail.pswg.container.SwgPackets;
import com.parzivail.util.block.BlockEntityClientSerializable;
import com.parzivail.util.entity.EntityUtil;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.listener.ClientPlayPacketListener;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class CreatureCageBlockEntity extends BlockEntity implements BlockEntityClientSerializable
public class CreatureCageBlockEntity extends BlockEntity
{
private NbtCompound containedEntityData;
private Entity containedEntity;
Expand Down Expand Up @@ -44,22 +45,15 @@ public void readNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryLooku
}

@Override
public NbtCompound toInitialChunkDataNbt(RegistryWrapper.WrapperLookup registryLookup)
{
return toClientTag(super.toInitialChunkDataNbt(registryLookup));
}

@Override
public void fromClientTag(NbtCompound tag)
public Packet<ClientPlayPacketListener> toUpdatePacket()
{
readNbt(tag);
return BlockEntityUpdateS2CPacket.create(this);
}

@Override
public NbtCompound toClientTag(NbtCompound tag)
public NbtCompound toInitialChunkDataNbt(RegistryWrapper.WrapperLookup registryLookup)
{
writeNbt(tag);
return tag;
return createComponentlessNbt(registryLookup);
}

public boolean hasContainedEntity()
Expand All @@ -77,7 +71,7 @@ public Entity getContainedEntity()
public void setContainedEntity(Entity e)
{
this.containedEntity = e;
sync();
world.updateListeners(getPos(), getCachedState(), getCachedState(), Block.NOTIFY_ALL_AND_REDRAW);
markDirty();
}

Expand Down Expand Up @@ -105,10 +99,4 @@ private void tickEntity()
if (entity != null)
entity.tick();
}

@Override
public Identifier getSyncPacketId()
{
return SwgPackets.S2C.SyncBlockToClient;
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
package com.parzivail.pswg.blockentity;

import com.parzivail.pswg.container.SwgBlocks;
import com.parzivail.pswg.container.SwgPackets;
import com.parzivail.util.block.BlockEntityClientSerializable;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtList;
import net.minecraft.nbt.NbtLong;
import net.minecraft.nbt.NbtLongArray;
import net.minecraft.network.listener.ClientPlayPacketListener;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class PowerCouplingBlockEntity extends BlockEntity implements BlockEntityClientSerializable
public class PowerCouplingBlockEntity extends BlockEntity
{
private int syncTimer = 0;
private final Set<BlockPos> targetPositions = new HashSet<>();
Expand All @@ -34,11 +35,7 @@ protected void writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryL
{
super.writeNbt(nbt, registryLookup);

var targets = new NbtList();
for (var pos : this.targetPositions)
targets.add(NbtLong.of(pos.asLong()));

nbt.put("targets", targets);
nbt.put("targets", new NbtLongArray(this.targetPositions.stream().mapToLong(BlockPos::asLong).toArray()));
}

@Override
Expand All @@ -48,9 +45,16 @@ protected void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLo

targetPositions.clear();

var targets = nbt.getList("targets", NbtElement.LONG_TYPE);
for (var target : targets)
targetPositions.add(BlockPos.fromLong(((NbtLong)target).longValue()));
// TODO: old code for migrating old data, remove at some point
{
var targets = nbt.getList("targets", NbtElement.LONG_TYPE);
for (var target : targets)
targetPositions.add(BlockPos.fromLong(((NbtLong)target).longValue()));
}
var targets = nbt.getLongArray("targets");
for (var target: targets) {
targetPositions.add(BlockPos.fromLong(target));
}

if (world != null && !world.isClient)
syncTimer = 10;
Expand All @@ -59,7 +63,8 @@ protected void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLo
private void removeFrom(BlockPos pos)
{
targetPositions.remove(pos.subtract(this.pos));
sync();
world.updateListeners(pos, getCachedState(), getCachedState(), Block.NOTIFY_ALL_AND_REDRAW);
markDirty();
}

public boolean attachTo(World world, BlockPos pos)
Expand All @@ -68,28 +73,21 @@ public boolean attachTo(World world, BlockPos pos)
return false;

targetPositions.add(pos.subtract(this.pos));
sync();
world.updateListeners(pos, getCachedState(), getCachedState(), Block.NOTIFY_ALL_AND_REDRAW);
markDirty();
return true;
}

@Override
public NbtCompound toInitialChunkDataNbt(RegistryWrapper.WrapperLookup registryLookup)
public Packet<ClientPlayPacketListener> toUpdatePacket()
{
return toClientTag(super.toInitialChunkDataNbt());
return BlockEntityUpdateS2CPacket.create(this);
}

@Override
public void fromClientTag(NbtCompound tag)
{
readNbt(tag);
}

@Override
public NbtCompound toClientTag(NbtCompound tag)
public NbtCompound toInitialChunkDataNbt(RegistryWrapper.WrapperLookup registryLookup)
{
writeNbt(tag);
return tag;
return createComponentlessNbt(registryLookup);
}

public Set<BlockPos> getTargets()
Expand Down Expand Up @@ -123,20 +121,15 @@ public static <T extends BlockEntity> void serverTick(World world, BlockPos bloc
// Remove all positions from the queue which are not currently invalid
t.removalQueue.keySet().retainAll(t.removalQueue.keySet().stream().filter(invalidTargets::contains).toList());

// TODO: why does a freshly loaded block need a delayed sync?
if (t.syncTimer > 0)
{
t.syncTimer--;
if (t.syncTimer == 0)
{
t.sync();
world.updateListeners(t.pos, t.getCachedState(), t.getCachedState(), Block.NOTIFY_ALL);
t.markDirty();
}
}
}

@Override
public Identifier getSyncPacketId()
{
return SwgPackets.S2C.SyncBlockToClient;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@

import com.parzivail.pswg.block.Sliding1x2DoorBlock;
import com.parzivail.pswg.container.SwgBlocks;
import com.parzivail.pswg.container.SwgPackets;
import com.parzivail.util.block.BlockEntityClientSerializable;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class SlidingDoorBlockEntity extends BlockEntity implements BlockEntityClientSerializable
public class SlidingDoorBlockEntity extends BlockEntity
{
private static final int ANIMATION_TIME = 10;

Expand Down Expand Up @@ -43,7 +40,7 @@ protected void writeNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryL
@Override
public NbtCompound toInitialChunkDataNbt(RegistryWrapper.WrapperLookup registryLookup)
{
return toClientTag(super.toInitialChunkDataNbt(registryLookup));
return createComponentlessNbt(registryLookup);
}

@Override
Expand All @@ -54,20 +51,6 @@ public void readNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryLooku
timer = tag.getByte("timer");
}

@Override
public void fromClientTag(NbtCompound compoundTag)
{
timer = compoundTag.getByte("timer");
}

@Override
public NbtCompound toClientTag(NbtCompound compoundTag)
{
compoundTag.putByte("timer", timer);

return compoundTag;
}

public boolean isMoving()
{
var state = world.getBlockState(pos);
Expand Down Expand Up @@ -122,7 +105,7 @@ public void start(boolean opening)
{
setTimer(ANIMATION_TIME);
setDirection(opening);
sync();
world.updateListeners(pos, getCachedState(), getCachedState(), Block.NOTIFY_ALL_AND_REDRAW);
}

@Environment(EnvType.CLIENT)
Expand Down Expand Up @@ -166,12 +149,6 @@ public static <T extends BlockEntity> void tick(World world, BlockPos blockPos,
}
}

@Override
public Identifier getSyncPacketId()
{
return SwgPackets.S2C.SyncBlockToClient;
}

public BlockState getOccupiedState()
{
return world.getBlockState(pos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public static class C2S

public static class S2C
{
public static final Identifier SyncBlockToClient = Resources.id("csync");
public static final CustomPayload.Id<PreciseEntityVelocityUpdateS2CPacket> PreciseEntityVelocityUpdate = new CustomPayload.Id<>(Resources.id("pevel"));
public static final CustomPayload.Id<PreciseEntitySpawnS2CPacket> PreciseEntitySpawn = new CustomPayload.Id<>(Resources.id("pesp"));
public static final CustomPayload.Id<OpenEntityInventoryS2CPacket> OpenEntityInventory = new CustomPayload.Id<>(Resources.id("entity_inventory"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@

import com.parzivail.pswg.Resources;
import com.parzivail.pswg.container.SwgBlocks;
import com.parzivail.pswg.container.SwgPackets;
import com.parzivail.pswg.container.SwgScreenTypes;
import com.parzivail.util.block.BlockEntityClientSerializable;
import com.parzivail.util.blockentity.InventoryBlockEntity;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.listener.ClientPlayPacketListener;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.screen.NamedScreenHandlerFactory;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;

public class BlasterWorkbenchBlockEntity extends InventoryBlockEntity implements NamedScreenHandlerFactory, BlockEntityClientSerializable
public class BlasterWorkbenchBlockEntity extends InventoryBlockEntity implements NamedScreenHandlerFactory
{
public static final int SLOT_BLASTER = 0;
public static final int SLOT_1 = 1;
Expand All @@ -45,42 +44,27 @@ public ScreenHandler createMenu(int syncId, PlayerInventory inv, PlayerEntity pl
}

@Override
public NbtCompound toInitialChunkDataNbt(RegistryWrapper.WrapperLookup registryLookup)
{
return toClientTag(super.toInitialChunkDataNbt(registryLookup));
}

@Override
public void markDirty()
{
super.markDirty();
sync();
}

@Override
public void readNbt(NbtCompound tag)
public Packet<ClientPlayPacketListener> toUpdatePacket()
{
if (this.world != null && this.world.isClient)
fromClientTag(tag);
super.readNbt(tag);
return BlockEntityUpdateS2CPacket.create(this);
}

@Override
public void fromClientTag(NbtCompound compoundTag)
public NbtCompound toInitialChunkDataNbt(RegistryWrapper.WrapperLookup registryLookup)
{
inventory.set(SLOT_BLASTER, ItemStack.fromNbt(compoundTag.getCompound("blaster")));
return createComponentlessNbt(registryLookup);
}

@Override
public NbtCompound toClientTag(NbtCompound compoundTag)
public void markDirty()
{
compoundTag.put("blaster", inventory.get(SLOT_BLASTER).writeNbt(new NbtCompound()));
return compoundTag;
super.markDirty();
world.updateListeners(pos, getCachedState(), getCachedState(), BlasterWorkbenchBlock.NOTIFY_ALL);
}

@Override
public Identifier getSyncPacketId()
public void readNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup)
{
return SwgPackets.S2C.SyncBlockToClient;
super.readNbt(tag, registryLookup);
}
}
Loading

0 comments on commit 7cbaa5a

Please sign in to comment.