ActuallyAdditions/src/main/java/ellpeck/actuallyadditions/inventory/ContainerDrill.java

225 lines
7 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
* This file ("ContainerDrill.java") is part of the Actually Additions Mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
* <EFBFBD> 2015 Ellpeck
*/
2015-06-21 02:28:49 +02:00
package ellpeck.actuallyadditions.inventory;
import cofh.api.energy.IEnergyContainerItem;
2015-06-21 02:28:49 +02:00
import ellpeck.actuallyadditions.inventory.slot.SlotImmovable;
import ellpeck.actuallyadditions.items.ItemDrill;
import ellpeck.actuallyadditions.items.ItemDrillUpgrade;
import invtweaks.api.container.InventoryContainer;
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;
import net.minecraft.item.ItemStack;
@InventoryContainer
public class ContainerDrill extends Container{
private static final int SLOT_AMOUNT = 5;
private InventoryDrill drillInventory = new InventoryDrill();
private InventoryPlayer inventory;
public ContainerDrill(InventoryPlayer inventory){
this.inventory = inventory;
for(int i = 0; i < SLOT_AMOUNT; i++){
this.addSlotToContainer(new Slot(drillInventory, i, 44+i*18, 19){
@Override
public boolean isItemValid(ItemStack stack){
return stack.getItem() instanceof ItemDrillUpgrade || stack.getItem() instanceof IEnergyContainerItem;
2015-06-21 02:28:49 +02:00
}
});
}
for(int i = 0; i < 3; i++){
for(int j = 0; j < 9; j++){
this.addSlotToContainer(new Slot(inventory, j+i*9+9, 8+j*18, 58+i*18));
}
}
for(int i = 0; i < 9; i++){
2015-10-02 16:48:01 +02:00
if(i == inventory.currentItem){
this.addSlotToContainer(new SlotImmovable(inventory, i, 8+i*18, 116));
}
else{
this.addSlotToContainer(new Slot(inventory, i, 8+i*18, 116));
}
2015-06-21 02:28:49 +02:00
}
ItemStack stack = inventory.getCurrentItem();
if(stack != null && stack.getItem() instanceof ItemDrill){
ItemStack[] slots = ((ItemDrill)stack.getItem()).getSlotsFromNBT(inventory.getCurrentItem());
if(slots != null && slots.length > 0) this.drillInventory.slots = slots;
}
}
@Override
public boolean canInteractWith(EntityPlayer player){
return this.drillInventory.isUseableByPlayer(player);
}
@Override
public void onContainerClosed(EntityPlayer player){
ItemStack stack = inventory.getCurrentItem();
if(stack != null && stack.getItem() instanceof ItemDrill){
((ItemDrill)stack.getItem()).writeSlotsToNBT(this.drillInventory.slots, inventory.getCurrentItem());
}
2015-10-02 16:48:01 +02:00
super.onContainerClosed(player);
2015-06-21 02:28:49 +02:00
}
@Override
public ItemStack slotClick(int par1, int par2, int par3, EntityPlayer player){
//par3 appears to be the type of clicking
//par3 == 2 appears to be one of the number keys being hit
2015-10-02 16:48:01 +02:00
if(par3 == 2 && par2 == inventory.currentItem){
return null;
}
else{
return super.slotClick(par1, par2, par3, player);
}
}
2015-06-21 02:28:49 +02:00
@Override
public ItemStack transferStackInSlot(EntityPlayer player, int slot){
final int inventoryStart = 5;
final int inventoryEnd = inventoryStart+26;
final int hotbarStart = inventoryEnd+1;
final int hotbarEnd = hotbarStart+8;
Slot theSlot = (Slot)this.inventorySlots.get(slot);
2015-10-02 16:48:01 +02:00
if(theSlot != null && theSlot.getHasStack()){
2015-07-15 09:28:01 +02:00
ItemStack newStack = theSlot.getStack();
ItemStack currentStack = newStack.copy();
2015-06-21 02:28:49 +02:00
2015-07-15 09:28:01 +02:00
//Other Slots in Inventory excluded
if(slot >= inventoryStart){
//Shift from Inventory
if(newStack.getItem() instanceof ItemDrillUpgrade || newStack.getItem() instanceof IEnergyContainerItem){
2015-07-15 09:28:01 +02:00
if(!this.mergeItemStack(newStack, 0, 5, false)) return null;
2015-06-21 02:28:49 +02:00
}
2015-07-15 09:28:01 +02:00
//
2015-06-21 02:28:49 +02:00
2015-07-15 09:28:01 +02:00
else if(slot >= inventoryStart && slot <= inventoryEnd){
if(!this.mergeItemStack(newStack, hotbarStart, hotbarEnd+1, false)) return null;
2015-06-21 02:28:49 +02:00
}
2015-10-02 16:48:01 +02:00
else if(slot >= inventoryEnd+1 && slot < hotbarEnd+1 && !this.mergeItemStack(newStack, inventoryStart, inventoryEnd+1, false)){
return null;
}
2015-07-15 09:28:01 +02:00
}
2015-07-15 11:23:11 +02:00
else if(!this.mergeItemStack(newStack, inventoryStart, hotbarEnd+1, false)) return null;
2015-06-21 02:28:49 +02:00
2015-10-02 16:48:01 +02:00
if(newStack.stackSize == 0){
theSlot.putStack(null);
}
else{
theSlot.onSlotChanged();
}
2015-06-21 02:28:49 +02:00
2015-10-02 16:48:01 +02:00
if(newStack.stackSize == currentStack.stackSize) return null;
2015-07-15 09:28:01 +02:00
theSlot.onPickupFromSlot(player, newStack);
return currentStack;
2015-06-21 02:28:49 +02:00
}
return null;
}
public static class InventoryDrill implements IInventory{
public ItemStack[] slots = new ItemStack[SLOT_AMOUNT];
@Override
public String getInventoryName(){
return "drill";
}
@Override
public boolean hasCustomInventoryName(){
return false;
}
@Override
public int getInventoryStackLimit(){
return 64;
}
@Override
public void markDirty(){
}
@Override
public void openInventory(){
}
@Override
public void closeInventory(){
}
@Override
public boolean isItemValidForSlot(int slot, ItemStack stack){
return true;
}
@Override
public boolean isUseableByPlayer(EntityPlayer player){
return true;
}
@Override
public ItemStack getStackInSlotOnClosing(int i){
return getStackInSlot(i);
}
@Override
public void setInventorySlotContents(int i, ItemStack stack){
this.slots[i] = stack;
this.markDirty();
}
@Override
public int getSizeInventory(){
return slots.length;
}
@Override
public ItemStack getStackInSlot(int i){
if(i < this.getSizeInventory()){
return slots[i];
}
return null;
}
@Override
public ItemStack decrStackSize(int i, int j){
2015-10-02 16:48:01 +02:00
if(slots[i] != null){
2015-06-21 02:28:49 +02:00
ItemStack stackAt;
if(slots[i].stackSize <= j){
stackAt = slots[i];
slots[i] = null;
this.markDirty();
return stackAt;
}
else{
stackAt = slots[i].splitStack(j);
2015-10-02 16:48:01 +02:00
if(slots[i].stackSize == 0) slots[i] = null;
2015-06-21 02:28:49 +02:00
this.markDirty();
return stackAt;
}
}
return null;
}
}
}