2015-08-29 14:33:25 +02:00
|
|
|
|
/*
|
|
|
|
|
* This file ("ContainerCrafter.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-03-07 12:51:28 +01:00
|
|
|
|
package ellpeck.actuallyadditions.inventory;
|
2015-03-07 02:23:31 +01:00
|
|
|
|
|
2015-04-27 17:08:50 +02:00
|
|
|
|
import invtweaks.api.container.InventoryContainer;
|
2015-03-07 02:23:31 +01:00
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
|
import net.minecraft.entity.player.InventoryPlayer;
|
|
|
|
|
import net.minecraft.inventory.*;
|
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
|
import net.minecraft.item.crafting.CraftingManager;
|
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
|
2015-04-27 17:08:50 +02:00
|
|
|
|
@InventoryContainer
|
2015-03-07 02:23:31 +01:00
|
|
|
|
public class ContainerCrafter extends Container{
|
|
|
|
|
|
|
|
|
|
public InventoryCrafting craftMatrix = new InventoryCrafting(this, 3, 3);
|
|
|
|
|
public IInventory craftResult = new InventoryCraftResult();
|
|
|
|
|
|
|
|
|
|
public final int x;
|
|
|
|
|
public final int y;
|
|
|
|
|
public final int z;
|
|
|
|
|
public final World world;
|
|
|
|
|
|
|
|
|
|
public ContainerCrafter(EntityPlayer player){
|
|
|
|
|
InventoryPlayer inventory = player.inventory;
|
|
|
|
|
|
|
|
|
|
this.world = player.worldObj;
|
|
|
|
|
this.x = (int)player.posX;
|
|
|
|
|
this.y = (int)player.posY;
|
|
|
|
|
this.z = (int)player.posZ;
|
|
|
|
|
|
|
|
|
|
this.addSlotToContainer(new SlotCrafting(inventory.player, this.craftMatrix, this.craftResult, 0, 124, 35));
|
2015-10-02 16:48:01 +02:00
|
|
|
|
for(int i = 0; i < 3; i++){
|
|
|
|
|
for(int j = 0; j < 3; j++){
|
|
|
|
|
this.addSlotToContainer(new Slot(this.craftMatrix, j+i*3, 30+j*18, 17+i*18));
|
2015-03-07 02:23:31 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-02 16:48:01 +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, 84+i*18));
|
2015-03-07 02:23:31 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-10-02 16:48:01 +02:00
|
|
|
|
for(int i = 0; i < 9; i++){
|
|
|
|
|
this.addSlotToContainer(new Slot(inventory, i, 8+i*18, 142));
|
2015-03-07 02:23:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.onCraftMatrixChanged(this.craftMatrix);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2015-03-08 14:58:26 +01:00
|
|
|
|
public void onCraftMatrixChanged(IInventory inv){
|
2015-03-07 02:23:31 +01:00
|
|
|
|
this.craftResult.setInventorySlotContents(0, CraftingManager.getInstance().findMatchingRecipe(this.craftMatrix, this.world));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onContainerClosed(EntityPlayer player){
|
|
|
|
|
super.onContainerClosed(player);
|
|
|
|
|
|
2015-10-02 16:48:01 +02:00
|
|
|
|
if(!this.world.isRemote){
|
|
|
|
|
for(int i = 0; i < 9; ++i){
|
2015-03-07 02:23:31 +01:00
|
|
|
|
ItemStack stack = this.craftMatrix.getStackInSlotOnClosing(i);
|
|
|
|
|
if(stack != null) player.dropPlayerItemWithRandomChoice(stack, false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean canInteractWith(EntityPlayer player){
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ItemStack transferStackInSlot(EntityPlayer player, int slot){
|
|
|
|
|
ItemStack stack = null;
|
|
|
|
|
Slot theSlot = (Slot)this.inventorySlots.get(slot);
|
|
|
|
|
|
|
|
|
|
if(theSlot != null && theSlot.getHasStack()){
|
|
|
|
|
ItemStack savedStack = theSlot.getStack();
|
|
|
|
|
stack = savedStack.copy();
|
|
|
|
|
|
|
|
|
|
if(slot == 0){
|
|
|
|
|
if(!this.mergeItemStack(savedStack, 10, 46, true)) return null;
|
|
|
|
|
theSlot.onSlotChange(savedStack, stack);
|
|
|
|
|
}
|
2015-10-02 16:48:01 +02:00
|
|
|
|
else if(slot >= 10 && slot < 37 && !this.mergeItemStack(savedStack, 37, 46, false)){
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
else if(slot >= 37 && slot < 46 && !this.mergeItemStack(savedStack, 10, 37, false)){
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2015-03-07 02:23:31 +01:00
|
|
|
|
else if(!this.mergeItemStack(savedStack, 10, 46, false)) return null;
|
|
|
|
|
|
2015-10-02 16:48:01 +02:00
|
|
|
|
if(savedStack.stackSize == 0){
|
|
|
|
|
theSlot.putStack(null);
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
theSlot.onSlotChanged();
|
|
|
|
|
}
|
2015-03-07 02:23:31 +01:00
|
|
|
|
|
|
|
|
|
if(savedStack.stackSize == stack.stackSize) return null;
|
|
|
|
|
|
|
|
|
|
theSlot.onPickupFromSlot(player, savedStack);
|
|
|
|
|
}
|
|
|
|
|
return stack;
|
|
|
|
|
}
|
|
|
|
|
}
|