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

317 lines
10 KiB
Java
Raw Normal View History

2015-05-10 20:00:58 +02:00
package techreborn.tiles;
2015-08-10 20:28:52 +02:00
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
2015-05-10 20:00:58 +02:00
import net.minecraft.entity.player.EntityPlayer;
2015-08-10 20:28:52 +02:00
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.*;
import net.minecraft.util.EnumFacing;
2015-11-23 20:19:18 +01:00
import net.minecraftforge.fml.common.registry.GameRegistry;
2017-01-08 13:10:13 +01:00
import reborncore.api.recipe.IBaseRecipeType;
import reborncore.api.recipe.RecipeHandler;
import reborncore.api.tile.IInventoryProvider;
2016-10-08 21:46:16 +02:00
import reborncore.common.IWrenchable;
2016-11-04 21:03:05 +01:00
import reborncore.common.tile.TileLegacyMachineBase;
2015-11-08 13:15:45 +01:00
import reborncore.common.util.Inventory;
import reborncore.common.util.ItemUtils;
2017-01-08 13:10:13 +01:00
2016-04-03 15:07:45 +02:00
import techreborn.api.Reference;
import techreborn.init.ModBlocks;
2015-05-10 20:00:58 +02:00
2016-11-04 21:03:05 +01:00
public class TileAlloyFurnace extends TileLegacyMachineBase implements IWrenchable, IInventoryProvider {
2016-03-25 10:47:34 +01:00
2017-01-08 13:10:13 +01:00
public int tickTime;
public Inventory inventory = new Inventory(4, "TileAlloyFurnace", 64, this);
public int burnTime;
public int currentItemBurnTime;
public int cookTime;
int input1 = 0;
int input2 = 1;
int output = 2;
int fuel = 3;
public TileAlloyFurnace() {
}
/**
* Returns the number of ticks that the supplied fuel item will keep the
* furnace burning, or 0 if the item isn't fuel
*/
public static int getItemBurnTime(final ItemStack stack) {
if (stack == null) {
return 0;
} else {
final Item item = stack.getItem();
if (item instanceof ItemBlock && Block.getBlockFromItem(item) != Blocks.AIR) {
final Block block = Block.getBlockFromItem(item);
if (block == Blocks.WOODEN_SLAB) {
return 150;
}
if (block.getMaterial(block.getDefaultState()) == Material.WOOD) {
return 300;
}
if (block == Blocks.COAL_BLOCK) {
return 16000;
}
}
if (item instanceof ItemTool && ((ItemTool) item).getToolMaterialName().equals("WOOD"))
return 200;
if (item instanceof ItemSword && ((ItemSword) item).getToolMaterialName().equals("WOOD"))
return 200;
// if (item instanceof ItemHoe && ((ItemHoe)
// item).getToolMaterialName().equals("WOOD")) return 200;
if (item == Items.STICK)
return 100;
if (item == Items.COAL)
return 1600;
if (item == Items.LAVA_BUCKET)
return 20000;
if (item == Item.getItemFromBlock(Blocks.SAPLING))
return 100;
if (item == Items.BLAZE_ROD)
return 2400;
return GameRegistry.getFuelValue(stack);
}
}
@Override
public void updateEntity() {
super.updateEntity();
final boolean flag = this.burnTime > 0;
boolean flag1 = false;
if (this.burnTime > 0) {
--this.burnTime;
}
if (!this.world.isRemote) {
if (this.burnTime != 0 || this.getStackInSlot(this.input1) != ItemStack.EMPTY && this.getStackInSlot(this.fuel) != ItemStack.EMPTY) {
if (this.burnTime == 0 && this.canSmelt()) {
this.currentItemBurnTime = this.burnTime = TileAlloyFurnace.getItemBurnTime(this.getStackInSlot(this.fuel));
if (this.burnTime > 0) {
flag1 = true;
if (this.getStackInSlot(this.fuel) != ItemStack.EMPTY) {
this.decrStackSize(this.fuel, 1);
}
}
}
if (this.isBurning() && this.canSmelt()) {
++this.cookTime;
if (this.cookTime == 200) {
this.cookTime = 0;
this.smeltItem();
flag1 = true;
}
} else {
this.cookTime = 0;
}
}
if (flag != this.burnTime > 0) {
flag1 = true;
// TODO sync on/off
}
}
if (flag1) {
this.markDirty();
}
}
public boolean hasAllInputs(final IBaseRecipeType recipeType) {
if (recipeType == null) {
return false;
}
for (final ItemStack input : recipeType.getInputs()) {
Boolean hasItem = false;
for (int inputslot = 0; inputslot < 2; inputslot++) {
if (ItemUtils.isItemEqual(input, this.inventory.getStackInSlot(inputslot), true, true,
recipeType.useOreDic()) && this.inventory.getStackInSlot(inputslot).getCount() >= input.getCount()) {
hasItem = true;
}
}
if (!hasItem)
return false;
}
return true;
}
private boolean canSmelt() {
if (this.getStackInSlot(this.input1) == ItemStack.EMPTY || this.getStackInSlot(this.input2) == ItemStack.EMPTY) {
return false;
} else {
ItemStack itemstack = null;
for (final IBaseRecipeType recipeType : RecipeHandler.getRecipeClassFromName(Reference.alloySmelteRecipe)) {
if (this.hasAllInputs(recipeType)) {
itemstack = recipeType.getOutput(0);
break;
}
}
if (itemstack == null)
return false;
if (this.getStackInSlot(this.output) == ItemStack.EMPTY)
return true;
if (!this.getStackInSlot(this.output).isItemEqual(itemstack))
return false;
final int result = this.getStackInSlot(this.output).getCount() + itemstack.getCount();
return result <= this.getInventoryStackLimit() && result <= this.getStackInSlot(this.output).getMaxStackSize(); // Forge
// BugFix:
// Make
// it
// respect
// stack
// sizes
// properly.
}
}
/**
* Turn one item from the furnace source stack into the appropriate smelted
* item in the furnace result stack
*/
public void smeltItem() {
if (this.canSmelt()) {
ItemStack itemstack = null;
for (final IBaseRecipeType recipeType : RecipeHandler.getRecipeClassFromName(Reference.alloySmelteRecipe)) {
if (this.hasAllInputs(recipeType)) {
itemstack = recipeType.getOutput(0);
break;
}
if (itemstack != ItemStack.EMPTY) {
break;
}
}
if (this.getStackInSlot(this.output) == ItemStack.EMPTY) {
this.setInventorySlotContents(this.output, itemstack.copy());
} else if (this.getStackInSlot(this.output).getItem() == itemstack.getItem()) {
this.decrStackSize(this.output, -itemstack.getCount());
}
for (final IBaseRecipeType recipeType : RecipeHandler.getRecipeClassFromName(Reference.alloySmelteRecipe)) {
boolean hasAllRecipes = true;
if (this.hasAllInputs(recipeType)) {
} else {
hasAllRecipes = false;
}
if (hasAllRecipes) {
for (final ItemStack input : recipeType.getInputs()) {
for (int inputSlot = 0; inputSlot < 2; inputSlot++) {
if (ItemUtils.isItemEqual(input, this.inventory.getStackInSlot(inputSlot), true, true,
recipeType.useOreDic())) {
this.inventory.decrStackSize(inputSlot, input.getCount());
break;
}
}
}
}
}
}
}
/**
* Furnace isBurning
*/
public boolean isBurning() {
return this.burnTime > 0;
}
public int getBurnTimeRemainingScaled(final int scale) {
if (this.currentItemBurnTime == 0) {
this.currentItemBurnTime = 200;
}
return this.burnTime * scale / this.currentItemBurnTime;
}
public int getCookProgressScaled(final int scale) {
return this.cookTime * scale / 200;
}
@Override
public boolean wrenchCanSetFacing(final EntityPlayer entityPlayer, final EnumFacing side) {
return false;
}
@Override
public EnumFacing getFacing() {
return this.getFacingEnum();
}
@Override
public boolean wrenchCanRemove(final EntityPlayer entityPlayer) {
return entityPlayer.isSneaking();
}
@Override
public float getWrenchDropRate() {
return 1.0F;
}
@Override
public ItemStack getWrenchDrop(final EntityPlayer entityPlayer) {
return new ItemStack(ModBlocks.IRON_ALLOY_FURNACE, 1);
}
public boolean isComplete() {
return false;
}
@Override
public Inventory getInventory() {
return this.inventory;
}
@Override
public int[] getSlotsForFace(final EnumFacing side) {
return new int[] { 0, 1, 2 };
}
@Override
public boolean canInsertItem(final int index, final ItemStack itemStackIn, final EnumFacing direction) {
return index == 0 || index == 1;
}
@Override
public boolean canExtractItem(final int index, final ItemStack stack, final EnumFacing direction) {
return index == 2;
}
public int getBurnTime()
{
return this.burnTime;
}
public void setBurnTime(final int burnTime)
{
this.burnTime = burnTime;
}
public int getCurrentItemBurnTime()
{
return this.currentItemBurnTime;
}
public void setCurrentItemBurnTime(final int currentItemBurnTime)
{
this.currentItemBurnTime = currentItemBurnTime;
}
public int getCookTime()
{
return this.cookTime;
}
public void setCookTime(final int cookTime)
{
this.cookTime = cookTime;
}
2015-05-10 20:00:58 +02:00
}