2

[Coding] Rotating a custom rendered block - Player Position

X_pilot's Avatar X_pilot2/24/14 3:51 am
2 emeralds 2.3k 1
2/24/2014 7:33 am
X_pilot's Avatar X_pilot
Hey there!
I'm X_pilot and I'm quite new to Custom Rendered blocks. I figured out how to get my custom rendered block into MC, but it used to stand still. Now with the help of some code, I have finally made it rotate a bit. but. When you place it facing 0 or 180 (or near those angles) it will go 1 direction. When you place it in 90 or -90, it will face another direction (but the same direction, nomatter if you are closer to 90 or -90).

Here's my code:
Block class
package taco.blocks;

import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import taco.main;
import taco.tileentity.TileEntityGrinder;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class MeatGrinderBlock extends BlockContainer {

public MeatGrinderBlock(int id, Material par2Material) {
super(id, par2Material);
setUnlocalizedName("grinder");
setCreativeTab(main.tabTaco);
this.setHardness(2F);

}
public TileEntity createNewTileEntity(World world) {
return new TileEntityGrinder();
}

public int getRenderType(){
return -1;
}

public boolean isOpaqueCube(){
return false;
}

public boolean renderAsNormalBlock(){
return false;
}
public boolean onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9)
{
if(par5EntityPlayer.getHeldItem().itemID == Item.beefRaw.itemID){
par5EntityPlayer.getHeldItem().itemID = main.meatRaw.itemID;
par5EntityPlayer.addChatMessage("Brshshshshzzzzzzz");
}
else
{
par5EntityPlayer.addChatMessage("Please insert raw beef!");
}
return true;
}
@Override
public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entity, ItemStack stack)
{
if (entity == null)
{
return;
}

TileEntityGrinder tile = (TileEntityGrinder) world.getBlockTileEntity(x, y, z);
tile.direction = MathHelper.floor_double((double)(entity.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
}


@SideOnly(Side.CLIENT)
public void registerIcons(IconRegister icon){
this.blockIcon = icon.registerIcon("minecraft:grinder");
}

}


Renderer Class
package taco.client.render;

import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.entity.Entity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;

import org.lwjgl.opengl.GL11;

import taco.models.MeatGrinder;
import taco.tileentity.TileEntityGrinder;

public class RendererGrinder extends TileEntitySpecialRenderer {

private MeatGrinder model;

public static final ResourceLocation texGrinder = new ResourceLocation("textures/blocks/MGrinder.png");

public RendererGrinder(){
this.model = new MeatGrinder();

}


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);
GL11.glRotatef(180, 0F, 0F, 1F);

TileEntityGrinder myTile = (TileEntityGrinder) tileentity;
int direction = myTile.direction;
GL11.glRotatef(direction *180, 0.0F, 1.0F, 0.0F);

this.bindTexture(texGrinder);

GL11.glPushMatrix();

this.model.renderModel(0.0625F);

GL11.glPopMatrix();

GL11.glPopMatrix();

}
public void renderBlockYour(TileEntityGrinder tl, World world, int i, int j, int k, Block block) {
Tessellator tessellator = Tessellator.instance;
//This will make your block brightness dependent from surroundings lighting.
float f = block.getBlockBrightness(world, i, j, k);
int l = world.getLightBrightnessForSkyBlocks(i, j, k, 0);
int l1 = l % 65536;
int l2 = l / 65536;
tessellator.setColorOpaque_F(f, f, f);
OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit, (float)l1, (float)l2);

/*This will rotate your model corresponding to player direction that was when you placed the block. If you want this to work,
add these lines to onBlockPlacedBy method in your block class.
int dir = MathHelper.floor_double((double)((player.rotationYaw * 4F) / 360F) + 0.5D) & 3;
world.setBlockMetadataWithNotify(x, y, z, dir, 0);*/

int dir = world.getBlockMetadata(i, j, k);

GL11.glPushMatrix();
GL11.glTranslatef(0.5F, 0, 0.5F);
//This line actually rotates the renderer.
GL11.glRotatef(dir * (-90F), 0F, 1F, 0F);
GL11.glTranslatef(-0.5F, 0, -0.5F);
bindTexture(texGrinder);

GL11.glPopMatrix();
}

}


Tile entity Grinder Class
package taco.tileentity;

import net.minecraft.tileentity.TileEntity;

public class TileEntityGrinder extends TileEntity {
public int direction;

}


this is the result:



Please help me
Posted by X_pilot's Avatar
X_pilot
Level 54 : Grandmaster Necromancer
101

Create an account or sign in to comment.

1

X_pilot
02/24/2014 7:33 am
Level 54 : Grandmaster Necromancer
X_pilot's Avatar
Solved!
I changed the "direction * 180" to "direction * 90 + 90" to get my rotations correct!

I'd like to thank Zeranny for helping me
1
Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome