TechReborn/src/main/java/techreborn/tiles/TileAutoCraftingTable.java

389 lines
10 KiB
Java
Raw Normal View History

2017-06-20 19:54:45 +02:00
package techreborn.tiles;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
2017-06-20 22:38:51 +02:00
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.inventory.InventoryCrafting;
2017-06-21 17:22:57 +02:00
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
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.
*/
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;
public boolean customRecipe = true;
InventoryCrafting inventoryCrafting = null;
IRecipe lastCustomRecipe = null;
2017-06-20 22:38:51 +02:00
public void setCurrentRecipe(IRecipe recipe, boolean customRecipe) {
if(recipe != null){
currentRecipe = recipe.getRegistryName();
} else {
currentRecipe = null;
}
this.customRecipe = customRecipe;
cachedRecipe = null;
}
public void setCurrentRecipe(ResourceLocation recipe, boolean customRecipe) {
2017-06-20 22:31:21 +02:00
currentRecipe = recipe;
this.customRecipe = customRecipe;
cachedRecipe = null;
2017-06-20 22:31:21 +02:00
}
@Nullable
2017-06-21 17:22:57 +02:00
public IRecipe getIRecipe() {
if(customRecipe){
InventoryCrafting crafting = getCraftingInventory();
for(IRecipe testRecipe : CraftingManager.REGISTRY){
if(testRecipe.matches(crafting, world)){
return testRecipe;
}
}
}
2017-06-21 17:22:57 +02:00
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();
}
public InventoryCrafting getCraftingInventory(){
if(inventoryCrafting == null){
inventoryCrafting = new InventoryCrafting(new Container() {
@Override
public boolean canInteractWith(EntityPlayer playerIn) {
return false;
}
}, 3, 3);
}
for (int i = 0; i < 8; i++) {
inventoryCrafting.setInventorySlotContents(i, inventory.getStackInSlot(i));
}
return inventoryCrafting;
}
2017-06-21 17:22:57 +02:00
@Override
public void update() {
super.update();
if (world.isRemote) {
return;
}
2017-06-21 17:22:57 +02:00
IRecipe recipe = getIRecipe();
if (recipe != null || customRecipe) {
2017-06-21 17:22:57 +02:00
if (progress >= maxProgress) {
if (make(recipe)) {
progress = 0;
}
} else {
if (canMake(recipe)) {
if (canUseEnergy(euTick)) {
progress++;
useEnergy(euTick);
}
}
}
}
}
public boolean canMake(IRecipe recipe) {
if(customRecipe){
recipe = getIRecipe();
}
if (recipe != null && recipe.canFit(3, 3)) {
2017-06-21 17:22:57 +02:00
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);
int requiredSize = customRecipe ? 1 : 0;
if (stacksInSlots[i] > requiredSize) {
2017-06-21 17:35:15 +02:00
if (ingredient.apply(stack)) {
foundIngredient = true;
stacksInSlots[i]--;
2017-06-21 17:35:15 +02:00
break;
}
2017-06-21 17:22:57 +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)) {
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)) {
if(recipe == null && customRecipe){
if(lastCustomRecipe == null){
return false;
}//Should be uptodate as we just set it in canMake
recipe = lastCustomRecipe;
}
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
//TOOD check items to be retuned
break;
}
2017-06-21 17:22:57 +02:00
}
}
}
ItemStack output = inventory.getStackInSlot(9);
//TODO fire forge recipe event
ItemStack ouputStack = recipe.getCraftingResult(getCraftingInventory());
2017-06-21 17:22:57 +02:00
if (output.isEmpty()) {
inventory.setInventorySlotContents(9, ouputStack.copy());
2017-06-21 17:22:57 +02:00
} else {
//TODO use ouputStack in someway?
2017-06-21 17:22:57 +02:00
output.grow(recipe.getRecipeOutput().getCount());
}
return true;
}
return false;
2017-06-20 22:31:21 +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)
.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) {
if (currentRecipe != null) {
2017-06-21 18:13:07 +02:00
tag.setString("currentRecipe", currentRecipe.toString());
}
tag.setBoolean("customRecipe", customRecipe);
2017-06-21 18:05:48 +02:00
return super.writeToNBT(tag);
}
@Override
public void readFromNBT(NBTTagCompound tag) {
if (tag.hasKey("currentRecipe")) {
2017-06-21 18:13:07 +02:00
currentRecipe = new ResourceLocation(tag.getString("currentRecipe"));
}
customRecipe = tag.getBoolean("customRecipe");
2017-06-21 18:05:48 +02:00
super.readFromNBT(tag);
}
public boolean isItemValidForRecipeSlot(IRecipe recipe, ItemStack stack, int slotID) {
if (recipe == null) {
return true;
}
2017-06-22 00:31:40 +02:00
int bestSlot = findBestSlotForStack(recipe, stack);
if (bestSlot != -1) {
2017-06-22 00:31:40 +02:00
return bestSlot == slotID;
}
return true;
}
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);
if (ingredient != Ingredient.EMPTY && ingredient.apply(stack)) {
if (stackInSlot.isEmpty()) {
2017-06-22 00:31:40 +02:00
possibleSlots.add(i);
} 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;
for (Integer slot : possibleSlots) {
2017-06-22 00:31:40 +02:00
ItemStack slotStack = inventory.getStackInSlot(slot);
if (slotStack.isEmpty()) {
2017-06-22 00:31:40 +02:00
return slot;
}
if (smallestCount == null) {
2017-06-22 00:31:40 +02:00
smallestCount = Pair.of(slot, slotStack.getCount());
} else if (smallestCount.getRight() >= slotStack.getCount()) {
2017-06-22 00:31:40 +02:00
smallestCount = Pair.of(slot, slotStack.getCount());
}
}
if (smallestCount != null) {
2017-06-22 00:31:40 +02:00
return smallestCount.getLeft();
}
return -1;
}
2017-06-22 00:31:40 +02:00
@Override
public boolean isItemValidForSlot(int index, ItemStack stack) {
int bestSlot = findBestSlotForStack(getIRecipe(), stack);
if (bestSlot != -1) {
2017-06-22 00:31:40 +02:00
return index == bestSlot;
}
2017-06-22 00:31:40 +02:00
return super.isItemValidForSlot(index, stack);
}
2017-06-22 00:31:40 +02:00
@Override
public boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direction) {
if (index == 9) {
2017-06-22 00:31:40 +02:00
return false;
}
int bestSlot = findBestSlotForStack(getIRecipe(), itemStackIn);
if (bestSlot != -1) {
2017-06-22 00:31:40 +02:00
return index == bestSlot;
}
2017-06-22 00:31:40 +02:00
return false;
}
@Override
public int[] getSlotsForFace(EnumFacing side) {
return new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
}
2017-06-20 19:54:45 +02:00
}