Jump to content

[SOLVED] TileEntity problem


TheTrollingTrollguy

Recommended Posts

Hello. I have a problem with my tile entity. When I place it in the world is has correct textures, but when I exit the world and enter it again, the textures reset. Basically I have a tile entity just to save what subtype my block has and based on that give it textures for all sides, but they reset and the subtype isn't saved after exiting the world. So can anyone help me?

 

Code:

 

BlocksOfAsphalt_White_0:

package trafficStuffMod.common;

import java.util.List;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Icon;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;

public class BlocksOfAsphalt_White_0 extends BlockContainer
{
public static final String[] subTypes = new String[] {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
													  "10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
													  "20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
													  "30", "31", "32", "33", "34", "35", "36", "37", "38", "39"};

@SideOnly(Side.CLIENT) private Icon ATW_0; @SideOnly(Side.CLIENT) private Icon ATW_1; @SideOnly(Side.CLIENT) private Icon ATW_2; @SideOnly(Side.CLIENT) private Icon ATW_3; @SideOnly(Side.CLIENT) private Icon ATW_4;
@SideOnly(Side.CLIENT) private Icon ATW_5; @SideOnly(Side.CLIENT) private Icon ATW_6; @SideOnly(Side.CLIENT) private Icon ATW_7; @SideOnly(Side.CLIENT) private Icon ATW_8; @SideOnly(Side.CLIENT) private Icon ATW_9;
@SideOnly(Side.CLIENT) private Icon ATW_10; @SideOnly(Side.CLIENT) private Icon ATW_11; @SideOnly(Side.CLIENT) private Icon ATW_12; @SideOnly(Side.CLIENT) private Icon ATW_13; @SideOnly(Side.CLIENT) private Icon ATW_14;
@SideOnly(Side.CLIENT) private Icon ATW_15; @SideOnly(Side.CLIENT) private Icon ATW_16; @SideOnly(Side.CLIENT) private Icon ATW_17; @SideOnly(Side.CLIENT) private Icon ATW_18; @SideOnly(Side.CLIENT) private Icon ATW_19;
@SideOnly(Side.CLIENT) private Icon ATW_20; @SideOnly(Side.CLIENT) private Icon ATW_21; @SideOnly(Side.CLIENT) private Icon ATW_22; @SideOnly(Side.CLIENT) private Icon ATW_23; @SideOnly(Side.CLIENT) private Icon ATW_24;

public BlocksOfAsphalt_White_0(int ID, Material material)
{
	super(ID, material);
	this.setCreativeTab(TrafficStuffMod.tabBlocksOfAsphalt);
	this.setStepSound(Block.soundStoneFootstep);
}

public TileEntity createNewTileEntity(World world)
{
	return new TileEntityBlocksOfAsphalt_White_0();
}

@Override
public boolean hasTileEntity(int metadata)
{
	return true;
}

@Override
public void breakBlock(World world, int X, int Y, int Z, int ID, int metadata)
{
    super.breakBlock(world, X, Y, Z, ID, metadata);
    world.removeBlockTileEntity(X, Y, Z);
}

@SideOnly(Side.CLIENT)
    public Icon getBlockTexture(IBlockAccess IBlockAccess, int X, int Y, int Z, int side)
    {
	TileEntityBlocksOfAsphalt_White_0 tileEntityBlocksOfAsphalt_White_0 = (TileEntityBlocksOfAsphalt_White_0) IBlockAccess.getBlockTileEntity(X, Y, Z);
		 return this.getIcon(side, tileEntityBlocksOfAsphalt_White_0.getSubType());
    }

public Icon getIcon(int	side, int meta)
{
	if(meta == 0) {switch(side) {case 0: return this.ATW_0; case 1: return this.ATW_0; case 2: return this.ATW_0; case 3: return this.ATW_0; case 4: return this.ATW_0; case 5: return this.ATW_0;}}
	if(meta == 1) {switch(side) {case 0: return this.ATW_0; case 1: return this.ATW_1; case 2: return this.ATW_0; case 3: return this.ATW_0; case 4: return this.ATW_0; case 5: return this.ATW_0;}}
	if(meta == 2) {switch(side) {case 0: return this.ATW_0; case 1: return this.ATW_2; case 2: return this.ATW_0; case 3: return this.ATW_0; case 4: return this.ATW_0; case 5: return this.ATW_0;}}
	if(meta == 3) {switch(side) {case 0: return this.ATW_0; case 1: return this.ATW_3; case 2: return this.ATW_0; case 3: return this.ATW_0; case 4: return this.ATW_0; case 5: return this.ATW_0;}}
	if(meta == 4) {switch(side) {case 0: return this.ATW_0; case 1: return this.ATW_4; case 2: return this.ATW_0; case 3: return this.ATW_0; case 4: return this.ATW_0; case 5: return this.ATW_0;}}
	if(meta == 5) {switch(side) {case 0: return this.ATW_0; case 1: return this.ATW_5; case 2: return this.ATW_0; case 3: return this.ATW_0; case 4: return this.ATW_0; case 5: return this.ATW_0;}}
	if(meta == 6) {switch(side) {case 0: return this.ATW_0; case 1: return this.ATW_6; case 2: return this.ATW_0; case 3: return this.ATW_0; case 4: return this.ATW_0; case 5: return this.ATW_0;}}
	if(meta == 7) {switch(side) {case 0: return this.ATW_0; case 1: return this.ATW_7; case 2: return this.ATW_0; case 3: return this.ATW_0; case 4: return this.ATW_0; case 5: return this.ATW_0;}}
	if(meta ==  {switch(side) {case 0: return this.ATW_0; case 1: return this.ATW_8; case 2: return this.ATW_0; case 3: return this.ATW_0; case 4: return this.ATW_0; case 5: return this.ATW_0;}}
	if(meta == 9) {switch(side) {case 0: return this.ATW_0; case 1: return this.ATW_9; case 2: return this.ATW_0; case 3: return this.ATW_0; case 4: return this.ATW_0; case 5: return this.ATW_0;}}
	if(meta == 10) {switch(side) {case 0: return this.ATW_0; case 1: return this.ATW_10; case 2: return this.ATW_0; case 3: return this.ATW_0; case 4: return this.ATW_0; case 5: return this.ATW_0;}}
	if(meta == 11) {switch(side) {case 0: return this.ATW_0; case 1: return this.ATW_11; case 2: return this.ATW_0; case 3: return this.ATW_0; case 4: return this.ATW_0; case 5: return this.ATW_0;}}
	if(meta == 12) {switch(side) {case 0: return this.ATW_0; case 1: return this.ATW_12; case 2: return this.ATW_0; case 3: return this.ATW_0; case 4: return this.ATW_0; case 5: return this.ATW_0;}}
	if(meta == 13) {switch(side) {case 0: return this.ATW_0; case 1: return this.ATW_13; case 2: return this.ATW_0; case 3: return this.ATW_0; case 4: return this.ATW_0; case 5: return this.ATW_0;}}
	if(meta == 14) {switch(side) {case 0: return this.ATW_0; case 1: return this.ATW_14; case 2: return this.ATW_0; case 3: return this.ATW_0; case 4: return this.ATW_0; case 5: return this.ATW_0;}}
	if(meta == 15) {switch(side) {case 0: return this.ATW_0; case 1: return this.ATW_15; case 2: return this.ATW_0; case 3: return this.ATW_0; case 4: return this.ATW_0; case 5: return this.ATW_0;}}
	if(meta == 16) {switch(side) {case 0: return this.ATW_0; case 1: return this.ATW_16; case 2: return this.ATW_0; case 3: return this.ATW_0; case 4: return this.ATW_0; case 5: return this.ATW_0;}}
	if(meta == 17) {switch(side) {case 0: return this.ATW_0; case 1: return this.ATW_17; case 2: return this.ATW_0; case 3: return this.ATW_0; case 4: return this.ATW_0; case 5: return this.ATW_0;}}
	return this.blockIcon;
}

@SideOnly(Side.CLIENT)
public void registerIcons(IconRegister iconRegister)
{	
	this.ATW_0 = iconRegister.registerIcon("asphaltTop");
	this.ATW_1 = iconRegister.registerIcon("asphaltTop_white_1");
	this.ATW_2 = iconRegister.registerIcon("asphaltTop_white_2");
	this.ATW_3 = iconRegister.registerIcon("asphaltTop_white_3");
	this.ATW_4 = iconRegister.registerIcon("asphaltTop_white_4");
	this.ATW_5 = iconRegister.registerIcon("asphaltTop_white_5");
	this.ATW_6 = iconRegister.registerIcon("asphaltTop_white_6");
	this.ATW_7 = iconRegister.registerIcon("asphaltTop_white_7");
	this.ATW_8 = iconRegister.registerIcon("asphaltTop_white_8");
	this.ATW_9 = iconRegister.registerIcon("asphaltTop_white_9");
	this.ATW_10 = iconRegister.registerIcon("asphaltTop_white_10");
	this.ATW_11 = iconRegister.registerIcon("asphaltTop_white_11");
	this.ATW_12 = iconRegister.registerIcon("asphaltTop_white_12");
	this.ATW_13 = iconRegister.registerIcon("asphaltTop_white_13");
	this.ATW_14 = iconRegister.registerIcon("asphaltTop_white_14");
	this.ATW_15 = iconRegister.registerIcon("asphaltTop_white_15");
	this.ATW_16 = iconRegister.registerIcon("asphaltTop_white_16");
	this.ATW_17 = iconRegister.registerIcon("asphaltTop_white_17");
	this.ATW_18 = iconRegister.registerIcon("asphaltTop_white_18");
	this.ATW_19 = iconRegister.registerIcon("asphaltTop_white_19");
	this.ATW_20 = iconRegister.registerIcon("asphaltTop_white_20");
	this.ATW_21 = iconRegister.registerIcon("asphaltTop_white_21");
	this.ATW_22 = iconRegister.registerIcon("asphaltTop_white_22");
	this.ATW_23 = iconRegister.registerIcon("asphaltTop_white_23");
	this.ATW_24 = iconRegister.registerIcon("asphaltTop_white_24");
}

protected boolean canSilkHarvest()
{
	return false;
}

@SideOnly(Side.CLIENT)
public void getSubBlocks(int ID, CreativeTabs creativeTabs, List list)
{
	for(int i = 0; i < this.subTypes.length; i++)
	{
		list.add(new ItemStack(ID, 1, i));
	}
}


}

ItemBlocksOfAsphalt_White_0:

package trafficStuffMod.common;

import java.util.Iterator;
import java.util.List;
import java.util.Random;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.block.BlockSkull;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityEggInfo;
import net.minecraft.entity.EntityList;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntitySkull;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;

public class ItemBlocksOfAsphalt_White_0 extends ItemBlock
{
static int blockID = TrafficStuffMod.blocksOfAsphalt_White_0.blockID;

public static final String[] subTypes = new String[] {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
													  "10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
													  "20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
													  "30", "31", "32", "33", "34", "35", "36", "37", "38", "39"};

public ItemBlocksOfAsphalt_White_0(int ID)
{
	super(ID);
	this.setHasSubtypes(true);
}

public boolean shouldRotateAroundWhenRendering()
{
    return true;
}

public void addInformation(ItemStack itemStack, EntityPlayer entityPlayer, List list, boolean something)
    {
    	int subType = itemStack.getItemDamage();
    	if (subType == 0)
    	{
    		list.add("Plain");
    	}
    }

@Override
public boolean onItemUse(ItemStack itemStack, EntityPlayer entityPlayer, World world, int X, int Y, int Z, int side, float hitX, float hitY, float hitZ)
{	

	if (side == 0)
        {
            --Y;
        }

        if (side == 1)
        {
            ++Y;
        }

        if (side == 2)
        {
            --Z;
        }

        if (side == 3)
        {
            ++Z;
        }

        if (side == 4)
        {
            --X;
        }

         if (side == 5)
        {
            ++X;
        }
	if(itemStack.stackSize == 0)
	{
		return false;
	}
	else if (!entityPlayer.canPlayerEdit(X, Y, Z, side, itemStack))
	{
		return false;
	}
	else if (Y == 255 && Block.blocksList[this.itemID].blockMaterial.isSolid())
	{
		return false;
	}
	else
	{
		world.setBlock(X, Y, Z, this.blockID);
        int i1 = 0;

        TileEntity tileentity = world.getBlockTileEntity(X, Y, Z);

        if (tileentity != null && tileentity instanceof TileEntityBlocksOfAsphalt_White_0)
        {
        	((TileEntityBlocksOfAsphalt_White_0)tileentity).setSubType(itemStack.getItemDamage());
        }
        --itemStack.stackSize;
        return true;
	}
}

public String getUnlocalizedName(ItemStack itemStack)
{
	return "blocksOfAsphalt_White_0" + "." + itemStack.getItemDamage();
}
}

 

TileEntityBlocksOfAsphalt_White_0:

package trafficStuffMod.common;

import java.util.logging.Level;

import cpw.mods.fml.common.FMLLog;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet132TileEntityData;
import net.minecraft.server.MinecraftServer;
import net.minecraft.tileentity.TileEntity;

public class TileEntityBlocksOfAsphalt_White_0 extends TileEntity
{
public int subType;

public TileEntityBlocksOfAsphalt_White_0() {}

public int getSubType()
{
	return subType;
}

public void setSubType(int subTypes)
{
	this.subType = subTypes;
}

@Override
public void onChunkUnload()
{
	NBTTagCompound NBT = new NBTTagCompound();
	this.writeToNBT(NBT);
}

@Override
public void readFromNBT(NBTTagCompound NBT)
{
	super.readFromNBT(NBT);
	this.subType = NBT.getInteger("subType");
}

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

@Override
public Packet getDescriptionPacket()
{
	NBTTagCompound nbttagcompound = new NBTTagCompound();
    this.writeToNBT(nbttagcompound);
    return new Packet132TileEntityData(this.xCoord, this.yCoord, this.zCoord, 4, nbttagcompound);
}
}

 

and in my main class I have this:

 

public static Block blocksOfAsphalt_White_0;      public static int blocksOfAsphalt_White_0ID;

public static TileEntity BOA_white_0 = new TileEntityBlocksOfAsphalt_White_0();

blocksOfAsphalt_White_0 = new BlocksOfAsphalt_White_0(blocksOfAsphalt_White_0ID, Material.rock).setHardness(15.0F).setResistance(20.0F).setUnlocalizedName("blocksOfAsphalt_0_white_0");

GameRegistry.registerBlock(blocksOfAsphalt_White_0, ItemBlocksOfAsphalt_White_0.class, "blocksOfAsphalt_White_0");

GameRegistry.registerTileEntity(TileEntityBlocksOfAsphalt_White_0.class, "tileEntityBlocksOfAsphalt_White_0"); 

 

Link to comment
Share on other sites

Place a breakpoint at the this.subType = NBT.getInteger("subType");

and another one at the NBT.setInteger("subType", subType); line.

 

Then run the debugger and see what the values are when it's getting saved and when it's getting loaded.

If the value is the never changing from the default value, then the problem is with the packets probably.

If it's the correct value on save and load then it's something else.

 

Start with the debugging and let's take it from there depending on your results :)

If you guys dont get it.. then well ya.. try harder...

Link to comment
Share on other sites

When the world unloads and the write is called (this is done server side) is the variable holding the correct subtype?

I assume not. And I assume it holds the default value instead? :)

 

My thinking is that the server is never told about all this shit, since Icons are client side and all.

Inside the setSubType() method, right below the line "this.subType = subTypes;" you should try to send a packet to the server.

Remember to use "getDescriptionPacket()" to get the packet which you send, I'm just assuming you are familiar with packets if not check the forge wiki ;)

If you guys dont get it.. then well ya.. try harder...

Link to comment
Share on other sites

This is very simple, you are using the premade packet and don't even need your packet handler to manage this one I believe.

But hey, do you have the method "onPacketData(packet)" implemented? If not then that MAY alone be the cause of your problem.

If you have one, or if adding it does not resolve the problem. Then add the code to send a packet to the server as I mentioned in the last post.

 

We are talking about 4-5 lines of code to be added, so this isn't anything to worry about for Netty ;)

If you guys dont get it.. then well ya.. try harder...

Link to comment
Share on other sites

public void setSubType(int subTypes)
{
   this.subType = subTypes;
   Packet packet = this.getDescriptionPacket();
   // Send Packet to server here. 
}

 

Also override/add the onPacketData method similarly to how you did for the getDescriptionPacket method so you're TE knows to read the data for your field from that packet ;)

If you guys dont get it.. then well ya.. try harder...

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.



×
×
  • Create New...

Important Information

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