Jump to content

[1.8] Slab collision detection


tailslide

Recommended Posts

Hello, I am new here.  I am working my way through the book 'Minecraft Mod development in 24 hours' and it's pretty great but unfortunately it was written before 1.8.  I am learning a lot converting all the examples over but I have gotten a little stumped after making my own metadata block that is two full size blocks and a half height slab.  Everything looks great and places great but when I walk over the blocks it seems like it is basing the collision detection on the block I am looking at rather than the block I am standing on.  For example, if I look at a half height block I can walk onto a full height block, and if I look at a full height block I can't walk onto a half height block.

 

Any ideas?

 

Block Class:

 

 

package com.firelightsoftware.fsimod;

 

import java.util.List;

import java.util.Random;

 

import net.minecraft.block.Block;

import net.minecraft.block.material.Material;

import net.minecraft.block.properties.IProperty;

import net.minecraft.block.properties.PropertyEnum;

import net.minecraft.block.properties.PropertyInteger;

import net.minecraft.block.state.BlockState;

import net.minecraft.block.state.IBlockState;

import net.minecraft.client.Minecraft;

import net.minecraft.client.resources.model.ModelBakery;

import net.minecraft.client.resources.model.ModelResourceLocation;

import net.minecraft.creativetab.CreativeTabs;

import net.minecraft.item.Item;

import net.minecraft.item.ItemStack;

import net.minecraft.util.BlockPos;

import net.minecraft.util.EnumFacing;

import net.minecraft.util.IStringSerializable;

import net.minecraft.world.IBlockAccess;

import net.minecraftforge.fml.relauncher.Side;

import net.minecraftforge.fml.relauncher.SideOnly;

 

public class BlockSamStone extends Block {

public static final PropertyEnum BlockType =  PropertyEnum.create("blocktype", EnumSamBlockTypes.class);

private String myname = "samstone";

public String getName() { return myname; }

public BlockSamStone()

{

super(Material.rock);

// setBlockName(FsiMod.MODID + "_" + myname); obsolete in 1.8

setUnlocalizedName(FsiMod.MODID + "_" + myname);

//setTextureName(FsiMod.MODID + ":" + name); obsolete in 1.8

setCreativeTab(CreativeTabs.tabBlock);

setHardness(2F); // how hard it is to break

setResistance(5F); // how hard it is to explode with TNT/creeper

setStepSound(soundTypeStone); // determines sound it makes when walked on / mined

setHarvestLevel("pickaxe",2); // need an iron pickaxe or better to harvest materials from it

}

 

 

///  For some reason this is setting collision bounds for block

//  we are walking on based on the block we are looking at???

//

@Override

    public void setBlockBoundsBasedOnState(IBlockAccess worldIn, BlockPos pos)

    {

getBlockState();

IBlockState iblockstate = worldIn.getBlockState(pos);

        if (iblockstate.getBlock() == this)

        {

EnumSamBlockTypes btype = (EnumSamBlockTypes)(iblockstate.getValue(BlockType));

 

        switch (btype)

        {

        case SAMSTONE_ORE:

        this.setBlockBounds(0.0F, 0F, 0.0F, 1.0F, 1.0F, 1.0F);

        break;

        case SAMSTONE_WALL:

        this.setBlockBounds(0.0F, 0F, 0.0F, 1.0F, 1.0F, 1.0F);

        break;

        case SAMSTONE_SLAB:

        this.setBlockBounds(0.0F, 0F, 0.0F, 1.0F, 0.5F, 1.0F);

        break;

        }

 

        }

    }

 

@Override

public boolean isOpaqueCube()

{

return false;  //tells minecraft we can see through parts of this cube (not a full cube)

};

 

@Override

protected BlockState createBlockState()

{

    return new BlockState(this, new IProperty[] {BlockType});

}

 

/**

* Convert the given metadata into a BlockState for this Block

*/

@Override

public IBlockState getStateFromMeta(int meta)

{

    return getDefaultState().withProperty(BlockType, EnumSamBlockTypes.byMetadata(meta));

}

 

/**

* Convert the BlockState into the correct metadata value

*/

@Override

public int getMetaFromState(IBlockState state)

{

        return ((EnumSamBlockTypes)state.getValue(BlockType)).getMetadata();

}

 

 

@Override

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

    {

        return FsiMod.samdust;

    }

 

@SuppressWarnings({ "unchecked", "rawtypes"})

@SideOnly(Side.CLIENT)

@Override

    public void getSubBlocks(Item itemIn, CreativeTabs tab, List list)

    {

// make sure all metadata blocks show up in creative tab

        for (int var4=0; var4 < EnumSamBlockTypes.values().length; ++var4)

        {

        list.add(new ItemStack(itemIn, 1, var4));

        }

    }

 

 

 

public void RegisterModels()

{

    // This is the new 1.8 way of registering textures.

// required in order for the renderer to know how to render your item.  Likely to change in the near future.

    final int DEFAULT_ITEM_SUBTYPE = 0;

    ModelResourceLocation itemModelResourceLocation = new ModelResourceLocation(FsiMod.MODID + ":" + myname, "inventory");

String[] initstr;

initstr= new String[EnumSamBlockTypes.values().length];

int x = 0;

for (EnumSamBlockTypes btype : EnumSamBlockTypes.values() )

{

initstr[x]=  FsiMod.MODID +":" + btype.toString();

ModelResourceLocation itemModelResourceLocation1 = new ModelResourceLocation(initstr[x], "inventory");

Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(Item.getItemFromBlock(this), btype.meta, itemModelResourceLocation1);

x++;

}

 

ModelBakery.addVariantName(Item.getItemFromBlock(this), initstr);

 

}

 

 

  // create a new enum for our block types, with some supporting methods to convert to & from metadata, and to get

  //  human-readable names.

  public static enum EnumSamBlockTypes implements IStringSerializable

  {

    SAMSTONE_ORE(0, "samstone_ore"),

    SAMSTONE_WALL(1, "samstone_wall"),

    SAMSTONE_SLAB(2, "samstone_slab");

   

    public int getMetadata()

    {

      return this.meta;

    }

 

    @Override

    public String toString()

    {

      return this.name;

    }

 

    public static EnumSamBlockTypes byMetadata(int meta)

    {

      if (meta < 0 || meta >= META_LOOKUP.length)

      {

        meta = 0;

      }

 

      return META_LOOKUP[meta];

    }

 

    public String getName()

    {

      return this.name;

    }

 

    private final int meta;

    private final String name;

    private static final EnumSamBlockTypes[] META_LOOKUP = new EnumSamBlockTypes[values().length];

 

    private EnumSamBlockTypes(int i_meta, String i_name)

    {

      this.meta = i_meta;

      this.name = i_name;

    }

 

    static

    {

      for (EnumSamBlockTypes btype : values()) {

        META_LOOKUP[btype.getMetadata()] = btype;

      }

    }

  }

 

}

 

 

 

Link to comment
Share on other sites

You need to override getCollisionBoundingBox or addCollisionBoxesToList. These two is called during the collision check.

 

Thanks that works perfectly. Here is the override in case it helps anyone.

 

    @Override
    public AxisAlignedBB getCollisionBoundingBox(World worldIn, BlockPos pos, IBlockState state)
    {
	float f = 1.0F;
	IBlockState iblockstate = worldIn.getBlockState(pos);
     if (iblockstate.getBlock() == this)
        {
			EnumSamBlockTypes btype = (EnumSamBlockTypes)(iblockstate.getValue(BlockType));

	        switch (btype)
	        {
		        case SAMSTONE_SLAB:
		            f = 0.5F;
		        default:
	        }

        }
     return new AxisAlignedBB((double)pos.getX() + this.minX, (double)pos.getY() + this.minY, (double)pos.getZ() + this.minZ, (double)pos.getX() + this.maxX, (double)((float)pos.getY() + (float) f), (double)pos.getZ() + this.maxZ);
    }

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.