Skip to content

Commit

Permalink
fix client load save desync on facades
Browse files Browse the repository at this point in the history
  • Loading branch information
Lothrazar committed Nov 6, 2024
1 parent 34ccc89 commit bf72f0c
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 104 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ curse_id=239286
mod_version=1.8.0

mc_version=1.18.2
forge_version=40.2.17
forge_version=40.2.21

# optional dependencies
jei_version=10.2.1.1005
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,12 +403,6 @@ private static boolean moveItemsInternal(int max, IItemHandler handlerHere, int
}
return sizeAfter > 0;
}
//
//
//
//
//
//

protected boolean moveEnergy(Direction myFacingDir, int quantity) {
return moveEnergy(myFacingDir, this.worldPosition.relative(myFacingDir), quantity);
Expand Down Expand Up @@ -484,49 +478,6 @@ private static boolean moveEnergyInternal(final int quantity, final IEnergyStora
}
return true;
}
// protected boolean moveEnergy(Direction myFacingDir, int quantity) {
// return moveEnergy(myFacingDir, worldPosition.relative(myFacingDir), quantity);
// }
//
// protected boolean moveEnergy(Direction myFacingDir, BlockPos posTarget, int quantity) {
// if (this.level.isClientSide) {
// return false; //important to not desync cables
// }
// IEnergyStorage handlerHere = this.getCapability(CapabilityEnergy.ENERGY, myFacingDir).orElse(null);
// if (handlerHere == null || handlerHere.getEnergyStored() == 0) {
// return false;
// }
// if (myFacingDir == null) {
// myFacingDir = Direction.UP;
// }
// Direction themFacingMe = myFacingDir.getOpposite();
// BlockEntity tileTarget = level.getBlockEntity(posTarget);
// if (tileTarget == null) {
// return false;
// }
// IEnergyStorage handlerOutput = tileTarget.getCapability(CapabilityEnergy.ENERGY, themFacingMe).orElse(null);
// if (handlerOutput == null) {
// return false;
// }
// if (handlerHere != null && handlerOutput != null
// && handlerHere.canExtract() && handlerOutput.canReceive()) {
// //first simulate
// int drain = handlerHere.extractEnergy(quantity, true);
// if (drain > 0) {
// //now push it into output, but find out what was ACTUALLY taken
// int filled = handlerOutput.receiveEnergy(drain, false);
// //now actually drain that much from here
// handlerHere.extractEnergy(filled, false);
// if (filled > 0 && tileTarget instanceof TileCableEnergy) {
// // not so compatible with other fluid systems. itl do i guess
// TileCableEnergy cable = (TileCableEnergy) tileTarget;
// cable.updateIncomingEnergyFace(themFacingMe);
// }
// return filled > 0;
// }
// }
// return false;
// }

@Override
public void load(CompoundTag tag) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,12 @@ default void loadFacade(CompoundTag compound) {
if (compound.contains(NBT_FACADE)) {
this.setFacade(compound.getCompound(NBT_FACADE));
}
else {
this.setFacade(null);
}
}

default void saveFacade(CompoundTag compound) {
CompoundTag facadeState = getFacade();
if (facadeState == null) {
compound.remove(NBT_FACADE);
compound.put(NBT_FACADE, new CompoundTag());
}
else {
compound.put(NBT_FACADE, facadeState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public boolean shouldRenderOffScreen(TileCableBase te) {

@Override
public void render(TileCableBase te, float v, PoseStack matrixStack, MultiBufferSource ibuffer, int packedLight, int packedOverlay) {
if (te.getFacade() != null
&& ConfigRegistry.CABLE_FACADES.get()) {
if (ConfigRegistry.CABLE_FACADES.get()) {
BlockState facadeState = te.getFacadeState(te.getLevel());
FacadeUtil.renderBlockState(te.getLevel(), te.getBlockPos(), brd, renderer, ibuffer, matrixStack, facadeState, packedLight, packedOverlay);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ public boolean shouldRenderOffScreen(TileLightFacade te) {

@Override
public void render(TileLightFacade te, float v, PoseStack matrixStack, MultiBufferSource ibuffer, int packedLight, int packedOverlay) {
if (te.getFacade() != null) {
BlockState facadeState = te.getFacadeState(te.getLevel());
FacadeUtil.renderBlockState(te.getLevel(), te.getBlockPos(), brd, renderer, ibuffer, matrixStack, facadeState, packedLight, packedOverlay);
}
BlockState facadeState = te.getFacadeState(te.getLevel());
FacadeUtil.renderBlockState(te.getLevel(), te.getBlockPos(), brd, renderer, ibuffer, matrixStack, facadeState, packedLight, packedOverlay);
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
package com.lothrazar.cyclic.block.facade.light;

import java.util.ArrayList;
import java.util.List;
import com.lothrazar.cyclic.block.TileBlockEntityCyclic;
import com.lothrazar.cyclic.block.facade.ITileFacade;
import com.lothrazar.cyclic.registry.TileRegistry;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.network.Connection;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;

public class TileLightFacade extends TileBlockEntityCyclic implements ITileFacade {

public TileLightFacade(BlockPos pos, BlockState state) {
super(TileRegistry.LIGHT_CAMO.get(), pos, state);
}

@Override
public CompoundTag getUpdateTag() {
CompoundTag syncData = super.getUpdateTag();
return syncData;
}

@Override
public void onDataPacket(Connection net, ClientboundBlockEntityDataPacket pkt) {
if (pkt.getTag().contains(NBT_FACADE)) {
this.load(pkt.getTag());
super.onDataPacket(net, pkt);
}
}

@Override
public void load(CompoundTag tag) {
super.load(tag);
this.loadFacade(tag);
super.load(tag);
}

@Override
Expand All @@ -29,11 +41,6 @@ public void saveAdditional(CompoundTag tag) {
super.saveAdditional(tag);
}

@Override
public AABB getRenderBoundingBox() {
return BlockEntity.INFINITE_EXTENT_AABB;
}

@Override
public void setField(int field, int value) {}

Expand All @@ -42,12 +49,6 @@ public int getField(int field) {
return 0;
}

public List<BlockPos> getShape() {
List<BlockPos> lis = new ArrayList<BlockPos>();
lis.add(worldPosition);
return lis;
}

private CompoundTag facadeState = null;

@Override
Expand All @@ -56,7 +57,7 @@ public CompoundTag getFacade() {
}

@Override
public void setFacade(CompoundTag facadeState) {
this.facadeState = facadeState;
public void setFacade(CompoundTag facadeStateIn) {
this.facadeState = facadeStateIn;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
import net.minecraft.world.level.block.state.BlockState;

public class SoundmuffRender implements BlockEntityRenderer<SoundmuffTileFacade> {
public class SoundmuffRenderFacade implements BlockEntityRenderer<SoundmuffTileFacade> {

private BlockRenderDispatcher brd;
private ModelBlockRenderer renderer;

public SoundmuffRender(BlockEntityRendererProvider.Context d) {
public SoundmuffRenderFacade(BlockEntityRendererProvider.Context d) {
this.brd = d.getBlockRenderDispatcher();
this.renderer = brd.getModelRenderer();
}
Expand All @@ -26,9 +26,7 @@ public boolean shouldRenderOffScreen(SoundmuffTileFacade te) {

@Override
public void render(SoundmuffTileFacade te, float v, PoseStack matrixStack, MultiBufferSource ibuffer, int packedLight, int packedOverlay) {
if (te.getFacade() != null) {
BlockState facadeState = te.getFacadeState(te.getLevel());
FacadeUtil.renderBlockState(te.getLevel(), te.getBlockPos(), brd, renderer, ibuffer, matrixStack, facadeState, packedLight, packedOverlay);
}
BlockState facadeState = te.getFacadeState(te.getLevel());
FacadeUtil.renderBlockState(te.getLevel(), te.getBlockPos(), brd, renderer, ibuffer, matrixStack, facadeState, packedLight, packedOverlay);
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
package com.lothrazar.cyclic.block.facade.soundmuff;

import java.util.ArrayList;
import java.util.List;
import com.lothrazar.cyclic.block.TileBlockEntityCyclic;
import com.lothrazar.cyclic.block.facade.ITileFacade;
import com.lothrazar.cyclic.registry.TileRegistry;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.network.Connection;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;

public class SoundmuffTileFacade extends TileBlockEntityCyclic implements ITileFacade {

public SoundmuffTileFacade(BlockPos pos, BlockState state) {
super(TileRegistry.SOUNDPROOFING_GHOST.get(), pos, state);
}

@Override
public CompoundTag getUpdateTag() {
CompoundTag syncData = super.getUpdateTag();
return syncData;
}

@Override
public void onDataPacket(Connection net, ClientboundBlockEntityDataPacket pkt) {
if (pkt.getTag().contains(NBT_FACADE)) {
this.load(pkt.getTag());
super.onDataPacket(net, pkt);
}
}

@Override
public void load(CompoundTag tag) {
this.loadFacade(tag);
Expand All @@ -29,11 +41,6 @@ public void saveAdditional(CompoundTag tag) {
super.saveAdditional(tag);
}

@Override
public AABB getRenderBoundingBox() {
return BlockEntity.INFINITE_EXTENT_AABB;
}

@Override
public void setField(int field, int value) {}

Expand All @@ -42,12 +49,6 @@ public int getField(int field) {
return 0;
}

public List<BlockPos> getShape() {
List<BlockPos> lis = new ArrayList<BlockPos>();
lis.add(worldPosition);
return lis;
}

private CompoundTag facadeState = null;

@Override
Expand All @@ -56,7 +57,7 @@ public CompoundTag getFacade() {
}

@Override
public void setFacade(CompoundTag facadeState) {
this.facadeState = facadeState;
public void setFacade(CompoundTag facadeStateIn) {
this.facadeState = facadeStateIn;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ public static void handle(BlockFacadeMessage message, Supplier<NetworkEvent.Cont
if (bs.getBlock() instanceof IBlockFacade facadeBlock) {
ITileFacade tile = facadeBlock.getTileFacade(serverWorld, message.pos);
if (message.erase) {
tile.setFacade(null);
tile.setFacade(new CompoundTag());
}
else {
tile.setFacade(message.blockStateTag);
}
serverWorld.markAndNotifyBlock(message.pos, serverWorld.getChunkAt(message.pos),
bs, bs, 3, 1);
serverWorld.sendBlockUpdated(message.pos, bs, bs, 3);
serverWorld.blockUpdated(message.pos, bs.getBlock());
}
});
ctx.get().setPacketHandled(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import com.lothrazar.cyclic.block.endershelf.EnderShelfRenderer;
import com.lothrazar.cyclic.block.facade.RenderCableFacade;
import com.lothrazar.cyclic.block.facade.light.RenderLightFacade;
import com.lothrazar.cyclic.block.facade.soundmuff.SoundmuffRender;
import com.lothrazar.cyclic.block.facade.soundmuff.SoundmuffRenderFacade;
import com.lothrazar.cyclic.block.fan.RenderFan;
import com.lothrazar.cyclic.block.fishing.RenderFisher;
import com.lothrazar.cyclic.block.forester.RenderForester;
Expand Down Expand Up @@ -114,7 +114,7 @@ public static void onRegisterRenderers(EntityRenderersEvent.RegisterRenderers ev
event.registerBlockEntityRenderer(TileRegistry.SCREEN.get(), RenderScreentext::new);
event.registerBlockEntityRenderer(TileRegistry.COMPUTER_SHAPE.get(), RenderShapedata::new);
event.registerBlockEntityRenderer(TileRegistry.SOLIDIFIER.get(), RenderSolidifier::new);
event.registerBlockEntityRenderer(TileRegistry.SOUNDPROOFING_GHOST.get(), SoundmuffRender::new);
event.registerBlockEntityRenderer(TileRegistry.SOUNDPROOFING_GHOST.get(), SoundmuffRenderFacade::new);
event.registerBlockEntityRenderer(TileRegistry.SPRINKLER.get(), RenderSprinkler::new);
event.registerBlockEntityRenderer(TileRegistry.TANK.get(), RenderTank::new);
event.registerBlockEntityRenderer(TileRegistry.WIRELESS_TRANSMITTER.get(), RenderTransmit::new);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/lothrazar/cyclic/util/FacadeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public class FacadeUtil {

public static void renderBlockState(Level level, BlockPos pos, BlockRenderDispatcher brd, ModelBlockRenderer renderer,
MultiBufferSource ibuffer, PoseStack matrixStack, BlockState facadeState, int packedLight, int packedOverlay) {
if (facadeState == null) {
return;
}
BakedModel model = brd.getBlockModel(facadeState);
// int color = Minecraft.getInstance().getBlockColors().getColor(facadeState, null, null, 0);
// float r = (color >> 16 & 255) / 255.0F;
Expand Down

0 comments on commit bf72f0c

Please sign in to comment.