Jump to content

Recommended Posts

Posted

Hey there,

 

I am trying to do something very simply but am of course failing somewhat.

 

I want a block which, when placed, stored a double in the new TileEntity which is the angle at which the block was placed (using rotationYaw), and sets a boolean in the new TE to be true.

 

Then, when the block is subsequently right clicked, I want it to take the angle previously stored and the angle it was right-clicked at, and put them into a function, and get a new boolean. This new boolean is then stored in the TE, and the old angle stored is replaced by the new angle at which the block was clicked.

 

These are my current Block and TE classes:

public class BlockSimpleQuantum extends BlockContainer {

public BlockSimpleQuantum(boolean s) {
	super(Material.iron);
	spin = s;
}

public final boolean spin;

public TileEntity createNewTileEntity(World world, int par1) {
	return new TileEntitySimpleQuantum();
}

@SideOnly(Side.CLIENT)
protected IIcon blockIcon;

@SideOnly(Side.CLIENT)
public void registerBlockIcons(IIconRegister i) {
	blockIcon = i.registerIcon("nr:" + this.getUnlocalizedName().substring(5) + (this.spin ? "Up" : "Down"));
}

@SideOnly(Side.CLIENT)
public IIcon getIcon(int p_149691_1_, int p_149691_2_) {
	return blockIcon;
}

public static void set(boolean s, World worldObj, int xCoord, int yCoord, int zCoord) {
	if(s) worldObj.setBlock(xCoord, yCoord, zCoord, NRBlocks.simpleQuantumUp);
	else worldObj.setBlock(xCoord, yCoord, zCoord, NRBlocks.simpleQuantumDown);
}

public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ) {
	if (!player.isSneaking()) {
		TileEntitySimpleQuantum t = (TileEntitySimpleQuantum) world.getTileEntity(x, y, z);
		if ((player != null) && (!world.isRemote)) {
			double newAngle = (double) player.rotationYaw;
			double p = Math.pow(Math.cos(((t.angle-newAngle)/2)*(Math.PI/180)), 2);
			double rand = new Random().nextDouble();

			if (p == 0) t.spin=false;
			else if (p >= rand) t.spin=true;
			else if (p < rand) t.spin=false;

			t.angle = newAngle;
		}
		if (world.isRemote) player.addChatMessage(new ChatComponentText(EnumChatFormatting.WHITE + (t.angle + " " + t.spin)));
	}
	return true;
}

public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entityLivingBase, ItemStack itemstack) {
	double l = (double) entityLivingBase.rotationYaw;
	TileEntitySimpleQuantum t = (TileEntitySimpleQuantum) world.getTileEntity(x, y, z);
	t.angle = l;
	t.spin=true;

	if (world.isRemote) ((ICommandSender) entityLivingBase).addChatMessage(new ChatComponentText(EnumChatFormatting.WHITE + (t.angle + " " + t.spin)));
}

public boolean canCreatureSpawn(EnumCreatureType type, IBlockAccess world, int x, int y, int z) {
	return false;
}
}

public class TileEntitySimpleQuantum extends TileEntity {

public double angle = 0;
public boolean spin = true;
public boolean spin1 = true;

public TileEntitySimpleQuantum() {
	super();
    }

public void updateEntity() {
    	super.updateEntity();
    	if (spin != spin1) {
    		spin1 = spin;
    		BlockSimpleQuantum.set(spin, this.worldObj, this.xCoord, this.yCoord, this.zCoord);
    	}
    	markDirty();
    }

public void readFromNBT(NBTTagCompound nbt) {
	super.readFromNBT(nbt);

 	this.angle = nbt.getDouble("angle");
  	this.spin = nbt.getBoolean("spin");
  	this.spin1 = nbt.getBoolean("spin1");
}

public void writeToNBT(NBTTagCompound nbt) {
  	super.writeToNBT(nbt);

  	nbt.setDouble("angle", this.angle);
 	nbt.setBoolean("spin", this.spin);
 	nbt.setBoolean("spin1", this.spin1);
}
}

 

Thanks for any help in advance ;)

Posted

Wow, forgot a whole paragraph...

 

This is what happens in-game:

I place the block down and the correct angle and boolean values are read out.

However, the subsequent right-clicks are not working as intended.

If I right click, and the new boolean is printed in the chat to still be true, the angle printed to the chat does not change from the original, and the block's texture is still the 'true' one.

If the new boolean is false, then the angle goes to 0.0 in the chat, the block texture changes to the 'false' one, and clicks after that always print '0.0, true' to the chat, while the texture doesn't update correctly.

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.