I have been trying to implement my own custom inventory, and lets just say the results aren't very satisfactory. I am displaying the inventory and everything is there correctly. Then I try and move an item. Nothing happens. I have all the flags (I think) that I need to call saying that the player can interact with the inventory.
So without further ka fluffle.
GuiDendrikBeltInventory:
package com.kwibble.dendri.client.gui.inventory;
import com.kwibble.dendri.ModuleDendri;
import com.kwibble.dendri.inventory.ContainerDendrikBeltInventory;
import com.kwibble.dendri.entity.dendri.EntityDendri;
import com.kwibble.dendri.entity.player.PlayerInformation;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;
public class GuiDendrikBeltInventory extends GuiContainer
{
/**
* x size of the inventory window in pixels. Defined as float, passed as int
*/
private float xSizeFloat;
/**
* y size of the inventory window in pixels. Defined as float, passed as int.
*/
private float ySizeFloat;
private ResourceLocation tab_main, tab_dendri;
private PlayerInformation playerinfo;
public GuiDendrikBeltInventory(EntityPlayer player, PlayerInformation playerinfo)
{
super(new ContainerDendrikBeltInventory(player.inventory, player));
this.allowUserInput = true;
this.playerinfo = playerinfo;
this.tab_main = new ResourceLocation(ModuleDendri.MODID, "textures/gui/dendrikbeltinventory/tab_main.png");
this.tab_dendri = new ResourceLocation(ModuleDendri.MODID, "textures/gui/dendrikbeltinventory/tab_dendri.png");
}
@Override
public void initGui()
{
this.buttonList.clear();
super.initGui();
}
@Override
public void drawScreen(int par1, int par2, float par3)
{
super.drawScreen(par1, par2, par3);
this.xSizeFloat = (float)par1;
this.ySizeFloat = (float)par2;
}
@Override
protected void drawGuiContainerBackgroundLayer(float p_146976_1_, int p_146976_2_, int p_146976_3_)
{
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
this.mc.getTextureManager().bindTexture(this.tab_main);
int k = this.guiLeft;
int l = this.guiTop;
this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize);
func_147046_a(k + 34, l + 75, 30, (float)(k + 34) - this.xSizeFloat, (float)(l + 75 - 50) - this.ySizeFloat, this.mc.thePlayer);
}
public static void func_147046_a(int p_147046_0_, int p_147046_1_, int p_147046_2_, float p_147046_3_, float p_147046_4_, EntityLivingBase p_147046_5_)
{
GL11.glEnable(GL11.GL_COLOR_MATERIAL);
GL11.glPushMatrix();
GL11.glTranslatef((float)p_147046_0_, (float)p_147046_1_, 50.0F);
GL11.glScalef((float)(-p_147046_2_), (float)p_147046_2_, (float)p_147046_2_);
GL11.glRotatef(180.0F, 0.0F, 0.0F, 1.0F);
float f2 = p_147046_5_.renderYawOffset;
float f3 = p_147046_5_.rotationYaw;
float f4 = p_147046_5_.rotationPitch;
float f5 = p_147046_5_.prevRotationYawHead;
float f6 = p_147046_5_.rotationYawHead;
GL11.glRotatef(135.0F, 0.0F, 1.0F, 0.0F);
RenderHelper.enableStandardItemLighting();
GL11.glRotatef(-135.0F, 0.0F, 1.0F, 0.0F);
GL11.glRotatef(-((float)Math.atan((double)(p_147046_4_ / 40.0F))) * 20.0F, 1.0F, 0.0F, 0.0F);
p_147046_5_.renderYawOffset = (float)Math.atan((double)(p_147046_3_ / 40.0F)) * 20.0F;
p_147046_5_.rotationYaw = (float)Math.atan((double)(p_147046_3_ / 40.0F)) * 40.0F;
p_147046_5_.rotationPitch = -((float)Math.atan((double)(p_147046_4_ / 40.0F))) * 20.0F;
p_147046_5_.rotationYawHead = p_147046_5_.rotationYaw;
p_147046_5_.prevRotationYawHead = p_147046_5_.rotationYaw;
GL11.glTranslatef(0.0F, p_147046_5_.yOffset, 0.0F);
RenderManager.instance.playerViewY = 180.0F;
RenderManager.instance.renderEntityWithPosYaw(p_147046_5_, 0.0D, 0.0D, 0.0D, 0.0F, 1.0F);
p_147046_5_.renderYawOffset = f2;
p_147046_5_.rotationYaw = f3;
p_147046_5_.rotationPitch = f4;
p_147046_5_.prevRotationYawHead = f5;
p_147046_5_.rotationYawHead = f6;
GL11.glPopMatrix();
RenderHelper.disableStandardItemLighting();
GL11.glDisable(GL12.GL_RESCALE_NORMAL);
OpenGlHelper.setActiveTexture(OpenGlHelper.lightmapTexUnit);
GL11.glDisable(GL11.GL_TEXTURE_2D);
OpenGlHelper.setActiveTexture(OpenGlHelper.defaultTexUnit);
}
}
ContainerDendrikBeltInventory:
package com.kwibble.dendri.inventory;
import com.kwibble.dendri.entity.player.PlayerInformation;
import com.kwibble.dendri.network.DendriNetwork;
import com.kwibble.dendri.network.message.MessageClientPlayerInfo;
import com.kwibble.dendri.network.message.MessageServerRequestPlayerInfo;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemArmor;
import net.minecraft.item.ItemStack;
/**
* Created by Kwibble on 25/06/14.
*/
public class ContainerDendrikBeltInventory extends Container
{
private static final int INV_START = 3, INV_END = INV_START+26, HOTBAR_START = INV_END+1, HOTBAR_END = HOTBAR_START+8;
public ContainerDendrikBeltInventory(final InventoryPlayer inventoryPlayer, final EntityPlayer player)
{
PlayerInformation playerinfo = PlayerInformation.forPlayer(player);
if (!player.worldObj.isRemote)
DendriNetwork.dendriNetwork.sendTo(new MessageClientPlayerInfo(playerinfo), ( EntityPlayerMP ) player);
int i;
int j;
this.addSlotToContainer(new SlotDendrikBelt(playerinfo, 0, 62, );
for (i = 0; i < 4; ++i)
{
int x = 27;
int y = 0;
if (i > 0)
{
x = 28;
y = 1;
}
this.addSlotToContainer(new SlotMistBottle(playerinfo.getInventoryDendrikBelt(), 1 + i, 68 + (i * x) - y, 45));
}
for (i = 0; i < 3; ++i)
{
for (j = 0; j < 9; ++j)
{
this.addSlotToContainer(new Slot(inventoryPlayer, j + ( i + 1 ) * 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 player)
{
return true;
}
/**
* Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that.
*/
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 < INV_START)
{
if (!this.mergeItemStack(itemstack1, INV_START, HOTBAR_END + 1, true))
{
return null;
}
slot.onSlotChange(itemstack1, itemstack);
}
else
{
if (par2 >= INV_START && par2 < HOTBAR_START)
{
if (!this.mergeItemStack(itemstack1, HOTBAR_START, HOTBAR_END + 1, false))
{
return null;
}
}
else if (par2 >= HOTBAR_START && par2 < 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;
}
}
InventoryDendrikBelt:
package com.kwibble.dendri.inventory;
import com.kwibble.dendri.item.ItemDendrikBelt;
import com.kwibble.dendri.network.DendriNetwork;
import com.kwibble.dendri.network.message.MessageServerRequestPlayerInfo;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
/**
* Created by Kwibble on 25/06/14.
*/
public class InventoryDendrikBelt implements IInventory
{
public ItemStack[] mainInventory = new ItemStack[1];
public void setCurrentDendrikBelt(ItemStack par1ItemStack)
{
this.mainInventory[0] = par1ItemStack;
}
public Item getCurrentDendrikBelt()
{
return this.mainInventory[0] != null ? this.mainInventory[0].getItem() : new ItemStack(Blocks.stone).getItem();
}
/**
* Returns the stack in slot i
*
* @param slot
*/
@Override
public ItemStack getStackInSlot(int slot) {
if (slot > 0) return null;
return this.mainInventory[slot];
}
/**
* Removes from an inventory slot (first arg) up to a specified number (second arg) of items and returns them in a
* new stack.
*
* @param slot
* @param amount
*/
@Override
public ItemStack decrStackSize(int slot, int amount) {
ItemStack stack = getStackInSlot(slot);
if (stack != null){
if (stack.stackSize > amount){
stack = stack.splitStack(amount);
if (stack.stackSize == 0){
setInventorySlotContents(slot, null);
}
} else {
setInventorySlotContents(slot, null);
}
this.markDirty();
}
return stack;
}
/**
* 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.
*
* @param par1
*/
@Override
public ItemStack getStackInSlotOnClosing(int par1)
{
return null;
}
/**
* Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
*
* @param slotID
* @param itemstack
*/
@Override
public void setInventorySlotContents(int slotID, ItemStack itemstack) {
this.mainInventory[slotID] = itemstack;
if (itemstack != null && itemstack.stackSize > this.getInventoryStackLimit()){
itemstack.stackSize = this.getInventoryStackLimit();
}
this.markDirty();
}
/**
* Returns the name of the inventory
*/
@Override
public String getInventoryName()
{
return "dendrikbeltinventory";
}
/**
* Returns if the inventory is named
*/
@Override
public boolean hasCustomInventoryName()
{
return true;
}
/**
* Returns the maximum stack size for a inventory slot.
*/
@Override
public int getInventoryStackLimit()
{
return 1;
}
/**
* For tile entities, ensures the chunk containing the tile entity is saved to disk later - the game won't think it
* hasn't changed and skip it.
*/
@Override
public void markDirty()
{
// Something is supposed to go here...
}
/**
* Do not make give this method the name canInteractWith because it clashes with Container
*
* @param par1EntityPlayer
*/
@Override
public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer)
{
return true;
}
@Override
public void openInventory()
{}
@Override
public void closeInventory()
{}
/**
* Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot.
*
* @param slotID
* @param par2ItemStack
*/
@Override
public boolean isItemValidForSlot(int slotID, ItemStack par2ItemStack)
{
return (slotID == 0 && par2ItemStack.getItem() instanceof ItemDendrikBelt);
}
/**
* Returns the number of slots in the inventory.
*/
@Override
public int getSizeInventory()
{
return this.mainInventory.length;
}
public void writeToNBT(NBTTagCompound nbt)
{
Item item = this.getCurrentDendrikBelt();
if (item instanceof ItemDendrikBelt)
{
((ItemDendrikBelt) this.getCurrentDendrikBelt()).saveDendrikBeltStorage(nbt);
}
}
public void readFromNBT(NBTTagCompound nbt)
{
if (this.getCurrentDendrikBelt() instanceof ItemDendrikBelt)
((ItemDendrikBelt) this.getCurrentDendrikBelt()).loadDendrikBeltStorage(nbt);
}
}
SlotDendrikBelt:
package com.kwibble.dendri.inventory;
import com.kwibble.dendri.ModuleDendri;
import com.kwibble.dendri.entity.player.PlayerInformation;
import com.kwibble.dendri.item.ItemDendrikBelt;
import com.kwibble.dendri.network.DendriNetwork;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IIcon;
import net.minecraft.util.ResourceLocation;
/**
* Created by Kwibble on 25/06/14.
*/
public class SlotDendrikBelt extends Slot
{
private PlayerInformation playerinfo;
private final ResourceLocation backgroundIcon = new ResourceLocation(ModuleDendri.MODID, "textures/items/master_dendrik_belt.png");
public SlotDendrikBelt(PlayerInformation playerinfo, int par2, int par3, int par4)
{
super(playerinfo.getInventoryDendrikBelt(), par2, par3, par4);
this.inventory.setInventorySlotContents(0, new ItemStack(playerinfo.getCurrentDendrikBelt()));
this.playerinfo = playerinfo;
}
/**
* Helper method to put a stack in the slot.
*
* @param par1ItemStack
*/
@Override
public void putStack(ItemStack par1ItemStack)
{
super.putStack(par1ItemStack);
}
/**
* Check if the stack is a valid item for this slot. Always true beside for the armor slots.
*
* @param par1ItemStack
*/
@Override
public boolean isItemValid(ItemStack par1ItemStack)
{
return (par1ItemStack.getItem() instanceof ItemDendrikBelt);
}
}
SlotMistBottle:
package com.kwibble.dendri.inventory;
import com.kwibble.dendri.ModuleDendri;
import com.kwibble.dendri.item.ItemMistBottle;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
/**
* Created by Kwibble on 25/06/14.
*/
public class SlotMistBottle extends Slot
{
public SlotMistBottle(InventoryDendrikBelt inventoryDendrikBelt, int par2, int par3, int par4)
{
super(inventoryDendrikBelt, par2, par3, par4);
this.setBackgroundIconTexture(new ResourceLocation(ModuleDendri.MODID, "textures/items/master_dendrik_belt.png"));
}
/**
* Helper method to put a stack in the slot.
*
* @param par1ItemStack
*/
@Override
public void putStack(ItemStack par1ItemStack)
{
super.putStack(par1ItemStack);
}
/**
* Check if the stack is a valid item for this slot. Always true beside for the armor slots.
*
* @param par1ItemStack
*/
@Override
public boolean isItemValid(ItemStack par1ItemStack)
{
return (par1ItemStack.getItem() instanceof ItemMistBottle);
}
}
And if you need where I am saving my inventory... Here it is in my PlayerInformation (IExtendedEntityProperties):
package com.kwibble.dendri.entity.player;
import com.kwibble.dendri.ModuleDendri;
import com.kwibble.dendri.inventory.InventoryDendrikBelt;
import com.kwibble.dendri.item.ItemDendrikBelt;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.World;
import net.minecraftforge.common.IExtendedEntityProperties;
public class PlayerInformation implements IExtendedEntityProperties
{
public static final String IDENTIFIER = ModuleDendri.MODID + "_playerProperties";
private static final String SHOULD_DISPLAY_OVERLAY_IDENTIFIER = IDENTIFIER + "_shouldDisplayOverlay";
private static final String IS_OVERLAY_MINIMIZED_IDENTIFIER = IDENTIFIER + "_isOverlayMinimized";
private static final String CURRENT_DENDRIK_BELT_IDENTIFIER = IDENTIFIER + "_currentDendrikbelt";
public static PlayerInformation forPlayer(Entity player)
{
return (PlayerInformation) player.getExtendedProperties(IDENTIFIER);
}
private EntityPlayer player;
private boolean isOverlayMinimized;
private boolean shouldDisplayOverlay;
private InventoryDendrikBelt inventoryDendrikBelt = new InventoryDendrikBelt();
public PlayerInformation(EntityPlayer player)
{
this.player = player;
}
@Override
public void init(Entity entity, World world)
{
}
@Override
public void saveNBTData(NBTTagCompound nbt)
{
if (this.getCurrentDendrikBelt() == null)
{
this.setShouldDisplayOverlay(false);
}
NBTTagCompound compound = new NBTTagCompound();
System.out.println("Saving NBT Data:");
compound.setBoolean(IS_OVERLAY_MINIMIZED_IDENTIFIER, this.isOverlayMinimized);
System.out.println("Saved isOverlayMinimized with value of: " + this.isOverlayMinimized);
compound.setBoolean(SHOULD_DISPLAY_OVERLAY_IDENTIFIER, this.shouldDisplayOverlay);
System.out.println("Saved shouldDisplayOverlay with value of: " + this.shouldDisplayOverlay);
this.inventoryDendrikBelt.writeToNBT(compound);
System.out.println("Saved currentDendrikBelt with value of: " + this.getCurrentDendrikBelt());
System.out.println("NBT Data Saved");
nbt.setTag(IDENTIFIER, compound);
}
@Override
public void loadNBTData(NBTTagCompound nbt)
{
NBTTagCompound compound = nbt.getCompoundTag(IDENTIFIER);
System.out.println("Loading NBT Data:");
this.isOverlayMinimized = compound.getBoolean(IS_OVERLAY_MINIMIZED_IDENTIFIER);
System.out.println("Loaded isOverlayMinimized with value of: " + this.isOverlayMinimized);
this.shouldDisplayOverlay = compound.getBoolean(SHOULD_DISPLAY_OVERLAY_IDENTIFIER);
System.out.println("Loaded shouldDisplayOverlay with value of: " + this.shouldDisplayOverlay);
this.inventoryDendrikBelt.readFromNBT(compound);
System.out.println("Loaded currentDendrikBelt with value of: " + this.getCurrentDendrikBelt());
System.out.println("Loaded NBT Data");
if (this.getCurrentDendrikBelt() == null)
{
this.setShouldDisplayOverlay(false);
}
}
public void setCurrentDendrikBelt(Item belt)
{
if (belt instanceof ItemDendrikBelt)
this.inventoryDendrikBelt.setCurrentDendrikBelt(new ItemStack(belt));
else
System.out.println("A Dendrik Belt was not passed in");
}
public Item getCurrentDendrikBelt()
{
return this.inventoryDendrikBelt.getCurrentDendrikBelt();
}
public void setIsOverlayMinimized(boolean isOverlayMinimized)
{
this.isOverlayMinimized = isOverlayMinimized;
}
public boolean getIsOverlayMinimized()
{
return this.isOverlayMinimized;
}
public void setShouldDisplayOverlay(boolean shouldDisplayOverlay)
{
this.shouldDisplayOverlay = shouldDisplayOverlay;
}
public boolean getShouldDisplayOverlay()
{
return this.shouldDisplayOverlay;
}
public InventoryDendrikBelt getInventoryDendrikBelt()
{
return this.inventoryDendrikBelt;
}
}
Any and all help is appreciated, thanks!