2017-06-20 19:54:45 +02:00
|
|
|
package techreborn.tiles;
|
|
|
|
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2017-06-20 22:38:51 +02:00
|
|
|
import net.minecraft.inventory.IInventory;
|
2017-06-21 21:26:56 +02:00
|
|
|
import net.minecraft.inventory.ISidedInventory;
|
2017-06-21 17:22:57 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
2017-06-20 22:31:21 +02:00
|
|
|
import net.minecraft.item.crafting.IRecipe;
|
2017-06-21 17:22:57 +02:00
|
|
|
import net.minecraft.item.crafting.Ingredient;
|
2017-06-21 18:05:48 +02:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2017-06-20 19:54:45 +02:00
|
|
|
import net.minecraft.util.EnumFacing;
|
2017-06-20 22:31:21 +02:00
|
|
|
import net.minecraft.util.ResourceLocation;
|
|
|
|
import net.minecraftforge.fml.common.registry.ForgeRegistries;
|
2017-06-21 17:22:57 +02:00
|
|
|
import org.apache.commons.lang3.tuple.Pair;
|
2017-06-20 22:38:51 +02:00
|
|
|
import reborncore.api.tile.IInventoryProvider;
|
2017-06-20 19:54:45 +02:00
|
|
|
import reborncore.common.powerSystem.TilePowerAcceptor;
|
2017-06-20 22:38:51 +02:00
|
|
|
import reborncore.common.util.Inventory;
|
2017-06-21 17:22:57 +02:00
|
|
|
import reborncore.common.util.ItemUtils;
|
2017-06-20 19:54:45 +02:00
|
|
|
import techreborn.client.container.IContainerProvider;
|
|
|
|
import techreborn.client.container.builder.BuiltContainer;
|
|
|
|
import techreborn.client.container.builder.ContainerBuilder;
|
|
|
|
|
2017-06-20 22:31:21 +02:00
|
|
|
import javax.annotation.Nullable;
|
2017-06-22 00:31:40 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2017-06-20 22:31:21 +02:00
|
|
|
|
2017-06-20 19:54:45 +02:00
|
|
|
/**
|
|
|
|
* Created by modmuss50 on 20/06/2017.
|
|
|
|
*/
|
2017-06-21 21:26:56 +02:00
|
|
|
public class TileAutoCraftingTable extends TilePowerAcceptor implements IContainerProvider, IInventoryProvider, ISidedInventory {
|
2017-06-20 22:31:21 +02:00
|
|
|
|
|
|
|
ResourceLocation currentRecipe;
|
|
|
|
|
2017-06-22 00:31:40 +02:00
|
|
|
public Inventory inventory = new Inventory(10, "TileAutoCraftingTable", 64, this);
|
2017-06-21 17:22:57 +02:00
|
|
|
public int progress;
|
|
|
|
public int maxProgress = 120;
|
|
|
|
public int euTick = 10;
|
|
|
|
public Pair<ResourceLocation, IRecipe> cachedRecipe;
|
2017-06-20 22:38:51 +02:00
|
|
|
|
2017-06-21 17:22:57 +02:00
|
|
|
public void setCurrentRecipe(ResourceLocation recipe) {
|
2017-06-20 22:31:21 +02:00
|
|
|
currentRecipe = recipe;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
2017-06-21 17:22:57 +02:00
|
|
|
public IRecipe getIRecipe() {
|
|
|
|
if (currentRecipe == null) {
|
2017-06-20 22:31:21 +02:00
|
|
|
return null;
|
|
|
|
}
|
2017-06-21 17:22:57 +02:00
|
|
|
if (cachedRecipe == null || !cachedRecipe.getLeft().equals(currentRecipe)) {
|
|
|
|
IRecipe recipe = ForgeRegistries.RECIPES.getValue(currentRecipe);
|
|
|
|
if (recipe != null) {
|
|
|
|
cachedRecipe = Pair.of(currentRecipe, recipe);
|
|
|
|
return recipe;
|
|
|
|
}
|
|
|
|
cachedRecipe = null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return cachedRecipe.getRight();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void update() {
|
|
|
|
super.update();
|
2017-06-22 00:39:40 +02:00
|
|
|
if (world.isRemote) {
|
2017-06-21 21:26:56 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-06-21 17:22:57 +02:00
|
|
|
IRecipe recipe = getIRecipe();
|
|
|
|
if (recipe != null) {
|
|
|
|
if (progress >= maxProgress) {
|
|
|
|
if (make(recipe)) {
|
|
|
|
progress = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (canMake(recipe)) {
|
|
|
|
if (canUseEnergy(euTick)) {
|
|
|
|
progress++;
|
|
|
|
useEnergy(euTick);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean canMake(IRecipe recipe) {
|
|
|
|
if (recipe.canFit(3, 3)) {
|
|
|
|
boolean missingOutput = false;
|
2017-06-21 17:35:15 +02:00
|
|
|
int[] stacksInSlots = new int[9];
|
|
|
|
for (int i = 0; i < 9; i++) {
|
|
|
|
stacksInSlots[i] = inventory.getStackInSlot(i).getCount();
|
|
|
|
}
|
2017-06-21 17:22:57 +02:00
|
|
|
for (Ingredient ingredient : recipe.getIngredients()) {
|
|
|
|
if (ingredient != Ingredient.EMPTY) {
|
|
|
|
boolean foundIngredient = false;
|
|
|
|
for (int i = 0; i < 9; i++) {
|
|
|
|
ItemStack stack = inventory.getStackInSlot(i);
|
2017-06-22 00:39:40 +02:00
|
|
|
if (stacksInSlots[i] > 0) {
|
2017-06-21 17:35:15 +02:00
|
|
|
if (ingredient.apply(stack)) {
|
|
|
|
foundIngredient = true;
|
2017-06-22 00:39:40 +02:00
|
|
|
stacksInSlots[i]--;
|
2017-06-21 17:35:15 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-06-21 17:22:57 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-22 00:39:40 +02:00
|
|
|
if (!foundIngredient) {
|
2017-06-21 17:22:57 +02:00
|
|
|
missingOutput = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!missingOutput) {
|
|
|
|
if (hasOutputSpace(recipe.getRecipeOutput())) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasOutputSpace(ItemStack output) {
|
|
|
|
ItemStack stack = inventory.getStackInSlot(9);
|
|
|
|
if (stack.isEmpty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (ItemUtils.isItemEqual(stack, output, true, true)) {
|
2017-06-22 00:39:40 +02:00
|
|
|
if (stack.getMaxStackSize() > stack.getCount() + output.getCount()) {
|
2017-06-21 17:22:57 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean make(IRecipe recipe) {
|
|
|
|
if (canMake(recipe)) {
|
2017-06-22 00:39:40 +02:00
|
|
|
for (int i = 0; i < recipe.getIngredients().size(); i++) {
|
|
|
|
Ingredient ingredient = recipe.getIngredients().get(i);
|
|
|
|
//Looks for the best slot to take it from
|
|
|
|
ItemStack bestSlot = inventory.getStackInSlot(i);
|
|
|
|
if (ingredient.apply(bestSlot)) {
|
|
|
|
bestSlot.shrink(1);
|
|
|
|
} else {
|
|
|
|
for (int j = 0; j < 9; j++) {
|
|
|
|
ItemStack stack = inventory.getStackInSlot(j);
|
|
|
|
if (ingredient.apply(stack)) {
|
|
|
|
stack.shrink(1); //TODO is this right? or do I need to use it as an actull crafting grid
|
|
|
|
break;
|
|
|
|
}
|
2017-06-21 17:22:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ItemStack output = inventory.getStackInSlot(9);
|
|
|
|
if (output.isEmpty()) {
|
|
|
|
inventory.setInventorySlotContents(9, recipe.getRecipeOutput().copy());
|
|
|
|
} else {
|
|
|
|
output.grow(recipe.getRecipeOutput().getCount());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2017-06-20 22:31:21 +02:00
|
|
|
}
|
|
|
|
|
2017-06-22 00:39:40 +02:00
|
|
|
public boolean hasIngredient(Ingredient ingredient) {
|
2017-06-21 18:05:48 +02:00
|
|
|
for (int i = 0; i < 9; i++) {
|
|
|
|
ItemStack stack = inventory.getStackInSlot(i);
|
|
|
|
if (ingredient.apply(stack)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-20 19:54:45 +02:00
|
|
|
public TileAutoCraftingTable() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public double getBaseMaxPower() {
|
2017-06-21 11:00:53 +02:00
|
|
|
return 10000;
|
2017-06-20 19:54:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public double getBaseMaxOutput() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public double getBaseMaxInput() {
|
2017-06-21 11:00:53 +02:00
|
|
|
return 32;
|
2017-06-20 19:54:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canAcceptEnergy(EnumFacing enumFacing) {
|
2017-06-21 11:00:53 +02:00
|
|
|
return true;
|
2017-06-20 19:54:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canProvideEnergy(EnumFacing enumFacing) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BuiltContainer createContainer(EntityPlayer player) {
|
2017-06-20 22:38:51 +02:00
|
|
|
return new ContainerBuilder("autocraftingTable").player(player.inventory).inventory().hotbar()
|
2017-06-21 11:00:53 +02:00
|
|
|
.addInventory().tile(this)
|
|
|
|
.slot(0, 28, 25).slot(1, 46, 25).slot(2, 64, 25)
|
|
|
|
.slot(3, 28, 43).slot(4, 46, 43).slot(5, 64, 43)
|
|
|
|
.slot(6, 28, 61).slot(7, 46, 61).slot(8, 64, 61)
|
2017-06-22 00:39:40 +02:00
|
|
|
.outputSlot(9, 145, 42).syncEnergyValue()
|
2017-06-21 17:22:57 +02:00
|
|
|
.syncIntegerValue(this::getProgress, this::setProgress)
|
|
|
|
.syncIntegerValue(this::getMaxProgress, this::setMaxProgress)
|
|
|
|
.addInventory().create();
|
2017-06-20 19:54:45 +02:00
|
|
|
}
|
2017-06-20 22:31:21 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canBeUpgraded() {
|
|
|
|
return false;
|
|
|
|
}
|
2017-06-20 22:38:51 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public IInventory getInventory() {
|
|
|
|
return inventory;
|
|
|
|
}
|
2017-06-21 17:22:57 +02:00
|
|
|
|
|
|
|
public int getProgress() {
|
|
|
|
return progress;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setProgress(int progress) {
|
|
|
|
this.progress = progress;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getMaxProgress() {
|
|
|
|
if (maxProgress == 0) {
|
|
|
|
maxProgress = 1;
|
|
|
|
}
|
|
|
|
return maxProgress;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMaxProgress(int maxProgress) {
|
|
|
|
this.maxProgress = maxProgress;
|
|
|
|
}
|
2017-06-21 18:05:48 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public NBTTagCompound writeToNBT(NBTTagCompound tag) {
|
2017-06-22 00:39:40 +02:00
|
|
|
if (currentRecipe != null) {
|
2017-06-21 18:13:07 +02:00
|
|
|
tag.setString("currentRecipe", currentRecipe.toString());
|
|
|
|
}
|
2017-06-21 18:05:48 +02:00
|
|
|
return super.writeToNBT(tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readFromNBT(NBTTagCompound tag) {
|
2017-06-22 00:39:40 +02:00
|
|
|
if (tag.hasKey("currentRecipe")) {
|
2017-06-21 18:13:07 +02:00
|
|
|
currentRecipe = new ResourceLocation(tag.getString("currentRecipe"));
|
|
|
|
}
|
2017-06-21 18:05:48 +02:00
|
|
|
super.readFromNBT(tag);
|
|
|
|
}
|
2017-06-21 21:26:56 +02:00
|
|
|
|
2017-06-22 00:39:40 +02:00
|
|
|
public boolean isItemValidForRecipeSlot(IRecipe recipe, ItemStack stack, int slotID) {
|
|
|
|
if (recipe == null) {
|
2017-06-21 21:26:56 +02:00
|
|
|
return true;
|
|
|
|
}
|
2017-06-22 00:31:40 +02:00
|
|
|
int bestSlot = findBestSlotForStack(recipe, stack);
|
2017-06-22 00:39:40 +02:00
|
|
|
if (bestSlot != -1) {
|
2017-06-22 00:31:40 +02:00
|
|
|
return bestSlot == slotID;
|
|
|
|
}
|
2017-06-21 21:26:56 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-22 00:39:40 +02:00
|
|
|
public int findBestSlotForStack(IRecipe recipe, ItemStack stack) {
|
|
|
|
if (recipe == null) {
|
2017-06-22 00:31:40 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
List<Integer> possibleSlots = new ArrayList<>();
|
|
|
|
for (int i = 0; i < 9; i++) {
|
|
|
|
ItemStack stackInSlot = inventory.getStackInSlot(i);
|
|
|
|
Ingredient ingredient = recipe.getIngredients().get(i);
|
2017-06-22 00:39:40 +02:00
|
|
|
if (ingredient != Ingredient.EMPTY && ingredient.apply(stack)) {
|
|
|
|
if (stackInSlot.isEmpty()) {
|
2017-06-22 00:31:40 +02:00
|
|
|
possibleSlots.add(i);
|
2017-06-22 00:39:40 +02:00
|
|
|
} else if (stackInSlot.getItem() == stack.getItem() && stackInSlot.getItemDamage() == stack.getItemDamage()) {
|
|
|
|
if (stackInSlot.getMaxStackSize() >= stackInSlot.getCount() + stack.getCount()) {
|
2017-06-22 00:31:40 +02:00
|
|
|
possibleSlots.add(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//Slot, count
|
|
|
|
Pair<Integer, Integer> smallestCount = null;
|
2017-06-22 00:39:40 +02:00
|
|
|
for (Integer slot : possibleSlots) {
|
2017-06-22 00:31:40 +02:00
|
|
|
ItemStack slotStack = inventory.getStackInSlot(slot);
|
2017-06-22 00:39:40 +02:00
|
|
|
if (slotStack.isEmpty()) {
|
2017-06-22 00:31:40 +02:00
|
|
|
return slot;
|
|
|
|
}
|
2017-06-22 00:39:40 +02:00
|
|
|
if (smallestCount == null) {
|
2017-06-22 00:31:40 +02:00
|
|
|
smallestCount = Pair.of(slot, slotStack.getCount());
|
2017-06-22 00:39:40 +02:00
|
|
|
} else if (smallestCount.getRight() >= slotStack.getCount()) {
|
2017-06-22 00:31:40 +02:00
|
|
|
smallestCount = Pair.of(slot, slotStack.getCount());
|
|
|
|
}
|
|
|
|
}
|
2017-06-22 00:39:40 +02:00
|
|
|
if (smallestCount != null) {
|
2017-06-22 00:31:40 +02:00
|
|
|
return smallestCount.getLeft();
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2017-06-21 21:26:56 +02:00
|
|
|
|
2017-06-22 00:31:40 +02:00
|
|
|
@Override
|
|
|
|
public boolean isItemValidForSlot(int index, ItemStack stack) {
|
|
|
|
int bestSlot = findBestSlotForStack(getIRecipe(), stack);
|
2017-06-22 00:39:40 +02:00
|
|
|
if (bestSlot != -1) {
|
2017-06-22 00:31:40 +02:00
|
|
|
return index == bestSlot;
|
2017-06-21 21:26:56 +02:00
|
|
|
}
|
2017-06-22 00:31:40 +02:00
|
|
|
return super.isItemValidForSlot(index, stack);
|
|
|
|
}
|
2017-06-21 21:26:56 +02:00
|
|
|
|
2017-06-22 00:31:40 +02:00
|
|
|
@Override
|
|
|
|
public boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direction) {
|
2017-06-22 00:39:40 +02:00
|
|
|
if (index == 9) {
|
2017-06-22 00:31:40 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int bestSlot = findBestSlotForStack(getIRecipe(), itemStackIn);
|
2017-06-22 00:39:40 +02:00
|
|
|
if (bestSlot != -1) {
|
2017-06-22 00:31:40 +02:00
|
|
|
return index == bestSlot;
|
2017-06-21 21:26:56 +02:00
|
|
|
}
|
2017-06-22 00:31:40 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int[] getSlotsForFace(EnumFacing side) {
|
2017-06-22 00:39:40 +02:00
|
|
|
return new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
2017-06-21 21:26:56 +02:00
|
|
|
}
|
2017-06-20 19:54:45 +02:00
|
|
|
}
|