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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • For the moment it seems fixed. I deleted the JEI Config file, the Journeymap Server Config file, and disabled both the mods (even while disabled, it would still crash until those config files were gone). They're useful mods and I'd hate to not use it, would just deleting and redownloading the mods work?
    • For hours I have been trying to just instal Mr. Crayfish's Refurbished Furniture Mod, but each step to fix the error codes, the more problems arise. The farthest I got through the steps was getting to the Forge Installer, but once I had selected the file, an error saying "The directory is missing a launcher profile. Please run the minecraft launcher first". At this point I don;'t know what more I can do. Please help.
    • I create my mod pack,yesterday my mod pack is fine but i add one mod and error. I'm delete this mmod but minecraft is still stop on CONFIG_LOAD then I tried to delete config and restart it but again. If you can pleace help me. https://imgur.com/ngZBzuv
    • game crashes before even opening (log:https://mclo.gs/M8xvX7c)
    • I have created a custom entity that extends "TamableAnimal", but I am wanting to have it spawn in the ocean. I have it spawning right now, but it spawns way too frequently even with weight set to 1. I am guessing it is because it is rolling in the spawn pool of land animals since TameableAnimal extends Animal and is different than WaterAnimal, and since no land animals spawn in the ocean it just fills every inch up with my custom entity. I have followed basic tutorials for spawning entities with Forge, but I feel like I am missing something about how to change what spawn pool this custom entity ends up in. Is it possible to change that or do I need to refactor it to be based off of WaterAnimal to get those spawn? My biome modifier JSON file: { "type": "forge:add_spawns", "biomes": "#minecraft:is_ocean", "spawners": { "type": "darwinsmysticalmounts:water_animal", "weight": 20, "minCount": 1, "maxCount": 1 } } My client event: event.register(ModEntityTypes.WATER_ANIMAL.get(), SpawnPlacements.Type.NO_RESTRICTIONS, Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, WaterWyvernEntity::checkCustomWaterAnimalSpawnRules, SpawnPlacementRegisterEvent.Operation.REPLACE); And the actual custom spawn rule that makes it spawn in the water: public static boolean checkCustomWaterAnimalSpawnRules(EntityType<WaterAnimalEntity> pAnimal, LevelAccessor pLevel, MobSpawnType pSpawnType, BlockPos pPos, RandomSource pRandom) { return pPos.getY() > pLevel.getSeaLevel() - 16 && pLevel.getFluidState(pPos.below()).is(FluidTags.WATER); }  
  • Topics

×
×
  • Create New...

Important Information

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