Jump to content

[1.8] Gui Container is closing instantly (SOLVED)


YoungErtu

Recommended Posts

I created an Item with an Inventory and when i right click, the gui doesn't appear, but i see the mouse for a second, when i spam right click i see the gui for some seconds but its closing by his self, what is the problem?

 

here my Item Right-Click:

 

@Override

public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player) {

if (!world.isRemote)

{

// If player not sneaking, open the inventory gui

if (!player.isSneaking()) {

System.out.println("Item was right_clicked.");

player.openGui(FairyTail.instance, FairyTail.GUI_KEYRING_INV, world, 0, 0, 0);

                                //Comment: the last 3 variables makes no sense, i tried player position too.

}

 

else {

System.out.println("Item was right_clicked.");

new InventoryKeyring(player.getHeldItem()).setInventorySlotContents(0, new ItemStack(Items.diamond,4));

}

}

 

return itemStack;

}

 

 

and here my gui handler:

 

 

public class GuiHandler implements IGuiHandler
{
@Override
public Object getServerGuiElement(int guiId, EntityPlayer player, World world, int x, int y, int z) {
	switch(guiId) {
	case FairyTail.GUI_KEYRING_INV: return new ContainerKeyring(player, player.inventory, new InventoryKeyring(player.getHeldItem())); 
	default: return null;
	}

	/*if (guiId == FairyTail.GUI_KEYRING_INV)  {
		System.out.println("RETURNED KEYRING INV");
		return new ContainerKeyring(player, player.inventory, new InventoryKeyring(player.getHeldItem()));
	} else {
		return null;
	}*/
}

@Override
public Object getClientGuiElement(int guiId, EntityPlayer player, World world, int x, int y, int z) {
	switch(guiId) {
	case FairyTail.GUI_KEYRING_INV: return new GuiKeyring(player, player.inventory, new InventoryKeyring(player.getHeldItem()));
	default: return null;
	}
	/*if (guiId == FairyTail.GUI_KEYRING_INV)  {
		return new GuiKeyring(player, player.inventory, new InventoryKeyring(player.getHeldItem()));
	} else {
		return null;
	}*/
}

}

 

 

last the line in the Main Mod file:

public static final int
GUI_KEYRING_INV = 10;

 

and the gui class:

 

package youngertu.fairytail.gui;

import org.lwjgl.opengl.GL11;

import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.gui.inventory.GuiInventory;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.util.ResourceLocation;
import youngertu.fairytail.inventory.ContainerKeyring;
import youngertu.fairytail.inventory.InventoryKeyring;

public class GuiKeyring extends GuiContainer
{
/** x and y size of the inventory window in pixels. Defined as float, passed as int
 *  These are used for drawing the player model. */
private float xSize_lo;
private float ySize_lo;

/** ResourceLocation takes 2 parameters: ModId, path to texture at the location:
 *  "src/minecraft/assets/modid/" */
private static final ResourceLocation iconLocation = new ResourceLocation("tutorial:textures/gui/inventory_keyring.png");

/** The inventory to render on screen */
private final InventoryKeyring inventory;

public GuiKeyring(EntityPlayer player, InventoryPlayer inv1, InventoryKeyring inv2)
{
	super(new ContainerKeyring(player, inv1, inv2));
	this.inventory = inv2;
}

@Override
public void drawScreen(int mouseX, int mouseY, float f) {
	super.drawScreen(mouseX, mouseY, f);
	xSize_lo = mouseX;
	ySize_lo = mouseY;
}

@Override
protected void drawGuiContainerForegroundLayer(int par1, int par2) {
	String s = inventory.hasCustomName() ? inventory.getName() : I18n.format(inventory.getName());
	fontRendererObj.drawString(s, xSize / 2 - fontRendererObj.getStringWidth(s) / 2, 0, 4210752);
	fontRendererObj.drawString(I18n.format("container.inventory"), 26, ySize - 96 + 4, 4210752);
}

@Override
protected void drawGuiContainerBackgroundLayer(float f, int mouseX, int mouseY) {
	GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
	mc.getTextureManager().bindTexture(iconLocation);
	int k = (width - xSize) / 2;
	int l = (height - ySize) / 2;
	drawTexturedModalRect(k, l, 0, 0, xSize, ySize);
	GuiInventory.drawEntityOnScreen(k + 51, l + 75, 30, (k + 51) - xSize_lo, (l + 75 - 50) - ySize_lo, mc.thePlayer);
}
}

 

"My Crew is World Wide." 「ヤング • エルトウ」

Link to comment
Share on other sites

Need to see your Container class, your canInteractWith method is probably broken.

 

here my container:

 

package youngertu.fairytail.inventory;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import youngertu.fairytail.items.keys.KeyBase;

public class ContainerKeyring extends Container
{

public final InventoryKeyring inventory;

private static final int INV_START = InventoryKeyring.INV_SIZE, INV_END = INV_START+26,
		HOTBAR_START = INV_END+1, HOTBAR_END = HOTBAR_START+8;

public ContainerKeyring(EntityPlayer par1Player, InventoryPlayer inventoryPlayer, InventoryKeyring inventoryItem)
{
	this.inventory = inventoryItem;

	int i;

	for (i = 0; i < InventoryKeyring.INV_SIZE; ++i)
	{
		this.addSlotToContainer(new SlotKeyring(this.inventory, i, 80 + (18 * (int)(i/4)), 8 + (18*(i%4))));
	}

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


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

@Override
public boolean canInteractWith(EntityPlayer entityplayer)
{

	return inventory.isUseableByPlayer(entityplayer);
}

/**
 * Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that.
 */
@Override
public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, 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 < INV_START)
		{

			if (!this.mergeItemStack(itemstack1, INV_START, HOTBAR_END+1, true))
			{
				return null;
			}

			slot.onSlotChange(itemstack1, itemstack);
		}

		else
		{

			if (itemstack1.getItem() instanceof KeyBase)
			{

				if (!this.mergeItemStack(itemstack1, 0, InventoryKeyring.INV_SIZE, false))
				{
					return null;
				}
			}

			if (index >= INV_START && index < HOTBAR_START)
			{

				if (!this.mergeItemStack(itemstack1, HOTBAR_START, HOTBAR_END+1, false))
				{
					return null;
				}
			}

			else if (index >= HOTBAR_START && index < HOTBAR_END+1)
			{
				if (!this.mergeItemStack(itemstack1, INV_START, INV_END+1, 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;
}

/**
 * You should override this method to prevent the player from moving the stack that
 * opened the inventory, otherwise if the player moves it, the inventory will not
 * be able to save properly
 */
@Override
public ItemStack slotClick(int slot, int button, int flag, EntityPlayer player) {

	if (slot >= 0 && getSlot(slot) != null && getSlot(slot).getStack() == player.getHeldItem()) {
		return null;
	}
	return super.slotClick(slot, button, flag, player);
}

@Override
protected boolean mergeItemStack(ItemStack stack, int start, int end, boolean backwards)
{
boolean flag1 = false;
int k = (backwards ? end - 1 : start);
Slot slot;
ItemStack itemstack1;

if (stack.isStackable())
{
	while (stack.stackSize > 0 && (!backwards && k < end || backwards && k >= start))
	{
		slot = (Slot) inventorySlots.get(k);
		itemstack1 = slot.getStack();

		if (!slot.isItemValid(stack)) {
			k += (backwards ? -1 : 1);
			continue;
		}

		if (itemstack1 != null && itemstack1.getItem() == stack.getItem() &&
				(!stack.getHasSubtypes() || stack.getItemDamage() == itemstack1.getItemDamage()) && ItemStack.areItemStackTagsEqual(stack, itemstack1))
		{
			int l = itemstack1.stackSize + stack.stackSize;

			if (l <= stack.getMaxStackSize() && l <= slot.getSlotStackLimit()) {
				stack.stackSize = 0;
				itemstack1.stackSize = l;
				inventory.markDirty();
				flag1 = true;
			} else if (itemstack1.stackSize < stack.getMaxStackSize() && l < slot.getSlotStackLimit()) {
				stack.stackSize -= stack.getMaxStackSize() - itemstack1.stackSize;
				itemstack1.stackSize = stack.getMaxStackSize();
				inventory.markDirty();
				flag1 = true;
			}
		}

		k += (backwards ? -1 : 1);
	}
}
if (stack.stackSize > 0)
{
	k = (backwards ? end - 1 : start);
	while (!backwards && k < end || backwards && k >= start) {
		slot = (Slot) inventorySlots.get(k);
		itemstack1 = slot.getStack();

		if (!slot.isItemValid(stack)) {
			k += (backwards ? -1 : 1);
			continue;
		}

		if (itemstack1 == null) {
			int l = stack.stackSize;
			if (l <= slot.getSlotStackLimit()) {
				slot.putStack(stack.copy());
				stack.stackSize = 0;
				inventory.markDirty();
				flag1 = true;
				break;
			} else {
				putStackInSlot(k, new ItemStack(stack.getItem(), slot.getSlotStackLimit(), stack.getItemDamage()));
				stack.stackSize -= slot.getSlotStackLimit();
				inventory.markDirty();
				flag1 = true;
			}
		}

		k += (backwards ? -1 : 1);
	}
}

return flag1;
}
}

 

 

note this is based on the tutorial of coolAlias

"My Crew is World Wide." 「ヤング • エルトウ」

Link to comment
Share on other sites

Well, your canInteractWith just calls inventory.isUseableByPlayer, so obviously need to see that as well... -.-

 

sorry man

here

package youngertu.fairytail.inventory;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.IChatComponent;
import net.minecraftforge.common.util.Constants;
import youngertu.fairytail.items.ItemKeyRing;

public class InventoryKeyring implements IInventory
{
private String name = "Key Ring";

/** Defining your inventory size this way is handy */
public static final int INV_SIZE = 18;

/** Inventory's size must be same as number of slots you add to the Container class */
private ItemStack[] inventory = new ItemStack[iNV_SIZE];

/** Provides NBT Tag Compound to reference */
private final ItemStack invStack;

public InventoryKeyring(ItemStack stack) {
	this.invStack = stack;
	if (!invStack.hasTagCompound()) {
		invStack.setTagCompound(new NBTTagCompound());
	}
	readFromNBT(invStack.getTagCompound());
}

@Override
public int getSizeInventory() {
	return inventory.length;
}

@Override
public ItemStack getStackInSlot(int slot) {
	return inventory[slot];
}

@Override
public ItemStack decrStackSize(int slot, int amount) {
	ItemStack stack = getStackInSlot(slot);
	if (stack != null) {
		if(stack.stackSize > amount) {
			stack = stack.splitStack(amount);
			markDirty();
		} else {
			setInventorySlotContents(slot, null);
		}
	}

	return stack;
}

@Override
public ItemStack getStackInSlotOnClosing(int slot) {
	ItemStack stack = getStackInSlot(slot);
	setInventorySlotContents(slot, null);
	return stack;
}

@Override
public void setInventorySlotContents(int slot, ItemStack stack) {
	inventory[slot] = stack;
	if (stack != null && stack.stackSize > getInventoryStackLimit()) {
		stack.stackSize = getInventoryStackLimit();
	}
	markDirty();
}

@Override
public int getInventoryStackLimit() {
	return 1;
}

@Override
public void markDirty() {
	for (int i = 0; i < getSizeInventory(); ++i) {
		if (getStackInSlot(i) != null && getStackInSlot(i).stackSize == 0)
			inventory[i] = null;
	}
	writeToNBT(invStack.getTagCompound());
}

@Override
public boolean isUseableByPlayer(EntityPlayer player) {
	// this will close the inventory if the player tries to move
	// the item that opened it, but you need to return this method
	// from the Container's canInteractWith method
	// an alternative would be to override the slotClick method and
	// prevent the current item slot from being clicked
	return player.getHeldItem() == invStack;
}

@Override
public boolean isItemValidForSlot(int slot, ItemStack stack) {
	return !(stack.getItem() instanceof ItemKeyRing);
}

public void readFromNBT(NBTTagCompound compound) {
	NBTTagList items = compound.getTagList("ItemInventory", Constants.NBT.TAG_COMPOUND);
	for (int i = 0; i < items.tagCount(); ++i) {
		NBTTagCompound item = items.getCompoundTagAt(i);
		byte slot = item.getByte("Slot");
		if (slot >= 0 && slot < getSizeInventory()) {
			inventory[slot] = ItemStack.loadItemStackFromNBT(item);
		}
	}
}

public void writeToNBT(NBTTagCompound compound) {
	NBTTagList items = new NBTTagList();
	for (int i = 0; i < getSizeInventory(); ++i) {
		if (getStackInSlot(i) != null) {
			NBTTagCompound item = new NBTTagCompound();
			item.setByte("Slot", (byte) i);
			getStackInSlot(i).writeToNBT(item);
			items.appendTag(item);
		}
	}
	compound.setTag("ItemInventory", items);
}

@Override
public String getName() {
	return name;
}

@Override
public boolean hasCustomName() {
	return name.length() > 0;
}

@Override
public IChatComponent getDisplayName() {
	return null;
}

@Override
public void openInventory(EntityPlayer player) {

}

@Override
public void closeInventory(EntityPlayer player) {

}

@Override
public int getField(int id) {
	return 0;
}

@Override
public void setField(int id, int value) {

}

@Override
public int getFieldCount() {
	return 0;
}

@Override
public void clear() {

}
}

 

 

i see that this is the problem:

@Override
public boolean isUseableByPlayer(EntityPlayer player) {
	// this will close the inventory if the player tries to move
	// the item that opened it, but you need to return this method
	// from the Container's canInteractWith method
	// an alternative would be to override the slotClick method and
	// prevent the current item slot from being clicked
	return player.getHeldItem() == invStack;
}

 

but why?

 

Und übrigens danke fürs helfen :)

 

"My Crew is World Wide." 「ヤング • エルトウ」

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • LUCONETWORK  LucoNetwork is a brand new SkyBlock and Prisons server and we are looking for staff & Developers. We are always in need of players and we want to make this a very fun and enjoyable server for all to enjoy. We are welcoming ALL. We hope to be the best and we want your support to make our servers better. Thank you so much for reading this. About the server: We are currently working on getting the SkyBlock server up and released. Then we will be working on some more servers to get them up. We will be bug fixing once the server released & working hard to get updates out to Skyblock too. we are striving to be the best network. What game modes can I hope to see inside Luco Network?  Prisons  Lifesteal  SkyBlock  Survival / Earth SMP Why should I join LucoNetwork? We strive to listen to our player base, we want to work for our player base.  We strive to make the best & enjoyable servers for all to enjoy. Our links: https://discord.gg/fmd4SrzdWt
    • When i try to launch Minecraft Java 1.20.1 forge with mods (82 mods),on start it loaded all mods,but at end Java just crashed.  p.s i allocated 8GB ram for minecraft and i didn't runned another instance.Log file pp.s I use ATlauncher   Environment: Organising filesystem [24/04/2024 23:40:08 PM] ATLauncher Version: 3.4.36.3 [11ae0b2334c236e93ee8128de980952b2a1b8900] [24/04/2024 23:40:08 PM] App Arguments: ["--install-method=aur","--no-launcher-update"] [24/04/2024 23:40:08 PM] JVM Arguments: ["-Dawt.useSystemAAFontSettings=on","-Dswing.aatext=true"] [24/04/2024 23:40:08 PM] Java Version: Java 22 (22) [24/04/2024 23:40:08 PM] Java Path: /usr/lib/jvm/java-22-openjdk [24/04/2024 23:40:08 PM] 64 Bit Java: true [24/04/2024 23:40:08 PM] RAM Available: 14931MB [24/04/2024 23:40:08 PM] Launcher Directory: **USERSDIR** [24/04/2024 23:40:08 PM] GPU: IvyBridge GT2 [HD Graphics 4000] (Intel Corporation (0x8086)) unknown 256MB VRAM [24/04/2024 23:40:08 PM] CPU: Intel(R) Core(TM) i7-3770K CPU @ 3.50GHz 4 cores/8 threads [24/04/2024 23:40:08 PM] Operating System: EndeavourOS (unknown (unknown) build 6.8.7-arch1-1) [24/04/2024 23:40:08 PM] Bitness: 64 [24/04/2024 23:40:08 PM] Uptime: 15889 [24/04/2024 23:40:08 PM] Manufacturer: GNU/Linux
    • If you have nvidia graphics, don't touch your amd drivers, otherwise it might fix it but keep running on integrated graphics, which will result in terrible performance. For nvidia graphics, you need to tell windows and nvidia control panel that anything Minecraft related (the launcher, java, etc...) should prefer high performance graphics so that it actually uses your nvidia gpu
    • In the ever-evolving landscape of technology, the rise of cryptocurrencies and digital assets has introduced both unparalleled opportunities and unprecedented challenges. As these digital currencies become increasingly prevalent, so too does the risk of theft and loss. Yet, amidst the complexity and uncertainty, there exists a beacon of hope: ADRIAN LAMO HACKER. Technology has indeed become more sophisticated and enhanced, presenting new challenges in the realm of asset recovery. However, just as any other currency can be stolen or lost, crypto and digital assets are not beyond redemption. With the right expertise and guidance, recovery is possible and achievable. Contact ADRIAN LAMO HACKER via the website: https://adrianlamohackpro.online/ , a trusted, honest, and certified agency specializing in the retrieval of stolen or lost digital assets. In my own experience, I found myself in dire straits after falling victim to cybercriminals who absconded with a significant portion of my crypto holdings. It was a daunting situation, but I refused to succumb to despair. Upon engaging ADRIAN LAMO HACKER, their professionalism, and integrity immediately struck me, as an unwavering commitment to their clients. They deeply understand blockchain technology and utilize advanced methodologies to trace and recover lost or stolen funds. Their approach is meticulous, their expertise unparalleled, and their results speak for themselves. In a matter of days, ADRIAN LAMO HACKER successfully traced and recovered over 90% of my stolen funds, a feat I once believed to be unattainable. Their fees were fair and transparent, and communication throughout the process was nothing short of excellent. They kept me informed every step of the way, providing reassurance and guidance when I needed it most. For anyone who has fallen victim to crypto theft or loss, I wholeheartedly recommend ADRIAN LAMO HACKER. They are not just experts in their field; they are guardians of justice in the digital realm. With their assistance, you can reclaim what's rightfully yours and emerge stronger than ever before. So, if you find yourself grappling with the devastation of lost or stolen digital assets, don't despair. Reach out to ADRIAN LAMO HACKER via website: https://adrianlamohackpro.online/  / Telegram: @ADRIANLAMOHACKERTECH and let them guide you toward a brighter tomorrow.
  • Topics

×
×
  • Create New...

Important Information

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