Block code:
package com.cynda.cyndas_toolbox.block;
import com.cynda.cyndas_toolbox.CyndasToolbox;
import com.cynda.cyndas_toolbox.EnumHandler.SocketBlockSide;
import com.cynda.cyndas_toolbox.tiles.Test;
import net.minecraft.block.Block;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import javax.annotation.Nullable;
public class BlockSocket extends Block implements ITileEntityProvider{
final static PropertyEnum<SocketBlockSide> EAST = PropertyEnum.create("east", SocketBlockSide.class);
final static PropertyEnum<SocketBlockSide> WEST = PropertyEnum.create("west", SocketBlockSide.class);
final static PropertyEnum<SocketBlockSide> NORTH = PropertyEnum.create("north", SocketBlockSide.class);
final static PropertyEnum<SocketBlockSide> SOUTH = PropertyEnum.create("south", SocketBlockSide.class);
final static PropertyEnum<SocketBlockSide> UP = PropertyEnum.create("up", SocketBlockSide.class);
final static PropertyEnum<SocketBlockSide> DOWN = PropertyEnum.create("down", SocketBlockSide.class);
public BlockSocket(String unlocalizedName){
super(Material.IRON);
setUnlocalizedName(unlocalizedName);
setCreativeTab(CyndasToolbox.tabBlocks);
setDefaultState(getBlockState().getBaseState().withProperty(EAST, SocketBlockSide.BLANK));
setDefaultState(getBlockState().getBaseState().withProperty(WEST, SocketBlockSide.BLANK));
setDefaultState(getBlockState().getBaseState().withProperty(NORTH, SocketBlockSide.BLANK));
setDefaultState(getBlockState().getBaseState().withProperty(SOUTH, SocketBlockSide.BLANK));
setDefaultState(getBlockState().getBaseState().withProperty(UP, SocketBlockSide.BLANK));
setDefaultState(getBlockState().getBaseState().withProperty(DOWN, SocketBlockSide.BLANK));
}
@SideOnly(Side.CLIENT)
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, @Nullable ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) {
if(!worldIn.isRemote) {
// worldIn.markBlocksDirtyVertical(pos.getX(), pos.getZ(), pos.getX(), pos.getZ());
TileEntity te = worldIn.getTileEntity(pos);
if (te != null && te instanceof Test) {
Test tileTest = (Test) te;
state = setFaceRandom(state, getSide(side), playerIn, tileTest, side);
worldIn.setBlockState(pos, state);
tileTest.markDirty();
if (playerIn.isSneaking()) {
playerIn.addChatMessage(new TextComponentString("Clicks " + tileTest.getClicks()));
} else {
tileTest.setClicks(tileTest.getClicks() + 1);
tileTest.markDirty();
}
}
}
return true;
}
private IBlockState setFaceRandom(IBlockState state, PropertyEnum side, EntityPlayer playerIn, Test tileTest, EnumFacing sideFace){
if(playerIn.getHeldItemMainhand()==null){
state = state.withProperty(side, SocketBlockSide.INPUT_POWER);
tileTest.setSide(sideFace, "input_power");
System.out.println("INPUT");
}else {
state = state.withProperty(side, SocketBlockSide.BLANK);
tileTest.setSide(sideFace, "blank");
System.out.println("BLANK");
}
return state;
}
private PropertyEnum getSide(EnumFacing side){
PropertyEnum enumSide = EAST;
switch (side){
case EAST:
enumSide = EAST;
break;
case WEST:
enumSide = WEST;
break;
case NORTH:
enumSide = NORTH;
break;
case SOUTH:
enumSide = SOUTH;
break;
case UP:
enumSide = UP;
break;
case DOWN:
enumSide = DOWN;
break;
}
return enumSide;
}
public void setEast(SocketBlockSide value){
setDefaultState(getBlockState().getBaseState().withProperty(EAST, value));
}
@Override
public IBlockState getStateFromMeta(int meta) {return this.getDefaultState();}
@Override
public int getMetaFromState(IBlockState state) {return 0;}
//Methods for ITileEntityProvider
@Override
public TileEntity createNewTileEntity(World worldIn, int meta) { return new Test(); }
@Override
protected BlockStateContainer createBlockState() {
return new BlockStateContainer(this, EAST, WEST, NORTH, SOUTH, UP, DOWN);
}
public PropertyEnum<SocketBlockSide> getEAST() {return EAST;}
public PropertyEnum<SocketBlockSide> getWEST() {return WEST;}
public PropertyEnum<SocketBlockSide> getNORTH() {return NORTH;}
public PropertyEnum<SocketBlockSide> getSOUTH() {return SOUTH;}
public PropertyEnum<SocketBlockSide> getUP() {return UP;}
public PropertyEnum<SocketBlockSide> getDOWN() {return DOWN;}
}
TileEntity code:
package com.cynda.cyndas_toolbox.tiles;
import akka.io.Tcp;
import com.cynda.cyndas_toolbox.EnumHandler.SocketBlockSide;
import com.cynda.cyndas_toolbox.block.BlockSocket;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
public class Test extends TileEntity{
private int clicks = 0;
private int east = 0;
private IntegerProperty eastProptery = new SimpleIntegerProperty(east);
// private String west = "blank";
// private String north = "blank";
// private String south = "blank";
// private String up = "blank";
// private String down = "blank";
private World world;
final static PropertyEnum<SocketBlockSide> EAST = PropertyEnum.create("east", SocketBlockSide.class);
final static PropertyEnum<SocketBlockSide> WEST = PropertyEnum.create("west", SocketBlockSide.class);
final static PropertyEnum<SocketBlockSide> NORTH = PropertyEnum.create("north", SocketBlockSide.class);
final static PropertyEnum<SocketBlockSide> SOUTH = PropertyEnum.create("south", SocketBlockSide.class);
final static PropertyEnum<SocketBlockSide> UP = PropertyEnum.create("up", SocketBlockSide.class);
final static PropertyEnum<SocketBlockSide> DOWN = PropertyEnum.create("down", SocketBlockSide.class);
public Test(){
eastProptery.addListener(new InvalidationListener() {
@Override
public void invalidated(Observable e) {
boolean firstLoad = true;
if(getWorld()==null){
System.out.println(e + " East + null");
// int x = Integer.valueOf(e.toString().substring(e.toString().length() - 2, e.toString().length() - 1)) - 1;
// System.out.println(x);
// eastProptery.setValue(x);
}else {
System.out.println(e + " East");
final IBlockState state = getWorld().getBlockState(getPos());
//
//// EAST = ((BlockSocket) getBlockType()).getEAST();
//// WEST = ((BlockSocket) getBlockType()).getWEST();
//// NORTH = ((BlockSocket) getBlockType()).getNORTH();
//// SOUTH = ((BlockSocket) getBlockType()).getSOUTH();
//// UP = ((BlockSocket) getBlockType()).getUP();
//// DOWN = ((BlockSocket) getBlockType()).getDOWN();
//
setState(state);
}
}
});
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
compound.setInteger("Clicks", clicks);
compound.setInteger("East", east);
// compound.setString("West", west);
// compound.setString("North", north);
// compound.setString("South", south);
// compound.setString("Up", up);
// compound.setString("Down", down);
markDirty();
super.writeToNBT(compound);
return compound;
}
@Override
public void readFromNBT(NBTTagCompound compound){
System.out.println("Hello NBTReadNOW");
super.readFromNBT(compound);
if(getWorld()==null){
clicks = compound.getInteger("Clicks");
east = compound.getInteger("East");
eastProptery.setValue(east);
System.out.println(compound.getInteger("East")+" Read "+compound.getInteger("Clicks"));
}
System.out.println(getWorld());
if(getWorld()!=null) {
System.out.println(eastProptery.intValue() +" East Value");
final IBlockState state = getWorld().getBlockState(getPos());
setState(state);
}
}
public int getClicks() {return clicks; }
public void setClicks(int clicks) { this.clicks = clicks; markDirty();}
public void setEast(int east){this.east = east; eastProptery.setValue(east);}
public void setSide(EnumFacing side, String value) {
switch (side){
case EAST:
east = SocketBlockSide.getName(value).getID();
eastProptery.setValue(east);
// System.out.println(eastProptery);
markDirty();
break;
case WEST:
// west = value;
markDirty();
break;
case NORTH:
// north = value;
markDirty();
break;
case SOUTH:
// south = value;
markDirty();
break;
case UP:
// up = value;
markDirty();
break;
case DOWN:
// down = value;
markDirty();
break;
}
}
public void setState(IBlockState state){
System.out.println(east+" Set State");
state = state.withProperty(EAST, SocketBlockSide.values()[eastProptery.intValue()]);
// state = state.withProperty(WEST, SocketBlockSide.getName(west));
// state = state.withProperty(NORTH, SocketBlockSide.getName(north));
// state = state.withProperty(SOUTH, SocketBlockSide.getName(south));
// state = state.withProperty(UP, SocketBlockSide.getName(up));
// state = state.withProperty(DOWN, SocketBlockSide.getName(down));
getWorld().setBlockState(pos, state);
System.out.println("set BlockState"+getPos().toString()+state.toString());
}
/**
* This controls whether the tile entity gets replaced whenever the block state
* is changed. Normally only want this when block actually is replaced.
*/
@Override
public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newState)
{
return (oldState.getBlock() != newState.getBlock());
}
}