Skip to content

Commit

Permalink
fix: fix coloring of vials in the vial holder not always updating and…
Browse files Browse the repository at this point in the history
… defaulting to white
  • Loading branch information
Elenterius committed Feb 11, 2024
1 parent 9806d3f commit 4712b27
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import com.github.elenterius.biomancy.util.VoxelShapeUtil;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.Mth;
import net.minecraft.util.RandomSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
Expand Down Expand Up @@ -108,6 +110,13 @@ public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return ModBlockEntities.VIAL_HOLDER.get().create(pos, state);
}

@Override
public void tick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {
if (level.getBlockEntity(pos) instanceof VialHolderBlockEntity vialHolder) {
vialHolder.updateBlockState(level);
}
}

@Override
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.github.elenterius.biomancy.init.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.Containers;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
Expand Down Expand Up @@ -40,19 +41,28 @@ protected void onContentsChanged(int slot) {
};
}

@Override
protected void syncToClient() {
if (level != null && !level.isClientSide) {
BlockState state = getBlockState();
level.sendBlockUpdated(getBlockPos(), state, state, Block.UPDATE_CLIENTS); //sync compound data

BlockState newState = getBlockState();
for (int i = 0; i < VialHolderBlock.VIAL_PROPERTIES.length; i++) {
BooleanProperty vialProperty = VialHolderBlock.VIAL_PROPERTIES[i];
newState = newState.setValue(vialProperty, inventory.getStackInSlot(i).getItem() instanceof SerumContainer);
}
//delay block state update to fix coloring of vials not always updating and defaulting to white
//this usually happens because the block state & renderer update before the updated compound data will be available at the client side
level.scheduleTick(getBlockPos(), state.getBlock(), 1);
}
}

level.setBlockAndUpdate(getBlockPos(), newState);
protected void updateBlockState(ServerLevel level) {
BlockState oldState = getBlockState();
BlockState newState = getBlockState();
for (int i = 0; i < VialHolderBlock.VIAL_PROPERTIES.length; i++) {
BooleanProperty vialProperty = VialHolderBlock.VIAL_PROPERTIES[i];
newState = newState.setValue(vialProperty, inventory.getStackInSlot(i).getItem() instanceof SerumContainer);
}

BlockState state = getBlockState();
level.sendBlockUpdated(getBlockPos(), state, state, Block.UPDATE_CLIENTS);
if (newState != oldState) {
level.setBlock(getBlockPos(), newState, Block.UPDATE_ALL);
}
}

Expand All @@ -68,14 +78,7 @@ public void load(CompoundTag tag) {
inventory.deserializeNBT(tag.getCompound(INVENTORY_TAG));

if (level != null && !level.isClientSide) {
BlockState state = getBlockState();
for (int i = 0; i < VialHolderBlock.VIAL_PROPERTIES.length; i++) {
BooleanProperty vialProperty = VialHolderBlock.VIAL_PROPERTIES[i];
state = state.setValue(vialProperty, inventory.getStackInSlot(i).getItem() instanceof SerumContainer);
}
if (state != getBlockState()) {
level.setBlockAndUpdate(getBlockPos(), state);
}
syncToClient();
}
}

Expand Down

0 comments on commit 4712b27

Please sign in to comment.