I am having issues with replacing a block through its attached TileEntity.
Goal
Have a block that charges up, changes from the "Off Block" to the "On Block". If an entity wanders over the "On Block" it interacts with entity, discharges back to "Off Block" and starts to recharge again.
First block of this type that I am trying is a beam transporter that will teleport an entity to a fixed location. (Using fixed location for now, will work on player programming of the destination later)
Work so far
I have two blocks, BeamOffBlock and BeamOnBlock, the BlockItems for both blocks, and a tile entity, BeamEntity
Same Tile Entity is linked to both block types
Both blocks, their respective block-items and the Tile Entity all appear to register correctly (Deferred Registration)
Both blocks can be placed in the world (creative mode)
Both blocks update on ticks, running same tile entity code (Debug code prints out correct "charge counter")
BeamOnBlock interacts with entities and teleports them to correct location (charge drops to 0 and it recharges, switch to off block during this time is disabled for now)
BeamOffBlock just updates the charge counter (within the Tile Entity) and does nothing else. (My error is at the switch to on stage.)
I also have numerous other basic blocks and items that function as expected
Issue
When BeamOffBlock reaches a charge level of 5, it should be removed and replaced with BeamOnBlock (same basic properties, but changed light level and different textures)
I just can't get the block replacement to work properly and depending on which side I try with, I either get a black and magenta textured minecraft:air block, or a block that flickers between the off block and black/magenta air. (I am aware that the black/magenta colour is the "default" more missing textures)
I also get the warning shows for both the server and render threads:
[minecraft/TileEntity]: Block entity invalid: rainbowgems:beam_station @ BlockPos{<item location>}
What is the correct way to swap a block that has a Tile Entity with another block that uses the same Tile Entity?
The following is my latest failed incantation:
@Override
public void tick() {
if (world.getBlockState(pos).getBlock() != this.getBlockState().getBlock()) return;
if (!initialized) init();
tick++;
if (tick >= 20) {
tick = 0;
charge++;
if (charge >= 5) {
charge = 5;
if (!world.isRemote && (world.getBlockState(pos).getBlock() instanceof BeamOffBlock)) {
BeamOnBlock putready = new BeamOnBlock();
world.destroyBlock(pos, false);
world.removeTileEntity(pos);
world.setBlockState(pos, putready.getDefaultState());
}
}
world.notifyBlockUpdate(pos, getBlockState(), getBlockState(), 3);
markDirty();
//DEBUG***
if (world.isRemote) {
System.out.printf("Server %d %d %d = %d\n", beam_x, beam_y, beam_z, charge);
} else{
System.out.printf("Render %d %d %d = %d\n", beam_x, beam_y, beam_z, charge);
}
//DEBUG***
}
}
One suspicion I have is the constructor of the BeamOnBlock and BeamOffBlock. Is there a way to say "If the Tile Entity already exists at this BlockPos, use it and don't create a new one."?
public class BeamOnBlock extends Block {
public BeamOnBlock() {
super(Properties.create(Material.PORTAL)
.hardnessAndResistance(50.0f,1200.0f)
.sound(SoundType.STONE)
.harvestLevel(3) //Diamond
.harvestTool(ToolType.PICKAXE)
.lightValue(15));
}
@Override
public boolean hasTileEntity(BlockState state) {
return true;
}
@Override
public TileEntity createTileEntity(BlockState state, IBlockReader world) {
return RegistryHandler.BEAM_ENTITY.get().create();
}
@Override
public void onEntityWalk(World worldIn, BlockPos pos, Entity entityIn) {
if (entityIn instanceof LivingEntity) {
if (worldIn.getTileEntity(pos) instanceof BeamEntity) {
((BeamEntity) worldIn.getTileEntity(pos)).Beam(worldIn, pos, entityIn);
}
}
}
}
Full listing is on GitHub with direct link to tile entity code here
Thanks