Jump to content

[1.6.4] Method Called on Tile Entity Creation


superninjaman45

Recommended Posts

Is there a method that is called when a tile entity is created. Well perhaps that is not exactly what I need to know. I need to assign a variable in the tile entity when it is created and only when it is created. It involves random numbers so i can't have it  being called constantly, only once. I hope that this isn't a dumb question. Thanks for your time. And sorry if this is not specific enough. Thanks

An average guy who mods Minecraft. If you need help and are willing to use your brain, don't be afraid to ask.

 

Also, check out the Unofficial Minecraft Coder Pack (MCP) Prerelease Center for the latest from the MCP Team!

 

Was I helpful? Leave some karma/thanks! :)

Link to comment
Share on other sites

Sorry. Just figured it out anyway. Shouldn't have posted. Thanks for replying though.

Edit:

Actually my troubles aren't over. Nothing is printed to the GUI. Well, if anything is, its blank anyway. I am swallowing a bit of pride here and I am going to post my code. Sigh. Any way you can help without telling me just to figure it out would be appreciated.

 

TileEntity

 

package superninjaman45.elvesanddwarves.blocks;

 

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.inventory.IInventory;

import net.minecraft.item.ItemStack;

import net.minecraft.nbt.NBTTagCompound;

import net.minecraft.tileentity.TileEntity;

import net.minecraft.world.World;

 

public class TileEntityVillageBlock extends TileEntity{

 

public String TownName;

 

public TileEntityVillageBlock() {

}

 

public TileEntityVillageBlock(String name) {

this.TownName = name;

System.out.print(TownName);

}

 

public void writeToNBT(NBTTagCompound var1)

  {

var1.setString("Village Name", this.TownName);

    super.writeToNBT(var1);

  }

 

public void readFromNBT(NBTTagCompound var1)

  {

    super.readFromNBT(var1);

    System.out.print("3");

    if(!var1.getString("Village Name").isEmpty())

    {

    System.out.print("4");

this.TownName = var1.getString("Village Name");

}

   

  }

 

public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer)

    {

        return this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : par1EntityPlayer.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;

    }

 

 

}

 

Block

 

package superninjaman45.elvesanddwarves.blocks;

 

import superninjaman45.elvesanddwarves.ElvesAndDwarves;

import cpw.mods.fml.common.network.FMLNetworkHandler;

import net.minecraft.block.BlockContainer;

import net.minecraft.block.material.Material;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.tileentity.TileEntity;

import net.minecraft.world.World;

 

public class BlockVillage extends BlockContainer{

 

public int nameNumber;

 

String[] townNames = new String[8];{

townNames[0] = "Halivaara";

townNames[1] = "Arkkukari";

townNames[2] = "Briar Glen";

townNames[3] = "Kameeraska";

townNames[4] = "Iron Forge";

townNames[5] = "Narnclaedra";

townNames[6] = "Ashborne";

townNames[7] = "Ninjavale";

}

 

 

public BlockVillage(int par1) {

super(par1, Material.iron);

this.nameNumber = (int) Math.floor(this.townNames.length * Math.random());

 

}

 

public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ) {

if(!world.isRemote) {

FMLNetworkHandler.openGui(player, ElvesAndDwarves.instance, 0, world, x, y, z);

}

return true;

}

 

/**

    * Returns a new instance of a block's tile entity class. Called on placing the block.

    */

 

  public TileEntity createNewTileEntity(World par1World)

  {

      System.out.print("1");

      return new TileEntityVillageBlock(townNames[nameNumber]);

  }

}

 

Gui

 

package superninjaman45.elvesanddwarves.client;

 

import org.lwjgl.opengl.GL11;

 

import superninjaman45.elvesanddwarves.blocks.TileEntityVillageBlock;

import net.minecraft.client.Minecraft;

import net.minecraft.client.gui.inventory.GuiContainer;

import net.minecraft.entity.player.InventoryPlayer;

import net.minecraft.inventory.Container;

import net.minecraft.util.ResourceLocation;

 

public class GuiVillageBlock extends GuiContainer{

public static final ResourceLocation texture = new ResourceLocation("ElvesAndDwarves:/textures/gui/deployer.png");

 

public GuiVillageBlock(TileEntityVillageBlock entity) {

super(new ContainerVillageBlock(entity));

xSize = 176;

ySize = 165;

}

 

 

@Override

public boolean doesGuiPauseGame()

{

return false;

}

 

public void drawGuiContainerBackgroundLayer(float f, int i, int j) {

TileEntityVillageBlock entity = new TileEntityVillageBlock();

GL11.glColor4f(1F, 1F, 1F, 1F);

Minecraft.getMinecraft().getTextureManager().bindTexture(texture);

drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);

int posX = (this.width - xSize) / 2;

        int posY = (this.height - ySize) / 2;

drawString(fontRenderer, entity.TownName, posX+50, posY+50, 0x000000);

System.out.print("2");

}

 

}

 

 

Yes the GUI is registered. And it does print things if I need it to.

An average guy who mods Minecraft. If you need help and are willing to use your brain, don't be afraid to ask.

 

Also, check out the Unofficial Minecraft Coder Pack (MCP) Prerelease Center for the latest from the MCP Team!

 

Was I helpful? Leave some karma/thanks! :)

Link to comment
Share on other sites

I did trying using world.getBlockTileEntity(x,y,z) The files were a lot different then so don't trying looking for it in them now. However I had the same problem.  Nothing would show up. Here is that older code.

 

Block

package superninjaman45.elvesanddwarves.blocks;

import superninjaman45.elvesanddwarves.ElvesAndDwarves;
import cpw.mods.fml.common.network.FMLNetworkHandler;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class BlockVillage extends BlockContainer{

public int nameNumber;

String[] townNames = new String[8];{
	townNames[0] = "Halivaara";
	townNames[1] = "Arkkukari";
	townNames[2] = "Briar Glen";
	townNames[3] = "Kameeraska";
	townNames[4] = "Iron Forge";
	townNames[5] = "Narnclaedra";
	townNames[6] = "Ashborne";
	townNames[7] = "Ninjavale";
	}


public BlockVillage(int par1) {
	super(par1, Material.iron);
	this.nameNumber = (int) Math.floor(this.townNames.length * Math.random());

		}

public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ) {
if(!world.isRemote) {
FMLNetworkHandler.openGui(player, ElvesAndDwarves.instance, 0, world, x, y, z);
}
return true;
}

/**
    * Returns a new instance of a block's tile entity class. Called on placing the block.
    */

   public TileEntity createNewTileEntity(World par1World)
   {
       return new TileEntityVillageBlock();
   }


   public void onBlockAdded(World par1World, int par2, int par3, int par4) {
     ((TileEntityVillageBlock)par1World.getBlockTileEntity(par2, par3, par4)).TownName = this.townNames[nameNumber];
   }
}

TileEntity

package superninjaman45.elvesanddwarves.blocks;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class TileEntityVillageBlock extends TileEntity implements IInventory{

public String TownName;

public void writeToNBT(NBTTagCompound var1)
  {
	var1.setString("Village Name", this.TownName);
    super.writeToNBT(var1);
  }

public void readFromNBT(NBTTagCompound var1)
  {
    super.readFromNBT(var1);
    if(!var1.getString("Village Name").isEmpty())
    {
	this.TownName = var1.getString("Village Name");
	}
    
  }

public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer)
    {
        return this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : par1EntityPlayer.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;
    }

@Override
public int getSizeInventory() {

	return 0;
}

@Override
public ItemStack getStackInSlot(int i) {

	return null;
}

@Override
public ItemStack decrStackSize(int i, int j) {

	return null;
}

@Override
public ItemStack getStackInSlotOnClosing(int i) {

	return null;
}

@Override
public void setInventorySlotContents(int i, ItemStack itemstack) {


}

@Override
public String getInvName() {

	return null;
}

@Override
public boolean isInvNameLocalized() {

	return false;
}

@Override
public int getInventoryStackLimit() {

	return 0;
}

@Override
public void openChest() {


}

@Override
public void closeChest() {

}

@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack) {

	return false;
}
}

 

Also, I'm not expecting magic. Did you read all the code? I thought it looked reasonable.

An average guy who mods Minecraft. If you need help and are willing to use your brain, don't be afraid to ask.

 

Also, check out the Unofficial Minecraft Coder Pack (MCP) Prerelease Center for the latest from the MCP Team!

 

Was I helpful? Leave some karma/thanks! :)

Link to comment
Share on other sites

   public BlockVillage(int par1) {
      super(par1, Material.iron);
      this.nameNumber = (int) Math.floor(this.townNames.length * Math.random());
      
         }

A block is only created once in the entire game. You should decide the name when the block is placed.

You have only 8 town names, you could use metadata and change the meta values.

Link to comment
Share on other sites

Ah, that makes sense! Thank you! Though I am not going to use metadata, the tile entity will have a lot more later, this is just a test. Its still not showing up on my GUI which is strange. See anything else?

An average guy who mods Minecraft. If you need help and are willing to use your brain, don't be afraid to ask.

 

Also, check out the Unofficial Minecraft Coder Pack (MCP) Prerelease Center for the latest from the MCP Team!

 

Was I helpful? Leave some karma/thanks! :)

Link to comment
Share on other sites

I believe I found the problem.  The part

 if(!var1.getString("Village Name").isEmpty())
    {
    System.out.print("1");
	this.TownName = var1.getString("Village Name");
	}
    

runs even if the value of var1 is empty. Not sure why that is.  I'll try something slightly different there.

An average guy who mods Minecraft. If you need help and are willing to use your brain, don't be afraid to ask.

 

Also, check out the Unofficial Minecraft Coder Pack (MCP) Prerelease Center for the latest from the MCP Team!

 

Was I helpful? Leave some karma/thanks! :)

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.