Jump to content

[1.7.10] IInventory openInventory() and closeInventory() not working.


Recommended Posts

Posted

It seems that the methods openInventory() and closeInventory() are not working. I have made it print when i open the inventory but it doesnt print anything....

 

The IInventory class:

 

 

package com.manslaughter777.moondimension.inventory;

import com.manslaughter777.moondimension.Main;
import com.manslaughter777.moondimension.items.ItemToolUpgrade;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemPickaxe;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.common.util.Constants;

public class InventoryTool implements IInventory {
private String name;

public static boolean check;

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

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

/** Provides NBT Tag Compound to reference */
public final ItemStack invItem;

/**
 * @param itemstack
 *            - the ItemStack to which this inventory belongs
 */
public InventoryTool(ItemStack stack) {
	this.invItem = stack;

	this.name = stack.getDisplayName();

	if (!stack.hasTagCompound()) {
		stack.setTagCompound(new NBTTagCompound());
	}

	readFromNBT(stack.getTagCompound());
}

public void checkContents() {
	if(this.getStackInSlot(0) != null && this.getStackInSlot(0).getItem() == Main.autoRepair) {
		check = true;
	} else check = false;

	System.out.println("YO");
}

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

@Override
public ItemStack getStackInSlot(int slot) {
	return slots[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);
	if (stack != null) {
		setInventorySlotContents(slot, null);
	}
	return stack;
}

@Override
public void setInventorySlotContents(int slot, ItemStack itemstack) {
	this.slots[slot] = itemstack;

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


	markDirty();
}

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

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

@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)
			slots[i] = null;
	}

	writeToNBT(invItem.getTagCompound());
}

@Override
public boolean isUseableByPlayer(EntityPlayer player) {

	return player.getHeldItem() == invItem;
}

@Override
public void openInventory() {
	checkContents();

	System.out.println("YEAH");
}

@Override
public void closeInventory() {
	checkContents();
}

/**
 * This method doesn't seem to do what it claims to do, as items can still
 * be left-clicked and placed in the inventory even when this returns false
 */
@Override
public boolean isItemValidForSlot(int slot, ItemStack itemstack) {

	return itemstack.getItem() instanceof ItemToolUpgrade;
}

/**
 * A custom method to read our inventory from an ItemStack's NBT compound
 */
public void readFromNBT(NBTTagCompound compound) {
	NBTTagList items = compound.getTagList("ItemInventory", Constants.NBT.TAG_COMPOUND);

	for (int i = 0; i < items.tagCount(); ++i) {
		NBTTagCompound item = (NBTTagCompound) items.getCompoundTagAt(i);
		byte slot = item.getByte("Slot");

		if (slot >= 0 && slot < getSizeInventory()) {
			slots[slot] = ItemStack.loadItemStackFromNBT(item);
		}
	}
}

/**
 * A custom method to write our inventory to an ItemStack's NBT compound
 */
public void writeToNBT(NBTTagCompound tagcompound) {
	NBTTagList items = new NBTTagList();

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

		if (getStackInSlot(i) != null) {

			NBTTagCompound item = new NBTTagCompound();
			item.setInteger("Slot", i);

			getStackInSlot(i).writeToNBT(item);

			items.appendTag(item);
		}
	}

	tagcompound.setTag("ItemInventory", items);
}
}

 

 

Posted

It wouldn't be called automatically unless you call it yourself.

 

So.. What is the purpose of the InventoryTool class? If it is from some container,

you should call openInventory/closeInventory there.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Posted

It wouldn't be called automatically unless you call it yourself.

 

So.. What is the purpose of the InventoryTool class? If it is from some container,

you should call openInventory/closeInventory there.

Yes it is from a container. My mistake, thought is was called automatically :(

Thnx

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.