Meastroman Posted May 17, 2014 Posted May 17, 2014 Hey fellow Modders! I am trying to make an item that spawns a Multi-block structure in the world. I started with an item that creates 1 BlockContainer with TileEntity. I can't get it to work. Here's what I know so far: - Replacing my custom block for a vanilla block does make it spawn in the world. - Replacing my custom block for a vanilla TileEntity does make it spawn. (I tried the anvil block). - I use all necessary functions used ItemSkull, TileEntitySkull, BlockSkull. (Or at least as far as I know). - world.getTileEntity(x, y, z); returns null. - world.getTileEntity also returns null after I used world.setTileEntity(x, y, z, tileentity);. - Placing the block by hand does make it a working TileEntity - Placing a block by hand calls createNewTileEntity() multiple times (mostly 2 times, but I've seen 3 as well (print statement)). - I did look at numerous Tutorials, topics on this forum, and minecraft code examples (like the skull). Most of them are 1.6 though (some older).. I'll update this list whenever I found a new usefull clue. My guess is I'm forgetting to call a function, but I wouldn't know which one. The main thing I don't understand is why my TileEntity works whenever I place the block by hand, but whenever I use the Item it fails. Here is the code: ItemTurret public class ItemTurret extends Item { public ItemTurret() { setUnlocalizedName("itemturret"); setCreativeTab(CreativeTabs.tabMisc); setMaxStackSize(1); } public boolean onItemUse(ItemStack itemStack, EntityPlayer playerEntity, World world, int x, int y, int z, int sideBlock, float px, float py, float pz) { if(world.isRemote) { return true; } else if(!playerEntity.capabilities.isCreativeMode || sideBlock != 1) { return false; } else { ++y; BlockTurret turretblock = new BlockTurret(); int dir = MathHelper.floor_double((double)(playerEntity.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3; if(!world.isAirBlock(x, y, z) || !playerEntity.canPlayerEdit(x, y, z, sideBlock, itemStack)) { return false; } world.setBlock(x, y, z, turretblock, dir, 0x02); return true; } } } TileEntityTurret public class TileEntityTurret extends TileEntity { public int base_x; public int base_y; public int base_z; public int health = 3; public void updateEntity() { if(health <= 0) { worldObj.setBlock(xCoord, yCoord, zCoord, Blocks.air); } } public void writeToNBT(NBTTagCompound nbt) { super.writeToNBT(nbt); nbt.setInteger("health", health); nbt.setInteger("px", base_x); nbt.setInteger("py", base_y); nbt.setInteger("pz", base_z); } public void readFromNBT(NBTTagCompound nbt) { super.readFromNBT(nbt); this.health = nbt.getInteger("health"); this.base_x = nbt.getInteger("px"); this.base_y = nbt.getInteger("py"); this.base_z = nbt.getInteger("pz"); } } BlockTurret public class BlockTurret extends BlockContainer { public BlockTurret() { super(Material.rock); setBlockName("blockturret"); setBlockTextureName(Reference.MODID + ":" + getUnlocalizedName().substring(5)); setCreativeTab(CreativeTabs.tabMisc); setBlockUnbreakable(); setResistance(6000000.0F); } @Override public void onBlockClicked(World world, int x, int y, int z, EntityPlayer player) { ItemStack itemstack = player.getCurrentEquippedItem(); TileEntityTurret TETurret = (TileEntityTurret)world.getTileEntity(x, y, z); if(itemstack != null) { Item item = player.getCurrentEquippedItem().getItem(); if(item instanceof ItemTool) { ItemTool tool = (ItemTool)item; int level = tool.getHarvestLevel(itemstack, tool.getToolMaterialName()); System.out.print(level + tool.getToolMaterialName() + "\n"); TETurret.health -= 3; } else if(item instanceof ItemSword) { ItemSword tool = (ItemSword)item; float level = tool.func_150931_i(); System.out.print(level + tool.getToolMaterialName() + "\n"); TETurret.health -= (int)(4.0 + level); } else { TETurret.health--; } } else { TETurret.health--; } } @Override public boolean shouldSideBeRendered(IBlockAccess iblockaccess, int i, int j, int k, int l){ return false; } @Override public boolean isOpaqueCube(){ return false; } @SideOnly(Side.CLIENT) public Item getItem(World p_149694_1_, int p_149694_2_, int p_149694_3_, int p_149694_4_) { return new ItemTurret(); } @Override public TileEntity createNewTileEntity(World var1, int var2) { return new TileEntityTurret(); } }
coolAlias Posted May 18, 2014 Posted May 18, 2014 BlockTurret turretblock = new BlockTurret(); ... Never create new instances of Items or Blocks - each one should only be instantiated one single time when your mod starts, and every other 'instance' should refer to that instance. This is because Items and Blocks are all declared as static fields. http://i.imgur.com/NdrFdld.png[/img]
Meastroman Posted May 18, 2014 Author Posted May 18, 2014 Wow... Thanks so much! I could not believe that actually worked, but it makes a lot of sense now haha. I wasted almost a week of my time on this problem. CASE CLOSED
Recommended Posts