Posted March 27, 20169 yr I've been working on a tile entity that makes various items in the game. I've managed to get the server to spawn the itemstacks but they are invisible in the slots until the user grabs them. I think maybe I'm only spawning on the server. Also, I've noticed that various itemstacks seem to be around that have a stack size of 0 if I try to grab from the slot after all of the items are gone. It basically shows a value of 0 in red where a number would be if the stack size is more than 1. What is it I'm doing wrong? Anything helps. Block Class: package com.littlepup.xcom.blocks.engineering_block; import com.littlepup.xcom.XcomMain; import com.littlepup.xcom.packets.PacketsMain; import com.littlepup.xcom.packets.XcomGuiHandler; import com.littlepup.xcom.packets.engineering.EngineerToServerMessage; import net.minecraft.block.BlockContainer; import net.minecraft.block.material.Material; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.BlockPos; import net.minecraft.util.EnumFacing; import net.minecraft.world.World; public class BlockEngineeringPanel extends BlockContainer { public BlockEngineeringPanel(Material materialIn) { super(materialIn); this.setBlockUnbreakable(); this.setCreativeTab(XcomMain.xcomTab); this.setUnlocalizedName("engineering_block"); } @Override public TileEntity createNewTileEntity(World world, int meta) { return new TileEntityEngineerPanel(); } @Override public int getRenderType() { return 3; } @Override public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ) { if(!world.isRemote) { TileEntityEngineerPanel engineerPanel = (TileEntityEngineerPanel) world.getTileEntity(pos); engineerPanel.setAvailibleProjects(); player.openGui(XcomMain.instance, XcomGuiHandler.engineerPanelGui, world, pos.getX(), pos.getY(), pos.getZ()); } return true; } } TileEntity Class: package com.littlepup.xcom.blocks.engineering_block; import java.util.List; import com.google.common.collect.Lists; import com.littlepup.xcom.blocks.research_block.TileEntityResearchPanel; import com.littlepup.xcom.items.ItemAlienMaterials; import com.littlepup.xcom.items.ItemAlienSpecies; import com.littlepup.xcom.items.ItemAlienTrophy; import com.littlepup.xcom.other.Utilities; import com.littlepup.xcom.packets.PacketsMain; import com.littlepup.xcom.packets.xcom_base.BaseInfoMessage; import com.littlepup.xcom.packets.xcom_base.BaseRequestInfoMessage; import com.littlepup.xcom.projects.XcomEngineeringProjects; import com.littlepup.xcom.projects.engineering.EngineeringProject; import com.littlepup.xcom.projects.research.ResearchProject; import com.littlepup.xcom.xcom_base.XcomBase; import com.littlepup.xcom.xcom_base.XcomBaseCollection; import net.minecraft.client.renderer.texture.ITickable; 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.network.NetworkManager; import net.minecraft.network.Packet; import net.minecraft.network.play.server.S35PacketUpdateTileEntity; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.ChatComponentText; import net.minecraft.util.IChatComponent; import net.minecraftforge.fml.common.network.NetworkRegistry.TargetPoint; public class TileEntityEngineerPanel extends TileEntity implements IInventory, ITickable { public TileEntityEngineerPanel() { this.storedMaterials = new ItemStack[this.getSizeInventory()]; } public int associatedBaseID; public XcomBase associatedBase; public static final int inventorySize = 12; public ItemStack[] storedMaterials = new ItemStack[this.inventorySize]; private ItemStack[] usingMaterials = {null, null, null, null, null, null}; boolean isManufacturing = false; EngineeringProject currentProject; List allProjects = XcomEngineeringProjects.getEngineeringProjects(); public List availibleProjects = Lists.newArrayList(); public int currentProjectID = 0; public long timeNeeded; public int amount = 1; public void updateBaseInfo(int baseID) { this.associatedBaseID = baseID; if(worldObj.isRemote) { BaseRequestInfoMessage askInfoMessage = new BaseRequestInfoMessage(this.getPos(), baseID); PacketsMain.xcom.sendToServer(askInfoMessage); } else { XcomBase base = (XcomBase) XcomBaseCollection.getBaseCollection(worldObj).xcom_bases.get(this.associatedBaseID); this.associatedBase = base; BaseInfoMessage message = new BaseInfoMessage(base, this.getPos()); PacketsMain.xcom.sendToAllAround(message, new TargetPoint(this.getWorld().provider.getDimensionId(), this.getPos().getX(), this.getPos().getY(), this.getPos().getZ(), 256.0D)); } this.markDirty(); } @Override public void tick() { if(this.isManufacturing()) { EngineeringProject eProj = this.currentProject; if(eProj.timeCost == 0) { this.endManufacturing(eProj); } else if(this.timeNeeded <= 0) { this.endManufacturing(eProj); } else { this.timeNeeded--; this.markDirty(); } } } public void startManufacturing(EngineeringProject project) { ItemStack[] needed = project.getMaterialsNeeded(); if(needed != null) { for(int l = 0; l < needed.length; l++) { int i1 = ((l + (needed.length * this.amount)) % (needed.length * this.amount)); int used = needed[i1].stackSize * this.amount - this.getStackInSlot(l).stackSize; if(used > 1) { ItemStack stack = new ItemStack(needed[i1].getItem(), used, needed[i1].getMetadata()); this.setInventorySlotContents(l, stack); } else { if(needed[i1].getItem() instanceof ItemAlienMaterials || needed[i1].getItem() instanceof ItemAlienTrophy) { used -= 64; } else { used--; } this.setInventorySlotContents(l, null); } } } if(project.timeCost == 0) { ItemStack[] output = project.getOutput(); if(output != null) { for(int l = 0; l < output.length * this.amount; l++) { int i1 = (l + output.length) % output.length; ItemStack stack; if(output[i1].getHasSubtypes()) { stack = new ItemStack(output[i1].getItem(), 1, output[i1].getMetadata()); } else { stack = new ItemStack(output[i1].getItem(), 1, 0); } this.setInventorySlotContents(l + 6, stack); } } } else { this.currentProject = project; this.timeNeeded = project.timeCost; this.isManufacturing = true; this.markDirty(); } } public void cancelManufacturing(EngineeringProject project) { if(worldObj.isRemote){ System.out.println("The cancelManufacturing method is being called clientSide."); } this.associatedBase.cash += project.getMoneyCost() * this.amount; for(int l = 0; l < 6; l++) { this.setInventorySlotContents(l, this.usingMaterials[l]); } this.isManufacturing = false; this.currentProject = null; this.markDirty(); } public void endManufacturing(EngineeringProject project) { if(worldObj.isRemote) { System.out.println("The endManufacturing method is being called clientside."); } int cashRefund = Utilities.roundDoubles(currentProject.getMoneyCost() * (this.associatedBase.numberWorkshops * 0.10 + this.associatedBase.engineeringAdjBonus)); this.associatedBase.cash += cashRefund; EngineeringProject eProj = this.currentProject; for(int l = 0; l < eProj.getOutput().length * amount; l++) { this.setInventorySlotContents(l + 6, eProj.getOutput()[(eProj.getOutput().length + l) % eProj.getOutput().length]); } this.currentProject = null; this.isManufacturing = false; this.markDirty(); } public int getRoomNeeded(EngineeringProject project) { ItemStack[] stack = project.getOutput(); int slotsNeeded = 0; for(int i2 = 0; i2 < stack.length; i2++) { int maxSize; if(stack[i2].getItem() instanceof ItemAlienMaterials || stack[i2].getItem() instanceof ItemAlienSpecies) { maxSize = 64; } else { maxSize = 1; } slotsNeeded += Math.ceil(amount/maxSize); } return slotsNeeded; } public void setAvailibleProjects() { if(this.worldObj.isRemote || this.associatedBase == null) { this.updateBaseInfo(associatedBaseID); } TileEntityResearchPanel rPanel = (TileEntityResearchPanel) this.worldObj.getTileEntity(associatedBase.rPos); List researched = rPanel.researchedProjects; this.availibleProjects.clear(); for(int i1 = 0; i1 < allProjects.size(); i1++) { ResearchProject needed = ((EngineeringProject) allProjects.get(i1)).getResearchNeeded(); if(needed == null) { this.availibleProjects.add(allProjects.get(i1)); } else { if(researched.contains(needed)) { this.availibleProjects.add(allProjects.get(i1)); } } } } public boolean isManufacturing() { return this.isManufacturing; } @Override public String getName() { return ""; } @Override public boolean hasCustomName() { return false; } @Override public int getSizeInventory() { return this.inventorySize; } @Override public ItemStack getStackInSlot(int index) { ItemStack[] stack = this.storedMaterials; if(index >= stack.length) { index -= 36; } return this.storedMaterials[index]; } @Override public ItemStack decrStackSize(int index, int count) { if(this.getStackInSlot(index) != null) { ItemStack stack; if(this.getStackInSlot(index).stackSize <= count) { stack = this.getStackInSlot(index); this.setInventorySlotContents(index, null); this.markDirty(); return stack; } else { stack = this.getStackInSlot(index).splitStack(count); if(this.getStackInSlot(index).stackSize <= 0) { this.setInventorySlotContents(index, null); } else { this.setInventorySlotContents(index, this.getStackInSlot(index)); } this.markDirty(); return stack; } } else { return null; } } @Override public ItemStack getStackInSlotOnClosing(int index) { ItemStack stack = this.getStackInSlot(index); this.setInventorySlotContents(index, null); return stack; } @Override public void setInventorySlotContents(int index, ItemStack stack) { if(index < 0 || index >= this.getSizeInventory()) { return; } if (stack != null && stack.stackSize > this.getInventoryStackLimit()) stack.stackSize = this.getInventoryStackLimit(); if (stack != null && stack.stackSize == 0) stack = null; this.storedMaterials[index] = stack; this.markDirty(); } @Override public int getInventoryStackLimit() { return 64; } @Override public boolean isUseableByPlayer(EntityPlayer player) { return Utilities.getDistance(player.getPosition(), this.getPos()) < 8.0D; } @Override public void openInventory(EntityPlayer player) { } @Override public void closeInventory(EntityPlayer player) {} @Override public boolean isItemValidForSlot(int index, ItemStack stack) { return true; } @Override public int getField(int id) { return 0; } @Override public void setField(int id, int value) {} @Override public int getFieldCount() { return 0; } @Override public void clear() { for(int i = 0; i < this.getSizeInventory(); i++) { this.setInventorySlotContents(i, null); } } @Override public void writeToNBT(NBTTagCompound nbtParent) { super.writeToNBT(nbtParent); nbtParent.setInteger("baseID", this.associatedBaseID); NBTTagList tag_list = new NBTTagList(); for(int l = 0; l < this.storedMaterials.length; ++l) { if(this.storedMaterials[l] != null) { NBTTagCompound item = new NBTTagCompound(); item.setInteger("slot", l); this.storedMaterials[l].writeToNBT(item); tag_list.appendTag(item); } } nbtParent.setTag("stored", tag_list); for(int l = 0; l < nbtParent.getTagList("stored", 10).tagCount(); l++) { NBTTagList list = nbtParent.getTagList("stored", 10); NBTTagCompound nbt = list.getCompoundTagAt(l); } } @Override public void readFromNBT(NBTTagCompound nbtParent) { super.readFromNBT(nbtParent); this.associatedBaseID = nbtParent.getInteger("baseID"); this.storedMaterials = new ItemStack[this.getSizeInventory()]; NBTTagList nbtList = (NBTTagList) nbtParent.getTag("stored"); if(nbtList != null) { for(int i1 = 0; i1 < nbtList.tagCount(); ++i1) { NBTTagCompound compound = nbtList.getCompoundTagAt(i1); int i2 = compound.getInteger("slot"); if(i2 >= 0 && i2 < this.inventorySize) { this.storedMaterials[i2] = ItemStack.loadItemStackFromNBT(compound); } } } } @Override public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity packet) { this.readFromNBT(packet.getNbtCompound()); } @Override public Packet getDescriptionPacket() { NBTTagCompound nbt = new NBTTagCompound(); this.writeToNBT(nbt); int meta = this.getBlockMetadata(); return new S35PacketUpdateTileEntity(this.getPos(), meta, nbt); } @Override public IChatComponent getDisplayName() { return new ChatComponentText("Engineering"); } } Gui Class: package com.littlepup.xcom.blocks.engineering_block; import java.util.List; import com.littlepup.xcom.packets.PacketsMain; import com.littlepup.xcom.packets.engineering.EngineerToServerMessage; import com.littlepup.xcom.projects.engineering.EngineeringProject; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.client.renderer.GlStateManager; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.MathHelper; import net.minecraft.util.ResourceLocation; import net.minecraft.world.World; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; @SideOnly(Side.CLIENT) public class GuiEngineerPanel extends GuiContainer { public GuiEngineerPanel(World world, EntityPlayer player, TileEntity tileEntity) { super(new ContainerEngineerPanel(world, player, tileEntity)); this.ePanel = (TileEntityEngineerPanel) tileEntity; EngineerToServerMessage updateProjectsMessage = new EngineerToServerMessage((TileEntityEngineerPanel) world.getTileEntity(ePanel.getPos()), -1, true); PacketsMain.xcom.sendToServer(updateProjectsMessage); this.availibleProjects = this.ePanel.availibleProjects; } private final ResourceLocation engineeringGui = new ResourceLocation("xcom:textures/gui/engineer_panel.png"); TileEntityEngineerPanel ePanel; List availibleProjects; @Override public void actionPerformed(GuiButton button) { EngineerToServerMessage message = null; switch(button.id) { case 0: message = new EngineerToServerMessage(this.ePanel, 0, false); break; case 1: message = new EngineerToServerMessage(this.ePanel, 1, false); break; case 2: this.ePanel.amount--; message = new EngineerToServerMessage(this.ePanel, 2, false); break; case 3: this.ePanel.amount++; message = new EngineerToServerMessage(this.ePanel, 3, false); break; case 4: this.ePanel.currentProjectID--; message = new EngineerToServerMessage(this.ePanel, 4, false); break; case 5: this.ePanel.currentProjectID++; message = new EngineerToServerMessage(this.ePanel, 5, false); break; default: message = new EngineerToServerMessage(); break; } PacketsMain.xcom.sendToServer(message); this.inventorySlots.detectAndSendChanges(); } @Override protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) { GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F); this.mc.getTextureManager().bindTexture(engineeringGui); int k = (this.width - this.xSize) / 2; int l = (this.height - this.ySize) / 2; this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize); } private void drawTimeRemaining(long ticks) { if(ticks != 0) { int days = MathHelper.floor_double(ticks / 24000); int min = MathHelper.floor_double((ticks - days * 24000)/1200); int sec = MathHelper.floor_double((ticks - (days * 24000 + min * 1200))/20); this.fontRendererObj.drawString("D: " + days, 130, 20, 80000); this.fontRendererObj.drawString("M: " + min, 130, 30, 80000); this.fontRendererObj.drawString("S: " + sec, 130, 40, 80000); } } @Override public void initGui() { super.initGui(); this.buttonList.add(new GuiButton(0, this.guiLeft + 45, this.guiTop + 15, 36, 12, "BEGIN")); this.buttonList.add(new GuiButton(1, this.guiLeft + 45, this.guiTop + 27, 36, 12, "CANCEL")); this.buttonList.add(new GuiButton(2, this.guiLeft + 45, this.guiTop + 39, 12, 12, "-")); this.buttonList.add(new GuiButton(3, this.guiLeft + 69, this.guiTop + 39, 12, 12, "+")); this.buttonList.add(new GuiButton(4, this.guiLeft + 85, this.guiTop + 58, 9, 12, "<")); this.buttonList.add(new GuiButton(5, this.guiLeft + 118, this.guiTop + 58, 9, 12, ">")); } @Override public void updateScreen() { super.updateScreen(); if(this.ePanel.isManufacturing()) { ((GuiButton)this.buttonList.get(0)).enabled = false; ((GuiButton)this.buttonList.get(1)).enabled = true; this.drawTimeRemaining(this.ePanel.currentProject.timeCost); } else { ((GuiButton)this.buttonList.get(0)).enabled = true; ((GuiButton)this.buttonList.get(1)).enabled = false; if(this.ePanel.currentProjectID == 0) { ((GuiButton)(this.buttonList.get(4))).enabled = false; } if(this.ePanel.currentProjectID == this.availibleProjects.size() - 1) { ((GuiButton)(this.buttonList.get(5))).enabled = false; } if(this.ePanel.currentProjectID != 0 && this.ePanel.currentProjectID != this.availibleProjects.size() - 1) { ((GuiButton)(this.buttonList.get(4))).enabled = true; ((GuiButton)(this.buttonList.get(5))).enabled = true; } if(this.ePanel.amount == 1) { ((GuiButton)(this.buttonList.get(2))).enabled = false; } else if(this.ePanel.getRoomNeeded((EngineeringProject) this.availibleProjects.get(ePanel.currentProjectID)) > 6) { ((GuiButton)(this.buttonList.get(3))).enabled = false; } else { ((GuiButton)(this.buttonList.get(2))).enabled = true; ((GuiButton)(this.buttonList.get(3))).enabled = true; } } } } Container Class: package com.littlepup.xcom.blocks.engineering_block; import com.littlepup.xcom.other.Utilities; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.ICrafting; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; public class ContainerEngineerPanel extends Container { public ContainerEngineerPanel(World world, EntityPlayer player, TileEntity tileEntity) { this.ePanel = (TileEntityEngineerPanel) tileEntity; worldObj = world; usingPlayer = player; playerInv = player.inventory; panelInv = (IInventory) tileEntity; ePanel = (TileEntityEngineerPanel) tileEntity; this.addSlots(); } World worldObj; IInventory playerInv; IInventory panelInv; EntityPlayer usingPlayer; TileEntityEngineerPanel ePanel; @Override public void addCraftingToCrafters(ICrafting crafting) { super.addCraftingToCrafters(crafting); crafting.func_175173_a(this, this.panelInv); } private void addSlots() { /*Slots 0-35 are the player's inventory * Note: since the engineer panel and player's inventory are * seperate, the slots can have the same ID if needed. * Slots 36-41 are the engineer panel's input * Slots 42-49 are the engineer panel's output */ //Player's non-hotbar inventory for(int x = 0; x < 9; ++x) { for(int y = 0; y < 3; ++y) { this.addSlotToContainer(new Slot(playerInv, x * 3 + y, 8 + x * 18, 84 + y * 18)); } } //Player's hotbar inventory for(int x = 0; x < 9; ++x) { this.addSlotToContainer(new Slot(playerInv, x + 27, 8 + x * 18, 142)); } //Engineer panel block's input inventory for(int x = 0; x < 2; ++x) { for(int y = 0; y < 3; ++y) { this.addSlotToContainer(new Slot(panelInv, x * 3 + y + 36, 9 + x * 18, 16 + y * 18)); } } //Engineer panel block's output inventory for(int x = 0; x < 2; ++x) { for(int y = 0; y < 3; ++y) { this.addSlotToContainer(new Slot(panelInv, x * 3 + y + 42, 133 + x * 18, 16 + y * 18)); } } } @Override public boolean canInteractWith(EntityPlayer player) { return Utilities.getDistance(player.getPosition(), ePanel.getPos()) < 8.0D; } @Override public ItemStack transferStackInSlot(EntityPlayer player, int slotID) { ItemStack stack1 = null; Slot slot = this.getSlot(slotID); IInventory inventory = slot.inventory; if(slot != null && slot.getHasStack()) { ItemStack stack2 = slot.getStack(); stack1 = stack2.copy(); //The slot is in the tile entity if(slotID >= 36) { if(!this.mergeItemStack(stack1, 36, 50, false)) { return null; } } else if(!this.mergeItemStack(stack1, 36, 50, false)) { return null; } if(stack1.stackSize == 0) { this.putStackInSlot(slotID, null); } else { slot.onSlotChanged(); } } return stack1; } }
March 27, 20169 yr I think maybe I'm only spawning on the server. Mhm. You should do it on both. Also, I've noticed that various itemstacks seem to be around that have a stack size of 0 if I try to grab from the slot after all of the items are gone. It basically shows a value of 0 in red where a number would be if the stack size is more than 1. What is it I'm doing wrong? Anything helps. Item stacks don't automagically delete themselves when their stack size is zero. Check for it and set them to null. Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
March 27, 20169 yr Author Okay, I'll try to get it to spawn client side. Where should I check to see if the stored itemstacks are null?
March 27, 20169 yr Okay, I'll try to get it to spawn client side. Where should I check to see if the stored itemstacks are null? When you are done calculating the results of the itemstack, check to see if the size < 0. If it is, set the slot contents to null else just set it to the itemstack.
March 27, 20169 yr When you are done calculating the results of the itemstack, check to see if the size < 0. If it is, set the slot contents to null else just set it to the itemstack. *Cough* size <= 0 Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
March 27, 20169 yr Author I was asking for the method used. Like is it the setInventorySlotContents or something else?
March 27, 20169 yr When you are done calculating the results of the itemstack, check to see if the size < 0. If it is, set the slot contents to null else just set it to the itemstack. Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
March 27, 20169 yr Author Okay, I see. I'll be sure to try that. I've managed to get it to spawn on the client side. Thanks for the help.
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.