2017-05-18 18:13:08 +02:00
|
|
|
/*
|
|
|
|
* This file is part of Industrial Wires.
|
2018-02-28 21:06:33 +01:00
|
|
|
* Copyright (C) 2016-2018 malte0811
|
2017-05-18 18:13:08 +02:00
|
|
|
* Industrial Wires is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
* Industrial Wires is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Industrial Wires. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package malte0811.industrialWires.crafting;
|
|
|
|
|
2017-05-19 17:45:30 +02:00
|
|
|
import blusunrize.immersiveengineering.api.ApiUtils;
|
2017-05-18 18:13:08 +02:00
|
|
|
import malte0811.industrialWires.IndustrialWires;
|
|
|
|
import malte0811.industrialWires.controlpanel.Lock;
|
|
|
|
import malte0811.industrialWires.controlpanel.PanelComponent;
|
|
|
|
import malte0811.industrialWires.items.ItemKey;
|
|
|
|
import malte0811.industrialWires.items.ItemPanelComponent;
|
|
|
|
import net.minecraft.inventory.InventoryCrafting;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.item.crafting.IRecipe;
|
2017-07-10 17:59:43 +02:00
|
|
|
import net.minecraft.item.crafting.Ingredient;
|
2017-05-18 18:13:08 +02:00
|
|
|
import net.minecraft.util.NonNullList;
|
|
|
|
import net.minecraft.world.World;
|
2017-07-09 18:10:06 +02:00
|
|
|
import net.minecraftforge.registries.IForgeRegistryEntry;
|
2017-05-18 18:13:08 +02:00
|
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
2018-08-05 16:49:55 +02:00
|
|
|
import javax.annotation.Nullable;
|
2017-05-19 17:45:30 +02:00
|
|
|
|
2017-07-09 18:10:06 +02:00
|
|
|
public class RecipeKeyLock extends IForgeRegistryEntry.Impl<IRecipe> implements IRecipe {
|
2017-05-18 18:13:08 +02:00
|
|
|
|
|
|
|
@Override
|
2018-08-05 16:49:55 +02:00
|
|
|
public boolean matches(@Nonnull InventoryCrafting inv, @Nullable World worldIn) {
|
2017-05-18 18:13:08 +02:00
|
|
|
return getLockId(inv) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public ItemStack getCraftingResult(@Nonnull InventoryCrafting inv) {
|
2017-05-20 17:14:43 +02:00
|
|
|
ItemStack ret = new ItemStack(IndustrialWires.key, 1, 1);
|
2017-05-18 18:13:08 +02:00
|
|
|
ItemKey.setId(ret, getLockId(inv));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-07-09 18:10:06 +02:00
|
|
|
public boolean canFit(int width, int height) {
|
|
|
|
return width*height>=2;
|
2017-05-18 18:13:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public ItemStack getRecipeOutput() {
|
2017-05-20 17:14:43 +02:00
|
|
|
return new ItemStack(IndustrialWires.key, 1, 1);
|
2017-05-18 18:13:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public NonNullList<ItemStack> getRemainingItems(@Nonnull InventoryCrafting inv) {
|
|
|
|
NonNullList<ItemStack> ret = NonNullList.withSize(inv.getSizeInventory(), ItemStack.EMPTY);
|
|
|
|
for (int i = 0; i < ret.size(); i++) {
|
|
|
|
ItemStack here = inv.getStackInSlot(i);
|
|
|
|
if (here.getItem() == IndustrialWires.panelComponent) {
|
2017-05-19 17:45:30 +02:00
|
|
|
ret.set(i, ApiUtils.copyStackWithAmount(here, 1));
|
2017-05-18 18:13:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int getLockId(@Nonnull InventoryCrafting inv) {
|
|
|
|
int id = 0;
|
|
|
|
boolean hasKey = false;
|
|
|
|
for (int i = 0; i < inv.getSizeInventory(); i++) {
|
|
|
|
ItemStack here = inv.getStackInSlot(i);
|
2017-05-20 18:50:04 +02:00
|
|
|
if (here.getItem() == IndustrialWires.key && here.getMetadata()==0) {
|
|
|
|
if (hasKey) {//too many keys
|
2017-05-18 18:13:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
hasKey = true;
|
|
|
|
} else if (here.getItem() == IndustrialWires.panelComponent) {
|
|
|
|
if (id != 0) {//too many locks/components
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
PanelComponent pc = ItemPanelComponent.componentFromStack(here);
|
|
|
|
if (pc instanceof Lock) {
|
|
|
|
id = ((Lock) pc).getLockID();
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!hasKey) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
//assumes that the recipe is valid
|
|
|
|
private ItemStack getKey(@Nonnull InventoryCrafting inv) {
|
|
|
|
for (int i = 0; i < inv.getSizeInventory(); i++) {
|
|
|
|
ItemStack here = inv.getStackInSlot(i);
|
|
|
|
if (here.getItem() == IndustrialWires.key) {
|
|
|
|
return here;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ItemStack.EMPTY;
|
|
|
|
}
|
2017-07-10 17:59:43 +02:00
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public NonNullList<Ingredient> getIngredients() {
|
|
|
|
NonNullList<Ingredient> ret = NonNullList.withSize(2, Ingredient.EMPTY);
|
|
|
|
ret.set(0, Ingredient.fromStacks(new ItemStack(IndustrialWires.key, 1, 0)));
|
|
|
|
ret.set(1, Ingredient.fromStacks(new ItemStack(IndustrialWires.panelComponent, 1, 7)));
|
|
|
|
return ret;
|
|
|
|
}
|
2017-05-18 18:13:08 +02:00
|
|
|
}
|