Jump to content

Recommended Posts

Posted

Hello, fellow forum adicts !

 

Soo... my custom crafting table is not working for some reason :/

 

The GUI wont open and the Console doesn't put out any information at all...

 

Here is my code:

 

The Block Class

package com.nikola.carbonmod.blocks;

import com.nikola.carbonmod.CarbonMod;
import com.nikola.carbonmod.container.ContainerIronForge;
import com.nikola.carbonmod.init.CarbonBlocks;

import net.minecraft.block.Block;
import net.minecraft.block.BlockWorkbench;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.inventory.Container;
import net.minecraft.util.BlockPos;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.IChatComponent;
import net.minecraft.world.IInteractionObject;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.network.internal.FMLNetworkHandler;

public class IronForge extends Block {

private static final String __OBFID = "CL_00000221";

public IronForge(Material materialIn) {
	super(materialIn);
	this.setHardness(3.0F);
	this.setResistance(15.0F);
	this.isBlockContainer = true;
}

@Override
public boolean isOpaqueCube() {
	return false;
}

@Override
public boolean isFullCube() {
	return false;
}

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
        {
        	FMLNetworkHandler.openGui(playerIn, CarbonMod.instance, CarbonBlocks.guiIDIronForge, worldIn, x, y, z);
            return true;
        }
    }

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 Container for The Block

package com.nikola.carbonmod.container;

import com.nikola.carbonmod.crafting.IronForgeCraftingManager;
import com.nikola.carbonmod.init.CarbonBlocks;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryCraftResult;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.inventory.Slot;
import net.minecraft.inventory.SlotCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;

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);
}

//GuiHandlerConstructor
public ContainerIronForge(InventoryPlayer invPlayer, World world, 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_);
}
}

 

The Gui Handler

package com.nikola.carbonmod.gui;

import com.nikola.carbonmod.container.ContainerIronForge;
import com.nikola.carbonmod.init.CarbonBlocks;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.network.IGuiHandler;

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 The GUI for The Block

package com.nikola.carbonmod.gui;

import com.nikola.carbonmod.container.ContainerIronForge;

import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ContainerWorkbench;
import net.minecraft.util.BlockPos;
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 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, int x, int y, int z)
    {
        this(playerInv, worldIn, BlockPos.ORIGIN);
    }

    public GuiIronForge(InventoryPlayer playerInv, World worldIn, BlockPos blockPosition)
    {
        super(new ContainerIronForge(playerInv, worldIn, blockPosition));
    }

    public 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);
    }

    public 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);
    }
}

To Craft, or not to Craft. That is the question.

Posted

Your onBlockActivated methode has not the right arguments:

 

    onBlockActivated(World worldIn, int x, int y , int z, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)

 

Right is:

 

  onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)

Sorry if there are mistakes in my english

Posted

Umm.. when I change that I get an error on openGui saying:

 

The method openGui(EntityPlayer, Object, int, World, int, int, int) in the type FMLNetworkHandler is not applicable for the arguments (EntityPlayer, CarbonMod, int, World, BlockPos)

To Craft, or not to Craft. That is the question.

Posted

You know, instead of trying to copy the method from another source. Try learning it yourself how it is used and what it does before. And also instead of writing everything on your own use ctrl+space. It's Magic.

Posted

I really want to learn Java :) but can't fint the right tutorial. Any suggestions ?

 

I recommend the following book: Java in Easy Steps http://www.amazon.com/Java-Easy-Steps-Fully-Updated/dp/1840784431

 

What I like about it is that it covers all the key topics but isn't as boring or scary as many coding books. It is a nice quick read and you'll learn a lot.

 

Also, I believe coding is something you need to learn from an actual book. Video tutorials have a set pace that may not suit where you're at in your learning. Also, people tend to skip ahead in videos and miss some of the key early concepts, whereas a book will lay it out for you in very specific way to build up your knowledge.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

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.