Jump to content

[1.10.2] [SOLVED] Can't set blockstate after block is placed


SuperHB

Recommended Posts

When I place down a block and right click it, I want to change the blockstate. But when I right click the block, it changes blockstates for a second before switching back.

 

Here is the code:

 

Block

public class BlockSentry extends Block implements ISpecialBlock, ITileEntityProvider {
public static final PropertyEnum LEVEL = PropertyEnum.create("level", BlockSentry.EnumLevel.class);
public static final PropertyEnum TEAM = PropertyEnum.create("team", EnumTeam.class);

public BlockSentry (Material material) {
	super(material);

	setDefaultState(blockState.getBaseState().withProperty(LEVEL, EnumLevel.ONE).withProperty(TEAM, EnumTeam.BLU));
}

@Override
protected BlockStateContainer createBlockState () {
	return new BlockStateContainer(this, new IProperty[] { LEVEL, TEAM });
}

@Override
public IBlockState getStateFromMeta (int meta) {
	switch (meta) {
		case 0:
			return getDefaultState().withProperty(LEVEL, EnumLevel.MINI).withProperty(TEAM, EnumTeam.BLU);
		case 1:
			return getDefaultState().withProperty(LEVEL, EnumLevel.ONE).withProperty(TEAM, EnumTeam.BLU);
		case 2:
			return getDefaultState().withProperty(LEVEL, EnumLevel.TWO).withProperty(TEAM, EnumTeam.BLU);
		case 3:
			return getDefaultState().withProperty(LEVEL, EnumLevel.THREE).withProperty(TEAM, EnumTeam.BLU);
		case 4:
			return getDefaultState().withProperty(LEVEL, EnumLevel.MINI).withProperty(TEAM, EnumTeam.RED);
		case 5:
			return getDefaultState().withProperty(LEVEL, EnumLevel.ONE).withProperty(TEAM, EnumTeam.RED);
		case 6:
			return getDefaultState().withProperty(LEVEL, EnumLevel.TWO).withProperty(TEAM, EnumTeam.RED);
		case 7:
			return getDefaultState().withProperty(LEVEL, EnumLevel.THREE).withProperty(TEAM, EnumTeam.RED);
	}
	return getDefaultState().withProperty(LEVEL, EnumLevel.MINI).withProperty(TEAM, EnumTeam.BLU);
}

@Override
public boolean onBlockActivated (World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) {
	if (worldIn.isRemote) {
		TileEntity tile = worldIn.getTileEntity(pos);

		if (tile instanceof TileEntitySentry) {
			TileEntitySentry sentry = (TileEntitySentry)tile;

			if (heldItem != null) {
				if (heldItem.getItem() == TFCItems.wrench) {
					worldIn.setBlockState(pos, getStateFromMeta(6));
				}
			}
		}
	}
	return true;
}

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

 

TileEntity

public class TileEntitySentry extends TileEntity {
public static int level;
private static int meta;

public TileEntitySentry (int meta) {
	level = getLevelFromMeta(meta);
	this.meta = meta;
}

public void setLevel (int level) {
	this.level = level;
}

public int getLevel () {
	return level;
}

public int getMeta () {
	return meta;
}

@Override
public NBTTagCompound writeToNBT (NBTTagCompound compound) {
	super.writeToNBT(compound);

	compound.setTag("sentry", new NBTTagList());
	NBTTagCompound sentry = compound.getCompoundTag("sentry");
	sentry.setInteger("level", level);

	return compound;
}

@Override
public void readFromNBT (NBTTagCompound compound) {
	super.readFromNBT(compound);

	level = compound.getInteger("level");
}

private int getLevelFromMeta (int meta) {
	switch (meta) {
		case 0: return 0;
		case 1: return 1;
		case 2: return 2;
		case 3: return 3;
		case 4: return 0;
		case 5: return 1;
		case 6: return 2;
		case 7: return 3;
	}
	return 0;
}

@Override
public boolean shouldRefresh (World world, BlockPos pos, IBlockState old, IBlockState newState) {
	return false;
}
}

Link to comment
Share on other sites

if (worldIn.isRemote)

 

Lets only do this on the client because the server has control over everything.

Why?

Raisins.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

World#isRemote

is

true

on the client and

false

on the server. The server is in control of the game state, but you're making your changes on the client so they get overwritten the next time it syncs.

 

Side note: Consider using bitwise operations when storing multiple values in metadata instead of using harcoded metadata values for each combination.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

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.