Jump to content

[Solved][1.8.9] Spawning block with rotation property.


TheRedDevil

Recommended Posts

I've created a block that stores the rotation data inside the tileentity.

 

The problem I'm having is when I right click the custom block I want it to create the same block 4 blocks away.

When I right click the block it places the block but facing North not any of the other directions.

 

The code I've tried to use inside the block code:

public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ) 
    {
        Block testBlock = ModBlocks.testBlock;
        IBlockState testState = testBlock.getStateFromMeta(getFabricType(state));

        //attempts to get the block placed with right property
        worldIn.setBlockState(new BlockPos(pos.getX() + 2, pos.getY(), pos.getZ()), testState.withProperty(FACING, EnumFacing.WEST));

        worldIn.setBlockState(new BlockPos(pos.getX() + 4, pos.getY(), pos.getZ()), ModBlocks.testBlock.getDefaultState().withProperty(FACING, EnumFacing.WEST));


        return true;
    }

 

The tileEntity code: I think the problem is caused by this line "private EnumFacing facing = EnumFacing.NORTH" not sure how I could alter this code to not change a placed block to North rotation.

 

 

public class TileEntityTest extends TileEntity
{
    [color=red]private EnumFacing facing = EnumFacing.NORTH;[/color]

    public EnumFacing getFacing()
    {
        return facing;
    }

    public void setFacing(EnumFacing facing)
    {
        this.facing = facing;
        markDirty();
    }

    @Override
    public void readFromNBT(NBTTagCompound compound)
    {
        super.readFromNBT(compound);
        facing = EnumFacing.getFront(compound.getInteger("facing"));
    }

    @Override
    public void writeToNBT(NBTTagCompound compound)
    {
        super.writeToNBT(compound);
        compound.setInteger("facing", facing.getIndex());
    }

    @Override
    public Packet getDescriptionPacket()
    {
        NBTTagCompound nbttagcompound = new NBTTagCompound();
        this.writeToNBT(nbttagcompound);
        return new S35PacketUpdateTileEntity(this.getPos(), this.getBlockMetadata(), nbttagcompound);
    }

    @Override
    public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity packet)
    {
        readFromNBT(packet.getNbtCompound());
        getWorld().markBlockForUpdate(getPos());
    }

    @Override
    public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newSate)
    {
        return oldState.getBlock() != newSate.getBlock();
    }
}

Link to comment
Share on other sites

If you are storing property in TileEntity - what sets that property? Fields don't get magically assigned, you need to use them somewhere. Does your Block class do this?

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

this is the block code, the facing property gets assigned when the block is placed.

public class BlockTest extends Block
{
    public static final IProperty<EnumFacing> FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
    public static final PropertyEnum BLOCKTYPE = PropertyEnum.create("block", EnumBlockType.class);


    public BlockTest(String unlocalizedName)
    {
        super(Material.stone);
    }

    @Override @SideOnly(Side.CLIENT)
    public void getSubBlocks(Item item, CreativeTabs tab, List<ItemStack> list)
    {
        for(EnumBlockType EnumBlockType : EnumBlockType.values())
        {
            list.add(new ItemStack(item, 1, EnumBlockType.getMetadata()));
        }
    }

    @Override
    public IBlockState getStateFromMeta(int meta)
    {
        return this.getDefaultState().withProperty(BLOCKTYPE, EnumBlockType.byMetaData(meta));
    }

    @Override
    public int getMetaFromState(IBlockState state)
    {
        return ((EnumBlockType)state.getValue(BLOCKTYPE)).getMetadata();
    }

    @Override
    protected BlockState createBlockState()
    {
        return new BlockState(this, BLOCKTYPE, FACING);
    }

    @Override
    public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos)
    {
        return state.withProperty(FACING, getFacing(worldIn, pos));
    }

    @Override
    public boolean hasTileEntity(IBlockState state)
    {
        return true;
    }

    @Override
    public TileEntity createTileEntity(World world, IBlockState state)
    {
        return new TileEntityBlockTest();
    }

    public TileEntityBlockTest getTileEntity(IBlockAccess world, BlockPos pos)
    {
        return (TileEntityBlockTest) world.getTileEntity(pos);
    }

    public EnumFacing getFacing(IBlockAccess world, BlockPos pos)
    {
        return getTileEntity(world, pos).getFacing();
    }

    public void setFacing(IBlockAccess world, BlockPos pos, EnumFacing facing)
    {
        getTileEntity(world, pos).setFacing(facing);
    }

    @Override
    public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
    {
        setFacing(worldIn, pos, BlockPistonBase.getFacingFromEntity(worldIn, pos, placer));
    }

    public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
    {
        Block testBlock = ModBlocks.testBlock;
        IBlockState testState = testBlock.getDefaultState();

        //attempts to get the block placed with right property
        worldIn.setBlockState(new BlockPos(pos.getX() + 2, pos.getY(), pos.getZ()), testState.withProperty(FACING, EnumFacing.WEST));

        worldIn.setBlockState(new BlockPos(pos.getX() + 4, pos.getY(), pos.getZ()), ModBlocks.testBlock.getDefaultState().withProperty(FACING, EnumFacing.WEST));


        return true;
    }
}

Link to comment
Share on other sites

The tileEntity code: I think the problem is caused by this line "private EnumFacing facing = EnumFacing.NORTH" not sure how I could alter this code to not change a placed block to North rotation.

 

That line is likely the problem.  Do what Diesieben07 says and get rid of the TE.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

I've removed the tileEntity and managed to place blocks with the correct blockType property but having few problems merging the facing property together.

 

Believe I need to combine these two lines together but having trouble merging both returns into one

 

Method I'm having trouble getting both property to work together.

@Override
    public int getMetaFromState(IBlockState state)
    {
        return  state.getValue(FACING).getIndex();
        //return state.getValue(BLOCKTYPE).getMetadata();
    }

Link to comment
Share on other sites

Metadata is only 4 bits worth of data.  Facing takes (minimum) 2 of those bits, leaving you 2 (four possible values) for BLOCKTYPE.

 

Show your BLOCKTYPE property declaration/definition.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

That could be the problem then as BLOCKTYPE uses 10 values :/

Thought facing might of taken 1 bit, as I'm just using horizontal properties with 4 values, my mistake xD

 

I could go back to storing the block face in the tileEntity and figure a way to fix my first post problem.

 

 

Link to comment
Share on other sites

Should I still use getStateFromMeta/getMetaFromState to store the "BLOCKTYPE"?

@Override
    public IBlockState getStateFromMeta(int meta)
    {
        return this.getDefaultState().withProperty(BLOCKTYPE, EnumBlockType.byMetaData(meta));
    }

    @Override
    public int getMetaFromState(IBlockState state)
    {
        return ((EnumBlockType)state.getValue(BLOCKTYPE)).getMetadata();
    }

    @Override
    protected BlockState createBlockState()
    {
        return new BlockState(this, BLOCKTYPE, FACING);
    }

    @Override
    public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos)
    {
        return state.withProperty(FACING, getFacing(worldIn, pos));
    }

 

When I place to block ingame it has the correct block type / facing n/e/s/w,

but when I right click the block it's placing the block in the north face not west.

public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
    {
        Block testBlock = ModBlocks.testBlock;
        IBlockState testState = testBlock.getDefaultState();

        worldIn.setBlockState(new BlockPos(pos.getX() + 2, pos.getY(), pos.getZ()), testState.withProperty(FACING, EnumFacing.WEST));
return true;
}

 

 

 

 

Link to comment
Share on other sites

That could be the problem then as BLOCKTYPE uses 10 values :/

Thought facing might of taken 1 bit, as I'm just using horizontal properties with 4 values, my mistake xD

 

I could go back to storing the block face in the tileEntity and figure a way to fix my first post problem.

 

I would store the FACING in the metadata (so many blocks already do, if a mod comes along with a tool that can rotate blocks, your block would be automatically rotatable with no effort if you do) and the BLOCKTYPE in the TileEntity.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

If I did store the BlockType inside the tileEntity is it possible to create recipes that uses each blockType?

Was main reason I stored the BlockType inside the metadata so I could create recipes for each metadata version.

 

I've not looked into 1.8/1.9 recipes wasn't sure if it was possible.

 

I have all code working now will keep it how it is :)

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
    • It is an issue with quark - update it to this build: https://www.curseforge.com/minecraft/mc-mods/quark/files/3642325
    • Remove Instant Massive Structures Mod from your server     Add new crash-reports with sites like https://paste.ee/  
    • Update your drivers: https://www.amd.com/en/support/graphics/amd-radeon-r9-series/amd-radeon-r9-200-series/amd-radeon-r9-280x
  • Topics

×
×
  • Create New...

Important Information

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