Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.konarjg.arcticraft.blocks.tileentities.containers;
- import com.konarjg.arcticraft.blocks.tileentities.TileEntityBarrel;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.entity.player.InventoryPlayer;
- import net.minecraft.inventory.Container;
- import net.minecraft.inventory.Slot;
- import net.minecraft.item.ItemStack;
- public class ContainerBarrel extends Container {
- TileEntityBarrel inventory;
- @Override
- protected boolean mergeItemStack(ItemStack stack, int start, int end, boolean backwards)
- {
- boolean flag1 = false;
- int k = start;
- Slot slot;
- ItemStack itemstack1;
- if (backwards) { k = end - 1; }
- if (stack.isStackable()) {
- while (stack.getCount() > 0 && (!backwards && k < end || backwards && k >= start))
- {
- slot = (Slot) inventorySlots.get(k);
- itemstack1 = slot.getStack();
- if (!slot.isItemValid(stack)) {
- continue;
- }
- if (itemstack1 != null && itemstack1.getItem() == stack.getItem() &&
- (!stack.getHasSubtypes() || stack.getItemDamage() == itemstack1.getItemDamage()) &&
- ItemStack.areItemStackTagsEqual(stack, itemstack1))
- {
- int l = itemstack1.getCount() + stack.getCount();
- if (l <= stack.getCount() && l <= slot.getSlotStackLimit()) {
- stack = ItemStack.EMPTY;
- itemstack1.setCount (l);
- inventory.markDirty();
- flag1 = true;
- } else if (itemstack1.getCount() < stack.getCount() && l < slot.getSlotStackLimit()) {
- stack.setCount(stack.getCount() - stack.getCount()- itemstack1.getCount());
- itemstack1.setCount(stack.getCount());
- inventory.markDirty();
- flag1 = true;
- }
- }
- if (backwards) { --k; }
- else { ++k; }
- }
- }
- if (stack.getMaxStackSize() > 0)
- {
- if (backwards) { k = end - 1; }
- else { k = start; }
- while (!backwards && k < end || backwards && k >= start) {
- slot = (Slot) inventorySlots.get(k);
- itemstack1 = slot.getStack();
- if (!slot.isItemValid(stack)) {
- continue;
- }
- if (itemstack1 == null) {
- int l = stack.getMaxStackSize ();
- if (l <= slot.getSlotStackLimit()) {
- slot.putStack(stack.copy());
- stack = ItemStack.EMPTY;
- inventory.markDirty();
- flag1 = true;
- break;
- } else {
- putStackInSlot(k, new ItemStack(stack.getItem(), slot.getSlotStackLimit(), stack.getItemDamage()));
- stack.setCount(stack.getCount() - slot.getSlotStackLimit());
- inventory.markDirty();
- flag1 = true;
- }
- }
- if (backwards) { --k; }
- else { ++k; }
- }
- }
- return flag1;
- }
- @Override
- public void onContainerClosed(EntityPlayer playerIn) {
- super.onContainerClosed(playerIn);
- inventory.closeInventory(playerIn);
- }
- public ContainerBarrel (InventoryPlayer playerInv, TileEntityBarrel te) {
- this.inventory = te;
- inventory.openInventory(playerInv.player);
- for (int x = 0; x < 2; x++) {
- for (int y = 0; y < 8; y++) {
- addSlotToContainer (new Slot (inventory, y + x * 8, 16 + y * 18, 24 + x * 18));
- }
- }
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 9; j++) {
- addSlotToContainer(new Slot(playerInv, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
- }
- }
- for (int k = 0; k < 9; k++) {
- addSlotToContainer(new Slot(playerInv, k, 8 + k * 18, 142));
- }
- }
- @Override
- public boolean canInteractWith(EntityPlayer playerIn) {
- return true;
- }
- public TileEntityBarrel getBarrelInventory () {
- return this.inventory;
- }
- @Override
- public ItemStack transferStackInSlot(EntityPlayer playerIn, int index) {
- ItemStack itemstack = ItemStack.EMPTY;
- Slot slot = inventorySlots.get(index);
- if (slot != null && slot.getHasStack()) {
- ItemStack itemstack2 = slot.getStack();
- itemstack = itemstack2.copy();
- int containerSlots = inventorySlots.size();
- if (index < containerSlots) {
- if (!this.mergeItemStack(itemstack2, containerSlots, inventorySlots.size(), true)) {
- return ItemStack.EMPTY;
- }
- } else if (!this.mergeItemStack(itemstack2, 0, containerSlots, false)) {
- return ItemStack.EMPTY;
- }
- if (itemstack2.getCount() == 0) {
- slot.putStack(ItemStack.EMPTY);
- } else {
- slot.onSlotChanged();
- }
- if (itemstack2.getCount() == itemstack.getCount()) {
- return ItemStack.EMPTY;
- }
- slot.onTake(playerIn, itemstack2);
- }
- return ItemStack.EMPTY;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment