Jump to content

Custom Door[SOLVED]


DoorCloser

Recommended Posts

I'm creating my custom door. So here is my codes:

 

BlockMyDoor:

 

package halflifemod.block;

import halflifemod.main.mod_HalflifeMod;

import java.util.Random;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.BlockDoor;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.IconFlipped;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.util.Icon;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;

public class BlockGreenDoor extends BlockDoor {

private static final String[] doorIconNames = new String[] {"greendoor_lower", "greendoor_upper"};

/** Used for pointing at icon names. */
    private final int doorTypeForIcon;
    @SideOnly(Side.CLIENT)
    private Icon[] iconArray;
    
    public BlockGreenDoor(int par1, Material par2Material)
    {
        super(par1, par2Material);

        if (par2Material == Material.iron)
        {
            this.doorTypeForIcon = 2;
        }
        else
        {
            this.doorTypeForIcon = 0;
        }

        float f = 0.5F;
        float f1 = 1.0F;
        this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, f1, 0.5F + f);
    }

    public int quantityDropped(Random random) {
	return 1;
}
   
    public int idDropped(int par1, Random par2Random, int par3)
    {
        return mod_HalflifeMod.greenDoorItem.itemID;
    }
    @SideOnly(Side.CLIENT)
    public int idPicked(World par1World, int par2, int par3, int par4)
    {
        return mod_HalflifeMod.greenDoorItem.itemID;
    }
    public String getTextureFIle(){
	return "/textures/blocks/greenDoor.png";
}
}

 

 

ItemMyDoor

 

package halflifemod.item;

import halflifemod.block.BlockGreenDoor;
import halflifemod.main.mod_HalflifeMod;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;

public class ItemGreenDoor extends Item
{
private Material doorMaterial;
    public ItemGreenDoor(int par1, Material par2Material)
    {
        super(par1);
        this.doorMaterial = par2Material;
        this.maxStackSize = 1;
        this.setCreativeTab(CreativeTabs.tabRedstone);
    }

    /**
     * Callback for item usage. If the item does something special on right clicking, he will have one of those. Return
     * True if something happen and false if it don't. This is for ITEMS, not BLOCKS
     */
    public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10)
    {
        if (par7 != 1)
        {
            return false;
        }
        else
        {
            ++par5;
            Block block;
            BlockGreenDoor greendoar;

            if (this.doorMaterial == Material.wood)
            {
                block = mod_HalflifeMod.greenDoor;
            }
            else
            {
                block = Block.doorIron;
            }

            if (par2EntityPlayer.canPlayerEdit(par4, par5, par6, par7, par1ItemStack) && par2EntityPlayer.canPlayerEdit(par4, par5 + 1, par6, par7, par1ItemStack))
            {
                if (!block.canPlaceBlockAt(par3World, par4, par5, par6))
                {
                    return false;
                }
                else
                {
                    int i1 = MathHelper.floor_double((double)((par2EntityPlayer.rotationYaw + 180.0F) * 4.0F / 360.0F) - 0.5D) & 3;
                    placeDoorBlock(par3World, par4, par5, par6, i1, block);
                    --par1ItemStack.stackSize;
                    return true;
                }
            }
            else
            {
                return false;
            }
        }
    }

    public static void placeDoorBlock(World par0World, int par1, int par2, int par3, int par4, Block par5Block)
    {
        byte b0 = 0;
        byte b1 = 0;

        if (par4 == 0)
        {
            b1 = 1;
        }

        if (par4 == 1)
        {
            b0 = -1;
        }

        if (par4 == 2)
        {
            b1 = -1;
        }

        if (par4 == 3)
        {
            b0 = 1;
        }

        int i1 = (par0World.isBlockNormalCube(par1 - b0, par2, par3 - b1) ? 1 : 0) + (par0World.isBlockNormalCube(par1 - b0, par2 + 1, par3 - b1) ? 1 : 0);
        int j1 = (par0World.isBlockNormalCube(par1 + b0, par2, par3 + b1) ? 1 : 0) + (par0World.isBlockNormalCube(par1 + b0, par2 + 1, par3 + b1) ? 1 : 0);
        boolean flag = par0World.getBlockId(par1 - b0, par2, par3 - b1) == par5Block.blockID || par0World.getBlockId(par1 - b0, par2 + 1, par3 - b1) == par5Block.blockID;
        boolean flag1 = par0World.getBlockId(par1 + b0, par2, par3 + b1) == par5Block.blockID || par0World.getBlockId(par1 + b0, par2 + 1, par3 + b1) == par5Block.blockID;
        boolean flag2 = false;

        if (flag && !flag1)
        {
            flag2 = true;
        }
        else if (j1 > i1)
        {
            flag2 = true;
        }

        par0World.setBlock(par1, par2, par3, par5Block.blockID, par4, 2);
        par0World.setBlock(par1, par2 + 1, par3, par5Block.blockID, 8 | (flag2 ? 1 : 0), 2);
        par0World.notifyBlocksOfNeighborChange(par1, par2, par3, par5Block.blockID);
        par0World.notifyBlocksOfNeighborChange(par1, par2 + 1, par3, par5Block.blockID);
    }
}

 

 

MainClass registry and stuff:

 

 


public final static Item greenDoorItem = new ItemGreenDoor(1505, Material.wood).setUnlocalizedName("greenDoor");

public static final Block greenDoor = (new BlockGreenDoor(1015, Material.wood)).setHardness(3.0F).setStepSound(Block.soundWoodFootstep).setUnlocalizedName("doorWood");

//REGISTRY

//Item
LanguageRegistry.addName(greenDoorItem, "Green Door");

//Block
GameRegistry.registerBlock(greenDoor, "greenDoor");
LanguageRegistry.addName(greenDoor, "Green Door");

 

 

That seems ok, but when i spawn my door, it basicly spawns the original wooden door. I think the problem is in Material.wood, but im not sure. Could you guys help?

Link to comment
Share on other sites

A little bit more explaination ( not bump ). So this code says, that if i use Material.wood, it will spawn my door, but if the Material will be Material.iron, it will spawan Block.doorIron:

public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10)
    {
        if (par7 != 1)
        {
            return false;
        }
        else
        {
            ++par5;
            Block block;
            

            if (this.doorMaterial == Material.wood)
            {
                block = mod_HalflifeMod.greenDoor;
            }
            else
            {
                block = Block.doorIron;
            }

            if (par2EntityPlayer.canPlayerEdit(par4, par5, par6, par7, par1ItemStack) && par2EntityPlayer.canPlayerEdit(par4, par5 + 1, par6, par7, par1ItemStack))
            {
                if (!block.canPlaceBlockAt(par3World, par4, par5, par6))
                {
                    return false;
                }
                else
                {
                    int i1 = MathHelper.floor_double((double)((par2EntityPlayer.rotationYaw + 180.0F) * 4.0F / 360.0F) - 0.5D) & 3;
                    placeDoorBlock(par3World, par4, par5, par6, i1, block);
                    --par1ItemStack.stackSize;
                    return true;
                }
            }
            else
            {
                return false;
            }
        }
    }

 

So, my Material is set to wood in the main class:

 

public final static Item greenDoorItem = new ItemGreenDoor(1505, Material.wood).setUnlocalizedName("greenDoor");

 

But when i spawn this door with Material.wood set on it, it spawns just a basic wooden door, but that block = mod_HalflifeMod.greenDoor; says to spawn greenDoor. When i use Material.iron it actually spawns iron Door, so thats ok. Then i dont know whats wrong with the code. Can you guys help?

Link to comment
Share on other sites

I have never used doors, but I suggest you take a good long look at BlockDoor and figure out how it textures itself. Then  you are going to need to override whatever that is and make it do your textures.

 

Ok, so i've added those strings and overrided them and it works! Finally. Thanks alot, never forgot your help. I'll give ya a "Thank You"

Code:

@SideOnly(Side.CLIENT)
    
    public Icon getIcon(int par1, int par2)
    {
        return this.iconArray[this.doorTypeForIcon];
    }
    @SideOnly(Side.CLIENT)
    @Override
    public Icon getBlockTexture(IBlockAccess par1IBlockAccess, int par2, int par3, int par4, int par5)
    {
        if (par5 != 1 && par5 != 0)
        {
            int i1 = this.getFullMetadata(par1IBlockAccess, par2, par3, par4);
            int j1 = i1 & 3;
            boolean flag = (i1 & 4) != 0;
            boolean flag1 = false;
            boolean flag2 = (i1 &  != 0;

            if (flag)
            {
                if (j1 == 0 && par5 == 2)
                {
                    flag1 = !flag1;
                }
                else if (j1 == 1 && par5 == 5)
                {
                    flag1 = !flag1;
                }
                else if (j1 == 2 && par5 == 3)
                {
                    flag1 = !flag1;
                }
                else if (j1 == 3 && par5 == 4)
                {
                    flag1 = !flag1;
                }
            }
            else
            {
                if (j1 == 0 && par5 == 5)
                {
                    flag1 = !flag1;
                }
                else if (j1 == 1 && par5 == 3)
                {
                    flag1 = !flag1;
                }
                else if (j1 == 2 && par5 == 4)
                {
                    flag1 = !flag1;
                }
                else if (j1 == 3 && par5 == 2)
                {
                    flag1 = !flag1;
                }

                if ((i1 & 16) != 0)
                {
                    flag1 = !flag1;
                }
            }

            return this.iconArray[this.doorTypeForIcon + (flag1 ? doorIconNames.length : 0) + (flag2 ? 1 : 0)];
        }
        else
        {
            return this.iconArray[this.doorTypeForIcon];
        }
    }

    @SideOnly(Side.CLIENT)
    @Override
    public void registerIcons(IconRegister par1IconRegister)
    {
        this.iconArray = new Icon[doorIconNames.length * 2];

        for (int i = 0; i < doorIconNames.length; ++i)
        {
            this.iconArray[i] = par1IconRegister.registerIcon(doorIconNames[i]);
            this.iconArray[i + doorIconNames.length] = new IconFlipped(this.iconArray[i], true, false);
        }
    }

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.



×
×
  • Create New...

Important Information

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