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 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-20 19:54:45 +02:00
|
|
|
/**
|
|
|
|
* Created by modmuss50 on 20/06/2017.
|
|
|
|
*/
|
2017-06-20 22:38:51 +02:00
|
|
|
public class TileAutoCraftingTable extends TilePowerAcceptor implements IContainerProvider, IInventoryProvider {
|
2017-06-20 22:31:21 +02:00
|
|
|
|
|
|
|
ResourceLocation currentRecipe;
|
|
|
|
|
2017-06-20 22:38:51 +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();
|
|
|
|
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-21 17:35:15 +02:00
|
|
|
if(stacksInSlots[i] > 0){
|
|
|
|
if (ingredient.apply(stack)) {
|
|
|
|
foundIngredient = true;
|
|
|
|
stacksInSlots[i] --;
|
|
|
|
break;
|
|
|
|
}
|
2017-06-21 17:22:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!foundIngredient){
|
|
|
|
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)) {
|
|
|
|
if (stack.getMaxStackSize() < stack.getCount() + output.getCount()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean make(IRecipe recipe) {
|
|
|
|
if (canMake(recipe)) {
|
|
|
|
for (Ingredient ingredient : recipe.getIngredients()) {
|
|
|
|
for (int i = 0; i < 9; i++) {
|
|
|
|
ItemStack stack = inventory.getStackInSlot(i);
|
|
|
|
if (ingredient.apply(stack)) {
|
|
|
|
stack.shrink(1); //TODO is this right? or do I need to use it as an actull crafting grid
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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-21 18:05:48 +02:00
|
|
|
public boolean hasIngredient(Ingredient ingredient){
|
|
|
|
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-21 17:22:57 +02:00
|
|
|
.outputSlot(9, 145, 42)
|
|
|
|
.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-21 18:13:07 +02:00
|
|
|
if(currentRecipe != null){
|
|
|
|
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-21 18:13:07 +02:00
|
|
|
if(tag.hasKey("currentRecipe")){
|
|
|
|
currentRecipe = new ResourceLocation(tag.getString("currentRecipe"));
|
|
|
|
}
|
2017-06-21 18:05:48 +02:00
|
|
|
super.readFromNBT(tag);
|
|
|
|
}
|
2017-06-20 19:54:45 +02:00
|
|
|
}
|