Mottbro Posted July 1, 2014 Posted July 1, 2014 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! Quote
Kwibble Posted July 1, 2014 Posted July 1, 2014 I think you will find blocks such as this are metadata blocks - a new metadata for each direction faced with metadata 0 being the default direction. Quote We all stuff up sometimes... But I seem to be at the bottom of that pot.
Kwibble Posted July 1, 2014 Posted July 1, 2014 And as another thing - you don't save your direction variable to NBT or anything. So of course its going to be a default direction. Quote We all stuff up sometimes... But I seem to be at the bottom of that pot.
Mottbro Posted July 1, 2014 Author Posted July 1, 2014 Ok so could you give me the code that I need to add? (Sorry I am fairly new to making mods) Quote
Kwibble Posted July 1, 2014 Posted July 1, 2014 Well, which would you prefer? Storing your already made direction value in nbt, or rewriting the code so that it has metadata? Quote We all stuff up sometimes... But I seem to be at the bottom of that pot.
Mottbro Posted July 1, 2014 Author Posted July 1, 2014 Whichever is easiest to do due to me being a bit of a newbe. Quote
Kwibble Posted July 1, 2014 Posted July 1, 2014 Well, I guess it would be easiest to save to NBT then 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 Quote We all stuff up sometimes... But I seem to be at the bottom of that pot.
Mottbro Posted July 1, 2014 Author Posted July 1, 2014 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 ) Quote
Kwibble Posted July 1, 2014 Posted July 1, 2014 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... Quote We all stuff up sometimes... But I seem to be at the bottom of that pot.
Mottbro Posted July 1, 2014 Author Posted July 1, 2014 I Just added this to the TileEntity class but it didn't work. - The Blocks rotated back when the world was reloaded. Do you know why it didn't work? Quote
Kwibble Posted July 1, 2014 Posted July 1, 2014 I'll test it out tomorrow and get back to you. That, or someone else will help you solve it first Quote We all stuff up sometimes... But I seem to be at the bottom of that pot.
Mottbro Posted July 9, 2014 Author Posted July 9, 2014 I am pretty sure that I need to sync the data from the server to the client but am unsure on the code. Does anyone know what I should add? Quote
Elyon Posted July 9, 2014 Posted July 9, 2014 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. Quote
TheGreyGhost Posted July 9, 2014 Posted July 9, 2014 Hi Check out TileEntityNote for a simple vanilla example. The missing super.writeToNBT(par1NBTTagCompound); and super.readFromNBT(par1NBTTagCompound); is probably the problem I imagine. -TGG Quote
Recommended Posts
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.