Jump to content

Recommended Posts

Posted

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." 「ヤング • エルトウ」

Posted

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." 「ヤング • エルトウ」

Posted

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." 「ヤング • エルトウ」

Posted

What the hell is invStack? Please dont tell me that you are comparing an itemsack and an item and expect it to be equals

 

getHeldItem() returns an Itemstack not a Item..

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

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.