Jump to content

[SOLVED] [1.15.2] Replace Block/TileEntity with another Block/TileEntity


Ozone

Recommended Posts

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

 

Edited by Ozone
Issue Resolved
Link to comment
Share on other sites

Instead of swapping blocks, you should use block state properties.

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

Yes. I did consider that, but the Forge documents say to "flatten the code" and use two blocks rather than states.

"A good rule of thumb is: if it has a different name, it should be a different block/item."

So I am trying to following the latest advice on that.

Link to comment
Share on other sites

4 hours ago, Ozone said:

Yes. I did consider that, but the Forge documents say to "flatten the code" and use two blocks rather than states.

"A good rule of thumb is: if it has a different name, it should be a different block/item."

So I am trying to following the latest advice on that.

The "flatten the code" is meant for different blocks. Previously due to the ID limit, mod authors commonly associate different types of item to the same ID (i.e. different colored cobblestone, or vanilla's different colored wool). These should be flattened and separated into different block registry entries.

 

However this is not the case in your case. Your two blocks are similar to the off and on state of redstone lamp, and your two blocks functions similarly (can be done with a single tile entity). It sounds like the only difference between the two blocks is the texture of the block, which would be easier to manage with block state properties.

Edited by DavidM

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.