Jump to content

[1.7.2] Block rotation


Pardeep

Recommended Posts

I have a block with a custom model and I have all the code setup so that it renders correctly and the same "front" face points towards the player, depending on from which direction the player places the block (e.g. rotates like a furnace). However, once I exit the world and reload it, the block automatically rotates back to standard position (facing south), instead of staying in the orientation it has been set to. Anyone know what the issue is? I think it has something to do with the NBT data, but i'm not sure why.

CODE:

Block:

package lifemod.blocks.toilet;

import lifemod.LifeMod;
import lifemod.lifepanel.LifePanel;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;

public class BlockToilet extends BlockContainer
{
public BlockToilet()
{
	super(Material.rock);
	this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
}

@Override
public TileEntity createNewTileEntity(World var1, int var2)
{
	return new TileEntityToilet(); 
}

    public int getRenderType()
    {
    	return -1;
    }
    
    public boolean isOpaqueCube()
    {
    	return false;
    }

    public boolean renderAsNormalBlock()
    {
    	return false;
    }
    
    @Override
    public void onBlockPlacedBy(World world, int i, int j, int k, EntityLivingBase entityliving, ItemStack itemStack)
    {
        int facing = MathHelper.floor_double((double) ((entityliving.rotationYaw * 4F) / 360F) + 0.5D) & 3;
        int newFacing = 0;
        if (facing == 0)
        {
        	newFacing = 2;
        }
        if (facing == 1)
        {
        	newFacing = 5;
        }
        if (facing == 2)
        {
        	newFacing = 3;
        }
        if (facing == 3)
        {
        	newFacing = 4;
        }
        TileEntity te = world.getTileEntity(i, j, k);
        if (te != null && te instanceof TileEntityToilet)
        {
        	TileEntityToilet tet = (TileEntityToilet) te;
            tet.setFacingDirection(newFacing);
            world.markBlockForUpdate(i, j, k);
        }
    }
}

TileEntity

package lifemod.blocks.toilet;

import lifemod.packethandler.PacketHandler;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.Packet;
import net.minecraft.tileentity.TileEntity;

public class TileEntityToilet extends TileEntity
{
private int facingDirection;

public int getFacingDirection()
    {
        return this.facingDirection;
    }

public void setFacingDirection(int par1)
    {
        this.facingDirection = par1;
    }

@Override
    public void readFromNBT(NBTTagCompound nbttagcompound)
    {
        super.readFromNBT(nbttagcompound);  
        facingDirection = nbttagcompound.getInteger("facingDirection");
    }

    @Override
    public void writeToNBT(NBTTagCompound nbttagcompound)
    {
        super.writeToNBT(nbttagcompound);
        nbttagcompound.setInteger("facingDirection", facingDirection);
    }
}

TileEntitySpecialRenderer:

package lifemod.blocks.toilet;

import lifemod.LifeMod;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.entity.Entity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;

import org.lwjgl.opengl.GL11;

public class TileEntitySpecialRendererToilet extends TileEntitySpecialRenderer
{
private final ModelToilet model = new ModelToilet();
    private static final ResourceLocation texture = new ResourceLocation("lifemod:textures/blocks/toilet.png");
    
    public void render(TileEntityToilet te, double x, double y, double z, float scale)
    {
    	GL11.glPushMatrix(); 	
        GL11.glTranslatef((float) x + 0.5F, (float) y + 1.5F, (float) z + 0.5F);
        this.bindTexture(texture);
        //Rotates model, as for some reason it is initially upside (180 = angle, 1.0F at end = about z axis)
        GL11.glRotatef(180, 0.0F, 0.0F, 1.0F);
        int facing = te.getFacingDirection();
        int k = 0;
        //South
        if (facing == 2) {
            k = 0;
        }
        //North
        if (facing == 3) {
            k = 180;
        }
        //East
        if (facing == 4) {
            k = -90;
        }
        //West
        if (facing == 5) {
            k = 90;
        }
        //Rotates model on the spot, depending on direction, making the front always to player) (k = angle, 1.0F in middle = about y axis)
        GL11.glRotatef(k, 0.0F, 1.0F, 0.0F);
        GL11.glDisable(GL11.GL_CULL_FACE);
        GL11.glEnable(GL11.GL_ALPHA_TEST);
        this.model.render((Entity)null, 0.0F, 0.0F, -0.1F, 0.0F, 0.0F, 0.0625F);
        GL11.glPopMatrix();
}

    
    public void renderTileEntityAt(TileEntity p_147500_1_, double p_147500_2_, double p_147500_4_, double p_147500_6_, float p_147500_8_)
    {
    	this.render((TileEntityToilet)p_147500_1_, p_147500_2_, p_147500_4_, p_147500_6_, p_147500_8_);
    }
}

Inside the ClientProxy:

ClientRegistry.bindTileEntitySpecialRenderer(TileEntityToilet.class, new TileEntitySpecialRendererToilet());

 

Thanks in advance

Link to comment
Share on other sites

First have you registered you tileentity? If you have add this to the tileentity code:

 

   @Override
    public Packet getDescriptionPacket()
    {
        NBTTagCompound tag = new NBTTagCompound();
        writeToNBT(tag);
        return new S35PacketUpdateTileEntity(xCoord, yCoord, zCoord, 1, tag);
    }

    @Override
    public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity packet)
    {
        readFromNBT(packet.func_148857_g());
    }

Link to comment
Share on other sites

First have you registered you tileentity? If you have add this to the tileentity code:

 

   @Override
    public Packet getDescriptionPacket()
    {
        NBTTagCompound tag = new NBTTagCompound();
        writeToNBT(tag);
        return new S35PacketUpdateTileEntity(xCoord, yCoord, zCoord, 1, tag);
    }

    @Override
    public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity packet)
    {
        readFromNBT(packet.func_148857_g());
    }

 

As soon as I placed the block in the world this crash report appeared and the game crashed:

---- Minecraft Crash Report ----
// Who set us up the TNT?

Time: 21/04/14 18:17
Description: Exception ticking world

java.lang.RuntimeException: class lifemod.blocks.toilet.TileEntityToilet is missing a mapping! This is a bug!
at net.minecraft.tileentity.TileEntity.writeToNBT(TileEntity.java:100)
at lifemod.blocks.toilet.TileEntityToilet.writeToNBT(TileEntityToilet.java:37)
at lifemod.blocks.toilet.TileEntityToilet.getDescriptionPacket(TileEntityToilet.java:51)
at net.minecraft.server.management.PlayerManager$PlayerInstance.sendTileToAllPlayersWatchingChunk(PlayerManager.java:521)
at net.minecraft.server.management.PlayerManager$PlayerInstance.sendChunkUpdate(PlayerManager.java:463)
at net.minecraft.server.management.PlayerManager.updatePlayerInstances(PlayerManager.java:94)
at net.minecraft.world.WorldServer.tick(WorldServer.java:194)
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:682)
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:604)
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:482)
at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:742)


A detailed walkthrough of the error, its code path and all known details is as follows:
---------------------------------------------------------------------------------------

-- Head --
Stacktrace:
at net.minecraft.tileentity.TileEntity.writeToNBT(TileEntity.java:100)
at lifemod.blocks.toilet.TileEntityToilet.writeToNBT(TileEntityToilet.java:37)
at lifemod.blocks.toilet.TileEntityToilet.getDescriptionPacket(TileEntityToilet.java:51)
at net.minecraft.server.management.PlayerManager$PlayerInstance.sendTileToAllPlayersWatchingChunk(PlayerManager.java:521)
at net.minecraft.server.management.PlayerManager$PlayerInstance.sendChunkUpdate(PlayerManager.java:463)
at net.minecraft.server.management.PlayerManager.updatePlayerInstances(PlayerManager.java:94)
at net.minecraft.world.WorldServer.tick(WorldServer.java:194)

-- Affected level --
Details:
Level name: New World
All players: 1 total; [EntityPlayerMP['Player715'/240, l='New World', x=257.46, y=4.00, z=-746.51]]
Chunk stats: ServerChunkCache: 625 Drop: 0
Level seed: -5659666563785698564
Level generator: ID 01 - flat, ver 0. Features enabled: true
Level generator options: 
Level spawn location: World: (254,4,-751), Chunk: (at 14,0,1 in 15,-47; contains blocks 240,0,-752 to 255,255,-737), Region: (0,-2; contains chunks 0,-64 to 31,-33, blocks 0,0,-1024 to 511,255,-513)
Level time: 3368 game time, 3368 day time
Level dimension: 0
Level storage version: 0x04ABD - Anvil
Level weather: Rain time: 143259 (now: false), thunder time: 17049 (now: false)
Level game mode: Game mode: creative (ID 1). Hardcore: false. Cheats: true
Stacktrace:
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:682)
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:604)
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:482)
at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:742)

-- System Details --
Details:
Minecraft Version: 1.7.2
Operating System: Windows 7 (amd64) version 6.1
Java Version: 1.8.0_05, Oracle Corporation
Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Oracle Corporation
Memory: 912588064 bytes (870 MB) / 1037959168 bytes (989 MB) up to 1037959168 bytes (989 MB)
JVM Flags: 3 total; -Xincgc -Xmx1024M -Xms1024M
AABB Pool Size: 4715 (264040 bytes; 0 MB) allocated, 0 (0 bytes; 0 MB) used
IntCache: cache: 0, tcache: 0, allocated: 0, tallocated: 0
FML: MCP v9.01-pre FML v7.2.156.1060 Minecraft Forge 10.12.1.1060 4 mods loaded, 4 mods active
mcp{8.09} [Minecraft Coder Pack] (minecraft.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
FML{7.2.156.1060} [Forge Mod Loader] (forgeSrc-1.7.2-10.12.1.1060.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
Forge{10.12.1.1060} [Minecraft Forge] (forgeSrc-1.7.2-10.12.1.1060.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
ss97lifemod{1.0.0} [Life Mod] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
Profiler Position: N/A (disabled)
Vec3 Pool Size: 596 (33376 bytes; 0 MB) allocated, 0 (0 bytes; 0 MB) used
Player Count: 1 / 8; [EntityPlayerMP['Player715'/240, l='New World', x=257.46, y=4.00, z=-746.51]]
Type: Integrated Server (map_client.txt)
Is Modded: Definitely; Client brand changed to 'fml,forge'

Link to comment
Share on other sites

Yep that is an unregistred tileentity! You need to add this in your common proxy:

 

GameRegistry.registerTileEntity(MyTileEntity.class, "MyTileEntityName");

 

I recommend in your proxy have a method called registerTileEntities().

 

ClientProxy:

 

@Override
public static void registerTileEntities()
{
    super.registerTileEntities();
    ClientRegistry.bindTileEntitySpecialRenderer(TileEntityToilet.class, new TileEntitySpecialRendererToilet());
}

 

CommonProxy:

 

public static void registerTileEntities()
{
    GameRegistry.registerTileEntity(TileEntityToilet.class, "Toilet");
}

 

And in your mod class in preInit add this:

 

proxy.registerTileEntities();

Link to comment
Share on other sites

Yep that is an unregistred tileentity! You need to add this in your common proxy:

 

GameRegistry.registerTileEntity(MyTileEntity.class, "MyTileEntityName");

 

I recommend in your proxy have a method called registerTileEntities().

 

ClientProxy:

 

@Override
public static void registerTileEntities()
{
    super.registerTileEntities();
    ClientRegistry.bindTileEntitySpecialRenderer(TileEntityToilet.class, new TileEntitySpecialRendererToilet());
}

 

CommonProxy:

 

public static void registerTileEntities()
{
    GameRegistry.registerTileEntity(TileEntityToilet.class, "Toilet");
}

 

And in your mod class in preInit add this:

 

proxy.registerTileEntities();

 

I had it registered anyway. But, now it works so thank you :D

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.