Jump to content

[1.7.2]Connect customly rendered blocks?


EctoWarrior

Recommended Posts

Hello everyone. I have a customly rendered table, and I am trying to figure out how to connect two tables if they are placed next to each other, similar to a fence or iron bar. I haven't seen much on how to do this, but I think I have it half done. The table that I place down next to it will update it's model accordingly, but the other one that is already there won't.

 

Here are some screenshots of the situation:

 

Tables when not connected:

 

1s5aqkC.png

 

 

Tables when connected:

 

tWibAPk.png

 

 

As you can see, the other two adjacent tables are not updating their model, even though they should be.

 

So I guess I have two questions:

1. How can I change a currently existing block's model?

2. If I'm doing this the complete wrong way, what is the best way of doing it?

 

Here is some of the code that I currently have:

 

BlockTable:

 

package com.ectowarrior.ectosessentials.block;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
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 com.ectowarrior.ectosessentials.lib.References;
import com.ectowarrior.ectosessentials.tileentity.TileEntityTable;
import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;
public class BlockTable extends BlockContainer {

public TileEntityTable table = new TileEntityTable();

public BlockTable(Material material) {
super(material);
setBlockName("table");
setBlockTextureName(References.MODID + ":" + "table");
setHardness(1F);
setStepSound(soundTypeWood);
setCreativeTab(CreativeTabs.tabBlock);
}

public TileEntity createNewTileEntity(World world, int i) {
     table = new TileEntityTable();
     return table;
}

@Override
public int getRenderType() {
     return -1;
}

@Override
public boolean isOpaqueCube() {
     return false;
}

@Override
public boolean renderAsNormalBlock() {
     return false;
}

public static TileEntity[] getAdjacentTileEntities(World world, int x, int y, int z) {
     System.out.println("Block Coords: " + x + ", " + y + ", " + z);
TileEntity[] tileEntity = new TileEntity[6];
tileEntity[0] = world.getTileEntity(x - 1, y, z);
tileEntity[1] = world.getTileEntity(x, y - 1, z);
tileEntity[2] = world.getTileEntity(x, y, z - 1);
tileEntity[3] = world.getTileEntity(x + 1, y, z);
tileEntity[4] = world.getTileEntity(x, y + 1, z);
tileEntity[5] = world.getTileEntity(x, y, z + 1);
// for(int i = 0; i < tileEntity.length; i++)
// System.out.println(tileEntity[i]);
return tileEntity;
}

public void UpdateModel(World world, int x, int y, int z) {
     TileEntity[] tileEntity = getAdjacentTileEntities(world, x, y, z);
     if (tileEntity[0] != null && tileEntity[0] instanceof TileEntityTable) { //-x
         table.renderLeg00 = false;
         table.renderLeg01 = false;
         System.out.println("Disabled legs in the negative x direction.");
     }
     if (tileEntity[2] != null && tileEntity[2] instanceof TileEntityTable) { //-z
         table.renderLeg01 = false;
         table.renderLeg11 = false;
         System.out.println("Disabled legs in the negative z direction.");
     }
     if (tileEntity[3] != null && tileEntity[3] instanceof TileEntityTable) { //+x
         table.renderLeg10 = false;
         table.renderLeg11 = false;
         System.out.println("Disabled legs in the positive x direction.");
     }
     if (tileEntity[5] != null && tileEntity[5] instanceof TileEntityTable) { //+z
         table.renderLeg00 = false;
         table.renderLeg10 = false;
         System.out.println("Disabled legs in the positive z direction.");
     }
}

@Override
public void onNeighborBlockChange(World world, int x, int y, int z, Block neighbor) {
     super.onNeighborBlockChange(world, x, y, z, neighbor);
     if(neighbor instanceof BlockTable) {
     System.out.println(neighbor + " has has changed. Changing model...");
     UpdateModel(world, x, y, z);
     }
}

@Override
public void onBlockAdded(World world, int x, int y, int z) {
     super.onBlockAdded(world, x, y, z);
     UpdateModel(world, x, y, z);
    
}
@Override
public void breakBlock(World world, int x, int y, int z, Block block, int metadata) {
     super.breakBlock(world, x, y, z, block, metadata);
     UpdateModel(world, x, y, z);
}

@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int p_149727_6_, float p_149727_7_, float p_149727_8_, float p_149727_9_) {
UpdateModel(world, x, y, z);
     return false;
}
}

 

 

ModelTable:

 

package com.ectowarrior.ectosessentials.model;
import com.ectowarrior.ectosessentials.tileentity.TileEntityTable;
import net.minecraft.client.model.ModelBase;
import net.minecraft.client.model.ModelRenderer;
import net.minecraft.entity.Entity;
public class ModelTable extends ModelBase
{
//fields
ModelRenderer Leg1;
ModelRenderer Leg2;
ModelRenderer Leg3;
ModelRenderer Leg4;
ModelRenderer Board1;
ModelRenderer Top;
ModelRenderer Board2;
ModelRenderer Board3;
ModelRenderer Board4;

public ModelTable()
{
textureWidth = 64;
textureHeight = 64;

     Leg1 = new ModelRenderer(this, 0, 0);
     Leg1.addBox(0F, 0F, 0F, 2, 15, 2);
     Leg1.setRotationPoint(6F, 9F, 6F);
     Leg1.setTextureSize(64, 64);
     Leg1.mirror = true;
     setRotation(Leg1, 0F, 0F, 0F);
     Leg2 = new ModelRenderer(this, 0, 0);
     Leg2.addBox(0F, 0F, 0F, 2, 15, 2);
     Leg2.setRotationPoint(6F, 9F, -8F);
     Leg2.setTextureSize(64, 64);
     Leg2.mirror = true;
     setRotation(Leg2, 0F, 0F, 0F);
     Leg3 = new ModelRenderer(this, 0, 0);
     Leg3.addBox(0F, 0F, 0F, 2, 15, 2);
     Leg3.setRotationPoint(-8F, 9F, 6F);
     Leg3.setTextureSize(64, 64);
     Leg3.mirror = true;
     setRotation(Leg3, 0F, 0F, 0F);
     Leg4 = new ModelRenderer(this, 0, 0);
     Leg4.addBox(0F, 0F, 0F, 2, 15, 2);
     Leg4.setRotationPoint(-8F, 9F, -8F);
     Leg4.setTextureSize(64, 64);
     Leg4.mirror = true;
     setRotation(Leg4, 0F, 0F, 0F);
     Board1 = new ModelRenderer(this, 0, 0);
     Board1.addBox(0F, 0F, 0F, 12, 2, 1);
     Board1.setRotationPoint(-6F, 9F, 6F);
     Board1.setTextureSize(64, 64);
     Board1.mirror = true;
     setRotation(Board1, 0F, 0F, 0F);
     Top = new ModelRenderer(this, 0, 0);
     Top.addBox(0F, 0F, 0F, 16, 1, 16);
     Top.setRotationPoint(-8F, 8F, -8F);
     Top.setTextureSize(64, 64);
     Top.mirror = true;
     setRotation(Top, 0F, 0F, 0F);
     Board2 = new ModelRenderer(this, 0, 0);
     Board2.addBox(0F, 0F, 0F, 12, 2, 1);
     Board2.setRotationPoint(-6F, 9F, -7F);
     Board2.setTextureSize(64, 64);
     Board2.mirror = true;
     setRotation(Board2, 0F, 0F, 0F);
     Board3 = new ModelRenderer(this, 0, 0);
     Board3.addBox(0F, 0F, 0F, 1, 2, 12);
     Board3.setRotationPoint(6F, 9F, -6F);
     Board3.setTextureSize(64, 64);
     Board3.mirror = true;
     setRotation(Board3, 0F, 0F, 0F);
     Board4 = new ModelRenderer(this, 0, 0);
     Board4.addBox(0F, 0F, 0F, 1, 2, 12);
     Board4.setRotationPoint(-7F, 9F, -6F);
     Board4.setTextureSize(64, 64);
     Board4.mirror = true;
     setRotation(Board4, 0F, 0F, 0F);
}

public void render(Entity entity, float f, float f1, float f2, float f3, float f4, float f5, TileEntityTable table)
{
super.render(entity, f, f1, f2, f3, f4, f5);
setRotationAngles(f, f1, f2, f3, f4, f5, entity);
if(table != null && table.renderLeg00 == true)
     Leg1.render(f5);
if(table != null && table.renderLeg01 == true)
     Leg2.render(f5);
if(table != null && table.renderLeg10 == true)
     Leg3.render(f5);
if(table != null && table.renderLeg11 == true)
     Leg4.render(f5);
Board1.render(f5);
Top.render(f5);
Board2.render(f5);
Board3.render(f5);
Board4.render(f5);
}

private void setRotation(ModelRenderer model, float x, float y, float z)
{
model.rotateAngleX = x;
model.rotateAngleY = y;
model.rotateAngleZ = z;
}

public void setRotationAngles(float f, float f1, float f2, float f3, float f4, float f5, Entity entity)
{
super.setRotationAngles(f, f1, f2, f3, f4, f5, entity);
}
}

 

 

TileEntityTable:

 

package com.ectowarrior.ectosessentials.tileentity;
import com.ectowarrior.ectosessentials.model.ModelTable;
import com.ectowarrior.ectosessentials.renderer.tileentity.TileEntityTableRenderer;
import net.minecraft.tileentity.TileEntity;
public class TileEntityTable extends TileEntity {
public ModelTable model;
public TileEntityTableRenderer renderer;
public boolean renderLeg00 = true;
public boolean renderLeg01 = true;
public boolean renderLeg10 = true;
public boolean renderLeg11 = true;
}

 

 

TileEntityTableRenderer:

 

package com.ectowarrior.ectosessentials.renderer.tileentity;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import org.lwjgl.opengl.GL11;
import com.ectowarrior.ectosessentials.lib.References;
import com.ectowarrior.ectosessentials.model.ModelTable;
import com.ectowarrior.ectosessentials.tileentity.TileEntityTable;
public class TileEntityTableRenderer extends TileEntitySpecialRenderer {
private final ModelTable model;

public TileEntityTableRenderer() {
model = new ModelTable();
}

private void adjustRotatePivotViaMeta(World world, int x, int y, int z) {
     int meta = world.getBlockMetadata(x, y, z);
     GL11.glPushMatrix();
     GL11.glRotatef(meta * (-90), 0.0F, 0.0F, 1.0F);
     GL11.glPopMatrix();
}
public void renderTileEntityAt(TileEntity te, double x, double y, double z, float scale) {
     TileEntityTable table = (TileEntityTable)te;
     table.model = model;
    
GL11.glPushMatrix();
     GL11.glTranslatef((float)x + 0.5F, (float)y + 1.5F, (float)z + 0.5F);
     ResourceLocation textures = (new ResourceLocation(References.MODID + ":" + "textures/blocks/table.png"));
     GL11.glRotatef(180F, 0.0F, 0.0F, 1.0F);
     bindTexture(textures);
    
     GL11.glPushMatrix();    
     model.render(null, 0.0F, 0.0F, -0.1F, 0.0F, 0.0F, 0.0625F, (TileEntityTable)te);
     GL11.glPopMatrix();
    
     GL11.glPopMatrix();
}

private void adjustLightFixture(World world, int i, int j, int k, Block block) {
     Tessellator tess = Tessellator.instance;
     float brightness = block.getMixedBrightnessForBlock(world, i, j, k);
     int skyLight = world.getLightBrightnessForSkyBlocks(i, j, k, 0);
     int modulousModifier = skyLight % 65536;
     int divModifier = skyLight / 65536;
     tess.setColorOpaque_F(brightness, brightness, brightness);
     OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit, (float) modulousModifier, divModifier);
}

}

 

 

Thanks in advance for any help.

Link to comment
Share on other sites

Hi

 

Personally, I would do this completely without TileEntities, just using Blocks and Metadata.  There are quite a few vanilla examples of this (fence, for example, like you said, or panes).  Whenever a block is placed or destroyed, it  makes a call to all its neighbors (I forget which method but it's easy to spot) so that they can update their metadata and change their displayed texture.

 

You can do the same thing with TileEntities too (just trigger the model change from the block update-my-neighbor method), but I suspect you don't need them.

 

-TGG

 

PS your method UpdateModel should be updateModel, otherwise it looks like a constructor.

 

 

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.