Jump to content

[1.7.2] Upon placing a block all blocks of same type rotate to face player


Recommended Posts

Posted

I wish to add a direction to my block based off of which way the Player is facing (like the furnace). My block is a BlockPC and I have it rendering fine with  custom model and upon being placed it is placed to face the player. However upon placing a new block of the same type while facing a different direction, all other blocks of this type that were previously placed in the world rotate to face the new direction rather than remaining in their originally placed positions. I am not sure how to fix this. Here is my code:

 

 

 

BlockPC.java

public class BlockPC extends BlockContainer {

public BlockPC(Material material) {
	super(material);
	this.setHardness(3.0f);
	this.setResistance(5.0f);
	this.setStepSound(soundTypeGrass);
	this.setBlockBounds(0F, 0F, 0F,1F,1.25F,1F);
	this.setCreativeTab(CommonProxy.soccerTab);

}


 public void onBlockPlacedBy(World world, int i, int j, int k, EntityLiving entityliving)
     {
             int l = MathHelper.floor_double((double)((entityliving.rotationYaw * 4F) / 360F) + 2.5D) & 3;
             world.setBlockMetadataWithNotify(i, j, k, l, 2);
             System.out.println(l);
     }


public boolean isOpaqueCube(){
return false;
}

public boolean renderAsNormalBlock(){
return false;
}

@Override
public int getRenderType(){
int id;
id = RenderingRegistry.getNextAvailableRenderId();
return id;
}
@Override
public TileEntity createNewTileEntity(World world, int var2) {
	return new TileEntityPC();
}

 @Override
    public void onBlockPlacedBy(World world, int i, int j, int k, EntityLivingBase entityliving, ItemStack itemStack)
    {
        int facing = MathHelper.floor_double((double) ((entityliving.rotationYaw * 4F) / 360F) + 0.5D) & 3;
        int newFacing = 0;
        if (facing == 0)
        {
        	newFacing = 2;
        }
        if (facing == 1)
        {
        	newFacing = 5;
        }
        if (facing == 2)
        {
        	newFacing = 3;
        }
        if (facing == 3)
        {
        	newFacing = 4;
        }
        TileEntity tileEntity = world.getTileEntity(i, j, k);
        if (tileEntity != null && tileEntity instanceof TileEntityPC)
        {
        	TileEntityPC tileEntityPC = (TileEntityPC) tileEntity;
        	tileEntityPC.setFacingDirection(newFacing);
            world.markBlockForUpdate(i, j, k);
        }
    }

@SideOnly(Side.CLIENT)
public void registerBlockIcons(IIconRegister iconRegister) {
	this.blockIcon = iconRegister.registerIcon(Soccer.modid + ":"
			+ this.getUnlocalizedName().substring(5));
}

}

 

TileEntityPC.java

public class TileEntityPC extends TileEntity{

private static int facingDirection;

public static int getFacingDirection()
    {
        return facingDirection;
    }

public void setFacingDirection(int par1)
    {
        this.facingDirection = par1;
    }

 @Override
    public Packet getDescriptionPacket()
    {
        NBTTagCompound tag = new NBTTagCompound();
        writeToNBT(tag);
        return new S35PacketUpdateTileEntity(xCoord, yCoord, zCoord, 1, tag);
    }

    @Override
    public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity packet)
    {
        readFromNBT(packet.func_148857_g());
    }

@Override
    public void readFromNBT(NBTTagCompound nbttagcompound)
    {
        super.readFromNBT(nbttagcompound);  
        facingDirection = nbttagcompound.getInteger("facingDirection");
    }

    @Override
    public void writeToNBT(NBTTagCompound nbttagcompound)
    {
        super.writeToNBT(nbttagcompound);
        nbttagcompound.setInteger("facingDirection", facingDirection);
    }
}

 

BlockRenderPC.java

public class BlockRenderPC extends TileEntitySpecialRenderer{

private static final ResourceLocation texture = new ResourceLocation("soccer:textures/blocks/blockPC.png");
private BlockModelPC modelPC;

public BlockRenderPC() {
	this.modelPC = new BlockModelPC();
}

@Override
public void renderTileEntityAt(TileEntity tileentity, double x, double y,
		double z, float f) {

	GL11.glPushMatrix(); 	
        GL11.glTranslatef((float) x + 0.5F, (float) y + 1.5F, (float) z + 0.5F);
        this.bindTexture(texture);
        //Rotates model, as for some reason it is initially upside (180 = angle, 1.0F at end = about z axis)
        GL11.glRotatef(180, 0.0F, 0.0F, 1.0F);
         int facing = TileEntityPC.getFacingDirection();
        int k = 0;
        //South
        if (facing == 2) {
            k = 0;
        }
        //North
        if (facing == 3) {
            k = 180;
        }
        //East
        if (facing == 4) {
            k = -90;
        }
        //West
        if (facing == 5) {
            k = 90;
        }
        //Rotates model on the spot, depending on direction, making the front always to player) (k = angle, 1.0F in middle = about y axis)
        GL11.glRotatef(k, 0.0F, 1.0F, 0.0F);
        GL11.glDisable(GL11.GL_CULL_FACE);
        GL11.glEnable(GL11.GL_ALPHA_TEST);
        this.modelPC.render((Entity)null, 0.0F, 0.0F, -0.1F, 0.0F, 0.0F, 0.0625F);
        GL11.glPopMatrix();
	}


protected ResourceLocation getBlockTexture(Block PC) {
	return this.texture;
}

}

 

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.