Jump to content

[1.7.10]Rotating custom model depending on which side it is placed


_Gustav0_

Recommended Posts

I am making a fun little mod for "practicing my skills on minecraft forge", but i have come to a little big problem, i cant get to render a custom model the way i do. I want it to rotate depending on which side of a block it was placed(kind of like a button or a lever) but i don´t have any idea of rendering or doing things like that. I've been trying things, looking other minecraft classes but i cant figure out how to do it. Can anyone help me please?

 

Block Class:

 

 

package io.github.gustavo76016.blocks;

 

import static net.minecraftforge.common.util.ForgeDirection.EAST;

import static net.minecraftforge.common.util.ForgeDirection.NORTH;

import static net.minecraftforge.common.util.ForgeDirection.SOUTH;

import static net.minecraftforge.common.util.ForgeDirection.WEST;

import io.github.gustavo76016.tileentities.GGBTileEntity;

import net.minecraft.block.Block;

import net.minecraft.block.BlockButton;

import net.minecraft.block.material.Material;

import net.minecraft.creativetab.CreativeTabs;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.tileentity.TileEntity;

import net.minecraft.world.World;

import net.minecraftforge.common.util.ForgeDirection;

import net.minecraftforge.event.entity.PlaySoundAtEntityEvent;

 

public class BlockGGB extends BlockButton {

 

public static BlockGGB blockGGB;

 

public static ForgeDirection dir1;

 

public BlockGGB() {

super(true);

this.setBlockName("blockGGButton");

this.setCreativeTab(CreativeTabs.tabMisc);

}

 

@Override

public boolean hasTileEntity(int metadata) {

 

return true;

}

 

@Override

public TileEntity createTileEntity(World world, int metadata) {

 

return new GGBTileEntity();

}

 

@Override

public int getRenderType() {

// TODO Auto-generated method stub

return -1;

}

 

@Override

public boolean isOpaqueCube() {

// TODO Auto-generated method stub

return false;

}

 

@Override

public boolean renderAsNormalBlock() {

// TODO Auto-generated method stub

return false;

}

 

@Override

public boolean onBlockActivated(World world, int p_149727_2_,

int p_149727_3_, int p_149727_4_, EntityPlayer player,

int p_149727_6_, float p_149727_7_, float p_149727_8_,

float p_149727_9_) {

world.playSoundAtEntity(player, "ggbuttons:ggButtonSound", 3f, 1f);

return true;

}

 

public boolean getOrientation(World world, int x, int y, int z, int side) {

 

ForgeDirection dir = ForgeDirection.getOrientation(side);

dir = dir1;

 

return (dir == NORTH && world.isSideSolid(x, y, z + 1, NORTH))

|| (dir == SOUTH && world.isSideSolid(x, y, z - 1, SOUTH))

|| (dir == WEST && world.isSideSolid(x + 1, y, z, WEST))

|| (dir == EAST && world.isSideSolid(x - 1, y, z, EAST));

}

 

public static int isPointingAt() {

if(dir1 == NORTH){

return 0;

}else if(dir1 == SOUTH){

return 1;

}else if(dir1 == EAST){

return 2;

}else if(dir1 == WEST){

return 3;

}else {

return 4;

}

}

}

 

 

 

Tile Entity Class:

 

 

package io.github.gustavo76016.tileentities;

 

import net.minecraft.tileentity.TileEntity;

 

 

public class GGBTileEntity extends TileEntity {

 

}

 

 

 

Renderer Class:

 

 

package io.github.gustavo76016.client.render;

 

import org.lwjgl.opengl.GL11;

 

import cpw.mods.fml.client.FMLClientHandler;

import io.github.gustavo76016.blocks.BlockGGB;

import io.github.gustavo76016.help.References;

import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;

import net.minecraft.tileentity.TileEntity;

import net.minecraft.util.ResourceLocation;

import net.minecraftforge.client.model.AdvancedModelLoader;

import net.minecraftforge.client.model.IModelCustom;

import net.minecraftforge.common.util.ForgeDirection;

 

public class RenderGGB extends TileEntitySpecialRenderer {

 

public BlockGGB blockGGB;

 

public ForgeDirection dir1 = blockGGB.dir1;

 

private static final ResourceLocation texture = new ResourceLocation(

References.MODID, "textures/models/blockGGButton.png");

private IModelCustom model;

 

public RenderGGB() {

this.model = AdvancedModelLoader.loadModel(new ResourceLocation(

References.MODID, "models/blockGGButton.obj"));

}

 

@Override

public void renderTileEntityAt(TileEntity tileentity, double x, double y,

double z, float var8) {

 

if(blockGGB.isPointingAt() == 0){

renderTileEntityAtNorth(tileentity, x, y, z, var8);

}

 

 

if(blockGGB.isPointingAt() == 1){

renderTileEntityAtSouth(tileentity, x, y, z, var8);

}

 

 

if(blockGGB.isPointingAt() == 2){

renderTileEntityAtEast(tileentity, x, y, z, var8);

}

 

 

if(blockGGB.isPointingAt() == 3){

renderTileEntityAtWest(tileentity, x, y, z, var8);

}

 

 

}

 

public void renderTileEntityAtNorth(TileEntity tileentity, double x, double y,

double z, float var8) {

 

GL11.glPushMatrix();

 

GL11.glTranslated(x, y, z);

 

GL11.glScalef(1f, 1f, 1f);

 

FMLClientHandler.instance().getClient().getTextureManager()

.bindTexture(texture);

 

GL11.glRotatef(-90f, 1f, 0f, 0f);

 

GL11.glTranslatef(0f, -1f, 0f);

 

model.renderAll();

 

GL11.glPopMatrix();

 

}

 

public void renderTileEntityAtSouth(TileEntity tileentity, double x, double y,

double z, float var8) {

 

GL11.glPushMatrix();

 

GL11.glTranslated(x, y, z);

 

GL11.glScalef(1f, 1f, 1f);

 

FMLClientHandler.instance().getClient().getTextureManager()

.bindTexture(texture);

 

GL11.glRotatef(90f, 1f, 0f, 0f);

 

GL11.glTranslatef(0f, 0f, -1f);

 

model.renderAll();

 

GL11.glPopMatrix();

 

}

 

public void renderTileEntityAtEast(TileEntity tileentity, double x, double y,

double z, float var8) {

 

GL11.glPushMatrix();

 

GL11.glTranslated(x, y, z);

 

GL11.glScalef(1f, 1f, 1f);

 

FMLClientHandler.instance().getClient().getTextureManager()

.bindTexture(texture);

 

GL11.glRotatef(-90f, 0f, 0f, 1f);

 

GL11.glTranslatef(-1f, 0f, 0f);

 

model.renderAll();

 

GL11.glPopMatrix();

 

}

 

public void renderTileEntityAtWest(TileEntity tileentity, double x, double y,

double z, float var8) {

 

GL11.glPushMatrix();

 

GL11.glTranslated(x, y, z);

 

GL11.glScalef(1f, 1f, 1f);

 

FMLClientHandler.instance().getClient().getTextureManager()

.bindTexture(texture);

 

GL11.glRotatef(90f, 0f, 0f, 1f);

 

GL11.glTranslatef(0f, -1f, 0f);

 

model.renderAll();

 

GL11.glPopMatrix();

 

}

 

}

 

 

 

As you can see i´ve been trying a few things but neither of them have worked :(. Feel free to correct anything.

Also, I don´t know if this is the correct category, please let me know :)

 

Thanks!

Link to comment
Share on other sites

these are 5 methods from the vanilla furnace the does what I believe your trying to do. study these and toy with them a bit. 2 of these add the textures. This will set the block so it always places facing the player.

public void onBlockAdded(World p_149726_1_, int p_149726_2_, int p_149726_3_, int p_149726_4_)
    {
        super.onBlockAdded(p_149726_1_, p_149726_2_, p_149726_3_, p_149726_4_);
        this.func_149930_e(p_149726_1_, p_149726_2_, p_149726_3_, p_149726_4_);
    }

    private void func_149930_e(World p_149930_1_, int p_149930_2_, int p_149930_3_, int p_149930_4_)
    {
        if (!p_149930_1_.isRemote)
        {
            Block block = p_149930_1_.getBlock(p_149930_2_, p_149930_3_, p_149930_4_ - 1);
            Block block1 = p_149930_1_.getBlock(p_149930_2_, p_149930_3_, p_149930_4_ + 1);
            Block block2 = p_149930_1_.getBlock(p_149930_2_ - 1, p_149930_3_, p_149930_4_);
            Block block3 = p_149930_1_.getBlock(p_149930_2_ + 1, p_149930_3_, p_149930_4_);
            byte b0 = 3;

            if (block.func_149730_j() && !block1.func_149730_j())
            {
                b0 = 3;
            }

            if (block1.func_149730_j() && !block.func_149730_j())
            {
                b0 = 2;
            }

            if (block2.func_149730_j() && !block3.func_149730_j())
            {
                b0 = 5;
            }

            if (block3.func_149730_j() && !block2.func_149730_j())
            {
                b0 = 4;
            }

            p_149930_1_.setBlockMetadataWithNotify(p_149930_2_, p_149930_3_, p_149930_4_, b0, 2);
        }
    }

    /**
     * Gets the block's texture. Args: side, meta
     */
    @SideOnly(Side.CLIENT)
    public IIcon getIcon(int p_149691_1_, int p_149691_2_)
    {
        return p_149691_1_ == 1 ? this.field_149935_N : (p_149691_1_ == 0 ? this.field_149935_N : (p_149691_1_ != p_149691_2_ ? this.blockIcon : this.field_149936_O));
    }
    /**
    * the field _149936_0 can basically be the same as N just use proper texture names. O just switches the face of the block if the stat go active.
    */
    @SideOnly(Side.CLIENT)
    public void registerBlockIcons(IIconRegister p_149651_1_)
    {
        this.blockIcon = p_149651_1_.registerIcon("furnace_side");
        this.field_149936_O = p_149651_1_.registerIcon(this.field_149932_b ? "furnace_front_on" : "furnace_front_off");
        this.field_149935_N = p_149651_1_.registerIcon("furnace_top");
    }

public void onBlockPlacedBy(World p_149689_1_, int p_149689_2_, int p_149689_3_, int p_149689_4_, EntityLivingBase p_149689_5_, ItemStack p_149689_6_)
    {
        int l = MathHelper.floor_double((double)(p_149689_5_.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;

        if (l == 0)
        {
            p_149689_1_.setBlockMetadataWithNotify(p_149689_2_, p_149689_3_, p_149689_4_, 2, 2);
        }

        if (l == 1)
        {
            p_149689_1_.setBlockMetadataWithNotify(p_149689_2_, p_149689_3_, p_149689_4_, 5, 2);
        }

        if (l == 2)
        {
            p_149689_1_.setBlockMetadataWithNotify(p_149689_2_, p_149689_3_, p_149689_4_, 3, 2);
        }

        if (l == 3)
        {
            p_149689_1_.setBlockMetadataWithNotify(p_149689_2_, p_149689_3_, p_149689_4_, 4, 2);
        }

        if (p_149689_6_.hasDisplayName())
        {
            ((TileEntityFurnace)p_149689_1_.getTileEntity(p_149689_2_, p_149689_3_, p_149689_4_)).func_145951_a(p_149689_6_.getDisplayName());
        }
    }

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.