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

133 lines
4.4 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
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2015-08-29 14:33:25 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.inventory;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
import invtweaks.api.container.InventoryContainer;
import net.minecraft.block.Block;
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.item.crafting.IRecipe;
import net.minecraft.world.World;
@InventoryContainer
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;
2016-11-26 21:32:27 +01:00
this.world = player.world;
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 index){
ItemStack itemstack = StackUtil.getNull();
Slot slot = this.inventorySlots.get(index);
if(slot != null && slot.getHasStack()){
ItemStack itemstack1 = slot.getStack();
itemstack = itemstack1.copy();
if(index == 0){
if(!this.mergeItemStack(itemstack1, 10, 46, true)){
return StackUtil.getNull();
2015-10-03 10:16:18 +02:00
}
slot.onSlotChange(itemstack1, itemstack);
}
else if(index >= 10 && index < 37){
if(!this.mergeItemStack(itemstack1, 37, 46, false)){
return StackUtil.getNull();
}
2015-10-02 16:48:01 +02:00
}
else if(index >= 37 && index < 46){
if(!this.mergeItemStack(itemstack1, 10, 37, false)){
return StackUtil.getNull();
}
2015-10-02 16:48:01 +02:00
}
else if(!this.mergeItemStack(itemstack1, 10, 46, false)){
return StackUtil.getNull();
2015-10-03 10:16:18 +02:00
}
if(!StackUtil.isValid(itemstack1)){
slot.putStack(StackUtil.getNull());
2015-10-02 16:48:01 +02:00
}
else{
slot.onSlotChanged();
2015-10-02 16:48:01 +02:00
}
if(StackUtil.getStackSize(itemstack1) == StackUtil.getStackSize(itemstack)){
return StackUtil.getNull();
2015-10-03 10:16:18 +02:00
}
2016-11-26 21:32:27 +01:00
slot.onTake(player, itemstack1);
}
return itemstack;
}
@Override
public boolean canMergeSlot(ItemStack stack, Slot slotIn){
return slotIn.inventory != this.craftResult && super.canMergeSlot(stack, slotIn);
}
2015-10-03 10:19:40 +02:00
@Override
public void onContainerClosed(EntityPlayer player){
super.onContainerClosed(player);
for(int i = 0; i < 9; ++i){
ItemStack stack = this.craftMatrix.removeStackFromSlot(i);
if(StackUtil.isValid(stack)){
if(!player.addItemStackToInventory(stack))
if(!this.world.isRemote) Block.spawnAsEntity(world, player.getPosition(), stack);
2015-10-03 10:19:40 +02:00
}
}
}
@Override
public void onCraftMatrixChanged(IInventory inv){
IRecipe output = CraftingManager.findMatchingRecipe(this.craftMatrix, this.world);
ItemStack stack = ItemStack.EMPTY;
if(output != null) stack = output.getRecipeOutput();
this.craftResult.setInventorySlotContents(0, stack.copy());
2015-10-03 10:19:40 +02:00
}
@Override
public boolean canInteractWith(EntityPlayer player){
2015-10-03 10:19:40 +02:00
return true;
}
}