Posted May 3, 201312 yr Hi, I just started working on my very first mod. Its the second Java project I'm working on, thus I do have some basic knowledge about the language itself. The mod is very similar to CPWs IronChest. In fact his code was among the many I looked at the one I learned most from. He is using a single block for all of his chests. So am I. He however is using separate TileEntity classes for every kind of chest. I guess he decided to do it this way because the chunk system calls a block's TileEntity's default constructor and does thus not supply any information about the block (metadata). This is the code of interest: public TileEntityAdvancedChest() { // problem area } public TileEntityAdvancedChest(ChestType type) { this.type = type; this.size = type.rows * type.columns; this.inv = new ItemStack[this.size]; } Using the block's metadata to create the desired TileEntity is easy as long as the right constructor is being called (the second one). But is there some way to get the block's metadata whenever the default constructor is being called? I don't like the solution of having a lot of different classes flying around. One idea I've had was creating classes during runtime, based on the different chest types (using a class template), but I don't have a clue how to achieve something like this in Java. The search for enum based generic classes didn't help to much, because they were using the enumerator class, whilst I'd like to somehow use the values. The registry would look like this: for (AdvancedChestType type : AdvancedChestType.values()) { type.registerName(advancedChestBlock); GameRegistry.registerTileEntity(TileEntityAdvancedChest<type>.class, "tileEntity" + type.realName); } Maybe someone around here knows a neat way for this to work out. Cyclonit
May 3, 201312 yr You probably can call worldObj.getBlockMetadata(X, Y, Z) in your constructor to get the block's metadata. With it you can call your second constructor with the type parameter. Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! | mah twitter This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.
May 3, 201312 yr Author I'd need the block's coordinates for that and I cannot use "this" in the constructor to get something.
May 3, 201312 yr I'd need the block's coordinates for that and I cannot use "this" in the constructor to get something. Yes, you can: this.xCoord this.yCoord this.zCoord and call your constructor: this(type) Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! | mah twitter This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.
May 3, 201312 yr Author Writing it this way: public TileEntityAdvancedChest() { int meta = this.getWorldObj().getBlockMetadata(this.xCoord, this.yCoord, this.zCoord); this(AdvancedChestType.values()[meta]); } Isn't possible because the constructor call must be the first line in the default constructor. public TileEntityAdvancedChest() { this(AdvancedChestType.values()[this.getWorldObj().getBlockMetadata(this.xCoord, this.yCoord, this.zCoord]); } Doesn't work because you cannot use "this" (or super) while calling a constructor for the class itself.
May 3, 201312 yr Writing it this way: public TileEntityAdvancedChest() { int meta = this.getWorldObj().getBlockMetadata(this.xCoord, this.yCoord, this.zCoord); this(AdvancedChestType.values()[meta]); } Isn't possible because the constructor call must be the first line in the default constructor. public TileEntityAdvancedChest() { this(AdvancedChestType.values()[this.getWorldObj().getBlockMetadata(this.xCoord, this.yCoord, this.zCoord]); } Doesn't work because you cannot use "this" (or super) while calling a constructor for the class itself. Oh yeah, sorry. My bad. There is a way... Can I see your Block class? Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! | mah twitter This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.
May 4, 201312 yr Author Of course: package cyclonit.advancedChests; import java.util.List; import java.util.Random; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import cyclonit.advancedChests.core.AdvancedChestType; import cyclonit.advancedChests.core.Constants; 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.item.EntityItem; 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.util.Icon; import net.minecraft.world.World; public class BlockAdvancedChest extends Block { private AdvancedChestType type; public BlockAdvancedChest(int blockID, AdvancedChestType type) { super(blockID, Material.iron); this.type = type; this.setCreativeTab(CreativeTabs.tabDecorations); this.setBlockBounds(0.0625F, 0.0F, 0.0625F, 0.9375F, 0.875F, 0.9375F); } @Override public void getSubBlocks(int blockID, CreativeTabs creativeTabs, List itemList) { for (AdvancedChestType type : AdvancedChestType.values()) { itemList.add(new ItemStack(blockID, 1, type.ordinal())); } } /* * Textures * * We are using a single block-ID to store several blocks. Thus we * must provide the block's texture based on the metadata instead * of using just the ID. */ @SideOnly(Side.CLIENT) public void registerIcons(IconRegister iconRegister) { for (AdvancedChestType type : AdvancedChestType.values()) { type.registerIcons(iconRegister); } } @SideOnly(Side.CLIENT) @Override public Icon getBlockTextureFromSideAndMetadata(int side, int meta) { return AdvancedChestType.values()[meta].getTexture(side); } /* * TileEntity * * Using a single block-ID for several blocks requires using the * metadata for deciding what kind of a TileEntity we need. We * therefore use createTileEntity(World, int) instead of the usual * createNewTileEntity(World). */ @Override public boolean hasTileEntity(int meta) { return meta < AdvancedChestType.values().length; } @Override public TileEntity createTileEntity(World world, int meta) { return new TileEntityAdvancedChest(AdvancedChestType.values()[meta]); } /* * Container */ @Override public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int a, float b, float c, float d) { TileEntity tileEntity = world.getBlockTileEntity(x, y, z); if (tileEntity == null || player.isSneaking()) { return false; } // open GUI 0 player.openGui(AdvancedChests.instance, 0, world, x, y, z); return true; } }
May 4, 201312 yr Author I'm doing that already. The metadata is being resolved to a type and then passed on to the TE constructor. The issue is this: 2013-05-04 09:45:51 [iNFO] [sTDERR] java.lang.InstantiationException: cyclonit.advancedChests.TileEntityAdvancedChest 2013-05-04 09:45:51 [iNFO] [sTDERR] at java.lang.Class.newInstance0(Unknown Source) 2013-05-04 09:45:51 [iNFO] [sTDERR] at java.lang.Class.newInstance(Unknown Source) 2013-05-04 09:45:51 [iNFO] [sTDERR] at net.minecraft.tileentity.TileEntity.createAndLoadEntity(TileEntity.java:137) 2013-05-04 09:45:51 [iNFO] [sTDERR] at net.minecraft.world.chunk.storage.AnvilChunkLoader.readChunkFromNBT(AnvilChunkLoader.java:432) 2013-05-04 09:45:51 [iNFO] [sTDERR] at net.minecraft.world.chunk.storage.AnvilChunkLoader.checkedReadChunkFromNBT(AnvilChunkLoader.java:103) 2013-05-04 09:45:51 [iNFO] [sTDERR] at net.minecraft.world.chunk.storage.AnvilChunkLoader.loadChunk(AnvilChunkLoader.java:83) 2013-05-04 09:45:51 [iNFO] [sTDERR] at net.minecraft.world.gen.ChunkProviderServer.safeLoadChunk(ChunkProviderServer.java:182) 2013-05-04 09:45:51 [iNFO] [sTDERR] at net.minecraft.world.gen.ChunkProviderServer.loadChunk(ChunkProviderServer.java:118) 2013-05-04 09:45:51 [iNFO] [sTDERR] at net.minecraft.server.MinecraftServer.initialWorldChunkLoad(MinecraftServer.java:281) 2013-05-04 09:45:51 [iNFO] [sTDERR] at net.minecraft.server.integrated.IntegratedServer.loadAllWorlds(IntegratedServer.java:88) 2013-05-04 09:45:51 [iNFO] [sTDERR] at net.minecraft.server.integrated.IntegratedServer.startServer(IntegratedServer.java:105) 2013-05-04 09:45:51 [iNFO] [sTDERR] at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:429) 2013-05-04 09:45:51 [iNFO] [sTDERR] at net.minecraft.server.ThreadMinecraftServer.run(ThreadMinecraftServer.java:16) The function TileEntity.createAndLoadEntity calls (TileEntity)oclass.newInstance(); and thus not my constructor but the default one.
May 4, 201312 yr Author What do you mean by that? The problem is, that the default constructor is being called during chunk loading. It does however not know (yet) what kind of a chest it is supposed to create (it would need the block's metadata). Here is my TE: package cyclonit.advancedChests; import cyclonit.advancedChests.core.AdvancedChestType; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; public class TileEntityAdvancedChest extends TileEntity implements IInventory { private AdvancedChestType type; private ItemStack[] inv; private int size; /* * Constructors * * We need the default constructor to bypass the first instantiation * by TileEntity.createAndLoadEntity(). */ public TileEntityAdvancedChest(AdvancedChestType type) { this.type = type; this.size = type.rows * type.columns; this.inv = new ItemStack[this.size]; } public AdvancedChestType getType() { return this.type; } @Override public int getSizeInventory() { return this.size; } @Override public ItemStack getStackInSlot(int i) { return this.inv[i]; } @Override public String getInvName() { return "container." + this.type.realName; } @Override public int getInventoryStackLimit() { return 64; } @Override public ItemStack decrStackSize(int slot, int amount) { if (this.inv[slot] != null) { ItemStack stack; if (this.inv[slot].stackSize <= amount) { stack = this.inv[slot]; this.inv[slot] = null; } else { stack = this.inv[slot].splitStack(amount); } this.onInventoryChanged(); return stack; } else { return null; } } @Override public ItemStack getStackInSlotOnClosing(int i) { // If there is something in the selected slot, drop it. if (this.inv[i] != null) { ItemStack itemstack = this.inv[i]; this.inv[i] = null; return itemstack; } else { return null; } } @Override public void setInventorySlotContents(int i, ItemStack itemStack) { this.inv[i] = itemStack; if (itemStack != null && itemStack.stackSize > this.getInventoryStackLimit()) { itemStack.stackSize = this.getInventoryStackLimit(); } this.onInventoryChanged(); } @Override public boolean isInvNameLocalized() { return false; } @Override public boolean isUseableByPlayer(EntityPlayer entityPlayer) { return this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : entityPlayer.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D; } @Override public boolean isStackValidForSlot(int i, ItemStack itemstack) { return true; } @Override public void openChest() { } @Override public void closeChest() { } /* * NBT */ @Override public void readFromNBT(NBTTagCompound tagCompound) { super.readFromNBT(tagCompound); NBTTagList tagList = tagCompound.getTagList("Inventory"); for (int i = 0; i < tagList.tagCount(); i++) { NBTTagCompound tag = (NBTTagCompound) tagList.tagAt(i); byte slot = tag.getByte("Slot"); if (slot >= 0 && slot < inv.length) { inv[slot] = ItemStack.loadItemStackFromNBT(tag); } } } @Override public void writeToNBT(NBTTagCompound tagCompound) { super.writeToNBT(tagCompound); NBTTagList itemList = new NBTTagList(); for (int i = 0; i < inv.length; i++) { ItemStack stack = inv[i]; if (stack != null) { NBTTagCompound tag = new NBTTagCompound(); tag.setByte("Slot", (byte) i); stack.writeToNBT(tag); itemList.appendTag(tag); } } tagCompound.setTag("Inventory", itemList); } }
May 4, 201312 yr Author Thank you both for your input In readFromNBT accessing the block's metadata directly is impossible because neither this.getBlockMetadata() nor this.getWorldObj() are available. But working around this is fairly straight forward: writeToNBT can access the block's metadata. I thus store the block's metadata alongside the chest's inventory and load it in readFromNBT. Here is my full solution: package cyclonit.advancedChests; import cyclonit.advancedChests.core.AdvancedChestType; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagInt; import net.minecraft.nbt.NBTTagList; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; public class TileEntityAdvancedChest extends TileEntity implements IInventory { private AdvancedChestType type; private ItemStack[] inv; private int size; /* * Constructors * * We need the default constructor to bypass the first instantiation * by TileEntity.createAndLoadEntity(). */ public TileEntityAdvancedChest() { super(); } public TileEntityAdvancedChest(AdvancedChestType type) { this.type = type; this.size = type.rows * type.columns; this.inv = new ItemStack[this.size]; } public AdvancedChestType getType() { return this.type; } @Override public int getSizeInventory() { return this.size; } @Override public ItemStack getStackInSlot(int i) { return this.inv[i]; } @Override public String getInvName() { return "container." + this.type.realName; } @Override public int getInventoryStackLimit() { return 64; } @Override public ItemStack decrStackSize(int slot, int amount) { if (this.inv[slot] != null) { ItemStack stack; if (this.inv[slot].stackSize <= amount) { stack = this.inv[slot]; this.inv[slot] = null; } else { stack = this.inv[slot].splitStack(amount); } this.onInventoryChanged(); return stack; } else { return null; } } @Override public ItemStack getStackInSlotOnClosing(int i) { // If there is something in the selected slot, drop it. if (this.inv[i] != null) { ItemStack itemstack = this.inv[i]; this.inv[i] = null; return itemstack; } else { return null; } } @Override public void setInventorySlotContents(int i, ItemStack itemStack) { this.inv[i] = itemStack; if (itemStack != null && itemStack.stackSize > this.getInventoryStackLimit()) { itemStack.stackSize = this.getInventoryStackLimit(); } this.onInventoryChanged(); } @Override public boolean isInvNameLocalized() { return false; } @Override public boolean isUseableByPlayer(EntityPlayer entityPlayer) { return this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : entityPlayer.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D; } @Override public boolean isStackValidForSlot(int i, ItemStack itemstack) { return true; } @Override public void openChest() { } @Override public void closeChest() { } /* * NBT * * In addition to storing the chest's inventory, we use the NBT * to store the chest's metadata. We can thus always load the * correct type without having to use different classes for every * kind of chest. */ @Override public void readFromNBT(NBTTagCompound tagCompound) { super.readFromNBT(tagCompound); // Load the type int meta = tagCompound.getInteger("Metadata"); this.type = AdvancedChestType.values()[meta]; this.size = this.type.rows * this.type.columns; this.inv = new ItemStack[this.size]; // Load the inventory NBTTagList tagList = tagCompound.getTagList("Inventory"); for (int i = 0; i < tagList.tagCount(); i++) { NBTTagCompound tag = (NBTTagCompound) tagList.tagAt(i); byte slot = tag.getByte("Slot"); if (slot >= 0 && slot < inv.length) { inv[slot] = ItemStack.loadItemStackFromNBT(tag); } } } @Override public void writeToNBT(NBTTagCompound tagCompound) { super.writeToNBT(tagCompound); // Save the inventory NBTTagList itemList = new NBTTagList(); for (int i = 0; i < inv.length; i++) { ItemStack stack = inv[i]; if (stack != null) { NBTTagCompound tag = new NBTTagCompound(); tag.setByte("Slot", (byte) i); stack.writeToNBT(tag); itemList.appendTag(tag); } } tagCompound.setTag("Inventory", itemList); // Save the metadata tagCompound.setInteger("Metadata", this.getBlockMetadata()); } }
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.