Jump to content

Recommended Posts

Posted

Hi,

I have create a custom crafting table (following TheGreyGhost's tutorial) and now I have the problem that my gui is not drawn. This problem generated two questions:

[*]Is it neccesary to have a TileEntity? Looking at the Workbench it is only a Block and the Container is holding the inventory for crafting and result.

[*]The drawGuiContainerForegroundLayer() and drawGuiContainerBackgroundLayer() are never executed, but GuiHandler works fine and returns a Gui (client side). Any idea why?

 

Thanks in advance,

McRaichu

 

Turorial: https://github.com/TheGreyGhost/MinecraftByExample/tree/master/src/main/java/minecraftbyexample/mbe31_inventory_furnace

It doesn't work, I don't know why.

It works, I don't know why.

Posted

Matthew 7:7 "Ask and it will be given to you"  ;)

 

Block Class

public class ConYard extends Block{
protected ConYard(Material materialIn) {
	super(materialIn);
}

public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
{
	if (worldIn.isRemote)	{
		return true;
	}
	else	{
		playerIn.openGui(ObeliskOfLightMain.instance, GuiHandlerConYard.getGuiID(), worldIn, pos.getX(), pos.getY(), pos.getZ());
		return true;
	}
}

 

 

Gui Handler

public class GuiHandlerConYard implements IGuiHandler {
private static final int GUIConYard = 10;
public static int getGuiID() {
	return GUIConYard;
}
@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world,	int x, int y, int z) {
	if (ID != getGuiID()) {
		System.err.println("Invalid ID: expected " + getGuiID() + ", received " + ID);
	}

	BlockPos pos = new BlockPos(x, y, z);
	if (world.getBlockState(pos).getBlock() instanceof ConYard) {
		return new ContainerConYard(player.inventory, world, pos);
	}
	return null;
}
@Override
public Object getClientGuiElement(int ID, EntityPlayer player, World world,	int x, int y, int z) {
	if (ID != getGuiID()) {
		System.err.println("Invalid ID: expected " + getGuiID() + ", received " + ID);
	}

	BlockPos pos = new BlockPos(x, y, z);
	if (world.getBlockState(pos).getBlock() instanceof ConYard) {
		return new GuiConYard(player.inventory, world, pos);
	}
	return null;
}
}

 

 

Container Class

public class ContainerConYard extends Container
{
    /** The crafting matrix inventory (3x3). */
    public InventoryCrafting craftMatrix = new InventoryCrafting(this, 3, 3);
    public IInventory craftResult = new InventoryCraftResult();
    private World worldObj;
    private BlockPos pos;

    public ContainerConYard(InventoryPlayer playerInventory, World worldIn, BlockPos posIn)
    {
        this.worldObj = worldIn;
        this.pos = posIn;
        this.addSlotToContainer(new SlotCrafting(playerInventory.player, this.craftMatrix, this.craftResult, 0, 124, 35));

        for (int i = 0; i < 3; ++i)
        {
            for (int j = 0; j < 3; ++j)
            {
                this.addSlotToContainer(new Slot(this.craftMatrix, j + i * 3, 30 + j * 18, 17 + i * 18));
            }
        }

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

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

        this.onCraftMatrixChanged(this.craftMatrix);
    }
        
    /**
     * Callback for when the crafting matrix is changed.
     */
    public void onCraftMatrixChanged(IInventory inventoryIn)
    {
        this.craftResult.setInventorySlotContents(0, CraftingManager.getInstance().findMatchingRecipe(this.craftMatrix, this.worldObj));
    }

    /**
     * Called when the container is closed.
     */
    public void onContainerClosed(EntityPlayer playerIn)
    {
        super.onContainerClosed(playerIn);

        if (!this.worldObj.isRemote)
        {
            for (int i = 0; i < 9; ++i)
            {
                ItemStack itemstack = this.craftMatrix.removeStackFromSlot(i);

                if (itemstack != null)
                {
                    playerIn.dropPlayerItemWithRandomChoice(itemstack, false);
                }
            }
        }
    }

    public boolean canInteractWith(EntityPlayer playerIn)
    {
        return this.worldObj.getBlockState(this.pos).getBlock() instanceof ConYard ? false : playerIn.getDistanceSq((double)this.pos.getX() + 0.5D, (double)this.pos.getY() + 0.5D, (double)this.pos.getZ() + 0.5D) <= 64.0D;
    }

    /**
     * Take a stack from the specified inventory slot.
     */
    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;
    }

    /**
     * Called to determine if the current slot is valid for the stack merging (double-click) code. The stack passed in
     * is null for the initial slot that was double-clicked.
     */
    public boolean canMergeSlot(ItemStack stack, Slot p_94530_2_)
    {
        return p_94530_2_.inventory != this.craftResult && super.canMergeSlot(stack, p_94530_2_);
    }
}

 

 

GuiContainer

public class GuiConYard extends GuiContainer{
    private static final ResourceLocation craftingTableGuiTextures = new ResourceLocation("textures/gui/container/crafting_table.png");

    public GuiConYard(InventoryPlayer playerInv, World worldIn)
    {
    	this(playerInv, worldIn, BlockPos.ORIGIN);
    }

    public GuiConYard(InventoryPlayer playerInv, World worldIn, BlockPos blockPosition)
    {
        super(new ContainerConYard(playerInv, worldIn, blockPosition));
    }

    /**
     * Draw the foreground layer for the GuiContainer (everything in front of the items). Args : mouseX, mouseY
     */
    @Override
    protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY)
    {
    	super.drawGuiContainerForegroundLayer(mouseX, 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);
    }

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

 

It doesn't work, I don't know why.

It works, I don't know why.

Posted

Matthew 7:7 "Ask and it will be given to you"  ;)

 

Block Class

public class ConYard extends Block{
protected ConYard(Material materialIn) {
	super(materialIn);
}

public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
{
	if (worldIn.isRemote)	{
		return true;
	}
	else	{
		playerIn.openGui(ObeliskOfLightMain.instance, GuiHandlerConYard.getGuiID(), worldIn, pos.getX(), pos.getY(), pos.getZ());
		return true;
	}
}

 

 

Gui Handler

public class GuiHandlerConYard implements IGuiHandler {
private static final int GUIConYard = 10;
public static int getGuiID() {
	return GUIConYard;
}
@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world,	int x, int y, int z) {
	if (ID != getGuiID()) {
		System.err.println("Invalid ID: expected " + getGuiID() + ", received " + ID);
	}

	BlockPos pos = new BlockPos(x, y, z);
	if (world.getBlockState(pos).getBlock() instanceof ConYard) {
		return new ContainerConYard(player.inventory, world, pos);
	}
	return null;
}
@Override
public Object getClientGuiElement(int ID, EntityPlayer player, World world,	int x, int y, int z) {
	if (ID != getGuiID()) {
		System.err.println("Invalid ID: expected " + getGuiID() + ", received " + ID);
	}

	BlockPos pos = new BlockPos(x, y, z);
	if (world.getBlockState(pos).getBlock() instanceof ConYard) {
		return new GuiConYard(player.inventory, world, pos);
	}
	return null;
}
}

 

 

Container Class

public class ContainerConYard extends Container
{
    /** The crafting matrix inventory (3x3). */
    public InventoryCrafting craftMatrix = new InventoryCrafting(this, 3, 3);
    public IInventory craftResult = new InventoryCraftResult();
    private World worldObj;
    private BlockPos pos;

    public ContainerConYard(InventoryPlayer playerInventory, World worldIn, BlockPos posIn)
    {
        this.worldObj = worldIn;
        this.pos = posIn;
        this.addSlotToContainer(new SlotCrafting(playerInventory.player, this.craftMatrix, this.craftResult, 0, 124, 35));

        for (int i = 0; i < 3; ++i)
        {
            for (int j = 0; j < 3; ++j)
            {
                this.addSlotToContainer(new Slot(this.craftMatrix, j + i * 3, 30 + j * 18, 17 + i * 18));
            }
        }

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

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

        this.onCraftMatrixChanged(this.craftMatrix);
    }
        
    /**
     * Callback for when the crafting matrix is changed.
     */
    public void onCraftMatrixChanged(IInventory inventoryIn)
    {
        this.craftResult.setInventorySlotContents(0, CraftingManager.getInstance().findMatchingRecipe(this.craftMatrix, this.worldObj));
    }

    /**
     * Called when the container is closed.
     */
    public void onContainerClosed(EntityPlayer playerIn)
    {
        super.onContainerClosed(playerIn);

        if (!this.worldObj.isRemote)
        {
            for (int i = 0; i < 9; ++i)
            {
                ItemStack itemstack = this.craftMatrix.removeStackFromSlot(i);

                if (itemstack != null)
                {
                    playerIn.dropPlayerItemWithRandomChoice(itemstack, false);
                }
            }
        }
    }

    public boolean canInteractWith(EntityPlayer playerIn)
    {
        return this.worldObj.getBlockState(this.pos).getBlock() instanceof ConYard ? false : playerIn.getDistanceSq((double)this.pos.getX() + 0.5D, (double)this.pos.getY() + 0.5D, (double)this.pos.getZ() + 0.5D) <= 64.0D;
    }

    /**
     * Take a stack from the specified inventory slot.
     */
    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;
    }

    /**
     * Called to determine if the current slot is valid for the stack merging (double-click) code. The stack passed in
     * is null for the initial slot that was double-clicked.
     */
    public boolean canMergeSlot(ItemStack stack, Slot p_94530_2_)
    {
        return p_94530_2_.inventory != this.craftResult && super.canMergeSlot(stack, p_94530_2_);
    }
}

 

 

GuiContainer

public class GuiConYard extends GuiContainer{
    private static final ResourceLocation craftingTableGuiTextures = new ResourceLocation("textures/gui/container/crafting_table.png");

    public GuiConYard(InventoryPlayer playerInv, World worldIn)
    {
    	this(playerInv, worldIn, BlockPos.ORIGIN);
    }

    public GuiConYard(InventoryPlayer playerInv, World worldIn, BlockPos blockPosition)
    {
        super(new ContainerConYard(playerInv, worldIn, blockPosition));
    }

    /**
     * Draw the foreground layer for the GuiContainer (everything in front of the items). Args : mouseX, mouseY
     */
    @Override
    protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY)
    {
    	super.drawGuiContainerForegroundLayer(mouseX, 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);
    }

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

 

It doesn't work, I don't know why.

It works, I don't know why.

Posted

yep, sounds right. But for now that is not important  (the workbench does the same  :P). And i tested and it returns true and still the gui doesn't show.

It doesn't work, I don't know why.

It works, I don't know why.

Posted

yep, sounds right. But for now that is not important  (the workbench does the same  :P). And i tested and it returns true and still the gui doesn't show.

It doesn't work, I don't know why.

It works, I don't know why.

Posted

"yep, sounds right" == you are right and yes workbench doesn't ... I missed the ! in !=. crap, 3 hours wasted because of one character. thx, works like a charm now

 

It doesn't work, I don't know why.

It works, I don't know why.

Posted

"yep, sounds right" == you are right and yes workbench doesn't ... I missed the ! in !=. crap, 3 hours wasted because of one character. thx, works like a charm now

 

It doesn't work, I don't know why.

It works, I don't know why.

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.