Jump to content

Crafting Table GUI immediately closes on open


Yamajac

Recommended Posts

GuiCraftingBench.java

 

 

public class GuiCraftingBench extends GuiContainer{

private ResourceLocation texture = new ResourceLocation(minetweaks.modid + ":" + "textures/gui/CraftingBench.png");

public GuiCraftingBench(InventoryPlayer invPlayer, World world, int x, int y, int z) {
	super(new ContainerCraftingBench(invPlayer,world, x,y,z));


	this.xSize = 176;
	this.ySize = 166;
}


public void onGuiClosed(){
	super.onGuiClosed();
}

protected void drawGuiContainerForegroundLayer(int i, int j){
	this.fontRendererObj.drawString(StatCollector.translateToLocal("Crafting Bench"), 100, 5, 0x000000);
}

@Override
protected void drawGuiContainerBackgroundLayer(float var1, int var2, int var3) {
	GL11.glColor4f(1F, 1F, 1F, 1F);

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

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

}

 

 

 

ContainerCraftingBench.java

 

 

public class ContainerCraftingBench extends Container {

public InventoryCrafting craftMatrix = new InventoryCrafting(this,3,3);
public IInventory craftResult;
private World worldObj;
private int posX;
private int posY;
private int posZ;


public ContainerCraftingBench(InventoryPlayer invPlayer, World world, int x, int y, int z){
	craftResult = new InventoryCraftResult();
	worldObj = world;
	posX=x;
	posY=y;
	posZ=z;
	this.addSlotToContainer(new SlotCrafting(invPlayer.player, craftMatrix, craftResult,0,131,36));
	for(int i=0; i < 3; i++){
		for(int j=0; j < 3; j++){
			this.addSlotToContainer(new Slot(craftMatrix,j + 1 *5, 4 + j * 18, 3 + i * 18));
		}
	}

	for(int i=0;i<3;i++){
		for(int j=0;j<9;j++){
			this.addSlotToContainer(new Slot(invPlayer,j + i * 9 + 9, 8 + j * 18, 94 + i * 18 ));
		}
	}

	for(int i=0;i<9;i++){
		this.addSlotToContainer(new Slot(invPlayer, i, 8 + i * 18,148));
	}

	onCraftMatrixChanged(craftMatrix);
}


public void onCraftMatrixChanged(IInventory iinventory){
	//craftResult.setInventorySlotContents(0,CraftingManager.getInstance().findMatchingRecipe(craftMatrix,worldObj));
}




@Override
public boolean canInteractWith(EntityPlayer par1EntityPlayer)
    {
	System.out.println("test");
        return this.worldObj.getBlock(this.posX, this.posY, this.posZ) != minetweaks.blockCraftingBench ? false : true;//par1EntityPlayer.getDistanceSq((double)this.posX + 0.5D, (double)this.posY + 0.5D, (double)this.posZ + 0.5D) <= 64.0D;
    }

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 == 0)
            {
                if (!this.mergeItemStack(itemstack1, 10, 46, true))
                {
                    return null;
                }

                slot.onSlotChange(itemstack1, itemstack);
            }
            else if (par2 >= 10 && par2 < 37)
            {
                if (!this.mergeItemStack(itemstack1, 37, 46, false))
                {
                    return null;
                }
            }
            else if (par2 >= 37 && par2 < 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(par1EntityPlayer, itemstack1);
        }

        return itemstack;
    }

public void onContainerClosed(EntityPlayer par1EntityPlayer)
    {
        super.onContainerClosed(par1EntityPlayer);

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

                if (itemstack != null)
                {
                    par1EntityPlayer.dropPlayerItemWithRandomChoice(itemstack, false);
                }
            }
        }*/
    }

}

 

 

CraftingBench.java

 

public class CraftingBench extends BlockContainer {


public CraftingBench(Material material) {
	super(material);

	this.setCreativeTab(getCreativeTabToDisplayOn().tabMaterials);

}

public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int q, float a, float b, float c){

	if(!player.isSneaking()){
		player.openGui(minetweaks.instance, minetweaks.guiIDCraftingBench, world, x, y, z);
		return true;
	}else{
		return false;
	}
}

public int getRenderType(){
	return -1;
}

public boolean isOpaqueCube(){
	return false;
}

public boolean renderAsNormalBlock(){
	return false;
}

@Override
public TileEntity createNewTileEntity(World var1, int var2) {
	return new TileEntityCraftingBench();
}

@SideOnly(Side.CLIENT)
public void registerBlockIcons(IIconRegister iconRegister){
	this.blockIcon = iconRegister.registerIcon(minetweaks.modid + ":" + this.getUnlocalizedName().substring(5));
}

}

 

 

GuiHandler.java

 

public class GuiHandler implements IGuiHandler{

@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
	if(ID == minetweaks.guiIDCraftingBench){
		return ID==minetweaks.guiIDCraftingBench && world.getBlock(x,y,z) == minetweaks.blockCraftingBench ? new ContainerWorkbench(player.inventory, world, x,y,z) : null;
	}
	return null;
}

@Override
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
	if(ID == minetweaks.guiIDCraftingBench){
		return ID==minetweaks.guiIDCraftingBench && world.getBlock(x,y,z) == minetweaks.blockCraftingBench ? new GuiCraftingBench(player.inventory, world, x,y,z) : null;
	}
	return null;
}


}

 

 

TileEntityCraftingBench.java

 

public class TileEntityCraftingBench extends TileEntity {

}

 

 

minetweaks.java

 

@Mod(modid = minetweaks.modid, version = minetweaks.version)
public class minetweaks {

public static final String modid = "minetweaks";
public static final String version = "Alpha v0.1";

@Instance(modid)
public static minetweaks instance;

// Utilities
public static Block blockCraftingBench;
public static final int guiIDCraftingBench = 1;

// Redstone
public static BlockPressurePlate obsidianPressurePlate;

// Keybindings
public static KeyBinding ctrl;

// Enchantments
public static Enchantment Haste;

@SidedProxy(clientSide = "net.minetweaks.mod.proxy.ClientProxy",serverSide = "net.minetweaks.mod.proxy.CommonProxy")
public static CommonProxy proxy;

@EventHandler
public void PreInit(FMLPreInitializationEvent preEvent){

	// Utilities
	blockCraftingBench = new CraftingBench(Material.wood).setBlockName("CraftingBench");
	GameRegistry.registerBlock(blockCraftingBench,"CraftingBench");

	// Redstone
	obsidianPressurePlate = (BlockPressurePlate) new obsidianPressurePlate("obsidian", Material.rock, BlockPressurePlate.Sensitivity.players).setBlockName("ObsidianPressurePlate");
	GameRegistry.registerBlock(obsidianPressurePlate,"ObsidianPressurePlate");

	// Keybindings
	ctrl = new KeyBinding("key.ctrl",Keyboard.KEY_LCONTROL,"key.categories.minetweaks");
	ClientRegistry.registerKeyBinding(ctrl);

	// Enchantments
	Haste = new enchantSpeed(64,7);

	// Renderers
	proxy.registerRenders();

	MinecraftForge.EVENT_BUS.register(new eventHooks());
	FMLCommonHandler.instance().bus().register(new eventHooks());
}

@EventHandler
public void init(FMLInitializationEvent event){
	NetworkRegistry.INSTANCE.registerGuiHandler(this, new GuiHandler());

}

@EventHandler
public void PostInit(FMLPostInitializationEvent postEvent){

}




}

 

 

 

The spoilers aren't revealing anything in the preview, so it may also do so when I post this. Can't figure out what's wrong with 'em though.

 

 

As for my problem, I can open up my GUI just fine, but it just flashes open and then closes immediately. I'm not quite sure what's wrong here, and googling it was giving me nothing.

Link to comment
Share on other sites

1. Why aren't you using a switch in your GuiHandler.

2. Please follow java conventions. ClassNamesShouldBeCamelCased.

3. You're trying to open the wrong container.

4. Why are you checking the block in the GuiHandler you already do that in the container so no point in checking twice as the block can't change and crafting tables don't need TileEntities which can change compared to a block which would be removed if it wasn't the same

If I helped please press the Thank You button.

Link to comment
Share on other sites

1. IDK, didn't think of using a switch, I suppose. I'm gonna go change that.

2. "ClassNamesShouldBeCamelCased" isn't camel cased, and no, they shouldn't be. They should be easily readable by everybody who's using them.

3. Ahh.. I new it was going to be some super simple thing I missed. Of course it's gonna close immediately when I'm opening a container that's telling it to close immediately.

4.Vanilla crafting benches don't require tile entities. Consistent inventories do require tile entities.

Link to comment
Share on other sites

About 2: Here's a quoute from Wikipedia:

CamelCase (camel case, camel caps or medial capitals) is the practice of writing compound words or phrases such that each next word or abbreviation begins with a capital letter.
So why areyou saying that isn't CamelCased? That is defenitly CamelCased. Please use those conventions!

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

And that's wrong. camelCase is when the first letter is lowercase, and all the rest are upper.

 

So his "ClassNamesShouldBeCamelCased" should've been "classNamesShouldBeCamelCase".

 

THAT is proper camelCase. What he displayed is a variation of camelCase.

 

Which just further proves my point. There is no single one proper notation. It's dependant entirely on what the people using it agree on and can read properly.

 

Personally, camelCase isn't my preference. Hungarian Notation is by far my favourite notation, and it's the one I use if I'm going to be doing any serious programming. If I'm just throwing something together for myself, I use whatever I happen to use. Furthermore, the only class name that didn't use his variation of camelCase was my main class, which means that he was nitpicking at a mere 1/6th of my class names for a reason that doesn't exist.

Link to comment
Share on other sites

I'll give up. I guess you don't want to follow the official Java Conventions...

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

There is no official notation for Java. There never has been, and there never will be. To even begin to think there could be, is preposterous.

 

And to prove my point, I will use the unofficial website you supplied me with.

 

"To illustrate the point it's worth mentioning that most software companies will have a document that outlines the naming conventions they want their programmers to follow."

 

This right here proves well enough that there are other naming conventions that will be used in different places by different groups. If there were to be only one single proper naming convention, documents would be unnecessary as using the official naming conventions would be part of being a good programmer - which is a requirement for having a job as a programmer.

 

But there isn't a single proper naming convention. It doesn't exist. It depends entirely on the programmer or the group of programmers to decide what notation is going to be best for their program.

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.