- 2
- 0
Здравствуйте, столкнулся с проблемой "перепрыгивания" предметов в кастомном инвентаре для рюкзака.
В некоторых слотах при нажатии левой кнопки мыши предмет вываливается.
В слотах рюкзака предметы не сохраняются.
С инвентарями у меня тяжко...
Видео для наглядности : http://youtu.be/SpEMv-ti1Cc
Container рюкзака
Инвентарь рюкзака
В некоторых слотах при нажатии левой кнопки мыши предмет вываливается.
В слотах рюкзака предметы не сохраняются.
С инвентарями у меня тяжко...
Видео для наглядности : http://youtu.be/SpEMv-ti1Cc
Container рюкзака
Код:
package mods.provoda.gui.backpack;
import mods.provoda.ModBase;
import mods.provoda.inventory.SlotAmmo;
import mods.provoda.inventory.SlotCustom;
import mods.provoda.inventory.SlotGrenade;
import mods.provoda.inventory.SlotLamp;
import mods.provoda.inventory.SlotMed;
import mods.provoda.inventory.SlotPower;
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;
public class ContainerBackPack extends Container
{
public final IInventory inventory;
public ContainerBackPack(EntityPlayer var1, InventoryPlayer var2, IInventory var3)
{
this.inventory = var3;
int j;
int k;
if(var1.inventory.getCurrentItem() != null)
{
if(var1.inventory.getCurrentItem().getItem() == ModBase.BackPackSmall)
{
for (j = 0; j < 5; ++j)
{
this.addSlotToContainer(new SlotBackPack(var3,j, 44+j*18, 103));
}
}
else if(var1.inventory.getCurrentItem().getItem() == ModBase.BackPackMedium)
{
for (j = 0; j < 5; ++j)
{
this.addSlotToContainer(new SlotBackPack(var3,j, 100, 60));
}
}
else if(var1.inventory.getCurrentItem().getItem() == ModBase.BackPackLarge)
{
for (j = 0; j < 5; ++j)
{
this.addSlotToContainer(new SlotBackPack(this.inventory,j, 140, 5));
}
}
}
//9 стандартных слотов
this.addSlotToContainer(new Slot(var2, 0, 62, 148+27));
this.addSlotToContainer(new SlotCustom(var2, 1, 80, 148+27));
this.addSlotToContainer(new SlotCustom(var2, 2, 98, 148+27));
this.addSlotToContainer(new SlotCustom(var2, 3, 62, 130+27));
this.addSlotToContainer(new SlotCustom(var2, 4, 80, 130+27));
this.addSlotToContainer(new SlotCustom(var2, 5, 98, 130+27));
this.addSlotToContainer(new SlotCustom(var2, 6, 62, 112+27));
this.addSlotToContainer(new SlotCustom(var2, 7, 80, 112+27));
this.addSlotToContainer(new SlotCustom(var2, 8, 98, 112+27));
/*Слоты стандартного инвентаря, которые не используются
* Нужны для того, что не было ошибки IndexOfBoundsException: Index: XX Size: XX
*/
for (int var4 = 1; var4 < 4; ++var4)
{
for ( int var5 = 2; var5 < 11; ++var5)
{
this.addSlotToContainer(new Slot(var2, var5 + (var4 + 1) * 9, 8 + var5 * 18, 84 + var4 * 18+500));
}
}
}
public boolean canInteractWith(EntityPlayer var1)
{
return true;
}
}
Код:
package mods.provoda.gui.backpack;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemCoal;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
public class InventoryBackPack implements IInventory
{
public static InventoryBackPack theMinecraft;
private String name = "Inventory Item";
private final ItemStack stack;
ItemStack[] inventory = new ItemStack[15];
public InventoryBackPack(ItemStack var1)
{
theMinecraft = this;
this.stack = var1;
if (!this.stack.hasTagCompound())
{
this.stack.setTagCompound(new NBTTagCompound());
}
this.readFromNBT(this.stack.getTagCompound());
}
/**
* Returns the number of slots in the inventory.
*/
public int getSizeInventory()
{
return this.inventory.length;
}
/**
* Returns the stack in slot i
*/
public ItemStack getStackInSlot(int var1)
{
return this.inventory[var1];
}
/**
* Removes from an inventory slot (first arg) up to a specified number (second arg) of items and returns them in a
* new stack.
*/
public ItemStack decrStackSize(int var1, int var2)
{
ItemStack var3 = this.getStackInSlot(var1);
if (var3 != null)
{
if (var3.stackSize > var2)
{
var3 = var3.splitStack(var2);
this.onInventoryChanged();
}
else
{
this.setInventorySlotContents(var1, (ItemStack)null);
}
}
return var3;
}
/**
* When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem -
* like when you close a workbench GUI.
*/
public ItemStack getStackInSlotOnClosing(int var1)
{
ItemStack var2 = this.getStackInSlot(var1);
this.setInventorySlotContents(var1, (ItemStack)null);
return var2;
}
/**
* Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
*/
public void setInventorySlotContents(int var1, ItemStack var2)
{
this.inventory[var1] = var2;
if (var2 != null && var2.stackSize > this.getInventoryStackLimit())
{
var2.stackSize = this.getInventoryStackLimit();
}
this.onInventoryChanged();
}
/**
* Returns the name of the inventory.
*/
public String getInvName()
{
return this.name;
}
/**
* If this returns false, the inventory name will be used as an unlocalized name, and translated into the player's
* language. Otherwise it will be used directly.
*/
public boolean isInvNameLocalized()
{
return this.name.length() > 0;
}
/**
* Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended. *Isn't
* this more of a set than a get?*
*/
public int getInventoryStackLimit()
{
return 64;
}
/**
* Called when an the contents of an Inventory change, usually
*/
public void onInventoryChanged()
{
for (int var1 = 0; var1 < this.getSizeInventory(); ++var1)
{
if (this.getStackInSlot(var1) != null && this.getStackInSlot(var1).stackSize == 0)
{
this.setInventorySlotContents(var1, (ItemStack)null);
}
}
this.writeToNBT(this.stack.getTagCompound());
}
/**
* Do not make give this method the name canInteractWith because it clashes with Container
*/
public boolean isUseableByPlayer(EntityPlayer var1)
{
return true;
}
public void openChest() {}
public void closeChest() {}
public void readFromNBT(NBTTagCompound var1)
{
NBTTagList var2 = var1.getTagList("ItemInventory");
for (int var3 = 0; var3 < var2.tagCount(); ++var3)
{
NBTTagCompound var4 = (NBTTagCompound)var2.tagAt(var3);
int var5 = var4.getInteger("Slot");
if (var5 >= 0 && var5 < this.getSizeInventory())
{
this.setInventorySlotContents(var5, ItemStack.loadItemStackFromNBT(var4));
}
}
}
public void writeToNBT(NBTTagCompound var1)
{
NBTTagList var2 = new NBTTagList();
for (int var3 = 0; var3 < this.getSizeInventory(); ++var3)
{
if (this.getStackInSlot(var3) != null)
{
NBTTagCompound var4 = new NBTTagCompound();
var4.setInteger("Slot", var3);
this.getStackInSlot(var3).writeToNBT(var4);
var2.appendTag(var4);
}
}
var1.setTag("ItemInventory", var2);
}
/**
* Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot.
*/
public boolean isStackValidForSlot(int var1, ItemStack var2)
{
return !(var2.getItem() instanceof ItemCoal);
}
}