Hi there
My aim was to create a single slot inventory when an item was right clicked. Im good with all the gui stuff but can seem to nail the capabilities and NBT.
I found this post(
I also don't know using the current setup how I would go about accessing the contents of the slot from within the item class.
Any guidance is appreciated.
Code:
ItemClass:
package net.themcjavafre4k.mcdj.item.headphones;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.ItemArmor;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.themcjavafre4k.mcdj.MCDJGuiHandler;
import net.themcjavafre4k.mcdj.MCDJMod;
import net.themcjavafre4k.mcdj.item.CustomRecord;
import net.themcjavafre4k.mcdj.item.MCDJItems;
public class ItemHeadphones extends ItemArmor{
private String title;
public ItemHeadphones(String name){
super(MCDJItems.headphoneMaterial, 1, EntityEquipmentSlot.HEAD);
this.title = name;
setUnlocalizedName(name);
setRegistryName(name);
setCreativeTab(MCDJMod.creativeTab);
INSTANCE = this;
}
public void registerItemModel(){
MCDJMod.proxy.registerItemRenderer(this, 0, title, "mcdj");
}
@Override
public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand){
ItemStack itemstack = player.getHeldItem(hand);
if (!world.isRemote){
player.openGui(MCDJMod.instance, MCDJGuiHandler.HEADPHONES, world, (int)player.posX, (int)player.posY, (int)player.posZ);
//LogHelper.info("Succesfully opened GUI");
}
return new ActionResult<ItemStack>(EnumActionResult.PASS, player.getHeldItemMainhand());
}
@Override
public ICapabilityProvider initCapabilities( ItemStack item, NBTTagCompound nbt ) {
return new HeadphonesProvider();
}
}
Provider:
package net.themcjavafre4k.mcdj.item.headphones;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;
public class HeadphonesProvider implements ICapabilityProvider, ICapabilitySerializable<NBTTagCompound>{
private ItemStackHandler inventory;
public HeadphonesProvider() {
inventory = new ItemStackHandler(1);
}
@Override
public NBTTagCompound serializeNBT() {
return inventory.serializeNBT();
}
@Override
public void deserializeNBT( NBTTagCompound nbt ) {
// nbt = new NBTTagCompound();
inventory.deserializeNBT(nbt);
}
@Override
public boolean hasCapability( Capability<?> capability, EnumFacing facing ) {
if( capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY ) {
return true;
}
return false;
}
@Override
public <T> T getCapability( Capability<T> capability, EnumFacing facing ) {
if( capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY ) {
return (T) inventory;
}
return null;
}
}
Container:
package net.themcjavafre4k.mcdj.item.headphones;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.items.IItemHandler;
import net.themcjavafre4k.mcdj.inventory.SlotInput;
import net.themcjavafre4k.mcdj.item.CustomRecord;
import net.themcjavafre4k.mcdj.item.MCDJItems;
public class ContainerHeadphones extends Container{
public IItemHandler inv;
public ContainerHeadphones(IItemHandler itemHandler, EntityPlayer player ) {
this.inv = itemHandler;
addSlotToContainer(new SlotInput(itemHandler, 0, 44, 35));
for(int i = 0; i < 3; i++){
for(int j = 0; j < 9; j++){
addSlotToContainer(new Slot(player.inventory, j+i*9+9, 8+j*18, 84+i*18));
}
}
for(int i = 0; i < 9; i++){
addSlotToContainer(new Slot(player.inventory, i, 8+i*18, 142));
}
}
@Override
public boolean canInteractWith(EntityPlayer playerIn) {
return true;
}
//Crashing game
// @Nullable
// @Override
// public ItemStack transferStackInSlot(EntityPlayer playerIn, int index)
// {
// ItemStack itemstack = ItemStack.EMPTY;
// Slot slot = this.inventorySlots.get(index);
//
// if (slot != null && slot.getHasStack())
// {
// ItemStack itemstack1 = slot.getStack();
// itemstack = itemstack1.copy();
//
// if (index == 2)
// {
// if (!this.mergeItemStack(itemstack1, 3, 39, true))
// {
// return ItemStack.EMPTY;
// }
//
// slot.onSlotChange(itemstack1, itemstack);
// }
// else if (!this.mergeItemStack(itemstack1, 3, 39, false))
// {
// return ItemStack.EMPTY;
// }
//
// if (itemstack1.isEmpty())
// {
// slot.putStack(ItemStack.EMPTY);
// }
// else
// {
// slot.onSlotChanged();
// }
//
// if (itemstack1.getCount() == itemstack.getCount())
// {
// return ItemStack.EMPTY;
// }
//
// slot.onTake(playerIn, itemstack1);
// }
//
// return itemstack;
// }
}
SlotInput:
package net.themcjavafre4k.mcdj.inventory;
import javax.annotation.Nonnull;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.SlotItemHandler;
import net.themcjavafre4k.mcdj.item.CustomRecord;
import net.themcjavafre4k.mcdj.item.MCDJItems;
public class SlotInput extends SlotItemHandler {
public SlotInput(IItemHandler inventory, int par2, int par3, int par4) {
super(inventory, par2, par3, par4);
}
@Override
public boolean isItemValid(ItemStack itemstack) {
return itemstack.getItem() instanceof CustomRecord;
//return true;
}
@Override
public int getSlotStackLimit() {
return 1;
}
}
Gui:
package net.themcjavafre4k.mcdj.item.headphones;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.themcjavafre4k.mcdj.MCDJMod;
import net.themcjavafre4k.mcdj.SoundHandler;
import net.themcjavafre4k.mcdj.item.MCDJItems;
@SideOnly(Side.CLIENT)
public class GuiHeadphones extends GuiContainer{
private static final ResourceLocation BG_TEXTURE = new ResourceLocation(MCDJMod.modId, "textures/gui/headphonegui.png");
public GuiHeadphones(Container container, InventoryPlayer inventory) {
super(container);
}
@Override
public void initGui(){
super.initGui();
}
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
this.drawDefaultBackground();
super.drawScreen(mouseX, mouseY, partialTicks);
this.renderHoveredToolTip(mouseX, mouseY);
}
@Override
protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY){
GlStateManager.color(1, 1, 1, 1);
mc.getTextureManager().bindTexture(BG_TEXTURE);
int x = (width - xSize) / 2;
int y = (height - ySize) / 2;
drawTexturedModalRect(x, y, 0, 0, xSize, ySize);
}
@Override
protected void drawGuiContainerForegroundLayer(int mousX, int mouseY){
String name = I18n.format(MCDJItems.headphones.getUnlocalizedName());
//fontRenderer.drawString("Search For Songs", 195, 4, 0xFFFFFF);
fontRenderer.drawString("Headphone Player", (xSize/2 - fontRenderer.getStringWidth(name)/2), 5, 0xFFFFFF);
//fontRenderer.drawString(invPlayer.getDisplayName().getUnformattedText(), 8, ySize/2 - 10, 0xFFFFFF);
}
}
If any more information is needed I will provide.