Jump to content

Custom Rendered Block Not Saving Direction


Mottbro

Recommended Posts

I have added a custom rendered block to Minecraft and have added some code so that it faces the player when it is placed. The problem is that when I reload the Minecraft world, the rotation of the block is not saved so they all face the default direction. I am pretty sure that there is a fairly simple fix but am not sure what it is.

 

CODE:

 

Block

package com.mottbro.coastercraftmod.blocks;

import com.mottbro.coastercraftmod.CoastercraftMod;
import com.mottbro.coastercraftmod.titleentity.TitleEntityTrackS;
import com.mottbro.coastercraftmod.titleentity.TitleEntityTrackStraightS;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;

public class TrackStraightS extends BlockContainer {

public TrackStraightS(Material material) {
	super(material);

	this.setHardness(2.0F);
	this.setResistance(5.0F);
	this.setHardness(4f);
	this.setCreativeTab(CoastercraftMod.tabMyMod);

}

public int getRenderType() {
	return -1;
}

public boolean isOpaqueCube() {
	return false;
}

public boolean renderAsNormalBlock() {
	return false;
}


//Rotate when placed
@Override
public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entity, ItemStack stack) {
	if (entity == null) {
		return;
	}
	TitleEntityTrackStraightS tile = (TitleEntityTrackStraightS) world.getTileEntity(x, y, z);
	tile.direction = MathHelper.floor_double((double) (entity.rotationYaw * 4.0F / 360) + 0.50) & 3;		

}



@Override
public TileEntity createNewTileEntity(World arg0, int arg1) {		
	return new TitleEntityTrackStraightS(); 
}
    




//Set Icon Image as the Unlocalised Name
@SideOnly(Side.CLIENT)
public void registerBlockIcons(IIconRegister iconRegister) {
	this.blockIcon = iconRegister.registerIcon("coastercraftmod" + ":" + this.getUnlocalizedName().substring(5));
}

}


 

 

TileEntity

package com.mottbro.coastercraftmod.titleentity;


import com.jcraft.jogg.Packet;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;

public class TitleEntityTrackStraightS extends TileEntity {
//rotate when placed
	public int direction;	



}

 

TileEntitySpecialRenderer

package com.mottbro.coastercraftmod.renderer;

import org.lwjgl.opengl.GL11;





import com.mottbro.coastercraftmod.model.ModelSupportBaseConcrete;
import com.mottbro.coastercraftmod.model.ModelTrackStraightSupport;
import com.mottbro.coastercraftmod.titleentity.TitleEntityTrackStraightS;

import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;

public class RenderTrackStraightSupport extends TileEntitySpecialRenderer {


//texture WORKING!
private static final ResourceLocation texture = new ResourceLocation("coastercraftmod" + ":" + "textures/model/TrackStraightSupport.png");

private ModelTrackStraightSupport model;

private TitleEntityTrackStraightS te;

public RenderTrackStraightSupport() {
	this.model = new ModelTrackStraightSupport();
}


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

		//Direction when placed
		TitleEntityTrackStraightS myTile = (TitleEntityTrackStraightS) tileentity;
		int direction = myTile.direction;
		GL11.glRotatef(direction * 90, 0.0F, 1.0F, 0.0F);


		GL11.glPushMatrix();
			this.model.renderModel(0.0625F);
		GL11.glPopMatrix();






	GL11.glPopMatrix();
}


private int getFacingDirection() {
	// TODO Auto-generated method stub
	return 0;
}

}

 

Thanks for your help!  :)

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

Well, I guess it would be easiest to save to NBT then :P

 

At any rate:

There are two NBT methods for your tile entity - a method that loads the nbt, and a method that saves the nbt. You will want both:

@Override
public void writeToNBT(NBTTagCompound nbt)
{

}

@Override
public void readFromNBT(NBTTagCompound nbt)
{

}

 

Now. Every NBTTagCompound has methods for writing primitive types: so ints, strings, bytes, everything you need! When saving these are used as such:

nbtTagInstance.set***("identifierString", variable); // Where *** is the type you want to save (e.g. Integer), identifierString is the the string which identifies this value, and variable which is the variable you want to store

For loading:

variable = nbt.get***("identifierString"); // Where *** is the type you want to load (e.g. Integer), identiferString is the string used to save the variable (MUST BE THE SAME), and variable is the variable you want to load the saved value to

 

P.S. Welcome to the forums :)

We all stuff up sometimes... But I seem to be at the bottom of that pot.

Link to comment
Share on other sites

Ok Thanks

So I need to modify the Tile Entity to write and read from NBT using:

@Override
	public void writeToNBT(NBTTagCompound nbt)
	{

	}

	@Override
	public void readFromNBT(NBTTagCompound nbt)
	{

	}

 

But where does the other 2 lines go and what do I need to substitute in for ***, identifierString and variable

(I know it is probably really obvious but I am hopeless  :-\)

 

Link to comment
Share on other sites

Okay...

@Override
public void writeToNBT(NBTTagCompound nbt)
{
        nbt.setInteger("ovenDirection", this.direction);
}

@Override
public void readFromNBT(NBTTagCompound nbt)
{
        this.direction = nbt.getInteger("ovenDirection");
}

 

That should do it...

We all stuff up sometimes... But I seem to be at the bottom of that pot.

Link to comment
Share on other sites

  • 2 weeks later...

Paste what you have now, along with any error logs you might be getting.

 

Please paste as a gist - that has line numbers, syntax highlighting, and unlike other popular paste-sites - no ads.

 

Additionally, it allows us to fork your code.

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.