Jump to content

Custom Crafting Table Recipe Book not transfering items


peter1745

Recommended Posts

Hello! I've been implementing a custom crafting table that have a larger crafting grid than the vanilla crafting table. The problem is that i want the Recipe Book, now, i've implemented the Recipe Book so it can be toggled, but when you click an item in the book, it doesn't transfer the items, and it doesn't show the "ghost" recipe if you don't have the required materials.

 

I've used the GuiCrafting class to implement the Recipe Book, but it won't work, i've tried debugging the functionallity, but i can't figure it out.

 

Here's my gui class:

 

public class GuiCarpentersTable extends GuiContainer implements IRecipeShownListener
{

	private static final ResourceLocation CARPENTERS_TABLE_TEXTURES = new ResourceLocation(Constants.MODID, "textures/gui/container/carpenters_table.png");

	private final GuiRecipeBook recipeBook;
	private GuiButtonImage recipeButton;
	private boolean widthTooNarrow;

	public GuiCarpentersTable(InventoryPlayer playerInv, World worldIn, BlockPos pos)
	{
		super(new ContainerCarpentersTable(playerInv, worldIn, pos));
		this.recipeBook = new GuiRecipeBook();
		this.ySize = 178;
	}

	@Override
	public void initGui()
	{
		super.initGui();
		this.widthTooNarrow = this.width < 379;
		this.recipeBook.func_194303_a(this.width, this.height, this.mc, this.widthTooNarrow, ((ContainerCarpentersTable)this.inventorySlots).craftingMatrix);
		this.guiLeft = this.recipeBook.updateScreenPosition(this.widthTooNarrow, this.width, this.xSize);
		this.recipeButton = new GuiButtonImage(10, this.guiLeft + 8, this.height / 2 - 49, 20, 18, 0, 180, 19, CARPENTERS_TABLE_TEXTURES);
		this.buttonList.add(this.recipeButton);
	}

	@Override
	public void updateScreen()
	{
		super.updateScreen();
		this.recipeBook.tick();
	}

	@Override
	public void drawScreen(int mouseX, int mouseY, float partialTicks)
	{
		this.drawDefaultBackground();

		if (this.recipeBook.isVisible() && this.widthTooNarrow)
		{
			this.drawGuiContainerBackgroundLayer(partialTicks, mouseX, mouseY);
			this.recipeBook.render(mouseX, mouseY, partialTicks);
		}
		else
		{
			this.recipeBook.render(mouseX, mouseY, partialTicks);
			super.drawScreen(mouseX, mouseY, partialTicks);
			this.recipeBook.renderGhostRecipe(this.guiLeft, this.guiTop, true, partialTicks);
		}

		this.renderHoveredToolTip(mouseX, mouseY);
		this.recipeBook.renderTooltip(this.guiLeft, this.guiTop, mouseX, mouseY);
	}

	protected boolean isPointInRegion(int rectX, int rectY, int rectWidth, int rectHeight, int pointX, int pointY)
	{
		return (!this.widthTooNarrow || !this.recipeBook.isVisible()) && super.isPointInRegion(rectX, rectY, rectWidth, rectHeight, pointX, pointY);
	}

	@Override
	protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException
	{
		if (!this.recipeBook.mouseClicked(mouseX, mouseY, mouseButton))
		{
			if (!this.widthTooNarrow || !this.recipeBook.isVisible())
			{
				super.mouseClicked(mouseX, mouseY, mouseButton);
			}
		}
	}

	@Override
	protected boolean hasClickedOutside(int mouseX, int mouseY, int guiLeft, int guiTop)
	{
		boolean outside = mouseX < guiLeft || mouseY < guiTop || mouseX >= guiLeft + this.xSize || mouseY >= guiTop + this.ySize;
		return this.recipeBook.hasClickedOutside(mouseX, mouseY, this.guiLeft, this.guiTop, this.xSize, this.ySize) && outside;
	}

	@Override
	protected void actionPerformed(GuiButton button) throws IOException
	{
		if (button.id == 10)
		{
			this.recipeBook.initVisuals(this.widthTooNarrow, ((ContainerCarpentersTable)this.inventorySlots).craftingMatrix);
			this.recipeBook.toggleVisibility();
			this.guiLeft = this.recipeBook.updateScreenPosition(this.widthTooNarrow, this.width, this.xSize);
			this.recipeButton.setPosition(this.guiLeft + 8, this.height / 2 - 49);
		}
	}

	@Override
	protected void keyTyped(char typedChar, int keyCode) throws IOException
	{
		if (!this.recipeBook.keyPressed(typedChar, keyCode))
		{
			super.keyTyped(typedChar, keyCode);
		}
	}

	@Override
	protected void handleMouseClick(Slot slotIn, int slotId, int mouseButton, ClickType type)
	{
		super.handleMouseClick(slotIn, slotId, mouseButton, type);
		this.recipeBook.slotClicked(slotIn);
	}

	@Override
	protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY)
	{
		String text = I18n.format(MEBlocks.CARPENTERS_TABLE.getUnlocalizedName() + ".name");
		MEUtils.renderText(text, this.xSize, 3, 0x404040);
		MEUtils.renderText(I18n.format("container.inventory"), 64, this.ySize - 93, 0x404040);
	}

	@Override
	protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY)
	{
		GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
		this.mc.getTextureManager().bindTexture(CARPENTERS_TABLE_TEXTURES);
		int i = this.guiLeft;
		int j = (this.height - this.ySize) / 2;
		this.drawTexturedModalRect(i, j, 0, 0, this.xSize, this.ySize);
	}

	@Override
	public void recipesUpdated()
	{
		this.recipeBook.recipesUpdated();
	}

	@Override
	public void onGuiClosed()
	{
		this.recipeBook.removed();
		super.onGuiClosed();
	}

	@Override
	public GuiRecipeBook func_194310_f()
	{
		return this.recipeBook;
	}
}

 

And here's the container class:

 

public class ContainerCarpentersTable extends Container
{

	public InventoryCrafting craftingMatrix = new InventoryCrafting(this, 5, 4);
	public InventoryCraftResult craftingResult = new InventoryCraftResult();

	private final BlockPos pos;
	private final World world;
	private final EntityPlayer player;

	public ContainerCarpentersTable(InventoryPlayer playerInv, World world, BlockPos pos)
	{
		this.pos = pos;
		this.world = world;
		this.player = playerInv.player;

		this.addSlotToContainer(new SlotCrafting(playerInv.player, this.craftingMatrix, this.craftingResult, 0, 142, 41));

		for (int i = 0; i < 4; ++i)
		{
			for (int j = 0; j < 5; ++j)
			{
				this.addSlotToContainer(new Slot(this.craftingMatrix, j + i * 5, 38 + j * 18, 13 + i * 18));
			}
		}

		for (int k = 0; k < 3; ++k)
		{
			for (int i1 = 0; i1 < 9; ++i1)
			{
				this.addSlotToContainer(new Slot(playerInv, i1 + k * 9 + 9, 8 + i1 * 18, 96 + k * 18));
			}
		}

		for (int l = 0; l < 9; ++l)
		{
			this.addSlotToContainer(new Slot(playerInv, l, 8 + l * 18, 154));
		}

	}

	@Override
	public void onCraftMatrixChanged(IInventory inventoryIn)
	{
		this.slotChangedCraftingGrid(this.world, this.player, this.craftingMatrix, this.craftingResult);
	}

	@Override
	public void onContainerClosed(EntityPlayer playerIn)
	{
		super.onContainerClosed(playerIn);

		if (!this.world.isRemote)
		{
			this.clearContainer(playerIn, this.world, this.craftingMatrix);
		}
	}

	@Override
	public boolean canInteractWith(EntityPlayer playerIn)
	{
		if (this.world.getBlockState(this.pos).getBlock() != MEBlocks.CARPENTERS_TABLE)
			return false;
		else
			return playerIn.getDistanceSq((double)this.pos.getX() + 0.5D, (double)this.pos.getY() + 0.5D, (double)this.pos.getZ() + 0.5D) <= 64.0D;
	}

	public ItemStack transferStackInSlot(EntityPlayer playerIn, int index)
	{
		ItemStack itemstack = ItemStack.EMPTY;
		Slot slot = this.inventorySlots.get(index);

		if (slot != null && slot.getHasStack())
		{
			ItemStack itemstack1 = slot.getStack();
			itemstack = itemstack1.copy();

			if (index == 0)
			{
				itemstack1.getItem().onCreated(itemstack1, this.world, playerIn);

				if (!this.mergeItemStack(itemstack1, 10, 46, true))
				{
					return ItemStack.EMPTY;
				}

				slot.onSlotChange(itemstack1, itemstack);
			}
			else if (index >= 10 && index < 37)
			{
				if (!this.mergeItemStack(itemstack1, 37, 46, false))
				{
					return ItemStack.EMPTY;
				}
			}
			else if (index >= 37 && index < 46)
			{
				if (!this.mergeItemStack(itemstack1, 10, 37, false))
				{
					return ItemStack.EMPTY;
				}
			}
			else if (!this.mergeItemStack(itemstack1, 10, 46, false))
			{
				return ItemStack.EMPTY;
			}

			if (itemstack1.isEmpty())
			{
				slot.putStack(ItemStack.EMPTY);
			}
			else
			{
				slot.onSlotChanged();
			}

			if (itemstack1.getCount() == itemstack.getCount())
			{
				return ItemStack.EMPTY;
			}

			ItemStack itemstack2 = slot.onTake(playerIn, itemstack1);

			if (index == 0)
			{
				playerIn.dropItem(itemstack2, false);
			}
		}

		return itemstack;
	}

	@Override
	public boolean canMergeSlot(ItemStack stack, Slot slotIn)
	{
		return slotIn.inventory != this.craftingResult && super.canMergeSlot(stack, slotIn);
	}

 

I haven't found any good tutorials or really any information when i've googled it, i've tried searching this forum but i can't figure it out

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.