Jump to content

Recommended Posts

Posted (edited)

Hey guys !
I'm trying to make an animated Block (opened and closed block).
I don't know how to start it because forge documentation didn't help me to figure out how to do this.
But i already did that : trough Propertybool and with  Blockstate JSON, i made different model of the block. If i change a boolean in my block class, my block is turning closed or opened peferctly the way i want.
Only the problem is that... well... if I try to change it in game, the change didn't take place. I have to restart the map (leave and directly go back in) to see the change.

SetBlockState didn't make anything change

If someone has a tutorial to show me, don't hésitate ?

Here my Block

Spoiler

package microcellule.minelyoko.Block;

import java.util.List;

import javax.annotation.Nullable;

import com.google.common.collect.Lists;

import microcellule.minelyoko.Interface.IConnectableSupCalc;
import net.minecraft.block.Block;
import net.minecraft.block.BlockDirectional;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;

public class ScannerBlock extends Block implements IConnectableSupCalc
{

    public static final String NAME = "scannerblock";
     boolean  closed =false ;

 

    public static final PropertyBool DOWN = PropertyBool.create("down");
    public static final PropertyBool DOWNCLOSE = PropertyBool.create("down_close");
    public static final PropertyBool MIDDLE = PropertyBool.create("middle");
    public static final PropertyBool MIDDLECLOSE = PropertyBool.create("middle_close");
    public static final PropertyBool UP = PropertyBool.create("up");
    public static final PropertyBool UPCLOSE = PropertyBool.create("up_close");

    public static final PropertyDirection FACING = BlockDirectional.FACING;


    protected static final AxisAlignedBB HITBOX = new AxisAlignedBB(0.4375D, 0.0D, 0.4375D, 0.5625D, 0.125D, 0.5625D);

  

 public ScannerBlock()
    {
        super(Material.IRON);        
        setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(DOWN, Boolean.valueOf(false)).withProperty(DOWNCLOSE, Boolean.valueOf(false)).withProperty(MIDDLE, Boolean.valueOf(false)).withProperty(MIDDLECLOSE, Boolean.valueOf(false)).withProperty(UP, Boolean.valueOf(false)).withProperty(UPCLOSE, Boolean.valueOf(false)));
        setResistance(5.0F);
        setHardness(3.0F);
        setCreativeTab(CreativeTabs.BUILDING_BLOCKS);
        setSoundType(SoundType.METAL);
        MineLyokoBlocks.setBlockName(this, NAME);

    }

    @Override
    public boolean isFullCube(IBlockState state)
    {
        return false;
    }

    @Override
    public boolean isOpaqueCube(IBlockState state)
    {
        return false;
    }
    @Override
    public IBlockState getActualState(IBlockState state, IBlockAccess world, BlockPos pos)
    {
        return state.withProperty(DOWN, Boolean.valueOf(checkId(0,world,pos))).withProperty(DOWNCLOSE, Boolean.valueOf(checkId(1,world,pos))).withProperty(MIDDLE, Boolean.valueOf(checkId(2,world,pos))).withProperty(MIDDLECLOSE, Boolean.valueOf(checkId(3,world,pos))).withProperty(UP,Boolean.valueOf(checkId(4,world,pos))).withProperty(UPCLOSE,Boolean.valueOf(checkId(5,world,pos)));
    }

    private boolean checkId(int id,IBlockAccess world, BlockPos pos)
    {
        boolean result = false;
        switch (id)
        {
        case 0:
            if (world.getBlockState(pos.down()).getBlock() != this && !closed)
            {
                result=true;
            }
            break;
        case 1:
            if (world.getBlockState(pos.down()).getBlock() != this && closed)
            {
                result=true;
            }
            break;
        case 2:
            if (world.getBlockState(pos.down()).getBlock() instanceof ScannerBlock && (world.getBlockState(pos.up()).getBlock() instanceof ScannerBlock && !closed))
            {
                result=true;
            }
            break;
        case 3:
            if (world.getBlockState(pos.down()).getBlock() instanceof ScannerBlock && (world.getBlockState(pos.up()).getBlock() instanceof ScannerBlock && closed))
            {
                result=true;
            }
            break;
        case 4:
            if (world.getBlockState(pos.down()).getBlock() instanceof ScannerBlock && world.getBlockState(pos.down(2)).getBlock() instanceof ScannerBlock && world.getBlockState(pos.down(3)).getBlock() instanceof ScannerBlock && !closed)
            {
                result=true;
            }
            break;
        case 5:
            if (world.getBlockState(pos.down()).getBlock() instanceof ScannerBlock && world.getBlockState(pos.down(2)).getBlock() instanceof ScannerBlock && world.getBlockState(pos.down(3)).getBlock() instanceof ScannerBlock && closed)
            {
                result=true;
            }
            break;
        default: result=false;
        }
        return result;
    }

    @Override
    protected BlockStateContainer createBlockState()
    {
        return new BlockStateContainer(this, new IProperty[] {FACING, DOWN, DOWNCLOSE, MIDDLE, MIDDLECLOSE, UP, UPCLOSE});
    }

  

    @Override
    public boolean canPlaceBlockAt(World worldIn, BlockPos pos)
    {
        if (worldIn.getBlockState(pos.up()).getBlock()== Blocks.AIR && worldIn.getBlockState(pos.up(2)).getBlock()== Blocks.AIR && worldIn.getBlockState(pos.up(3)).getBlock()== Blocks.AIR )
        {
            return true;
        }
        else return false;

    }

    @Override
    public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
    {
        if(worldIn.getBlockState(pos.down()).getBlock() != this)
        {
            worldIn.setBlockState(pos.up(), state);
            worldIn.setBlockState(pos.up(2), state);
            worldIn.setBlockState(pos.up(3), state);
        }


        super.onBlockAdded(worldIn, pos, state);
    }


    public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
    {
        super.breakBlock(worldIn, pos, state);

        if(worldIn.getBlockState(pos.down()).getBlock() instanceof ScannerBlock)
        {
            worldIn.setBlockToAir(pos.down());
        }
        if(worldIn.getBlockState(pos.up()).getBlock() instanceof ScannerBlock)
        {
            worldIn.setBlockToAir(pos.up());
        }

    }

    @Override
    public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
    {
        worldIn.setBlockState(pos, state.withProperty(FACING, EnumFacing.getDirectionFromEntityLiving(pos, placer)), 2);
    }

    public IBlockState getStateFromMeta(int meta)
    {
        return this.getDefaultState().withProperty(FACING, EnumFacing.getFront(meta & 7));
    }

    public int getMetaFromState(IBlockState state)
    {
        int i = 0;
        i = i | ((EnumFacing)state.getValue(FACING)).getIndex();
        return i;
    }

    public void setClosed(boolean statut)
    {
        this.closed = statut;
    }

    public boolean getClosed()
    {
        return closed;    
    }

}

 


(i think you can see i'm still a beginner with all this line of code. Yup  i can agree. I will learn how to do better later :p... And at least it works :p)

Edited by Microcellule

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.