NikolaTheProgrammerNoob Posted November 11, 2015 Posted November 11, 2015 Hello, fellow forum addicts ! Unfortunately, this is my 3rd post that is related to Custom Craftin Tables... My problem is that I can't get my Custom Crafting Table to open the f-ing GUI ! I've tryed EVERYTHING ! Copying Minecraft code. Looking other peoples code. Looking at code from other mods. I've even started learning Java and learnt all the basics and know how to read the code. I am still learning it. But I can't find a way for this f-ing GUI to work ! So I turn to you guys again, and please don't say something like:"Go learn Java !" or "Stop posting this! It's stupid !" Cause I am learning Java and there is no other way of me getting to know how to fix this. So please just tell me what to edit in the code and I will thank you for your kindness and sensibility. Here is the code for all of you wanting to help me: I will not be pasting the imports and the packageds just so I save up space ! The Block Class: public class IronForge extends Block { private static final String __OBFID = "CL_00000221"; private BlockPos blockPos; public IronForge(Material materialIn) { super(materialIn); this.setHardness(3.0F); this.setResistance(15.0F); this.setStepSound(soundTypeAnvil); this.setBlockBounds(0, 0, 0, 1, 0.85F, 1); } public boolean onBlockActivated(World worldIn, int x, int y, int z, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ) { if (worldIn.isRemote && playerIn.isSneaking()) { return true; } else { playerIn.openGui(Reference.MOD_ID, CarbonBlocks.guiIDIronForge, worldIn, x, y, z); return true; } } @Override public boolean isOpaqueCube() { return false; } public static class InterfaceCraftingTable implements IInteractionObject { private final World world; private final BlockPos position; private static final String __OBFID = "CL_00002127"; public InterfaceCraftingTable(World worldIn, BlockPos pos) { this.world = worldIn; this.position = pos; } public String getName() { return null; } public boolean hasCustomName() { return false; } public IChatComponent getDisplayName() { return new ChatComponentTranslation(CarbonBlocks.ironForge.getUnlocalizedName() + ".name", new Object[0]); } public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn) { return new ContainerIronForge(playerInventory, this.world, this.position); } public String getGuiID() { return "carbonmod:ironForge"; } } } The GUI for The Block Class: @SideOnly(Side.CLIENT) public class GuiIronForge extends GuiContainer { private static final ResourceLocation ironForgeGuiTextures = new ResourceLocation("textures/gui/container/ironForge.png"); private static final String __OBFID = "CL_00000750"; public GuiIronForge(InventoryPlayer playerInv, World worldIn) { this(playerInv, worldIn, BlockPos.ORIGIN); } public GuiIronForge(InventoryPlayer playerInv, World worldIn, BlockPos blockPosition) { super(new ContainerIronForge(playerInv, worldIn, blockPosition)); } protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) { this.fontRendererObj.drawString(I18n.format("container.crafting", new Object[0]), 28, 6, 4210752); this.fontRendererObj.drawString(I18n.format("container.inventory", new Object[0]), 8, this.ySize - 96 + 2, 4210752); } protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) { GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F); this.mc.getTextureManager().bindTexture(ironForgeGuiTextures); int k = (this.width - this.xSize) / 2; int l = (this.height - this.ySize) / 2; this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize); } } The Block Container Class: public class ContainerIronForge extends Container { public InventoryCrafting craftMatrix = new InventoryCrafting(this, 3, 3); public IInventory craftResult = new InventoryCraftResult();; private World worldObj; private BlockPos field_178145_h; private static final String __OBFID = "CL_00001744"; public ContainerIronForge(InventoryPlayer invPlayer, World worldIn, BlockPos p_i45800_3) { this.worldObj = worldIn; this.field_178145_h = p_i45800_3; this.addSlotToContainer(new SlotCrafting(invPlayer.player, this.craftMatrix, this.craftResult, 0, 124, 35)); int i; int j; for (i = 0; i < 3; ++i) { for (j = 0; j < 3; ++j) { this.addSlotToContainer(new Slot(this.craftMatrix, j + i * 3, 30 + j * 18, 17 + i * 18)); } } for (i = 0; i < 3; ++i) { for (j = 0; j < 9; ++j) { this.addSlotToContainer(new Slot(invPlayer, j + i * 9 + 9, 8 + j * 18, 84 + i * 18)); } } for (i = 0; i < 9; ++i) { this.addSlotToContainer(new Slot(invPlayer, i, 8 + i * 18, 142)); } this.onCraftMatrixChanged(this.craftMatrix); } public ContainerIronForge(InventoryPlayer playerInv, World worldIn, int x, int y, int z) { } public void onCraftMatrixChanged(IInventory inventoryIn) { //this.craftResult.setInventorySlotContents(0, IronForgeCraftingManager.getInstance().findMatchingRecipe(this.craftMatrix, this.worldObj)); } public void onContainerClosed(EntityPlayer playerIn) { super.onContainerClosed(playerIn); if (!this.worldObj.isRemote) { for (int i = 0; i < 9; ++i) { ItemStack itemstack = this.craftMatrix.getStackInSlotOnClosing(i); if (itemstack != null) { playerIn.dropPlayerItemWithRandomChoice(itemstack, false); } } } } @Override public boolean canInteractWith(EntityPlayer playerIn) { return this.worldObj.getBlockState(this.field_178145_h).getBlock() != CarbonBlocks.ironForge ? false : playerIn.getDistanceSq((double)this.field_178145_h.getX() + 0.5D, (double)this.field_178145_h.getY() + 0.5D, (double)this.field_178145_h.getZ() + 0.5D) <= 64.0D; } public ItemStack transferStackInSlot(EntityPlayer playerIn, int index) { ItemStack itemstack = null; Slot slot = (Slot) this.inventorySlots.get(index); if (slot != null && slot.getHasStack()) { ItemStack itemstack1 = slot.getStack(); itemstack = itemstack1.copy(); if (index == 0) { if (!this.mergeItemStack(itemstack1, 10, 46, true)) { return null; } slot.onSlotChange(itemstack1, itemstack); } else if (index >= 10 && index < 37) { if (!this.mergeItemStack(itemstack1, 37, 46, false)) { return null; } } else if (index >= 37 && index < 46) { if (!this.mergeItemStack(itemstack1, 10, 37, false)) { return null; } } else if (!this.mergeItemStack(itemstack1, 10, 46, false)) { return null; } if (itemstack1.stackSize == 0) { slot.putStack((ItemStack) null); } else { slot.onSlotChanged(); } if (itemstack1.stackSize == itemstack.stackSize) { return null; } slot.onPickupFromSlot(playerIn, itemstack1); } return itemstack; } @Override public boolean canMergeSlot(ItemStack p_94530_1_, Slot p_94530_2_) { return p_94530_2_.inventory != this.craftResult && super.canMergeSlot(p_94530_1_, p_94530_2_); } } And The GUI Handler Class: public class GuiHandler implements IGuiHandler { @Override public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { TileEntity entity = world.getTileEntity(new BlockPos(x, y, z)); if(ID == CarbonBlocks.guiIDIronForge) { return ID == CarbonBlocks.guiIDIronForge && world.getBlockState(new BlockPos(x, y, z)) == CarbonBlocks.ironForge ? new ContainerIronForge(player.inventory, world, x, y, z) : null; } return entity; } @Override public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { TileEntity entity = world.getTileEntity(new BlockPos(x, y, z)); if(ID == CarbonBlocks.guiIDIronForge) { return ID == CarbonBlocks.guiIDIronForge && world.getBlockState(new BlockPos(x, y, z)) == CarbonBlocks.ironForge ? new GuiIronForge(player.inventory, world/*, x, y, z*/) : null; } return entity; } } And I say this again, please don't post HATE replays because I am really, really.... really stuck. To Craft, or not to Craft. That is the question.
NikolaTheProgrammerNoob Posted November 11, 2015 Author Posted November 11, 2015 Thank you so much man !!!! To Craft, or not to Craft. That is the question.
NikolaTheProgrammerNoob Posted November 11, 2015 Author Posted November 11, 2015 Now the GUI opens but I can't move my items When I click an item to drag it, it just puts it self back and when I use middle mouse on it I get the full stack and when I try to craft with the full stacks and click on the item that gets crafted it disapears and the items in the crafting grid all jump to a red 0 ! Please help To Craft, or not to Craft. That is the question.
Cerandior Posted November 11, 2015 Posted November 11, 2015 This is the part of code that Vanilla uses to handle the slots things. Put this on your Container: public ItemStack transferStackInSlot(EntityPlayer entityplayer, int q) { ItemStack itemstack = null; Slot slot = (Slot)this.inventorySlots.get(q); if (slot != null && slot.getHasStack()) { ItemStack itemstack1 = slot.getStack(); itemstack = itemstack1.copy(); if (q == 0) { if (!this.mergeItemStack(itemstack1, 10, 46, true)) { return null; } slot.onSlotChange(itemstack1, itemstack); } else if (q >= 10 && q < 37) { if (!this.mergeItemStack(itemstack1, 37, 46, false)) { return null; } } else if (q >= 37 && q < 46) { if (!this.mergeItemStack(itemstack1, 10, 37, false)) { return null; } } else if (!this.mergeItemStack(itemstack1, 10, 46, false)) { return null; } if (itemstack1.stackSize == 0) { slot.putStack((ItemStack)null); } else { slot.onSlotChanged(); } if (itemstack1.stackSize == itemstack.stackSize) { return null; } slot.onPickupFromSlot(entityplayer, itemstack1); } return itemstack; } }
NikolaTheProgrammerNoob Posted November 11, 2015 Author Posted November 11, 2015 Didn't work To Craft, or not to Craft. That is the question.
Cerandior Posted November 11, 2015 Posted November 11, 2015 Didn't work My bad, it seems you already had this part on your code. No idea what i was thinking.
NikolaTheProgrammerNoob Posted November 12, 2015 Author Posted November 12, 2015 It turned out that it wasn't "calling" the InterfaceCraftingTable method so I put it in. Now when I try to craft in it and take the crafted item the ingredients turn into a red zero and if i shift+right click on the thing I want to craft the game crashes To Craft, or not to Craft. That is the question.
Cerandior Posted November 12, 2015 Posted November 12, 2015 Post your recipe class. Show the code of the class you use to add recipes to your custom crafting table.
NikolaTheProgrammerNoob Posted November 12, 2015 Author Posted November 12, 2015 Here is my recipes class. package com.nikola.carbonmod.crafting; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.nikola.carbonmod.init.CarbonBlocks; import com.nikola.carbonmod.init.CarbonItems; import net.minecraft.block.Block; import net.minecraft.init.Items; import net.minecraft.inventory.InventoryCrafting; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.IRecipe; import net.minecraft.world.World; public class CraftingManager { private static final CraftingManager instance = new CraftingManager(); private List recipes = Lists.newArrayList(); private static final String __OBFID = "CL_00000090"; public static CraftingManager getInstance() { return instance; } private CraftingManager() { recipes = new ArrayList(); this.addRecipe(new ItemStack(CarbonBlocks.carbonBlock, 1), new Object[] {"AAA", "AAA", "AAA", 'A', CarbonItems.carbon}); this.addRecipe(new ItemStack(CarbonBlocks.steelBlock, 1), new Object[] {"AAA", "AAA", "AAA", 'A', CarbonItems.steelIngot}); this.addRecipe(new ItemStack(CarbonBlocks.siliconeBlock, 1), new Object[] {"AAA", "AAA", "AAA", 'A', CarbonItems.siliconeIngot}); this.addRecipe(new ItemStack(CarbonItems.steelIngot, 1), new Object[] {"ABA", 'A', CarbonItems.carbon, 'B', Items.iron_ingot }); this.addRecipe(new ItemStack(CarbonItems.steelRod, 1), new Object[] {"A", "A", 'A', CarbonItems.steelIngot }); this.addRecipe(new ItemStack(CarbonItems.steelSword, 1), new Object[] {" A ", " A ", " B ", 'A', CarbonItems.steelIngot, 'B', Items.stick }); this.addRecipe(new ItemStack(CarbonItems.steelPickaxe, 1), new Object[] {"AAA", " B ", " B ",'A', CarbonItems.steelIngot, 'B', Items.stick }); this.addRecipe(new ItemStack(CarbonItems.steelAxe, 1), new Object[] {"AAA", "AB ", " B ",'A', CarbonItems.steelIngot, 'B', Items.stick }); this.addRecipe(new ItemStack(CarbonItems.steelAxe, 1), new Object[] {"AAA", " BA", " B ", 'A', CarbonItems.steelIngot, 'B', Items.stick }); this.addRecipe(new ItemStack(CarbonItems.steelShovel, 1), new Object[] {" A ", " B ", " B ", 'A', CarbonItems.steelIngot, 'B', Items.stick }); this.addRecipe(new ItemStack(CarbonItems.steelHelmet, 1), new Object[] {"AAA", "A A", 'A', CarbonItems.steelIngot }); this.addRecipe(new ItemStack(CarbonItems.steelChestplate, 1), new Object[] {"A A", "AAA", "AAA",'A', CarbonItems.steelIngot }); this.addRecipe(new ItemStack(CarbonItems.steelLeggings, 1), new Object[] {"AAA", "A A", "A A", 'A', CarbonItems.steelIngot }); this.addRecipe(new ItemStack(CarbonItems.steelBoots, 1), new Object[] {"A A", "A A", 'A', CarbonItems.steelIngot }); Collections.sort(this.recipes, new IronForgeRecipeSorter(this)); } public IronForgeShapedRecipes addRecipe(ItemStack stack, Object ... recipeComponents) { String s = ""; int i = 0; int j = 0; int k = 0; if (recipeComponents[i] instanceof String[]) { String[] astring = (String[])((String[])recipeComponents[i++]); for (int l = 0; l < astring.length; ++l) { String s1 = astring[l]; ++k; j = s1.length(); s = s + s1; } } else { while (recipeComponents[i] instanceof String) { String s2 = (String)recipeComponents[i++]; ++k; j = s2.length(); s = s + s2; } } HashMap hashmap; for (hashmap = Maps.newHashMap(); i < recipeComponents.length; i += 2) { Character character = (Character)recipeComponents[i]; ItemStack itemstack1 = null; if (recipeComponents[i + 1] instanceof Item) { itemstack1 = new ItemStack((Item)recipeComponents[i + 1]); } else if (recipeComponents[i + 1] instanceof Block) { itemstack1 = new ItemStack((Block)recipeComponents[i + 1], 1, 32767); } else if (recipeComponents[i + 1] instanceof ItemStack) { itemstack1 = (ItemStack)recipeComponents[i + 1]; } hashmap.put(character, itemstack1); } ItemStack[] aitemstack = new ItemStack[j * k]; for (int i1 = 0; i1 < j * k; ++i1) { char c0 = s.charAt(i1); if (hashmap.containsKey(Character.valueOf(c0))) { aitemstack[i1] = ((ItemStack)hashmap.get(Character.valueOf(c0))).copy(); } else { aitemstack[i1] = null; } } IronForgeShapedRecipes shapedrecipes = new IronForgeShapedRecipes(j, k, aitemstack, stack); this.recipes.add(shapedrecipes); return shapedrecipes; } public void addShapelessRecipe(ItemStack stack, Object ... recipeComponents) { ArrayList arraylist = Lists.newArrayList(); Object[] aobject = recipeComponents; int i = recipeComponents.length; for (int j = 0; j < i; ++j) { Object object1 = aobject[j]; if (object1 instanceof ItemStack) { arraylist.add(((ItemStack)object1).copy()); } else if (object1 instanceof Item) { arraylist.add(new ItemStack((Item)object1)); } else { if (!(object1 instanceof Block)) { throw new IllegalArgumentException("Invalid shapeless recipe: unknown type " + object1.getClass().getName() + "!"); } arraylist.add(new ItemStack((Block)object1)); } } this.recipes.add(new IronForgeShapelessRecipes(stack, arraylist)); } public void addRecipe(IRecipe recipe) { this.recipes.add(recipe); } public ItemStack findMatchingRecipe(InventoryCrafting p_82787_1_, World worldIn) { Iterator iterator = this.recipes.iterator(); IRecipe irecipe; do { if (!iterator.hasNext()) { return null; } irecipe = (IRecipe)iterator.next(); } while (!irecipe.matches(p_82787_1_, worldIn)); return irecipe.getCraftingResult(p_82787_1_); } public ItemStack[] func_180303_b(InventoryCrafting p_180303_1_, World worldIn) { Iterator iterator = this.recipes.iterator(); while (iterator.hasNext()) { IRecipe irecipe = (IRecipe)iterator.next(); if (irecipe.matches(p_180303_1_, worldIn)) { return irecipe.getRemainingItems(p_180303_1_); } } ItemStack[] aitemstack = new ItemStack[p_180303_1_.getSizeInventory()]; for (int i = 0; i < aitemstack.length; ++i) { aitemstack[i] = p_180303_1_.getStackInSlot(i); } return aitemstack; } public List getRecipeList() { return this.recipes; } } I know you haven't said what the problem is yet, but thanks anyway To Craft, or not to Craft. That is the question.
Earthcomputer Posted November 12, 2015 Posted November 12, 2015 You made a mistake in the onBlockActivated method of your block class: openGui should only be called on the server. Strange, but true I don't think it's possible for onBlockActivated to be called if the player is sneaking catch(Exception e) { } Yay, Pokémon exception handling, gotta catch 'em all (and then do nothing with 'em).
NikolaTheProgrammerNoob Posted November 12, 2015 Author Posted November 12, 2015 Yeah, umm.... how do I make it only get called on the server? To Craft, or not to Craft. That is the question.
Cerandior Posted November 12, 2015 Posted November 12, 2015 You made a mistake in the onBlockActivated method of your block class: openGui should only be called on the server. Strange, but true I don't think it's possible for onBlockActivated to be called if the player is sneaking You are wrong on both i guess. How would gui be opened on server? Then every player on the server would see the gui that one player opened. That would crash the server. And yes GUI can be opened while sneaking. It has nothing to do with sneaking as a process anyway. It is a way to set-up the opening of the gui so the player can right click the block to set other blocks next to it. But Nikola you have done a mistake. If you were to run this mod on a server, if a player would try to open the gui the server would crash. When you put this on your block class: public boolean onBlockActivated(World worldIn, int x, int y, int z, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ) { if (worldIn.isRemote && playerIn.isSneaking()) { return true; } else { Change it to: public boolean onBlockActivated(World worldIn, int x, int y, int z, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ) { if (!worldIn.isRemote && playerIn.isSneaking()) { return true; } else { If you can't find the difference.... On the if statement change from if(worldIn.isRemote && ...) to if(!worldIn.isRemote && ...) Basically add a " ! " before worldIn.
NikolaTheProgrammerNoob Posted November 12, 2015 Author Posted November 12, 2015 Okay, but what about the items returning a stack value of 0. Oh yeah, the original stacks actually return to the player inventroy when the item is crafted To Craft, or not to Craft. That is the question.
Earthcomputer Posted November 12, 2015 Posted November 12, 2015 Cerandior, yes, you're right. I meant open the *container* server side catch(Exception e) { } Yay, Pokémon exception handling, gotta catch 'em all (and then do nothing with 'em).
NikolaTheProgrammerNoob Posted November 12, 2015 Author Posted November 12, 2015 Still need help To Craft, or not to Craft. That is the question.
Cerandior Posted November 12, 2015 Posted November 12, 2015 So you don't get a crash anymore? I think i know what you mean. The items have like a red gradient on them and have a 0 on the bottom-right? That's a weird glitch. I have seen it on vanilla being done on an item duplication glitch that is 1.8 but i am not sure why that happens hence i don't know what is causing the problem on your block.
NikolaTheProgrammerNoob Posted November 12, 2015 Author Posted November 12, 2015 Yea, I don't crash unless I shift + right-click the crafted item. Then it just popsout thousands of same errors that they say is in the Minecraft Container class. To Craft, or not to Craft. That is the question.
NikolaTheProgrammerNoob Posted November 13, 2015 Author Posted November 13, 2015 I need help fast Can anyone please reply ? To Craft, or not to Craft. That is the question.
sham1 Posted November 13, 2015 Posted November 13, 2015 I need help fast Can anyone please reply ? Begging for help does not give a good view of you, considering that you could have looked the answer yourself from Google. But whatever. Care to show us your container class? If my post helped you, please press that "Thank You"-button to show your appreciation. Also if you don't know Java, I would suggest you read the official tutorials by Oracle to get an idea of how to do this. Thanks, and good modding! Also if you haven't, set up a Git repo for your mod not only for convinience but also to make it easier to help you.
Earthcomputer Posted November 13, 2015 Posted November 13, 2015 If you're only having problems with shift-right-clicking, then your problem is probably in the transferStackInSlot method catch(Exception e) { } Yay, Pokémon exception handling, gotta catch 'em all (and then do nothing with 'em).
NikolaTheProgrammerNoob Posted November 13, 2015 Author Posted November 13, 2015 I need help fast Can anyone please reply ? Begging for help does not give a good view of you, considering that you could have looked the answer yourself from Google. But whatever. Care to show us your container class? The Container Class is in the start of the post To Craft, or not to Craft. That is the question.
Cerandior Posted November 13, 2015 Posted November 13, 2015 Since you admit that you didnt know java well i am assuming that you followed some sort of tutorial. Have a look again to the tutorial you followed and see what is different and what you have possibly done wrong. The fact is that when you copy-paste someone's se tutorial it is highly unlikely that you will get away willout getting some sort of error. You have to come up with your own ideas so when you get errors you will know where to work to fix it. You should contact the guy who made ghe tutorial and ask for assistance. And dont tell me that yoh created the whole thing yourself. If you dont know java basics.you cant do that on your own
NikolaTheProgrammerNoob Posted November 14, 2015 Author Posted November 14, 2015 I actually know the basics of Java and the guy that made the tutorial was using 1.7.2 so I had to improvise And I did ask him a few days ago but no response To Craft, or not to Craft. That is the question.
DeltaDrizz Posted November 14, 2015 Posted November 14, 2015 Just a tip, don't use @SideOnly, use World#isRemote instead. Why? Read up here http://www.minecraftforge.net/forum/index.php/topic,22764.0.html
ovikk Posted November 14, 2015 Posted November 14, 2015 First problem You nees to open the gui on the client and the server. Your onBlockActivated method should look like this: public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ) { if (!player.isSneaking()) { player.openGui(Reference.MOD_ID, CarbonBlocks.guiIDIronForge, world, pos.getX(), pos.getY(), pos.getZ()); return true } return false; } Second problem You only get the red zeros if you forget to set the ItemStack to null when it reaches 0. If you decrease the stacksize to 0, but forget to remove by setting it to null, you will get a stacksize of zero in that slot. Third problem You're block crashes when you shift right click on an item, is that you have an error in your transferStackInSlot method.. I can't locate the error, but you should find it in the crash log. I might be terribly wrong.. Like really, really wrong. But I'm just trying to help.
Recommended Posts