Jump to content

Recommended Posts

Posted

I've been working on a mod and have gotten everything working except for the slabs. I can't get them to stack. I've looked at tutorials and other forums, but nothing is helpful.

 

BlockSlab:

package cherrymod.blocks;

import java.util.Random;
import com.jcraft.jorbis.Block;
import cherrymod.CherryMod;
import net.minecraft.block.BlockSlab;
import net.minecraft.block.BlockSlab.EnumBlockHalf;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public abstract class BlockCherrySlab extends BlockSlab
{
   private static final PropertyBool VARIANT_PROPERTY = PropertyBool.create("variant");
   
   public BlockCherrySlab()
   {
      super(Material.wood);
      
      IBlockState state = this.blockState.getBaseState();
      state = state.withProperty(VARIANT_PROPERTY, false);
      
      if (!this.isDouble())
      {
         state = state.withProperty(HALF, EnumBlockHalf.BOTTOM);
         setCreativeTab(CreativeTabs.tabBlock);
      }
      
      setDefaultState(state);
      setHardness(2.0f);
      setResistance(5.0f);
      setStepSound(soundTypeWood);
      
      useNeighborBrightness = !this.isDouble();
   }
   
   @Override
   @SideOnly(Side.CLIENT)
   public boolean shouldSideBeRendered(IBlockAccess world, BlockPos pos, EnumFacing side)
   {
      if (this.isDouble())
      {
         return side == EnumFacing.DOWN && this.minY > 0.0D ? true : (side == EnumFacing.UP && this.maxY < 1.0D ? true : (side == EnumFacing.NORTH && this.minZ > 0.0D ? true : (side == EnumFacing.SOUTH && this.maxZ < 1.0D ? true : (side == EnumFacing.WEST && this.minX > 0.0D ? true : (side == EnumFacing.EAST && this.maxX < 1.0D ? true : !world.getBlockState(pos).getBlock().isOpaqueCube())))));
      }
      else if (side != EnumFacing.UP && side != EnumFacing.DOWN && !(side == EnumFacing.DOWN && this.minY > 0.0D ? true : (side == EnumFacing.UP && this.maxY < 1.0D ? true : (side == EnumFacing.NORTH && this.minZ > 0.0D ? true : (side == EnumFacing.SOUTH && this.maxZ < 1.0D ? true : (side == EnumFacing.WEST && this.minX > 0.0D ? true : (side == EnumFacing.EAST && this.maxX < 1.0D ? true : !world.getBlockState(pos).getBlock().isOpaqueCube())))))))
      {
         return false;
      }
      else
      {
         BlockPos blockpos = pos.offset(side.getOpposite());
         IBlockState state1 = world.getBlockState(pos);
         IBlockState state2 = world.getBlockState(blockpos);
         boolean flag1 = state1.getBlock() == CherryMod.cherry_half_slab && state1.getValue(HALF) == EnumBlockHalf.TOP;
         boolean flag2 = state2.getBlock() == CherryMod.cherry_half_slab && state2.getValue(HALF) == EnumBlockHalf.TOP;
         
         return flag2 ? (side == EnumFacing.DOWN ? true : (side == EnumFacing.UP && side == EnumFacing.DOWN && this.minY > 0.0D ? true : (side == EnumFacing.UP && this.maxY < 1.0D ? true : (side == EnumFacing.NORTH && this.minZ > 0.0D ? true : (side == EnumFacing.SOUTH && this.maxZ < 1.0D ? true : (side == EnumFacing.WEST && this.minX > 0.0D ? true : (side == EnumFacing.EAST && this.maxX < 1.0D ? true : !world.getBlockState(pos).getBlock().isOpaqueCube()))))) ? true : state1.getBlock() != CherryMod.cherry_half_slab || !flag1)) : (side == EnumFacing.UP ? true : (side == EnumFacing.DOWN && super.shouldSideBeRendered(world, blockpos, side) ? true : state1.getBlock() != CherryMod.cherry_half_slab || flag1));
      }
   }

   @Override
   public Item getItemDropped(IBlockState state, Random rand, int fortune)
   {
      return Item.getItemFromBlock(CherryMod.cherry_half_slab);
   }
   
   @Override
   @SideOnly(Side.CLIENT)
   public Item getItem(World world, BlockPos pos)
   {
      return Item.getItemFromBlock(CherryMod.cherry_half_slab);
   }
   
   @Override
   public String getUnlocalizedName(int meta)
   {
      return this.getUnlocalizedName();
   }
   
   @Override
   protected BlockState createBlockState()
   {
      return this.isDouble() ? new BlockState(this, new IProperty[] {VARIANT_PROPERTY}) : new BlockState(this, new IProperty[] {VARIANT_PROPERTY, HALF});
   }
   
   @Override
   public IProperty getVariantProperty()
   {
      return VARIANT_PROPERTY;
   }
   
   @Override
   public Object getVariant(ItemStack stack)
   {
      return false;
   }
   
   @Override
   public IBlockState getStateFromMeta(int meta)
   {
      IBlockState state = this.getDefaultState().withProperty(VARIANT_PROPERTY, false);
      
      if (!this.isDouble())
      {
         state = state.withProperty(HALF, (meta &  == 0 ? EnumBlockHalf.BOTTOM : EnumBlockHalf.TOP);
      }
      
      return state;
   }
   
   @Override
   public int getMetaFromState(IBlockState state)
   {
      if (this.isDouble())
      {
         return 0;
      }
      
      if ((EnumBlockHalf)state.getValue(HALF) == EnumBlockHalf.TOP)
      {
         return 8;
      }
      else
      {
         return 0;
      }
   }
   
   @Override
   public int damageDropped(IBlockState state)
   {
      return 0;
   }
}

 

HalfSlab:

package cherrymod.blocks;

import cherrymod.CherryMod;
import cherrymod.items.ItemCherrySlab;
import net.minecraft.block.Block;
import net.minecraftforge.fml.common.registry.GameRegistry;

public class BlockCherryHalfSlab extends BlockCherrySlab
{
   String name;
   
   public BlockCherryHalfSlab(String name)
   {
      super();
      this.name = name;
      
      setUnlocalizedName(CherryMod.MODID + "_" + name);
   }
   
   @Override
   public boolean isDouble()
   {
      return false;
   }
   
   public String getName()
   {
      return this.name;
   }
}

 

DoubleSlab:

package cherrymod.blocks;

import cherrymod.CherryMod;
import cherrymod.items.ItemCherrySlab;
import net.minecraft.block.properties.IProperty;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.common.registry.GameRegistry;

public class BlockCherryDoubleSlab extends BlockCherrySlab
{
   String name;
   
   public BlockCherryDoubleSlab(String name)
   {
      super();
      this.name = name;
      
      setUnlocalizedName(CherryMod.MODID + "_" + name);
   }
   
   @Override
   public boolean isDouble()
   {
      return true;
   }
   
   public String getName()
   {
      return this.name;
   }
}

 

ItemSlab:

package cherrymod.items;

import cherrymod.CherryMod;
import cherrymod.blocks.BlockCherryDoubleSlab;
import cherrymod.blocks.BlockCherryHalfSlab;
import net.minecraft.block.Block;
import net.minecraft.block.BlockSlab;
import net.minecraft.block.BlockSlab.EnumBlockHalf;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class ItemCherrySlab extends ItemBlock
{
   private BlockCherryHalfSlab singleSlab;
   private BlockCherryDoubleSlab doubleSlab;
   
   public ItemCherrySlab(Block block, BlockCherryHalfSlab singleSlab, BlockCherryDoubleSlab doubleSlab)
   {
      super(block);
      this.singleSlab = singleSlab;
      this.doubleSlab = doubleSlab;
   }
   
   @Override
   public int getMetadata(int damage)
   {
      return 0;
   }
   
   @Override
   public String getUnlocalizedName(ItemStack stack)
   {
      return this.singleSlab.getUnlocalizedName();
   }
   
   @Override
   public String getUnlocalizedName()
   {
      return this.singleSlab.getUnlocalizedName();
   }
   
   public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
   {
      if (stack.stackSize == 0)
      {
         return false;
      }
      else if (!player.canPlayerEdit(pos.offset(side), side, stack))
      {
         return false;
      }
      else
      {
         Object object = this.singleSlab;
         IBlockState blockstate = world.getBlockState(pos);
         
         if (blockstate.getBlock() == this.singleSlab)
         {
            IProperty property = this.singleSlab.getVariantProperty();
            Comparable comparable = blockstate.getValue(property);
            EnumBlockHalf blockhalf = (EnumBlockHalf)blockstate.getValue(BlockSlab.HALF);
            
            if ((side == EnumFacing.UP && blockhalf == EnumBlockHalf.BOTTOM || side == EnumFacing.DOWN && blockhalf == EnumBlockHalf.TOP) && comparable == object)
            {
               IBlockState iblockstate = this.doubleSlab.getDefaultState().withProperty(property, comparable);
               
               if (world.checkNoEntityCollision(this.doubleSlab.getCollisionBoundingBox(world, pos, iblockstate)) && world.setBlockState(pos, iblockstate, 3))
               {
                  world.playSoundEffect((double)((float)pos.getX() + 0.5F), (double)((float)pos.getY() + 0.5F), (double)((float)pos.getZ() + 0.5F), this.doubleSlab.stepSound.getPlaceSound(), (this.doubleSlab.stepSound.getVolume() + 1.0F) / 2.0F, this.doubleSlab.stepSound.getFrequency() * 0.8F);
                  --stack.stackSize;
               }
               
               return true;
            }
         }
         
         return this.tryPlace(stack, world, pos.offset(side), object) ? true : super.onItemUse(stack, player, world, pos, side, hitX, hitY, hitZ);
      }
   }
   
   @SideOnly(Side.CLIENT)
   public boolean canPlaceBlockOnSide(World world, BlockPos pos, EnumFacing side, EntityPlayer player, ItemStack stack)
   {
      BlockPos blockpos = pos;
      IProperty property = this.singleSlab.getVariantProperty();
      Object object = this.singleSlab;
      IBlockState blockstate = world.getBlockState(pos);
      
      if (blockstate.getBlock() == this.singleSlab)
      {
         boolean flag = blockstate.getValue(BlockSlab.HALF) == EnumBlockHalf.TOP;
         
         if ((side == EnumFacing.UP && !flag || side == EnumFacing.DOWN && flag) && object == blockstate.getValue(property))
         {
            return true;
         }
      }
      
      pos = pos.offset(side);
      IBlockState iblockstate = world.getBlockState(pos);
      
      return iblockstate.getBlock() == this.singleSlab && object == iblockstate.getValue(property) ? true : super.canPlaceBlockOnSide(world, blockpos, side, player, stack);
   }
   
   private boolean tryPlace(ItemStack stack, World world, BlockPos pos, Object variantInStack)
   {
      IBlockState blockstate = world.getBlockState(pos);
      
      if (blockstate.getBlock() == this.singleSlab)
      {
         Comparable comparable = blockstate.getValue(this.singleSlab.getVariantProperty());
         
         if (comparable == variantInStack)
         {
            IBlockState iblockstate = this.doubleSlab.getDefaultState().withProperty(this.singleSlab.getVariantProperty(), comparable);
            
            if (world.checkNoEntityCollision(this.doubleSlab.getCollisionBoundingBox(world, pos, iblockstate)) && world.setBlockState(pos, iblockstate, 3))
            {
               world.playSoundEffect((double)((float)pos.getX() + 0.5F), (double)((float)pos.getY() + 0.5F), (double)((float)pos.getZ() + 0.5F), this.doubleSlab.stepSound.getPlaceSound(), (this.doubleSlab.stepSound.getVolume() + 1.0F) / 2.0F, this.doubleSlab.stepSound.getFrequency() * 0.8F);
               --stack.stackSize;
            }
            
            return true;
         }
      }
      
      return false;
   }
}

 

Any help would be great!

Posted

Does vanilla Minecraft subclass its slabs as double or half? If it doesn't, then you probably shouldn't either. Otherwise, you need to come up with a way to replace half-slabs with double slabs in the world. Why go to all that trouble when the vanilla code already handles half and double? Isn't that why you derived your cherry slab from BlockSlab in the first place?

 

It looks like you've made a tremendous amount of work for yourself. What am I missing here?

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Posted

Well it turns out the ItemSlab class was checking for a variant that didn't exist so the check for a single slab always returned false.

 

The working code:

   public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
   {
      if (stack.stackSize == 0)
      {
         return false;
      }
      else if (!player.canPlayerEdit(pos.offset(side), side, stack))
      {
         return false;
      }
      else
      {
         Object object = this.singleSlab;
         IBlockState blockstate = world.getBlockState(pos);
         
         if (blockstate.getBlock() == this.singleSlab)
         {
            EnumBlockHalf blockhalf = (EnumBlockHalf)blockstate.getValue(BlockSlab.HALF);
            
            if (side == EnumFacing.UP && blockhalf == EnumBlockHalf.BOTTOM || side == EnumFacing.DOWN && blockhalf == EnumBlockHalf.TOP)
            {
               IBlockState iblockstate = this.doubleSlab.getDefaultState();
               
               if (world.checkNoEntityCollision(this.doubleSlab.getCollisionBoundingBox(world, pos, iblockstate)) && world.setBlockState(pos, iblockstate, 3))
               {
                  world.playSoundEffect((double)((float)pos.getX() + 0.5F), (double)((float)pos.getY() + 0.5F), (double)((float)pos.getZ() + 0.5F), this.doubleSlab.stepSound.getPlaceSound(), (this.doubleSlab.stepSound.getVolume() + 1.0F) / 2.0F, this.doubleSlab.stepSound.getFrequency() * 0.8F);
                  --stack.stackSize;
               }
               
               return true;
            }
         }
         
         return this.tryPlace(stack, world, pos.offset(side), object) ? true : super.onItemUse(stack, player, world, pos, side, hitX, hitY, hitZ);
      }
   }
   
   @SideOnly(Side.CLIENT)
   public boolean canPlaceBlockOnSide(World world, BlockPos pos, EnumFacing side, EntityPlayer player, ItemStack stack)
   {
      BlockPos blockpos = pos;
      Object object = this.singleSlab;
      IBlockState blockstate = world.getBlockState(pos);
      
      if (blockstate.getBlock() == this.singleSlab)
      {
         boolean flag = blockstate.getValue(BlockSlab.HALF) == EnumBlockHalf.TOP;
         
         if (side == EnumFacing.UP && !flag || side == EnumFacing.DOWN && flag)
         {
            return true;
         }
      }
      
      pos = pos.offset(side);
      IBlockState iblockstate = world.getBlockState(pos);
      
      return iblockstate.getBlock() == this.singleSlab ? true : super.canPlaceBlockOnSide(world, blockpos, side, player, stack);
   }
   
   private boolean tryPlace(ItemStack stack, World world, BlockPos pos, Object variantInStack)
   {
      IBlockState blockstate = world.getBlockState(pos);
      
      if (blockstate.getBlock() == this.singleSlab)
      {
         IBlockState iblockstate = this.doubleSlab.getDefaultState();
            
         if (world.checkNoEntityCollision(this.doubleSlab.getCollisionBoundingBox(world, pos, iblockstate)) && world.setBlockState(pos, iblockstate, 3))
         {
            world.playSoundEffect((double)((float)pos.getX() + 0.5F), (double)((float)pos.getY() + 0.5F), (double)((float)pos.getZ() + 0.5F), this.doubleSlab.stepSound.getPlaceSound(), (this.doubleSlab.stepSound.getVolume() + 1.0F) / 2.0F, this.doubleSlab.stepSound.getFrequency() * 0.8F);
            --stack.stackSize;
         }
         
         return true;
      }
      
      return false;
   }

 

To answer your question, (if I'm understanding it correctly) in the BlockSlab class the vanilla Minecraft checks if the slab is a wood_slab or a stone_slab and so I had to override it to check for my slab as well.

 

   @Override
   @SideOnly(Side.CLIENT)
   public boolean shouldSideBeRendered(IBlockAccess world, BlockPos pos, EnumFacing side)
   {
      if (this.isDouble())
      {
         return side == EnumFacing.DOWN && this.minY > 0.0D ? true : (side == EnumFacing.UP && this.maxY < 1.0D ? true : (side == EnumFacing.NORTH && this.minZ > 0.0D ? true : (side == EnumFacing.SOUTH && this.maxZ < 1.0D ? true : (side == EnumFacing.WEST && this.minX > 0.0D ? true : (side == EnumFacing.EAST && this.maxX < 1.0D ? true : !world.getBlockState(pos).getBlock().isOpaqueCube())))));
      }
      else if (side != EnumFacing.UP && side != EnumFacing.DOWN && !(side == EnumFacing.DOWN && this.minY > 0.0D ? true : (side == EnumFacing.UP && this.maxY < 1.0D ? true : (side == EnumFacing.NORTH && this.minZ > 0.0D ? true : (side == EnumFacing.SOUTH && this.maxZ < 1.0D ? true : (side == EnumFacing.WEST && this.minX > 0.0D ? true : (side == EnumFacing.EAST && this.maxX < 1.0D ? true : !world.getBlockState(pos).getBlock().isOpaqueCube())))))))
      {
         return false;
      }
      else
      {
         BlockPos blockpos = pos.offset(side.getOpposite());
         IBlockState state1 = world.getBlockState(pos);
         IBlockState state2 = world.getBlockState(blockpos);
         boolean flag1 = isSlab(state1.getBlock()) && state1.getValue(HALF) == EnumBlockHalf.TOP;
         boolean flag2 = isSlab(state2.getBlock()) && state2.getValue(HALF) == EnumBlockHalf.TOP;
         
         return flag2 ? (side == EnumFacing.DOWN ? true : (side == EnumFacing.UP && side == EnumFacing.DOWN && this.minY > 0.0D ? true : (side == EnumFacing.UP && this.maxY < 1.0D ? true : (side == EnumFacing.NORTH && this.minZ > 0.0D ? true : (side == EnumFacing.SOUTH && this.maxZ < 1.0D ? true : (side == EnumFacing.WEST && this.minX > 0.0D ? true : (side == EnumFacing.EAST && this.maxX < 1.0D ? true : !world.getBlockState(pos).getBlock().isOpaqueCube()))))) ? true : !isSlab(state1.getBlock()) || !flag1)) : (side == EnumFacing.UP ? true : (side == EnumFacing.DOWN && super.shouldSideBeRendered(world, blockpos, side) ? true : !isSlab(state1.getBlock()) || flag1));
      }
   }
   
   protected static boolean isSlab(Block block)
   {
      return block == Blocks.stone_slab || block == Blocks.stone_slab2 || block == Blocks.wooden_slab || block == CherryMod.cherry_half_slab;
   }

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.