Jump to content

rotating block issue


UltraTechX

Recommended Posts

I have this maintenance controller code:

 

gist for the code here : https://gist.github.com/UltraTechX/928e14ca880a64339997

 

public class genericBlockTileEntity extends Block implements ITileEntityProvider {

 

public static final PropertyEnum ROTATION = PropertyEnum.create("rotation", RotationEnum.class);

 

 

private static String name = "genericBlockTileEntity";

 

 

private int rotation;

 

    public genericBlockTileEntity() {

        super(Material.iron);

        GameRegistry.registerTileEntity(genericTileEntity.class, name);

        GameRegistry.registerBlock(this, name);

        this.setCreativeTab(CreativeTabs.tabMisc);

        this.setUnlocalizedName(name);

        this.setDefaultState(this.blockState.getBaseState().withProperty(ROTATION, RotationEnum.NORTH));

        this.rotation = 0;

        this.setHardness(2.0f);

        this.setResistance(6.0f);

        this.setHarvestLevel("pickaxe", 2);

        this.isBlockContainer = true;

    }

 

    @Override

    public TileEntity createNewTileEntity(World worldIn, int meta) {

        return new genericTileEntity();

    }

 

    @Override

    public boolean onBlockEventReceived(World worldIn, BlockPos pos, IBlockState state, int eventID, int eventParam) {

        super.onBlockEventReceived(worldIn, pos, state, eventID, eventParam);

        TileEntity tileentity = worldIn.getTileEntity(pos);

        return tileentity == null ? false : tileentity.receiveClientEvent(eventID, eventParam);

    }

   

    public static String getName(){

    return name;

    }

   

    public int getMetaFromState(IBlockState state)

  {

    return ((RotationEnum)state.getValue(ROTATION)).getMetadata();

  }

 

  protected BlockState createBlockState()

  {

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

  }

 

  public IProperty getRotationProperty()

  {

    return ROTATION;

  }

 

 

 

  public IBlockState getStateFromMeta(int meta)

  {

    return getDefaultState().withProperty(ROTATION, RotationEnum.byMetadata(meta));

  }

   

    @Override

    public void breakBlock(World world, BlockPos pos, IBlockState blockstate) {

        genericTileEntity te = (genericTileEntity) world.getTileEntity(pos);

       

        this.rotation = 0;

       

        super.breakBlock(world, pos, blockstate);

    }

 

    @Override

    public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state){

    genericTileEntity te = new genericTileEntity();

   

    for (EnumFacing direction : EnumFacing.VALUES){ // For each direction

 

BlockPos neighbourPos = pos.offset(direction); // Offset the block's position by 1 block in the current direction

 

IBlockState neighbourState = worldIn.getBlockState(neighbourPos); // Get the IBlockState at the neighboring position

 

Block neighbourBlock = neighbourState.getBlock(); // Get the IBlockState's Block

 

if (neighbourBlock == Generic.railControl && direction != EnumFacing.UP && direction != EnumFacing.DOWN || neighbourBlock == Generic.railStation && direction != EnumFacing.UP && direction != EnumFacing.DOWN || neighbourBlock == Generic.rail && direction != EnumFacing.UP && direction != EnumFacing.DOWN){ // If the neighbouring block is a Control Rail,

if(direction == EnumFacing.NORTH){

this.rotation = 0;

}else if(direction == EnumFacing.SOUTH){

this.rotation = 1;

}else if(direction == EnumFacing.EAST){

this.rotation = 2;

}else if(direction == EnumFacing.WEST){

this.rotation = 3;

}

}

 

}

    }

   

    public void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, Block neighborBlock)

    {

    for (EnumFacing direction : EnumFacing.VALUES){ // For each direction

 

BlockPos neighbourPos = pos.offset(direction); // Offset the block's position by 1 block in the current direction

 

IBlockState neighbourState = worldIn.getBlockState(neighbourPos); // Get the IBlockState at the neighboring position

 

Block neighbourBlock = neighbourState.getBlock(); // Get the IBlockState's Block

 

if (neighbourBlock == Generic.railControl && direction != EnumFacing.UP && direction != EnumFacing.DOWN || neighbourBlock == Generic.railStation && direction != EnumFacing.UP && direction != EnumFacing.DOWN || neighbourBlock == Generic.rail && direction != EnumFacing.UP && direction != EnumFacing.DOWN){ // If the neighbouring block is a Control Rail,

if(direction == EnumFacing.NORTH){

this.rotation = 0;

}else if(direction == EnumFacing.SOUTH){

this.rotation = 1;

}else if(direction == EnumFacing.EAST){

this.rotation = 2;

}else if(direction == EnumFacing.WEST){

this.rotation = 3;

}

}

 

}

    }

   

   

    @Override

  public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos){

      if(this.rotation == 3){

      return state.withProperty(ROTATION, RotationEnum.WEST);

      }else if(this.rotation == 2){

  return state.withProperty(ROTATION, RotationEnum.EAST);

  }else if(this.rotation == 1){

  return state.withProperty(ROTATION, RotationEnum.SOUTH);

  }else{

  return state.withProperty(ROTATION, RotationEnum.NORTH);

  }

 

  }

   

    @Override

    public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {

        if (stack.hasDisplayName()) {

            ((genericTileEntity) worldIn.getTileEntity(pos)).setCustomName(stack.getDisplayName());

        }

    }

   

    @Override

    public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ) {

        if (world.isRemote) {

                player.openGui(Generic.instance, GUIs.genericTileEntity.ordinal(), world, pos.getX(), pos.getY(), pos.getZ());

        }

        return true;

    }

   

   

    public static enum RotationEnum

    implements IStringSerializable

  {

    NORTH(0, "north"), 

    SOUTH(1, "south"),

    EAST(2, "east"),

    WEST(3, "west");

 

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

 

private final int meta;

   

    private final String name;

   

    private RotationEnum(int meta, String name)

    {

        this.meta = meta;

        this.name = name;

    }

 

    public int getMetadata()

    {

        return this.meta;

    }

 

    public String toString()

    {

        return this.name;

    }

    public String getName()

    {

      return this.name;

    }

   

    public static RotationEnum byMetadata(int meta)

    {

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

        {

            meta = 0;

        }

 

        return META_LOOKUP[meta];

    }

   

    static

    {

        RotationEnum[] var0 = values();

        int var1 = var0.length;

 

        for (int var2 = 0; var2 < var1; ++var2)

        {

            RotationEnum var3 = var0[var2];

            META_LOOKUP[var3.getMetadata()] = var3;

        }

    }

  }

   

   

}

 

 

 

Its made so it will rotate if it detects my block next to it, but when one turns they all turn, and it only turns when I break the rail, any ideas? Thanks

 

 

gist can be found here : https://gist.github.com/UltraTechX/928e14ca880a64339997

I'm working on something big!  As long as there arent alot of big issues... [D

Link to comment
Share on other sites

There is only one instance of your Block. Think of the Block class as a BlockType. So, if you change a property of that BlockType (in your case the rotation field), it changes it for the type of block not the individual Block, meaning the Block as a whole (=all Blocks of that type) change.

 

You already have the block state stuff setup to store the rotation (why do you use RotationEnum instead of EnumFacing?), so you just need to use that.

 

ok, so how exactly do I get the entire script set up to work with EnumFacing, would i do a find and replace on Rotation Enum?

I'm working on something big!  As long as there arent alot of big issues... [D

Link to comment
Share on other sites

Not a find and replace exactly, but kinda, yes.

 

ok, so here is my code now:

 

package tutorial.generic;

import net.minecraft.block.Block;
import net.minecraft.block.BlockRailBase;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.IStringSerializable;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.registry.GameRegistry;
import tutorial.generic.railControlBlock.EnumPartType;

public class genericBlockTileEntity extends Block implements ITileEntityProvider {

public static final PropertyEnum ROTATION = PropertyEnum.create("rotation", EnumFacing.class);


private static String name = "genericBlockTileEntity";


private int rotation;

    public genericBlockTileEntity() {
        super(Material.iron);
        GameRegistry.registerTileEntity(genericTileEntity.class, name);
        GameRegistry.registerBlock(this, name);
        this.setCreativeTab(CreativeTabs.tabMisc);
        this.setUnlocalizedName(name);
        this.setDefaultState(this.blockState.getBaseState().withProperty(ROTATION, EnumFacing.NORTH));
        this.rotation = 0;
        this.setHardness(2.0f);
        this.setResistance(6.0f);
        this.setHarvestLevel("pickaxe", 2);
        this.isBlockContainer = true;
    }

    @Override
    public TileEntity createNewTileEntity(World worldIn, int meta) {
        return new genericTileEntity();
    }

    @Override
    public boolean onBlockEventReceived(World worldIn, BlockPos pos, IBlockState state, int eventID, int eventParam) {
        super.onBlockEventReceived(worldIn, pos, state, eventID, eventParam);
        TileEntity tileentity = worldIn.getTileEntity(pos);
        return tileentity == null ? false : tileentity.receiveClientEvent(eventID, eventParam);
    }
    
    public static String getName(){
    	return name;
    }
    
    public int getMetaFromState(IBlockState state)
  {
    return ((EnumFacing)state.getValue(ROTATION)).getMetadata();
  }
  
  protected BlockState createBlockState()
  {
    return new BlockState(this, new IProperty[] { ROTATION });
  }
  
  public IProperty getRotationProperty()
  {
    return ROTATION;
  }


  
  public IBlockState getStateFromMeta(int meta)
  {
    return getDefaultState().withProperty(ROTATION, EnumFacing.byMetadata(meta));
  }
    
    @Override
    public void breakBlock(World world, BlockPos pos, IBlockState blockstate) {
        genericTileEntity te = (genericTileEntity) world.getTileEntity(pos);
        
        this.rotation = 0;
        
        super.breakBlock(world, pos, blockstate);
    }

    @Override
    public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state){
    	genericTileEntity te = new genericTileEntity();
    	
    	for (EnumFacing direction : EnumFacing.VALUES){ // For each direction

		BlockPos neighbourPos = pos.offset(direction); // Offset the block's position by 1 block in the current direction

		IBlockState neighbourState = worldIn.getBlockState(neighbourPos); // Get the IBlockState at the neighboring position

		Block neighbourBlock = neighbourState.getBlock(); // Get the IBlockState's Block

		if (neighbourBlock == Generic.railControl && direction != EnumFacing.UP && direction != EnumFacing.DOWN || neighbourBlock == Generic.railStation && direction != EnumFacing.UP && direction != EnumFacing.DOWN || neighbourBlock == Generic.rail && direction != EnumFacing.UP && direction != EnumFacing.DOWN){ // If the neighbouring block is a Control Rail,
			if(direction == EnumFacing.NORTH){
				this.rotation = 0;
			}else if(direction == EnumFacing.SOUTH){
				this.rotation = 1;
			}else if(direction == EnumFacing.EAST){
				this.rotation = 2;
			}else if(direction == EnumFacing.WEST){
				this.rotation = 3;
			}
		}

	}
    }
    
    public void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, Block neighborBlock)
    {
    	
    	for (EnumFacing direction : EnumFacing.VALUES){ // For each direction

		BlockPos neighbourPos = pos.offset(direction); // Offset the block's position by 1 block in the current direction

		IBlockState neighbourState = worldIn.getBlockState(neighbourPos); // Get the IBlockState at the neighboring position

		Block neighbourBlock = neighbourState.getBlock(); // Get the IBlockState's Block

		if (neighbourBlock == Generic.railControl && direction != EnumFacing.UP && direction != EnumFacing.DOWN || neighbourBlock == Generic.railStation && direction != EnumFacing.UP && direction != EnumFacing.DOWN || neighbourBlock == Generic.rail && direction != EnumFacing.UP && direction != EnumFacing.DOWN){ // If the neighbouring block is a Control Rail,
			if(direction == EnumFacing.NORTH){
				this.rotation = 0;
			}else if(direction == EnumFacing.SOUTH){
				this.rotation = 1;
			}else if(direction == EnumFacing.EAST){
				this.rotation = 2;
			}else if(direction == EnumFacing.WEST){
				this.rotation = 3;
			}
		}

	}
    }
    
    
    @Override
  public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos){
    	  if(this.rotation == 3){
    		  return state.withProperty(ROTATION, EnumFacing.WEST);
    	  }else if(this.rotation == 2){
		  return state.withProperty(ROTATION, EnumFacing.EAST);
	  }else if(this.rotation == 1){
		  return state.withProperty(ROTATION, EnumFacing.SOUTH);
	  }else{
		  return state.withProperty(ROTATION, EnumFacing.NORTH);
	  }
	  
  }
    
    @Override
    public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
        if (stack.hasDisplayName()) {
            ((genericTileEntity) worldIn.getTileEntity(pos)).setCustomName(stack.getDisplayName());
        }
    }
    
    @Override
    public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ) {
        if (world.isRemote) {
                player.openGui(Generic.instance, GUIs.genericTileEntity.ordinal(), world, pos.getX(), pos.getY(), pos.getZ());
        }
        return true;
    }
    
    
    
    
}

 

 

there are two errors, any they are both about not having byMetadata and getMetadata functions in EnumFacing:

 

 

public IBlockState getStateFromMeta(int meta)

  {

    return getDefaultState().withProperty(ROTATION, EnumFacing.byMetadata(meta));

  }

 

 

 

public int getMetaFromState(IBlockState state)

  {

    return ((EnumFacing)state.getValue(ROTATION)).getMetadata();

  }

 

 

should I remove these?

I'm working on something big!  As long as there arent alot of big issues... [D

Link to comment
Share on other sites

First, you still have that private

rotation

field in your Block. Remove it!

Then, no, you should not remove it. But you need to encode the EnumFacing into metadata and back. Depending on how your Block rotates, this depends. I suggest you take a look at the vanilla furnace for an example of how to do this.

 

 

Thank you so much!  After copying everything except the last switch statement into my block, it works perfectly.

I'm working on something big!  As long as there arent alot of big issues... [D

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



×
×
  • Create New...

Important Information

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