Working item with inventory

This commit is contained in:
Christian Knaapen
2018-09-16 15:20:01 +02:00
parent d01fe52aba
commit efa47851be
7 changed files with 72 additions and 242 deletions

View File

@@ -0,0 +1,41 @@
package nl.requios.effortlessbuilding.inventory;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemStackHandler;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class RandomizerBagCapabilityProvider implements ICapabilitySerializable<NBTTagCompound> {
IItemHandler itemHandler = new ItemStackHandler(5);
@Override
public boolean hasCapability(@Nonnull Capability<?> capability, @Nullable EnumFacing facing) {
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
return true;
return false;
}
@Nullable
@Override
public <T> T getCapability(@Nonnull Capability<T> capability, @Nullable EnumFacing facing) {
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
return (T) itemHandler;
return null;
}
@Override
public NBTTagCompound serializeNBT() {
return ((ItemStackHandler)itemHandler).serializeNBT();
}
@Override
public void deserializeNBT(NBTTagCompound nbt) {
((ItemStackHandler)itemHandler).deserializeNBT(nbt);
}
}

View File

@@ -8,17 +8,19 @@ import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot; import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumHand; import net.minecraft.util.EnumHand;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.SlotItemHandler;
public class RandomizerBagContainer extends Container { public class RandomizerBagContainer extends Container {
private final IInventory bagInventory; private final IItemHandler bagInventory;
private final int sizeInventory; private final int sizeInventory;
public RandomizerBagContainer(InventoryPlayer parInventoryPlayer, IInventory parIInventory) { public RandomizerBagContainer(InventoryPlayer parInventoryPlayer, IItemHandler parIInventory) {
bagInventory = parIInventory; bagInventory = parIInventory;
sizeInventory = bagInventory.getSizeInventory(); sizeInventory = bagInventory.getSlots();
for (int i = 0; i < RandomizerBagInventory.INV_SIZE; ++i) { for (int i = 0; i < sizeInventory; ++i) {
this.addSlotToContainer(new Slot(bagInventory, i, 80 + (18 * (i / 4)), 8 + (18 * (i % 4)))); this.addSlotToContainer(new SlotItemHandler(bagInventory, i, 80 + (18 * (i / 4)), 8 + (18 * (i % 4))));
} }
// add player inventory slots // add player inventory slots
@@ -37,7 +39,7 @@ public class RandomizerBagContainer extends Container {
@Override @Override
public boolean canInteractWith(EntityPlayer playerIn) { public boolean canInteractWith(EntityPlayer playerIn) {
return bagInventory.isUsableByPlayer(playerIn); return true;
} }
@Override @Override

View File

@@ -3,32 +3,32 @@ package nl.requios.effortlessbuilding.inventory;
import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager; import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory; import net.minecraft.inventory.IInventory;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly; import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.items.IItemHandler;
import nl.requios.effortlessbuilding.EffortlessBuilding; import nl.requios.effortlessbuilding.EffortlessBuilding;
@SideOnly(Side.CLIENT) @SideOnly(Side.CLIENT)
public class RandomizerBagGuiContainer extends GuiContainer { public class RandomizerBagGuiContainer extends GuiContainer {
private static final ResourceLocation grinderGuiTextures = private static final ResourceLocation grinderGuiTextures =
new ResourceLocation(EffortlessBuilding.MODID new ResourceLocation(EffortlessBuilding.MODID
+ ":textures/gui/container/bagrandomizer.png"); + ":textures/gui/container/randomizerbag.png");
private final InventoryPlayer inventoryPlayer; private final InventoryPlayer inventoryPlayer;
private final IInventory tileGrinder; private final IItemHandler inventoryBag;
public RandomizerBagGuiContainer(InventoryPlayer parInventoryPlayer, public RandomizerBagGuiContainer(InventoryPlayer parInventoryPlayer,
IInventory parInventoryGrinder) { IItemHandler parInventoryBag) {
super(new RandomizerBagContainer(parInventoryPlayer, super(new RandomizerBagContainer(parInventoryPlayer,
parInventoryGrinder)); parInventoryBag));
inventoryPlayer = parInventoryPlayer; inventoryPlayer = parInventoryPlayer;
tileGrinder = parInventoryGrinder; inventoryBag = parInventoryBag;
} }
@Override @Override
protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) { protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
String s = tileGrinder.getDisplayName().getUnformattedText(); String s = "Randomizer bag";
fontRenderer.drawString(s, xSize / 2 - fontRenderer.getStringWidth(s) / 2, 6, 4210752); fontRenderer.drawString(s, xSize / 2 - fontRenderer.getStringWidth(s) / 2, 6, 4210752);
fontRenderer.drawString(inventoryPlayer.getDisplayName().getUnformattedText(), 8, ySize - 96 + 2, 4210752); fontRenderer.drawString(inventoryPlayer.getDisplayName().getUnformattedText(), 8, ySize - 96 + 2, 4210752);
} }
@@ -44,17 +44,5 @@ public class RandomizerBagGuiContainer extends GuiContainer {
int marginHorizontal = (width - xSize) / 2; int marginHorizontal = (width - xSize) / 2;
int marginVertical = (height - ySize) / 2; int marginVertical = (height - ySize) / 2;
drawTexturedModalRect(marginHorizontal, marginVertical, 0, 0, xSize, ySize); drawTexturedModalRect(marginHorizontal, marginVertical, 0, 0, xSize, ySize);
// Draw progress indicator
int progressLevel = getProgressLevel(24);
drawTexturedModalRect(marginHorizontal + 79, marginVertical + 34, 176, 14, progressLevel + 1, 16);
}
private int getProgressLevel(int progressIndicatorPixelWidth) {
int ticksGrindingItemSoFar = tileGrinder.getField(2);
int ticksPerItem = tileGrinder.getField(3);
return ticksPerItem != 0 && ticksGrindingItemSoFar != 0 ?
ticksGrindingItemSoFar * progressIndicatorPixelWidth / ticksPerItem
: 0;
} }
} }

View File

@@ -1,215 +0,0 @@
package nl.requios.effortlessbuilding.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.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraftforge.items.IItemHandler;
import javax.annotation.Nonnull;
public class RandomizerBagInventory implements IInventory {
//Reference to NBT data
private final ItemStack invItem;
public static final int INV_SIZE = 5;
private ItemStack[] inventory = new ItemStack[INV_SIZE];
public RandomizerBagInventory(ItemStack invItem) {
this.invItem = invItem;
if (!invItem.hasTagCompound()) {
invItem.setTagCompound(new NBTTagCompound());
}
readFromNBT(invItem.getTagCompound());
}
@Override
public int getSizeInventory() {
return INV_SIZE;
}
@Override
public boolean isEmpty() {
return false;
}
@Nonnull
@Override
public ItemStack getStackInSlot(int slot) {
return inventory[slot];
}
@Nonnull
@Override
public ItemStack decrStackSize(int slot, int amount) {
if (inventory[slot] != null)
{
ItemStack itemstack;
if (inventory[slot].getCount() <= amount)
{
itemstack = inventory[slot];
inventory[slot] = null;
return itemstack;
}
else
{
itemstack = inventory[slot].splitStack(amount);
if (inventory[slot].getCount() == 0)
{
inventory[slot] = null;
}
return itemstack;
}
}
else
{
return null;
}
}
@Override
public ItemStack removeStackFromSlot(int index) {
return null;
}
public void onInventoryChanged() {
for (int i = 0; i < getFieldCount(); ++i) {
if (getStackInSlot(i) != null && getStackInSlot(i).getCount() == 0) {
inventory[i] = null;
}
}
writeToNBT(invItem.getTagCompound());
}
public void setInventorySlotContents(int slot, ItemStack stack) {
inventory[slot] = stack;
if (stack != null && stack.getCount() > getInventoryStackLimit())
{
stack.setCount(getInventoryStackLimit());
}
markDirty();
}
@Override
public int getInventoryStackLimit() {
return 64;
}
@Override
public void markDirty() {
}
@Override
public boolean isUsableByPlayer(EntityPlayer player) {
return true;
}
@Override
public void openInventory(EntityPlayer player) {
}
@Override
public void closeInventory(EntityPlayer player) {
}
@Override
public boolean isItemValidForSlot(int index, ItemStack stack) {
return true;
}
@Override
public int getField(int id) {
//variables
return 0;
}
@Override
public void setField(int id, int value) {
}
@Override
public int getFieldCount() {
return 0;
}
@Override
public void clear() {
for (int i = 0; i < inventory.length; ++i)
{
inventory[i] = null;
}
}
public void readFromNBT(NBTTagCompound compound) {
// Gets the custom taglist we wrote to this compound, if any
// 1.7.2+ change to compound.getTagList("ItemInventory", Constants.NBT.TAG_COMPOUND);
NBTTagList items = compound.getTagList("ItemInventory", 0);
for (int i = 0; i < items.tagCount(); ++i) {
// 1.7.2+ change to items.getCompoundTagAt(i)
NBTTagCompound item = items.getCompoundTagAt(i);
int slot = item.getInteger("Slot");
// Just double-checking that the saved slot index is within our inventory array bounds
if (slot >= 0 && slot < getFieldCount()) {
inventory[slot] = new ItemStack(item);
}
}
}
/**
* A custom method to write our inventory to an ItemStack's NBT compound
*/
public void writeToNBT(NBTTagCompound tagcompound) {
// Create a new NBT Tag List to store itemstacks as NBT Tags
NBTTagList items = new NBTTagList();
for (int i = 0; i < getSizeInventory(); ++i) {
// Only write stacks that contain items
if (getStackInSlot(i) != null) {
// Make a new NBT Tag Compound to write the itemstack and slot index to
NBTTagCompound item = new NBTTagCompound();
item.setInteger("Slot", i);
// Writes the itemstack in slot(i) to the Tag Compound we just made
getStackInSlot(i).writeToNBT(item);
// add the tag compound to our tag list
items.appendTag(item);
}
}
// Add the TagList to the ItemStack's Tag Compound with the name "ItemInventory"
tagcompound.setTag("ItemInventory", items);
}
@Override
public String getName() {
return "Testname";
}
@Override
public boolean hasCustomName() {
return false;
}
@Override
public ITextComponent getDisplayName() {
return new TextComponentString("Testname");
}
}

View File

@@ -4,6 +4,9 @@ import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.EnumHand; import net.minecraft.util.EnumHand;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.fml.common.network.IGuiHandler; import net.minecraftforge.fml.common.network.IGuiHandler;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemStackHandler;
import nl.requios.effortlessbuilding.EffortlessBuilding; import nl.requios.effortlessbuilding.EffortlessBuilding;
import nl.requios.effortlessbuilding.inventory.RandomizerBagContainer; import nl.requios.effortlessbuilding.inventory.RandomizerBagContainer;
@@ -15,7 +18,7 @@ public class RandomizerGuiHandler implements IGuiHandler {
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
if (ID == EffortlessBuilding.RANDOMIZER_BAG_GUI) { if (ID == EffortlessBuilding.RANDOMIZER_BAG_GUI) {
// Use the player's held item to create the inventory // Use the player's held item to create the inventory
return new RandomizerBagContainer(player.inventory, new RandomizerBagInventory(player.getHeldItem(EnumHand.MAIN_HAND))); return new RandomizerBagContainer(player.inventory, player.getHeldItem(EnumHand.MAIN_HAND).getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null));
} }
return null; return null;
} }
@@ -26,7 +29,7 @@ public class RandomizerGuiHandler implements IGuiHandler {
if (ID == EffortlessBuilding.RANDOMIZER_BAG_GUI) { if (ID == EffortlessBuilding.RANDOMIZER_BAG_GUI) {
// We have to cast the new container as our custom class // We have to cast the new container as our custom class
// and pass in currently held item for the inventory // and pass in currently held item for the inventory
return new RandomizerBagGuiContainer(player.inventory, new RandomizerBagInventory(player.getHeldItem(EnumHand.MAIN_HAND))); return new RandomizerBagGuiContainer(player.inventory, player.getHeldItem(EnumHand.MAIN_HAND).getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null));
} }
return null; return null;
} }

View File

@@ -4,13 +4,18 @@ import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ActionResult; import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult; import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand; import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import nl.requios.effortlessbuilding.EffortlessBuilding; import nl.requios.effortlessbuilding.EffortlessBuilding;
import nl.requios.effortlessbuilding.inventory.RandomizerBagCapabilityProvider;
import javax.annotation.Nullable;
public class ItemRandomizerBag extends Item { public class ItemRandomizerBag extends Item {
@@ -53,4 +58,10 @@ public class ItemRandomizerBag extends Item {
public int getMaxItemUseDuration(ItemStack stack) { public int getMaxItemUseDuration(ItemStack stack) {
return 1; return 1;
} }
@Nullable
@Override
public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable NBTTagCompound nbt) {
return new RandomizerBagCapabilityProvider();
}
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB