Jump to content

[Solved] [1.8] How to Make a Custom Door Open from Only One Side?


サムエル

Recommended Posts

So I have created a custom iron door that is different in that it's indestructible. However, I also want it to only open from behind which essentially means the door will only open if it is receiving redstone power from behind the door (anything in front will be ignored). I believe I understand how this would be done (using the onNeighbourBlockChange() method), but I don't understand how to get the position of the door from within that method to find where the neighbour block is relative to the door. For all I know it could be in the first part of the code already and I'm just not seeing it. Either way, I need some help with this.

 

Here is my custom door code:

 

BlockChaosDoorUnbreakable.java

 

 

package com.samuel.chaosblock;

 

import java.util.Random;

 

import net.minecraft.block.Block;

import net.minecraft.block.BlockContainer;

import net.minecraft.block.BlockDoor;

import net.minecraft.block.material.Material;

import net.minecraft.block.properties.PropertyBool;

import net.minecraft.block.state.IBlockState;

import net.minecraft.creativetab.CreativeTabs;

import net.minecraft.entity.Entity;

import net.minecraft.entity.EntityLivingBase;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.init.Blocks;

import net.minecraft.init.Items;

import net.minecraft.item.Item;

import net.minecraft.tileentity.TileEntity;

import net.minecraft.util.AxisAlignedBB;

import net.minecraft.util.BlockPos;

import net.minecraft.util.EnumFacing;

import net.minecraft.world.Explosion;

import net.minecraft.world.World;

import net.minecraftforge.fml.relauncher.Side;

import net.minecraftforge.fml.relauncher.SideOnly;

 

public class BlockChaosDoorUnbreakable extends BlockDoor {

   

    public static final String name = "unbreakable_chaos_door";

   

    public BlockChaosDoorUnbreakable(String unlocalizedName, Material material, float hardness, float resistance) {

        super(material);

        this.setUnlocalizedName(unlocalizedName);

        this.setHardness(hardness);

        this.setResistance(resistance);

    }

 

   

    public BlockChaosDoorUnbreakable(String unlocalizedName, float hardness, float resistance) {

        this(unlocalizedName, Material.iron, hardness, resistance);

    }

 

    public BlockChaosDoorUnbreakable(String unlocalizedName) {

        this(unlocalizedName, -1.0f, 18000000.0f);

    }

   

    @Override

    @SideOnly(Side.CLIENT)

    public Item getItem(World worldIn, BlockPos pos)

    {

        return ChaosBlock.itemChaosDoorUnbreakable;

    }

   

    @Override

    public Item getItemDropped(IBlockState state, Random rand, int fortune)

    {

        return state.getValue(HALF) == BlockDoor.EnumDoorHalf.UPPER ? null : this.getItem();

    }

   

    private Item getItem() {

    return ChaosBlock.itemChaosDoorUnbreakable;

    }

   

    public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ) {

    return false; //Allow items to interact with the door

    }

   

    /**

    * Called when a neighboring block changes.

    */

    @Override

    public void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, Block neighborBlock)

    {

    int combinedMeta = combineMetadata(worldIn, pos);

    EnumFacing enumfacing = getFacing(combinedMeta);

   

        if (state.getValue(HALF) == BlockDoor.EnumDoorHalf.UPPER)

        {

            BlockPos blockpos1 = pos.down();

            IBlockState iblockstate1 = worldIn.getBlockState(blockpos1);

 

            if (iblockstate1.getBlock() != this)

            {

                worldIn.setBlockToAir(pos);

            }

            else if (neighborBlock != this)

            {

                this.onNeighborBlockChange(worldIn, blockpos1, iblockstate1, neighborBlock);

            }

        }

        else

        {

            boolean flag1 = false;

            BlockPos blockpos2 = pos.up();

            IBlockState iblockstate2 = worldIn.getBlockState(blockpos2);

 

            if (iblockstate2.getBlock() != this)

            {

                worldIn.setBlockToAir(pos);

                flag1 = true;

            }

 

            if (!World.doesBlockHaveSolidTopSurface(worldIn, pos.down()))

            {

                worldIn.setBlockToAir(pos);

                flag1 = true;

 

                if (iblockstate2.getBlock() == this)

                {

                    worldIn.setBlockToAir(blockpos2);

                }

            }

 

            if (flag1)

            {

                if (!worldIn.isRemote)

                {

                    this.dropBlockAsItem(worldIn, pos, state, 0);

                }

            }

            else

            {

                boolean flag = worldIn.isBlockPowered(pos) || worldIn.isBlockPowered(blockpos2);

 

                if ((flag || neighborBlock.canProvidePower()) && neighborBlock != this && flag != ((Boolean)iblockstate2.getValue(POWERED)).booleanValue())

                {

                    /*Where I want to set a boolean indicating whether the power source is

                  behind the door or not and cancel the following code if the power

                  source is not behind*/

               

                    worldIn.setBlockState(blockpos2, iblockstate2.withProperty(POWERED, Boolean.valueOf(flag)), 2);

 

                    if (flag != ((Boolean)state.getValue(OPEN)).booleanValue())

                    {

                        worldIn.setBlockState(pos, state.withProperty(OPEN, Boolean.valueOf(flag)), 2);

                        worldIn.markBlockRangeForRenderUpdate(pos, pos);

                        worldIn.playAuxSFXAtEntity((EntityPlayer)null, flag ? 1003 : 1006, pos, 0);

                    }

                }

            }

        }

    }

 

    public String getName() {

        return name;

    }

   

}

 

 

Link to comment
Share on other sites

First you need to know which way your door is facing - BlockDoor has a property for this that you can get by accessing your block's state. Once you know the door's facing, you can use BlockPos#offset(facing.getOpposite()) to get the block behind the door and check if that is the one giving it power.

 

Keep in mind that the state storing the door's facing is in the block comprising the lower half of the door.

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.