Posted February 12, 201411 yr So recently i have been working on a block in my mod that when clicked with a book will insert that book inside and you can then remove the book with another click. i have been able to add and remove the book as well as add a renderer for it. The problem is though that when i click the block with more then the max number for the inventory it takes all of them but doesn't give back the extra like it is supposed to. Block Code: package com.jdb.village.blocks; import java.util.Random; import com.jdb.village.VillageTech; import com.jdb.village.items.ItemJournal; import com.jdb.village.tileentity.TileEntityDesk; import net.minecraft.block.BlockContainer; import net.minecraft.block.material.Material; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.texture.IconRegister; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.item.Item; import net.minecraft.item.ItemBook; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.MathHelper; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; public class BlockDesk extends BlockContainer { public BlockDesk(int par1) { super(par1, Material.wood); this.setHardness(2.0f); this.setResistance(5.0f); this.setStepSound(soundWoodFootstep); // TODO Auto-generated constructor stub } @Override public boolean onBlockActivated(World world, int i, int j, int k, EntityPlayer entityPlayer, int face, float hitx, float hity, float hitz) { if(!world.isRemote) { TileEntityDesk tileEntityDesk = (TileEntityDesk) world.getBlockTileEntity(i, j, k); boolean bookAdded = placeBook(entityPlayer, tileEntityDesk); if(bookAdded == true) { return true; } dropSlot(world, i, j, k, 0); return true; } return true; } @Override public void breakBlock(World world, int x, int y, int z, int par5, int par6) { dropItems(world, x, y, z); super.breakBlock(world, x, y, z, par5, par6); } private void dropItems(World world, int x, int y, int z){ Random rand = new Random(); TileEntity tileEntity = world.getBlockTileEntity(x, y, z); if(!(tileEntity instanceof IInventory)) { return; } IInventory inventory = (IInventory) tileEntity; for(int i = 0; x < inventory.getSizeInventory(); x++) { ItemStack item = inventory.getStackInSlot(i); if((item != null) && (item.stackSize > 0)) { float rx = rand.nextFloat() * 0.8F + 0.1F; float ry = rand.nextFloat() * 0.8F + 0.1F; float rz = rand.nextFloat() * 0.8F + 0.1F; EntityItem entityItem = new EntityItem(world, x + rx, y + ry, z + rz, new ItemStack(item.itemID, item.stackSize, item.getItemDamage())); if(item.hasTagCompound()) { entityItem.getEntityItem().setTagCompound((NBTTagCompound)item.getTagCompound().copy()); } float factor = 0.05f; entityItem.motionX = (rand.nextGaussian() * factor); entityItem.motionY = (rand.nextGaussian() * factor + 0.2000000029802322D); entityItem.motionZ = (rand.nextGaussian() * factor); world.spawnEntityInWorld(entityItem); item.stackSize = 0; } } } @Override public TileEntity createNewTileEntity(World world) { return new TileEntityDesk(); } public boolean shouldSideBeRendered(IBlockAccess iblockaccess, int i, int j, int k, int l) { return false; } public boolean isOpaqueCube() { return false; } public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entityLivingBase, ItemStack itemStack) { int dir = MathHelper.floor_double((double)((entityLivingBase.rotationYaw * 4F) / 360F) + 0.5D) & 3; world.setBlockMetadataWithNotify(x, y, z, dir, 0); } @Override public void registerIcons(IconRegister iconRegister) { blockIcon = iconRegister.registerIcon("villagetech:journal"); } public void dropSlot(World world, int i, int j, int k, int slot) { TileEntity tileEntity = world.getBlockTileEntity(i, j, k); if (!(tileEntity instanceof IInventory)) { return; } IInventory inventory = (IInventory)tileEntity; ItemStack stuff = inventory.getStackInSlot(slot); if ((stuff != null) && (stuff.stackSize > 0)) { EntityItem entityItem = new EntityItem(world, i + 0.5F, j + 1.2F, k + 0.5F, new ItemStack(stuff.itemID, stuff.stackSize, stuff.getItemDamage())); if (stuff.hasTagCompound()) { entityItem.getEntityItem().setTagCompound((NBTTagCompound)stuff.getTagCompound().copy()); } entityItem.motionX = 0.5D; entityItem.motionY = 0.5D; entityItem.motionZ = 0.5D; world.spawnEntityInWorld(entityItem); stuff.stackSize = 0; inventory.setInventorySlotContents(slot, null); world.markBlockForUpdate(i, j, k); } } public boolean placeBook(EntityPlayer entityPlayer, TileEntityDesk tileEntityDesk) { ItemStack book = entityPlayer.getHeldItem(); if(book != null) { Item bookTest = book.getItem(); if(bookTest instanceof ItemBook) { int bookReturn = tileEntityDesk.addBook(book); if(bookReturn == 0) { entityPlayer.inventory.mainInventory[entityPlayer.inventory.currentItem] = null; } if(bookReturn > 0) { entityPlayer.inventory.mainInventory[entityPlayer.inventory.currentItem] = book; return true; } } } return false; } } TileEntity Code: package com.jdb.village.tileentity; import com.jdb.village.items.ItemJournal; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.ISidedInventory; import net.minecraft.item.Item; import net.minecraft.item.ItemBook; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.network.INetworkManager; import net.minecraft.network.packet.Packet; import net.minecraft.network.packet.Packet132TileEntityData; import net.minecraft.tileentity.TileEntity; public class TileEntityDesk extends TileEntity implements ISidedInventory { public ItemStack[] deskInventory; public TileEntityDesk() { deskInventory = new ItemStack[1]; } public Packet getDescriptionPacket() { NBTTagCompound nbtTag = new NBTTagCompound(); NBTTagList tagList = new NBTTagList(); for(int i = 0; i < deskInventory.length; i ++) { ItemStack itemStack = deskInventory[i]; if(itemStack != null) { NBTTagCompound tag = new NBTTagCompound(); tag.setByte("Slot", (byte) i); itemStack.writeToNBT(tag); tagList.appendTag(tag); } } nbtTag.setTag("deskInventory", tagList); return new Packet132TileEntityData(this.xCoord, this.yCoord, this.zCoord, 1, nbtTag); } public void onDataPacket(INetworkManager net, Packet132TileEntityData packet) { NBTTagCompound nbtTag = packet.data; NBTTagList tagList = nbtTag.getTagList("deskInventory"); deskInventory = new ItemStack[getSizeInventory()]; for (int i = 0; i < tagList.tagCount(); i++) { NBTTagCompound tag = (NBTTagCompound)tagList.tagAt(i); byte slot = tag.getByte("Slot"); if ((slot >= 0) && (slot < this.deskInventory.length)) { this.deskInventory[slot] = ItemStack.loadItemStackFromNBT(tag); } } } @Override public void writeToNBT(NBTTagCompound nbt) { super.writeToNBT(nbt); NBTTagList tagList = new NBTTagList(); for(int i = 0; i < deskInventory.length; i++) { ItemStack stack = deskInventory[i]; if(stack != null) { NBTTagCompound tag = new NBTTagCompound(); tag.setByte("Slot", (byte) i); stack.writeToNBT(tag); tagList.appendTag(tag); } } } @Override public void readFromNBT(NBTTagCompound nbt) { super.readFromNBT(nbt); NBTTagList tagList = nbt.getTagList("deskInventory"); deskInventory = new ItemStack[getSizeInventory()]; for(int i = 0; i < tagList.tagCount(); i++) { NBTTagCompound tag = (NBTTagCompound) tagList.tagAt(i); byte slot = tag.getByte("Slot"); if((slot >= 0) && (slot < deskInventory.length)) { deskInventory[slot] = ItemStack.loadItemStackFromNBT(tag); } } } public boolean hasBook() { ItemStack stack = getStackInSlot(0); if(stack != null) { return true; } return false; } public int addBook(ItemStack book) { if(book != null) { ItemStack deskBook = getStackInSlot(0); if(deskBook == null) { setInventorySlotContents(0, book); return 0; } int booksize = book.stackSize; int totalsize = booksize + deskBook.stackSize; if(totalsize < getInventoryStackLimit() + 1) { deskBook.stackSize = totalsize; worldObj.markBlockForUpdate(xCoord, yCoord, zCoord); return 0; } if(totalsize > getInventoryStackLimit()) { deskBook.stackSize = getInventoryStackLimit(); worldObj.markBlockForUpdate(xCoord, yCoord, zCoord); return totalsize - getInventoryStackLimit(); } } else { setInventorySlotContents(0, book); return 0; } return -1; } @Override public int getSizeInventory() { return deskInventory.length; } @Override public ItemStack getStackInSlot(int slot) { return deskInventory[slot]; } @Override public ItemStack decrStackSize(int slot, int amount) { ItemStack itemStack = getStackInSlot(slot); if(itemStack != null) { if(itemStack.stackSize <= amount) { setInventorySlotContents(slot, null); } else { itemStack = itemStack.splitStack(amount); if(itemStack.stackSize == 0) { setInventorySlotContents(slot, null); } } } return itemStack; } @Override public ItemStack getStackInSlotOnClosing(int slot) { ItemStack itemStack = getStackInSlot(slot); if(itemStack != null) { setInventorySlotContents(slot, null); } return itemStack; } @Override public void setInventorySlotContents(int slot, ItemStack itemStack) { deskInventory[slot] = itemStack; if((itemStack != null) && (itemStack.stackSize > getInventoryStackLimit())) { itemStack.stackSize = getInventoryStackLimit(); } worldObj.markBlockForUpdate(xCoord, yCoord, zCoord); } @Override public String getInvName() { return "Desk"; } @Override public boolean isInvNameLocalized() { return false; } @Override public int getInventoryStackLimit() { return 1; } @Override public boolean isUseableByPlayer(EntityPlayer entityPlayer) { return (worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) == this) && (entityPlayer.getDistanceSq(xCoord + 0.5D, yCoord + 0.5D, zCoord + 0.5D) < 64D); } @Override public void openChest() { } @Override public void closeChest() { } @Override public boolean isItemValidForSlot(int slot, ItemStack itemstack) { Item stackitem = itemstack.getItem(); if (stackitem != null) { if (slot == 0) { if ((stackitem instanceof ItemBook || stackitem instanceof ItemJournal)) { return true; } } } return false; } @Override public int[] getAccessibleSlotsFromSide(int side) { if(side == 0) { int[] sides = new int[1]; sides[0] = 3; return sides; } if(side != 1) { int[] sides = new int[3]; sides[0] = 0; sides[1] = 1; sides[2] = 2; return sides; } return null; } @Override public boolean canInsertItem(int slot, ItemStack itemstack, int side) { if (itemstack != null) { Item stackItem = itemstack.getItem(); if (stackItem instanceof ItemBook || stackItem instanceof ItemJournal && slot == 0) { return true; } } return false; } @Override public boolean canExtractItem(int slot, ItemStack itemstack, int side) { return true; } } P.S. I know i shouldn't use Language Registry it is just i haven't got around to making a .lang file which has mostly been me being lazy. BaseMod Code(Trimmed): package com.jdb.village; import com.jdb.village.blocks.BlockDesk; import com.jdb.village.handlers.PacketHandler; import com.jdb.village.handlers.VTTickHandler; import com.jdb.village.items.ItemJournal; import com.jdb.village.proxies.CommonProxy; import com.jdb.village.renders.DeskRenderer; import com.jdb.village.tileentity.TileEntityDesk; import net.minecraft.block.Block; import net.minecraft.item.Item; import net.minecraftforge.common.Configuration; import cpw.mods.fml.client.registry.ClientRegistry; import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.Mod.EventHandler; import cpw.mods.fml.common.Mod.Instance; import cpw.mods.fml.common.SidedProxy; import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.event.FMLPostInitializationEvent; import cpw.mods.fml.common.event.FMLPreInitializationEvent; import cpw.mods.fml.common.network.NetworkMod; import cpw.mods.fml.common.registry.GameRegistry; import cpw.mods.fml.common.registry.LanguageRegistry; import cpw.mods.fml.common.registry.TickRegistry; import cpw.mods.fml.relauncher.Side; @Mod(modid = VillageTech.modid, name = VillageTech.name, version = VillageTech.version) @NetworkMod(clientSideRequired = true, serverSideRequired = false, channels = ("villageTech"), packetHandler = PacketHandler.class) public class VillageTech { @Instance(VillageTech.modid) public static VillageTech instance; @SidedProxy(clientSide = "com.jdb.village.proxies.ClientProxy", serverSide = "com.jdb.village.proxies.CommonProxy") public static CommonProxy proxy; public static final String modid = "villagetech"; public static final String name = "Village Tech"; public static final String version = "Indev 0.1"; //Blocks public static Block desk; //Items public static Item journal; //Block ids public static int deskId; //Item ids public static int journalId; @EventHandler public void init(FMLInitializationEvent event) { //Blocks LanguageRegistry.addName(desk, "Desk"); GameRegistry.registerBlock(desk, modid); GameRegistry.registerTileEntity(TileEntityDesk.class, "desk"); //Items LanguageRegistry.addName(journal, "Journal"); GameRegistry.registerItem(journal, "Journal", VillageTech.modid); //Registries ClientRegistry.bindTileEntitySpecialRenderer(TileEntityDesk.class, new DeskRenderer()); } @EventHandler public void preInit(FMLPreInitializationEvent event) { Configuration config = new Configuration(event.getSuggestedConfigurationFile()); config.load(); //Ids deskId = config.getBlock("Desk", 675).getInt(); journalId = config.getItem("Journal", 4500).getInt(); config.save(); desk = new BlockDesk(deskId); journal = new ItemJournal(journalId); } @EventHandler public void postInit(FMLPostInitializationEvent event) { TickRegistry.registerTickHandler(new VTTickHandler(), Side.CLIENT); } Creator of Jobo's ModLoader If I helped you could you please click the thank you button and applaud my karma.
February 12, 201411 yr 1st: change topic name, your mod is not crashing 2nd: tell me if i got it right, you want to when a player insert a book on the block, but the block is already full, you want the block to drop all items + the overflowed item that the player placed in?
February 12, 201411 yr Author 1. Ok 2. It does that already the problem is that if I say have 33 or whatever books in my hand then click the block well it is empty it removes all 33 instead of just 1 and then when I reclick it to remove them it only ejects 1 so it is not returning the excess books to the player. Creator of Jobo's ModLoader If I helped you could you please click the thank you button and applaud my karma.
February 12, 201411 yr public boolean placeBook(EntityPlayer entityPlayer, TileEntityDesk tileEntityDesk) { ItemStack book = entityPlayer.getHeldItem(); if(book != null) { Item bookTest = book.getItem(); if(bookTest instanceof ItemBook) { int bookReturn = tileEntityDesk.addBook(book); if(bookReturn == 0) { entityPlayer.inventory.mainInventory[entityPlayer.inventory.currentItem] = null; } if(bookReturn > 0) { entityPlayer.inventory.mainInventory[entityPlayer.inventory.currentItem] = book; return true; } } } return false; } TO public boolean placeBook(EntityPlayer entityPlayer, TileEntityDesk tileEntityDesk) { ItemStack book = entityPlayer.getHeldItem(); int stackSZ =0; if(book != null) { stackSZ = book.stackSize; Item bookTest = book.getItem(); if(bookTest instanceof ItemBook) { int bookReturn = tileEntityDesk.addBook(book); if(bookReturn == 0) { entityPlayer.inventory.mainInventory[entityPlayer.inventory.currentItem] = null; } if(bookReturn > 0) { book.stackSize = stackSZ-1; entityPlayer.inventory.mainInventory[entityPlayer.inventory.currentItem] = book; return true; } } } return false; }
February 12, 201411 yr Author I'll try this as soon as I can that makes sense even though I thought that ItemStacks always stored there stacksize so I assume you could go book.stackSize--; instead of making a new variable for the stackSize. Anyways thanks for the help i'll see if this works. Creator of Jobo's ModLoader If I helped you could you please click the thank you button and applaud my karma.
February 12, 201411 yr yeah and looking now, i think you will need to set the stackSize after putting back on the player inventory.if my code dosent work try this way.
February 12, 201411 yr Author Ok I think that if what you had doesn't work I may have discovered that on my add book method when I check if there are any books currently in the inventory it says no and then sets a book inside aswell as returning 0 so then my place book see that it is returning 0 so it removes the stack. I had it like this because it used to be able to hold 64 books but changed it to hold 1 because I felt 64 unnecessary. However when it was 64 and it had no books inside it, it knew it could hold as many where in the players hand so it didn't need to check how many where in his hand and just simply would always return 0 since the player couldn't hold more than 64. Creator of Jobo's ModLoader If I helped you could you please click the thank you button and applaud my karma.
February 13, 201411 yr Author working code: Block: public boolean placeBook(EntityPlayer entityPlayer, TileEntityDesk tileEntityDesk) { ItemStack book = entityPlayer.getHeldItem(); if(book != null) { Item bookTest = book.getItem(); if(bookTest instanceof ItemBook) { int bookReturn = tileEntityDesk.addBook(book); if(bookReturn == 0) { entityPlayer.inventory.mainInventory[entityPlayer.inventory.currentItem] = null; } if(bookReturn > 0) { book.stackSize = bookReturn; entityPlayer.inventory.mainInventory[entityPlayer.inventory.currentItem] = book; return true; } } } TileEntity: public int addBook(ItemStack book) { if(book != null) { ItemStack deskBook = getStackInSlot(0); if(deskBook == null && book.stackSize == getInventoryStackLimit()) { setInventorySlotContents(0, book); return 0; } if(deskBook == null && book.stackSize > getInventoryStackLimit()) { int bookSize = book.stackSize; book.stackSize -= getInventoryStackLimit(); setInventorySlotContents(0,book); book.stackSize = bookSize; return book.stackSize - getInventoryStackLimit(); } } else { setInventorySlotContents(0, book); return 0; } return -1; } Creator of Jobo's ModLoader If I helped you could you please click the thank you button and applaud my karma.
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.