Jump to content

Accesing a TileEntity's var in the rendering process


TheArni

Recommended Posts

First of all, hello everyone, I'm TheArni (I'm sure you have noticed it...) and I'm new to the Forge Forums :D.

I'm working on a mod a couple of months ago: GasCraft. So, the problem I'm recently having is that I need to access to a variable of the TileEntity when I render it. But apparently, in the TileEntitySpecialRender class, in the method renderTileEntityAt() it gives me a copy of the class of the TileEntity, but I need the object because every TileEntity is different. BTW, I do this because every block renders different. It's essentially a pipe, and to *optimize* the rendering I only check the state of the pipe on block update, so I need to store it somewhere.

 

Long story short: I need to acces a specific TileEntity in the world because the method doesn't give the Tile in question, otherwise the class/type.

 

Oh, I hope I have explained correctly.

Code:

 

PipeRender.class

package TheArni.GasCraft.Render;

import TheArni.GasCraft.Models.ModelPipe;
import TheArni.GasCraft.Pipes.TileEntity.GasPipe_softCopper;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.tileentity.TileEntity;

public class PipeRender extends TileEntitySpecialRenderer {
static final float scale = (float) (1.0 / 16.0);

private ModelPipe Pipe = new ModelPipe(scale);

@Override
public void renderTileEntityAt(TileEntity tileEntity, double x, double y, double z, float tick) {
	Pipe.render((GasPipe_softCopper)tileEntity, x, y, z);
	int Blockx = tileEntity.xCoord;
	int Blocky = tileEntity.xCoord;
	int Blockz = tileEntity.xCoord;
	System.out.println(((GasPipe_softCopper)tileEntity).connectivity_0);
}

}

 

GasPipe_softCopper.class

package TheArni.GasCraft.Pipes.TileEntity;

import TheArni.GasCraft.GasCraft;
import TheArni.GasCraft.Network.IGasNetwork;
import TheArni.GasCraft.Network.IGasNetworkMachine;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class GasPipe_softCopper extends TileEntity {
public IGasNetwork network;

public boolean connectivity_0, connectivity_1, connectivity_2, connectivity_3;

public GasPipe_softCopper(){

}

public void checkConectivity(World world, int BlockX, int BlockY, int BlockZ){	
	if(world.getBlockId(BlockX+1, BlockY, BlockZ) == GasCraft.GasPipe1.blockID){

		this.connectivity_3 = true;
	} else if(world.getBlockTileEntity(BlockX+1, BlockY, BlockZ) instanceof IGasNetworkMachine){
		if(((IGasNetworkMachine)world.getBlockTileEntity(BlockX+1, BlockY, BlockZ)).canConectPipes()==true){
			this.connectivity_3 = true;
		}
	} else this.connectivity_3 = false;
	if(world.getBlockId(BlockX, BlockY, BlockZ+1) == GasCraft.GasPipe1.blockID){
		this.connectivity_0 = true;
	} else if (world.getBlockTileEntity(BlockX, BlockY, BlockZ+1) instanceof IGasNetworkMachine){
		if(((IGasNetworkMachine)world.getBlockTileEntity(BlockX, BlockY, BlockZ+1)).canConectPipes()==true){
			this.connectivity_0 = true;
		}
	} else this.connectivity_0 = false;
	if(world.getBlockId(BlockX-1, BlockY, BlockZ) == GasCraft.GasPipe1.blockID){
		this.connectivity_1 = true;
	} else if(world.getBlockTileEntity(BlockX-1, BlockY, BlockZ) instanceof IGasNetworkMachine){
		if(((IGasNetworkMachine)world.getBlockTileEntity(BlockX-1, BlockY, BlockZ)).canConectPipes()==true){
			this.connectivity_1 = true;
		}
	} else this.connectivity_1 = false;
	if(world.getBlockId(BlockX, BlockY, BlockZ-1) == GasCraft.GasPipe1.blockID){
		this.connectivity_2 = true;
	} else if(world.getBlockTileEntity(BlockX, BlockY, BlockZ-1) instanceof IGasNetworkMachine){
		if(((IGasNetworkMachine)world.getBlockTileEntity(BlockX, BlockY, BlockZ-1)).canConectPipes()==true){
			this.connectivity_2 = true;
		}
	} else this.connectivity_2 = false;
	System.out.println("Connectiviy checked, values are:"+connectivity_0+connectivity_1+connectivity_2+connectivity_3);
}
}

 

ModelPipe.class

package TheArni.GasCraft.Models;

import org.lwjgl.opengl.GL11;
import TheArni.GasCraft.GasCraft;
import TheArni.GasCraft.Pipes.TileEntity.GasPipe_softCopper;
import net.minecraft.client.model.ModelBase;
import net.minecraft.client.model.ModelRenderer;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.client.ForgeHooksClient;

public class ModelPipe extends ModelBase
{
//fields
ModelRenderer Pipe1;
ModelRenderer Pipemid;
ModelRenderer Pipe2;
ModelRenderer Pipe3;
ModelRenderer Pipe4;
private float scale;

public ModelPipe(float scale)
{
	this.scale = scale;
	textureWidth = 64;
	textureHeight = 32;

	Pipemid = new ModelRenderer(this, 20, 0);
	Pipemid.addBox(0F, 0F, 0F, 4, 3, 4);
	Pipemid.setRotationPoint(6F, 0F, 6F);
	Pipemid.setTextureSize(64, 32);
	Pipemid.mirror = true;

	Pipe1 = new ModelRenderer(this, 0, 16);
	Pipe1.addBox(0F, 0F, 0F, 6, 3, 4);
	Pipe1.setRotationPoint(0F, 0F, 6F);
	Pipe1.setTextureSize(64, 32);
	Pipe1.mirror = true;

	Pipe2 = new ModelRenderer(this, 0, 0);
	Pipe2.addBox(0F, 0F, 0F, 4, 3, 6);
	Pipe2.setRotationPoint(6F, 0F, 0F);
	Pipe2.setTextureSize(64, 32);
	Pipe2.mirror = true;

	Pipe3 = new ModelRenderer(this, 0, 9);
	Pipe3.addBox(0F, 0F, 0F, 6, 3, 4);
	Pipe3.setRotationPoint(10F, 0F, 6F);
	Pipe3.setTextureSize(64, 32);
	Pipe3.mirror = true;

	Pipe4 = new ModelRenderer(this, 0, 23);
	Pipe4.addBox(0F, 0F, 0F, 4, 3, 6);
	Pipe4.setRotationPoint(6F, 0F, 10F);
	Pipe4.setTextureSize(64, 32);
	Pipe4.mirror = true;
}

public void render(float scale)
{
	Pipe1.render(scale);
	Pipemid.render(scale);
	Pipe2.render(scale);
	Pipe3.render(scale);
	Pipe4.render(scale);
}
public void render(GasPipe_softCopper tile, double x, double y, double z) {
	GL11.glPushMatrix();
	GL11.glDisable(GL11.GL_LIGHTING);
	GL11.glTranslated(x, y, z);
	ForgeHooksClient.bindTexture("/TheArni/GasCraft/Textures/SoftCopperPipe.png", 0);
	variableRender(tile);
	Pipemid.render(scale);
	GL11.glEnable(GL11.GL_LIGHTING);
	GL11.glPopMatrix();

}

private void variableRender(GasPipe_softCopper tile) {
	if(tile.connectivity_0) Pipe4.render(scale);
	if(tile.connectivity_1) Pipe1.render(scale);
	if(tile.connectivity_2) Pipe2.render(scale);
	if(tile.connectivity_3) Pipe3.render(scale);
	//System.out.println(tile.connectivity_0);
}

}

 

Thanks a lot. If you need something else, tell me!

 

PD: I'm still working on 1.4.7 . I've got a lot of problems to solve before the mod-crushing 1.5.1 update :P

Link to comment
Share on other sites

Mmmm well, all that... I'm working arround, but it actually can update the TileEntity's vars. But it can't access it in the rendering process, so now I must care of the client side. Later I will test the mod with my friends... When I get a stable build...

 

 

Link to comment
Share on other sites

Please any idea? I tried to use:

tileEntity.worldObj.getBlockTileEntity(int x, int y, int z)

but that didn't work because in the models i only have this values on double format and I don't know why x.intValue doesn't work.

 

Help me please!

Link to comment
Share on other sites

OK, solved,

I tried casting the doubles to ints, so

tileEntity.worldObj.getBlockTileEntity((int) x,(int) y,(int) z);

 

But i though that it didn't work because of the casting, but it actually was because it was null, there was no entity there... at least now it doesn't crash, but now it doesn't render. OK, my job here it's done.

 

PD: Also thanks ChickenBones from the DW20 Irc, you helped me a lot, and I'm sorry to wasted your time D:

Link to comment
Share on other sites

If your using the coords the renderTileEntityAt is called with, it won't work. those values refer to the rendering position of the block and is different from its world position.

 

You need to use the tile entity that renderTileEntityAt is called with.

 

Have you overridden getDescriptionPacket and onDataPacket? You need to do this to properly access the values during rendering, even on single player.

 

Assuming you TileEntity has nbt saving properly implemented you can use

public Packet getDescriptionPacket()
{
NBTTagCompound tag = new NBTTagCompound();
this.writeToNBT(tag);
return new Packet132TileEntityData(this.xCoord, this.yCoord, this.zCoord, 1, tag);
}

public void onDataPacket(INetworkManager net, Packet132TileEntityData packet)
{
NBTTagCompound tag = packet.customParam1; // get tag
this.readFromNBT(tag);
}

in you TileEntity class

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hello, I was trying to play a MOD in my preferred language, but I see that only some items are translated, and I go to debug and I get this information (the only thing that is translated is the bestiary):   [14sep.2024 17:14:36.415] [Render thread/WARN] [net.minecraft.client.resources.language.ClientLanguage/]: Skipped language file: mowziesmobs:lang/es_es.json (com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected name at line 394 column 2 path $.config.mowziesmobs.ice_crystal_attack_multiplier) [14sep.2024 17:14:36.421] [Render thread/WARN] [net.minecraft.client.resources.language.ClientLanguage/]: Skipped language file: iceandfire:lang/es_es.json (com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1349 column 4 path $.iceandfire.sound.subtitle.dragonflute)   Is that the reason why everything is not translated? , and is there any way to fix it? Thanks
    • I had an amazing experience with The Hack Angels. I had given up hope, but they managed to recover my bitcoins from an online scammer. I was able to retrieve what I had thought was lost forever because of their knowledge and commitment. I heartily endorse the services of The Hack Angels to anyone in need of help recovering cryptocurrency. They really are authorities in their domain.  (Web: https://thehackangels.com)     Whats Ap; +1 520) - 200, 23  20 ) ,  Mail Box; support@thehackangels. com
    • I got my model to render from the models renderToBuffer method. But still not quite what I want. I want to render the model from my renderer's render method. I feel that having access to the renderer and it's methods will open some doors for me later down the line. //EntityRendererProvider.Context pContext = ; I want this //ToaPlayerRenderer render = new ToaPlayerRenderer(pContext, false); // if I can get the above line to work, having the methods from the renderer class would be incredibly helpful down the line RenderType rendertype = model.renderType(p.getSkinTextureLocation()); // this should be something like render.getTextureLocation() VertexConsumer vertexconsumer = buffer.getBuffer(rendertype); model.renderToBuffer(stack, vertexconsumer, paLights, 1, 1, 1, 1, 1); // I don't want the render to happen here since it doesn't use the renderer //model.render(p, 1f, pTicks, stack, buffer, paLights); I want to render the model using this It is certainly getting closer though. Probably. I am still worried that even if pContext is initialized this new instance of the renderer class will still hit me with the classic and all too familiar "can't use static method in non-static context"
    • Hello, I am learning how to create Multipart Entities and I tried creating a PartEntity based on the EnderDragonPart code. However, when I tested summoning the entity in the game, the PartEntity appeared at position x 0, y 0, z 0 within the game. I tried to make it follow the main entity, and after testing again, the part entity followed the main entity but seemed to teleport back to x 0, y 0, z 0 every tick (I'm just guessing). I don't know how to fix this can someone help me? My github https://github.com/SteveKK666/Forge-NewWorld-1.20.1/tree/master/src/main/java/net/kk/newworldmod/entity/custom Illustration  https://drive.google.com/file/d/157SPvyQCE8GcsRXyQQkD4Dyhalz6LjBn/view?usp=drive_link Sorry for my English; I’m not very good at it. 
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.