ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/ContainerCrafter.java

116 lines
3.6 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("ContainerCrafter.java") is part of the Actually Additions mod for Minecraft.
2015-08-29 14:33:25 +02:00
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-05-16 22:52:27 +02:00
* http://ellpeck.de/actaddlicense
2015-08-29 14:33:25 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2016-05-16 22:54:42 +02:00
* © 2015-2016 Ellpeck
2015-08-29 14:33:25 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.inventory;
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;
public class ContainerCrafter extends Container{
public final World world;
2016-05-19 20:05:12 +02:00
public final InventoryCrafting craftMatrix = new InventoryCrafting(this, 3, 3);
public final IInventory craftResult = new InventoryCraftResult();
public ContainerCrafter(EntityPlayer player){
InventoryPlayer inventory = player.inventory;
this.world = player.worldObj;
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-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-10-02 16:48:01 +02:00
for(int i = 0; i < 9; i++){
this.addSlotToContainer(new Slot(inventory, i, 8+i*18, 142));
}
this.onCraftMatrixChanged(this.craftMatrix);
}
@Override
public ItemStack transferStackInSlot(EntityPlayer player, int slot){
ItemStack stack = null;
2016-05-10 18:37:00 +02:00
Slot theSlot = this.inventorySlots.get(slot);
if(theSlot != null && theSlot.getHasStack()){
ItemStack savedStack = theSlot.getStack();
stack = savedStack.copy();
if(slot == 0){
2015-10-03 10:16:18 +02:00
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-10-03 10:16:18 +02:00
else if(!this.mergeItemStack(savedStack, 10, 46, false)){
return null;
}
2016-06-07 23:17:06 +02:00
if(savedStack.stackSize <= 0){
2015-10-02 16:48:01 +02:00
theSlot.putStack(null);
}
else{
theSlot.onSlotChanged();
}
2015-10-03 10:16:18 +02:00
if(savedStack.stackSize == stack.stackSize){
return null;
}
theSlot.onPickupFromSlot(player, savedStack);
}
return stack;
}
2015-10-03 10:19:40 +02:00
@Override
public void onContainerClosed(EntityPlayer player){
super.onContainerClosed(player);
if(!this.world.isRemote){
for(int i = 0; i < 9; ++i){
ItemStack stack = this.craftMatrix.removeStackFromSlot(i);
2015-10-03 10:19:40 +02:00
if(stack != null){
2016-04-20 21:39:03 +02:00
player.dropItem(stack, false);
2015-10-03 10:19:40 +02:00
}
}
}
}
@Override
public void onCraftMatrixChanged(IInventory inv){
this.craftResult.setInventorySlotContents(0, CraftingManager.getInstance().findMatchingRecipe(this.craftMatrix, this.world));
}
@Override
public boolean canInteractWith(EntityPlayer player){
2015-10-03 10:19:40 +02:00
return true;
}
}