Jump to content

Crash on Opening GUI. HELP!


314owen

Recommended Posts

Hello, I'm currently making a mod and have just watched around 7 hours of tutorials on how to make a GUI, I finally tested it and now it crashes every time I right click on the custom furnace. Anyone have any ideas how to fix this? When I right click, Minecraft doesn't respond and the server gets overloaded.

Link to comment
Share on other sites

Please Help! This is my code:

Block:

 

package endernoobs.mods.ScienceMod;

 

import java.util.Random;

 

import cpw.mods.fml.common.network.FMLNetworkHandler;

import cpw.mods.fml.relauncher.Side;

import cpw.mods.fml.relauncher.SideOnly;

import net.minecraft.block.Block;

import net.minecraft.block.BlockContainer;

import net.minecraft.block.material.Material;

import net.minecraft.client.renderer.texture.IconRegister;

import net.minecraft.entity.EntityLivingBase;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.inventory.Container;

import net.minecraft.inventory.IInventory;

import net.minecraft.item.ItemStack;

import net.minecraft.tileentity.TileEntity;

import net.minecraft.util.Icon;

import net.minecraft.util.MathHelper;

import net.minecraft.world.World;

 

public class BlockDNAExtractor extends BlockContainer{

 

private final boolean isActive;

 

@SideOnly(Side.CLIENT)

private Icon iconFront;

 

private static boolean keepInventory;

 

public BlockDNAExtractor(int id, boolean isActive) {

super(id, Material.rock);

 

this.isActive = isActive;

}

@SideOnly(Side.CLIENT)

public void registerIcons(IconRegister iconRegister){

this.blockIcon = iconRegister.registerIcon(ScienceModMain.modid + ":" + "DNAExtractor_top");

this.iconFront = iconRegister.registerIcon(ScienceModMain.modid + ":" + (this.isActive ? "DNAExtractor_active" : "DNAExtractor_front"));

}

@SideOnly(Side.CLIENT)

public Icon getIcon(int side, int metadata){

return side == metadata ? this.iconFront : this.blockIcon;

}

public int idDropped(int par1, Random random, int par3){

return ScienceModMain.BlockDNAExtractor.blockID;

}

 

public void onBlockAdded(World world, int x, int y, int z){

super.onBlockAdded(world, x, y, z);

this.setDefaultDirection(world, x, y, z);

 

}

 

private void setDefaultDirection(World world, int x, int y, int z){

 

if (!world.isRemote){

byte b0 = 3;

int l = world.getBlockId(x, y, z - 1);

int il = world.getBlockId(x, y, z + 1);

int jl = world.getBlockId(x - 1, y, z);

int kl = world.getBlockId(x + 1, y, z);

 

if (Block.opaqueCubeLookup[l] && !Block.opaqueCubeLookup[il]){

b0 = 3;

}

 

if (Block.opaqueCubeLookup[il] && !Block.opaqueCubeLookup[l]){

b0 = 2;

 

}

 

if (Block.opaqueCubeLookup[kl] && !Block.opaqueCubeLookup[jl]){

b0 = 5;

 

}

 

if (Block.opaqueCubeLookup[jl] && !Block.opaqueCubeLookup[kl]){

b0 = 4;

 

}

 

world.setBlockMetadataWithNotify(x, y, z, b0, 2);

}

}

 

public TileEntity createNewTileEntity(World world){

return new TileEntityDNAExtractor();

}

 

public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entityLivingBase, ItemStack itemstack){

int l = MathHelper.floor_double((double)(entityLivingBase.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;

 

if (l == 0){

 

world.setBlockMetadataWithNotify(x, y, z, 2, 2);

 

}

if (l == 1){

 

world.setBlockMetadataWithNotify(x, y, z, 5, 2);

 

}

if (l == 2){

 

world.setBlockMetadataWithNotify(x, y, z, 3, 2);

 

}

if (l == 3){

 

world.setBlockMetadataWithNotify(x, y, z, 4, 2);

 

}

if (itemstack.hasDisplayName()){

((TileEntityDNAExtractor)world.getBlockTileEntity(x, y, z)).setGuiDisplayName(itemstack.getDisplayName());

}

}

public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ){

if (!world.isRemote)

    {

        return true;

    }

    else

    {

        TileEntityDNAExtractor tileentitydnaextractor = (TileEntityDNAExtractor)world.getBlockTileEntity(x, y, z);

 

        if (tileentitydnaextractor != null)

        {

      player.openGui(ScienceModMain.instance, 0, world, x, y, z);

     

        }

 

        return true;

    }

}

public static void updateDNAExtractorBlockState(boolean active, World worldObj, int xCoord, int yCoord, int zCoord) {

int i = worldObj.getBlockMetadata(xCoord, yCoord, zCoord);

TileEntity tileentity = worldObj.getBlockTileEntity(xCoord, yCoord, zCoord);

keepInventory = true;

 

if(active){

worldObj.setBlock(xCoord, yCoord, zCoord, ScienceModMain.BlockDNAExtractorWorking.blockID);

}else{

worldObj.setBlock(xCoord, yCoord, zCoord, ScienceModMain.BlockDNAExtractor.blockID);

}

keepInventory = false;

 

worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, i, 2);

 

if (tileentity !=null){

tileentity.validate();

worldObj.setBlockTileEntity(xCoord, yCoord, zCoord, tileentity);

}

}

 

public boolean hasComparatorInputOverride(){

return true;

}

public int getComparatorInputOverride(World world, int x, int y, int z, int i){

return Container.calcRedstoneFromInventory((IInventory)world.getBlockTileEntity(x, y, z));

}

public int idPicked(World world, int x, int y, int z){

return ScienceModMain.BlockDNAExtractor.blockID;

 

}

}

 

 

 

GUI:

 

package endernoobs.mods.ScienceMod;

 

import org.lwjgl.opengl.GL11;

 

import net.minecraft.client.Minecraft;

import net.minecraft.client.gui.inventory.GuiContainer;

import net.minecraft.client.resources.I18n;

import net.minecraft.entity.player.InventoryPlayer;

import net.minecraft.inventory.Container;

import net.minecraft.util.ResourceLocation;

 

public class GuiDNAExtractor extends GuiContainer{

 

public static final ResourceLocation texture = new ResourceLocation(ScienceModMain.modid, "textures/gui/GuiDNAExtractor.png");

 

public TileEntityDNAExtractor DNAExtractor;

 

public GuiDNAExtractor(InventoryPlayer inventoryPlayer, TileEntityDNAExtractor entity) {

super(new ContainerDNAExtractor(inventoryPlayer, entity));

 

this.DNAExtractor = entity;

 

this.xSize = 176;

this.ySize = 166;

}

 

public void drawGuiContainerForegroundLayer(int par1, int par2){

String name = this.DNAExtractor.isInvNameLocalized() ? this.DNAExtractor.getInvName() : I18n.getString(this.DNAExtractor.getInvName());

 

this.fontRenderer.drawString(name, this.xSize / 2 - this.fontRenderer.getStringWidth(name) / 2, 6, 4210752);

this.fontRenderer.drawString(I18n.getString("container.inventory"), 8, this.ySize - 96 + 2, 4210752);

}

 

public void drawGuiContainerBackgroundLayer(float f, int i, int j) {

GL11.glColor4f(1F, 1F, 1F, 1F);

 

Minecraft.getMinecraft().getTextureManager().bindTexture(texture);

 

drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);

}

 

}

 

 

 

Tile Entity:

 

package endernoobs.mods.ScienceMod;

 

import net.minecraft.block.Block;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.inventory.ISidedInventory;

import net.minecraft.item.Item;

import net.minecraft.item.ItemStack;

import net.minecraft.item.crafting.FurnaceRecipes;

import net.minecraft.tileentity.TileEntity;

 

public class TileEntityDNAExtractor extends TileEntity implements ISidedInventory{

 

private String localizedName;

 

private static final int[] slots_top = new int[]{0};

private static final int[] slots_bottom = new int[]{2};

private static final int[] slots_sides = new int[]{1};

 

private ItemStack[] slots = new ItemStack[3];

 

public int furnaceSpeed = 300;

public int burnTime;

public int currentItemBurnTime;

public int cookTime;

 

 

public int getSizeInventory(){

return this.slots.length;

}

 

public String getInvName(){

return this.isInvNameLocalized() ? this.localizedName : "container.dnaextractor";

}

 

public boolean isInvNameLocalized(){

return this.localizedName !=null && this.localizedName.length() > 0;

}

 

public void setGuiDisplayName(String displayName) {

this.localizedName = displayName;

}

 

 

public ItemStack getStackInSlot(int i) {

return this.slots;

}

 

public ItemStack decrStackSize(int i, int j) {

if (this.slots !=null){

ItemStack itemstack;

 

if(this.slots.stackSize <= j){

itemstack = this.slots;

this.slots = null;

return itemstack;

}else{

itemstack = this.slots.splitStack(j);

 

if (this.slots.stackSize == 0){

this.slots = null;

}

return itemstack;

}

}

return null;

}

 

public ItemStack getStackInSlotOnClosing(int i) {

if (this.slots !=null){

ItemStack itemstack = this.slots;

this.slots = null;

return itemstack;

}

return null;

}

 

public void setInventorySlotContents(int i, ItemStack itemstack) {

this.slots = itemstack;

if (itemstack !=null && itemstack.stackSize > this.getInventoryStackLimit()){

itemstack.stackSize = this.getInventoryStackLimit();

}

}

 

public int getInventoryStackLimit() {

 

return 64;

}

 

public boolean isUseableByPlayer(EntityPlayer entityplayer) {

return this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : entityplayer.getDistanceSq((double)this.xCoord + 0.5D,(double)this.yCoord + 0.5D,(double)this.zCoord + 0.5D) <= 64.0D;

}

 

public void openChest() {}

public void closeChest() {}

 

public boolean isBurning(){

return this.burnTime > 0;

}

 

public void updateEntity(){

boolean flag = this.burnTime > 0;

boolean flag1 = false;

 

if (this.burnTime > 0){

this.burnTime--;

}

 

if (!this.worldObj.isRemote){

if (this.burnTime == 0 && this.canSmelt()){

this.currentItemBurnTime = this.burnTime = getItemBurnTime(this.slots[1]);

 

if (this.burnTime > 0){

flag1 = true;

 

if (this.slots[1] !=null){

this.slots[1].stackSize--;

 

if (this.slots[1].stackSize == 0){

this.slots[1] = this.slots[1].getItem().getContainerItemStack(this.slots[1]); }

}

}

}

 

if(this.isBurning() && this.canSmelt()){

this.cookTime++;

 

 

if (this.cookTime == this.furnaceSpeed){

this.cookTime = 0;

this.smeltItem();

flag1 = true;

}

 

}else{

this.cookTime = 0;

}

 

if (flag != this.burnTime > 0){

  flag1 = true;

BlockDNAExtractor.updateDNAExtractorBlockState(this.burnTime > 0, this.worldObj, this.xCoord, this.yCoord, this.zCoord);

}

if (flag1){

this.onInventoryChanged();

}

}

}

 

private boolean canSmelt(){

if (this.slots[0] == null){

return false;

}else{

ItemStack itemstack = DNARecipes.smelting().getSmeltingResult(this.slots[0]);

 

if (itemstack == null)return false;

if (this.slots[2] == null) return true;

if (!this.slots[2].isItemEqual(itemstack)) return false;

 

int result = this.slots[2].stackSize + itemstack.stackSize;

 

return (result <= getInventoryStackLimit() && result <= itemstack.getMaxStackSize());

}

}

 

public void smeltItem(){

if(this.canSmelt()){

ItemStack itemstack = DNARecipes.smelting().getSmeltingResult(this.slots[0]);

 

if (this.slots[2] == null){

this.slots[2] = itemstack.copy();

}else if(this.slots[2].isItemEqual(itemstack)){

this.slots[2].stackSize += itemstack.stackSize;

}

this.slots[0].stackSize--;

 

if (this.slots[0].stackSize <= 0){

this.slots[0] = null;

}

}

}

 

public static int getItemBurnTime(ItemStack itemstack){

if (itemstack == null){

return 0;

}

else

{

if (itemstack.itemID == Item.redstone.itemID) return 300;

if (itemstack.itemID == Block.blockRedstone.blockID) return 3000;

return 0;

}

}

public static boolean isItemFuel(ItemStack itemstack){

return getItemBurnTime(itemstack) > 0;

}

public boolean isItemValidForSlot(int i, ItemStack itemstack) {

return i == 2 ? false : (i == 1 ? isItemFuel(itemstack) : true);

}

 

 

public int[] getAccessibleSlotsFromSide(int var1) {

return var1 == 0 ? slots_bottom : (var1 == 1 ? slots_top : slots_sides);

}

 

public boolean canInsertItem(int i, ItemStack itemstack, int j) {

return this.isItemValidForSlot(i, itemstack);

}

 

public boolean canExtractItem(int i, ItemStack itemstack, int j) {

return j !=0 || i !=1 || itemstack.itemID == Item.bucketEmpty.itemID;

}

 

}

 

 

 

Container:

 

package endernoobs.mods.ScienceMod;

 

import cpw.mods.fml.relauncher.Side;

import cpw.mods.fml.relauncher.SideOnly;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.entity.player.InventoryPlayer;

import net.minecraft.inventory.Container;

import net.minecraft.inventory.ICrafting;

import net.minecraft.inventory.Slot;

import net.minecraft.inventory.SlotFurnace;

import net.minecraft.item.ItemStack;

 

public class ContainerDNAExtractor extends Container{

 

public int lastBurnTime;

public int lastItemBurnTime;

public int lastCookTime;

 

private TileEntityDNAExtractor DNAExtractor;

 

public ContainerDNAExtractor(InventoryPlayer inventory, TileEntityDNAExtractor tileentity){

this.DNAExtractor = tileentity;

 

 

this.addSlotToContainer(new Slot(tileentity, 0, 56, 17));

this.addSlotToContainer(new Slot(tileentity, 1, 56, 53));

this.addSlotToContainer(new SlotFurnace(inventory.player, tileentity, 2, 116, 35));

 

for (int i = 0; i < 3; i++){

for (int j = 0; j < 9; i++){

this.addSlotToContainer(new Slot(inventory, j + i*9 + 9, 8 + j*18, 84 + i*18));

}

}

for (int i = 0; i<9; i++){

this.addSlotToContainer(new Slot(inventory, i, 8+ i*18, 142));

}

}

public void addCraftingToCrafters(ICrafting icrafting){

super.addCraftingToCrafters(icrafting);

icrafting.sendProgressBarUpdate(this, 0, this.DNAExtractor.cookTime);

icrafting.sendProgressBarUpdate(this, 1, this.DNAExtractor.burnTime);

icrafting.sendProgressBarUpdate(this, 2, this.DNAExtractor.currentItemBurnTime);

 

}

 

public void detectAndSendChanges(){

super.detectAndSendChanges();

}

 

@SideOnly(Side.CLIENT)

public void updateProgressBar(int slot, int newValue){

if (slot == 0) this.DNAExtractor.cookTime = newValue;

if (slot == 1) this.DNAExtractor.burnTime = newValue;

if (slot == 2) this.DNAExtractor.currentItemBurnTime = newValue;

 

}

 

public ItemStack transferStackInSlot(EntityPlayer player, int slot){

return null;

}

 

public boolean canInteractWith(EntityPlayer entityplayer) {

return this.DNAExtractor.isUseableByPlayer(entityplayer);

}

 

}

 

 

Link to comment
Share on other sites

314owen? I recognize you from PlanetMinecraft. Well, welcome to to the forums. Like what Angus Young said, you need a GuiHandler. Here's an example:

 

// Lots of imports

public class GuiHandler implements IGuiHandler {
    @Override
    public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
        TileEntity tile = world.getTileEntity(x, y, z);
        if (tile != null && tile instanceof TileEntityDNAExtractor && ID == 0) {
            return new GuiDNAExtractor(player.inventory, (TileEntityDNAExtractor) tile);
        }
        return null;
    }
    @Override
    public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
        TileEntity tile = world.getTileEntity(x, y, z);
        if (tile != null && tile instanceof TileEntityDNAExtractor && ID == 0) {
            return new ContainerDNAExtractor(player.inventory, (TileEntityDNAExtractor) tile);
        }
        return null;
    }
}

if (user.hasKnowledgeOfJava) {

    if (user.question.hasCode) {

        return interpetHelpfulResponse(user.getQuestion());

    } else {

        return "Could you post your code please?";

    }

} else {

    return "Learn some freaking Java!";

}

Link to comment
Share on other sites

Right, the example provided by DiabolusNeil is pefect in your situation, but if in future you'll need to add other GUI blocks, it would be better to declare in your base mod class the variables representing the IDs, and check them with a switch in your GUI helper class, like this:

 

public class GuiHandler implements IGuiHandler {
	//Return an instance of your Container
	@Override
	public Object getServerGuiElement(int id, EntityPlayer player, World world, int x, int y, int z) 
	{
		TileEntity tileEntity = world.getTileEntity(x, y, z); //Get the TileEntity at x,y,z
		if(tileEntity == null) //If the TileEntity is null return null
			return null;

		switch(id)
		{
			case 0: //Your case
				if(tileEntity instanceof TileEntityDNAExtractor)
				{
					return new ContainerDNAExtractor(player.inventory, (TileEntityDNAExtractor) tileEntity);
				}
			case 1: //Other future cases
				if(tileEntity instanceof TileEntity....)
				{
					return new Container....(player.inventory, (TileEntity....) tileEntity);
				}
			case 2: //Other future cases
				if(tileEntity instanceof TileEntity....)
				{
					return new Container....(player.inventory, (TileEntity....) tileEntity);
				}
			//.......
			default: 
				return null;
		}  
	}

	//Return an instance of your GUI
	@Override
	public Object getClientGuiElement(int id, EntityPlayer player, World world, int x, int y, int z) 
	{
		TileEntity tileEntity = world.getTileEntity(x, y, z); //Get the TileEntity at x,y,z
		if(tileEntity == null) //If the TileEntity is null return null
			return null;

		switch(id)
		{
			case 0: //Foundry Gui
				if(tileEntity instanceof TileEntityDNAExtractor)
				{
					return new GuiDNAExtractor(player.inventory, (TileEntityDNAExtractor) tileEntity);
				}
			case 1: //Other future cases
				if(tileEntity instanceof TileEntity....)
				{
					return new Gui....(player.inventory, (TileEntity....) tileEntity);
				}
			case 2: //Other future cases
				if(tileEntity instanceof TileEntity....)
				{
					return new Gui....(player.inventory, (TileEntity....) tileEntity);
				}
			//.......
			default:
				return null;
		}
	}
}

 

And in your base mod class declare some variables like these:

public final static int DNAExtractorID = 0; //This represent your DNA Extractor GUI ID
//These will be future GUI IDs
public final static int ....ID = 1;
public final static int ....ID = 2; 

 

And when you open your GUI, in the "onBlockActivated" function:

player.openGui(ScienceModMain.instance, ScienceModMain.DNAExtractorID, world, x, y, z);

Link to comment
Share on other sites

Ok, I have fixed that and a few more problems have popped up, when does this not happen, you fix one problem, tons more come. After doing the GUI Handler, now It opens the gui, but it is missing its texture (the directory looks correct) and when you try to move items they go back to there slots, if I shift click, it crashes.

Link to comment
Share on other sites

Where is stored your GUI texture? It should be inside a packaged named:

assets.yourmodname.textures.gui

 

If your texture location is already correct try to replace your ResourceLocation in your GUI class with this:

public static final ResourceLocation texture = new ResourceLocation(ScienceModMain.modid + ":" + "textures/gui/GuiDNAExtractor.png");

 

And to fix the shift click crash you should Override the method "transferStackInSlot" in your Container class, it should be something like this:

@Override
    /**
     * Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that.
     */
    public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2)
    {
        // Here you have to return an ItemStack, depending of what you shift-clicked in your GUI
    }

 

I think your GUI is very similar to the furnace, so you could use this:

@Override
/**
     * Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that.
     */
    public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2)
    {
        ItemStack itemstack = null;
        Slot slot = (Slot)this.inventorySlots.get(par2);

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

            if (par2 == 2)
            {
                if (!this.mergeItemStack(itemstack1, 3, 39, true))
                {
                    return null;
                }

                slot.onSlotChange(itemstack1, itemstack);
            }
            else if (par2 != 1 && par2 != 0)
            {
                if (FurnaceRecipes.smelting().getSmeltingResult(itemstack1) != null)
                {
                    if (!this.mergeItemStack(itemstack1, 0, 1, false))
                    {
                        return null;
                    }
                }
                else if (TileEntityFurnace.isItemFuel(itemstack1))
                {
                    if (!this.mergeItemStack(itemstack1, 1, 2, false))
                    {
                        return null;
                    }
                }
                else if (par2 >= 3 && par2 < 30)
                {
                    if (!this.mergeItemStack(itemstack1, 30, 39, false))
                    {
                        return null;
                    }
                }
                else if (par2 >= 30 && par2 < 39 && !this.mergeItemStack(itemstack1, 3, 30, false))
                {
                    return null;
                }
            }
            else if (!this.mergeItemStack(itemstack1, 3, 39, false))
            {
                return null;
            }

            if (itemstack1.stackSize == 0)
            {
                slot.putStack((ItemStack)null);
            }
            else
            {
                slot.onSlotChanged();
            }

            if (itemstack1.stackSize == itemstack.stackSize)
            {
                return null;
            }

            slot.onPickupFromSlot(par1EntityPlayer, itemstack1);
        }

        return itemstack;
    }

 

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.