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;
|
2015-03-07 02:23:31 +01:00
|
|
|
|
2016-05-14 11:15:27 +02:00
|
|
|
|
2016-11-16 16:59:00 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
|
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;
|
|
|
|
|
2016-05-14 11:15:27 +02:00
|
|
|
|
2015-03-07 02:23:31 +01:00
|
|
|
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();
|
2015-03-07 02:23:31 +01:00
|
|
|
|
|
|
|
public ContainerCrafter(EntityPlayer player){
|
|
|
|
InventoryPlayer inventory = player.inventory;
|
|
|
|
|
2016-11-26 21:32:27 +01:00
|
|
|
this.world = player.world;
|
2015-03-07 02:23:31 +01:00
|
|
|
|
|
|
|
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
|
2016-06-17 13:03:57 +02:00
|
|
|
public ItemStack transferStackInSlot(EntityPlayer player, int index){
|
2016-11-16 16:59:00 +01:00
|
|
|
ItemStack itemstack = StackUtil.getNull();
|
2016-06-17 13:03:57 +02:00
|
|
|
Slot slot = this.inventorySlots.get(index);
|
2015-03-07 02:23:31 +01:00
|
|
|
|
2016-06-17 13:03:57 +02:00
|
|
|
if(slot != null && slot.getHasStack()){
|
|
|
|
ItemStack itemstack1 = slot.getStack();
|
|
|
|
itemstack = itemstack1.copy();
|
2015-03-07 02:23:31 +01:00
|
|
|
|
2016-06-17 13:03:57 +02:00
|
|
|
if(index == 0){
|
|
|
|
if(!this.mergeItemStack(itemstack1, 10, 46, true)){
|
2016-11-16 16:59:00 +01:00
|
|
|
return StackUtil.getNull();
|
2015-10-03 10:16:18 +02:00
|
|
|
}
|
2016-06-17 13:03:57 +02:00
|
|
|
|
|
|
|
slot.onSlotChange(itemstack1, itemstack);
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|
2016-06-17 13:03:57 +02:00
|
|
|
else if(index >= 10 && index < 37){
|
|
|
|
if(!this.mergeItemStack(itemstack1, 37, 46, false)){
|
2016-11-16 16:59:00 +01:00
|
|
|
return StackUtil.getNull();
|
2016-06-17 13:03:57 +02:00
|
|
|
}
|
2015-10-02 16:48:01 +02:00
|
|
|
}
|
2016-06-17 13:03:57 +02:00
|
|
|
else if(index >= 37 && index < 46){
|
|
|
|
if(!this.mergeItemStack(itemstack1, 10, 37, false)){
|
2016-11-16 16:59:00 +01:00
|
|
|
return StackUtil.getNull();
|
2016-06-17 13:03:57 +02:00
|
|
|
}
|
2015-10-02 16:48:01 +02:00
|
|
|
}
|
2016-06-17 13:03:57 +02:00
|
|
|
else if(!this.mergeItemStack(itemstack1, 10, 46, false)){
|
2016-11-16 16:59:00 +01:00
|
|
|
return StackUtil.getNull();
|
2015-10-03 10:16:18 +02:00
|
|
|
}
|
2015-03-07 02:23:31 +01:00
|
|
|
|
2016-11-16 16:59:00 +01:00
|
|
|
if(!StackUtil.isValid(itemstack1)){
|
|
|
|
slot.putStack(StackUtil.getNull());
|
2015-10-02 16:48:01 +02:00
|
|
|
}
|
|
|
|
else{
|
2016-06-17 13:03:57 +02:00
|
|
|
slot.onSlotChanged();
|
2015-10-02 16:48:01 +02:00
|
|
|
}
|
2015-03-07 02:23:31 +01:00
|
|
|
|
2016-11-16 16:59:00 +01:00
|
|
|
if(StackUtil.getStackSize(itemstack1) == StackUtil.getStackSize(itemstack)){
|
|
|
|
return StackUtil.getNull();
|
2015-10-03 10:16:18 +02:00
|
|
|
}
|
2015-03-07 02:23:31 +01:00
|
|
|
|
2016-11-26 21:32:27 +01:00
|
|
|
slot.onTake(player, itemstack1);
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|
2016-06-17 13:03:57 +02:00
|
|
|
|
|
|
|
return itemstack;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canMergeSlot(ItemStack stack, Slot slotIn){
|
|
|
|
return slotIn.inventory != this.craftResult && super.canMergeSlot(stack, slotIn);
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|
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){
|
2016-01-07 23:42:42 +01:00
|
|
|
ItemStack stack = this.craftMatrix.removeStackFromSlot(i);
|
2016-11-16 20:31:16 +01:00
|
|
|
if(StackUtil.isValid(stack)){
|
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
|
2016-05-29 23:49:35 +02:00
|
|
|
public boolean canInteractWith(EntityPlayer player){
|
2015-10-03 10:19:40 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|