Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions src/main/java/wayoftime/bloodmagic/common/block/BlockSpectral.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import java.util.Random;

import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.EnumProperty;
Expand All @@ -30,22 +32,14 @@ public BlockSpectral(Properties prop)
super(prop);
}

public void tick(BlockState state, ServerLevel world, BlockPos pos, Random rand)
{
switch (state.getValue(SPECTRAL_STATE))
{
case SOLID:
world.setBlock(pos, state.setValue(SPECTRAL_STATE, SpectralBlockType.LEAKING), 3);
world.scheduleTick(pos, this, BlockSpectral.DECAY_RATE);
break;
case LEAKING:
BlockEntity tile = world.getBlockEntity(pos);
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> type) {
return (level1, blockPos, blockState, tile) -> {
if (tile instanceof TileSpectral)
{
((TileSpectral) tile).revertToFluid();
((TileSpectral) tile).tick();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should pass the blockPos, level and tile from the lambda into the tick method instead of fetching them in the method. Also use the automatic cast when using instanceof instead of manual casting

}
break;
}
};
}

@Override
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/wayoftime/bloodmagic/common/tile/TileSpectral.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
import wayoftime.bloodmagic.common.block.type.SpectralBlockType;
import wayoftime.bloodmagic.common.tile.base.TileBase;

import java.util.Random;

import static wayoftime.bloodmagic.common.block.BlockSpectral.SPECTRAL_STATE;

public class TileSpectral extends TileBase
{
public BlockState storedBlock;
Expand Down Expand Up @@ -82,4 +86,23 @@ public CompoundTag serialize(CompoundTag tag)
tag.put("BlockState", NbtUtils.writeBlockState(storedBlock));
return tag;
}

public void tick()
{
BlockState state = this.getBlockState();
Comment on lines +89 to +92
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason the blocks vanish so quickly is that the BE is ticked every tick. Instead, use a tick_counter and check against the DECAY_RATE and only then tick it.

BlockPos pos = this.getBlockPos();
Level level = this.getLevel();
switch (state.getValue(SPECTRAL_STATE)) {
case SOLID -> {
level.setBlock(pos, state.setValue(SPECTRAL_STATE, SpectralBlockType.LEAKING), 3);
Copy link
Contributor

@dphaldes dphaldes Mar 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of '3' use the proper flag (Block.UPDATE_ALL). They are present in Block.java.

level.scheduleTick(pos, state.getBlock(), BlockSpectral.DECAY_RATE);
Copy link
Contributor

@dphaldes dphaldes Mar 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scheduleTick is not required anymore

}
case LEAKING -> {
BlockEntity tile = level.getBlockEntity(pos);
if (tile instanceof TileSpectral) {
((TileSpectral) tile).revertToFluid();
}
}
}
}
}