Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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;
}
}

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.

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.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.