From 0ae99ebae8b6b30e25db859e1b8c0416b60ce95d Mon Sep 17 00:00:00 2001 From: drcrazy Date: Mon, 2 Oct 2017 18:38:47 +0300 Subject: [PATCH] Keep state for running reactor. Closes #1187 --- src/main/java/techreborn/init/ModRecipes.java | 19 +-- .../init/recipes/FusionReactorRecipes.java | 52 ++++++ .../TileFusionControlComputer.java | 150 ++++++++---------- 3 files changed, 123 insertions(+), 98 deletions(-) create mode 100644 src/main/java/techreborn/init/recipes/FusionReactorRecipes.java diff --git a/src/main/java/techreborn/init/ModRecipes.java b/src/main/java/techreborn/init/ModRecipes.java index c357c26c6..70ae516e2 100644 --- a/src/main/java/techreborn/init/ModRecipes.java +++ b/src/main/java/techreborn/init/ModRecipes.java @@ -39,8 +39,6 @@ import reborncore.common.util.ItemUtils; import reborncore.common.util.OreUtil; import reborncore.common.util.RebornCraftingHelper; import techreborn.Core; -import techreborn.api.reactor.FusionReactorRecipe; -import techreborn.api.reactor.FusionReactorRecipeHelper; import techreborn.api.recipe.machines.*; import techreborn.blocks.BlockOre; import techreborn.compat.CompatManager; @@ -78,11 +76,11 @@ public class ModRecipes { ImplosionCompressorRecipes.init(); ScrapboxRecipes.init(); ChemicalReactorRecipes.init(); + FusionReactorRecipes.init(); addAlloySmelterRecipes(); addBlastFurnaceRecipes(); addVacuumFreezerRecipes(); - addReactorRecipes(); addIc2Recipes(); addGrinderRecipes(); addCompressorRecipes(); @@ -246,21 +244,6 @@ public class ModRecipes { } } - static void addReactorRecipes() { - FusionReactorRecipeHelper.registerRecipe( - new FusionReactorRecipe(ItemCells.getCellByName("helium3"), ItemCells.getCellByName("deuterium"), - ItemCells.getCellByName("heliumplasma"), 40000000, 32768, 1024)); - FusionReactorRecipeHelper.registerRecipe( - new FusionReactorRecipe(ItemCells.getCellByName("tritium"), ItemCells.getCellByName("deuterium"), - ItemCells.getCellByName("helium3"), 60000000, 32768, 2048)); - FusionReactorRecipeHelper.registerRecipe( - new FusionReactorRecipe(ItemCells.getCellByName("wolframium"), ItemCells.getCellByName("Berylium"), - ItemDusts.getDustByName("platinum"), 80000000, -2048, 1024)); - FusionReactorRecipeHelper.registerRecipe( - new FusionReactorRecipe(ItemCells.getCellByName("wolframium"), ItemCells.getCellByName("lithium"), - BlockOre.getOreByName("iridium"), 90000000, -2048, 1024)); - } - static void addVacuumFreezerRecipes() { RecipeHandler.addRecipe(new VacuumFreezerRecipe( new ItemStack(Blocks.ICE, 2), diff --git a/src/main/java/techreborn/init/recipes/FusionReactorRecipes.java b/src/main/java/techreborn/init/recipes/FusionReactorRecipes.java new file mode 100644 index 000000000..15e536668 --- /dev/null +++ b/src/main/java/techreborn/init/recipes/FusionReactorRecipes.java @@ -0,0 +1,52 @@ +/* + * This file is part of TechReborn, licensed under the MIT License (MIT). + * + * Copyright (c) 2017 TechReborn + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package techreborn.init.recipes; + +import techreborn.api.reactor.FusionReactorRecipe; +import techreborn.api.reactor.FusionReactorRecipeHelper; +import techreborn.blocks.BlockOre; +import techreborn.items.ItemCells; +import techreborn.items.ItemDusts; + +/** + * @author drcrazy + * + */ +public class FusionReactorRecipes extends RecipeMethods { + public static void init(){ + FusionReactorRecipeHelper.registerRecipe( + new FusionReactorRecipe(ItemCells.getCellByName("helium3"), ItemCells.getCellByName("deuterium"), + ItemCells.getCellByName("heliumplasma"), 40000000, 32768, 1024)); + FusionReactorRecipeHelper.registerRecipe( + new FusionReactorRecipe(ItemCells.getCellByName("tritium"), ItemCells.getCellByName("deuterium"), + ItemCells.getCellByName("helium3"), 60000000, 32768, 2048)); + FusionReactorRecipeHelper.registerRecipe( + new FusionReactorRecipe(ItemCells.getCellByName("wolframium"), ItemCells.getCellByName("Berylium"), + ItemDusts.getDustByName("platinum"), 80000000, -2048, 1024)); + FusionReactorRecipeHelper.registerRecipe( + new FusionReactorRecipe(ItemCells.getCellByName("wolframium"), ItemCells.getCellByName("lithium"), + BlockOre.getOreByName("iridium"), 90000000, -2048, 1024)); + } +} diff --git a/src/main/java/techreborn/tiles/fusionReactor/TileFusionControlComputer.java b/src/main/java/techreborn/tiles/fusionReactor/TileFusionControlComputer.java index d6a608595..b504016a1 100644 --- a/src/main/java/techreborn/tiles/fusionReactor/TileFusionControlComputer.java +++ b/src/main/java/techreborn/tiles/fusionReactor/TileFusionControlComputer.java @@ -173,87 +173,77 @@ public class TileFusionControlComputer extends TilePowerAcceptor implements IInv public void update() { super.update(); // TODO improve this code a lot + + if (this.world.isRemote) { + return; + } if (this.world.getTotalWorldTime() % 20 == 0) { this.checkCoils(); } - if (!this.world.isRemote) { - if (this.coilStatus == 1) { - if (this.currentRecipe == null) { - if (this.inventory.hasChanged || this.crafingTickTime != 0) { - for (final FusionReactorRecipe reactorRecipe : FusionReactorRecipeHelper.reactorRecipes) { - if (ItemUtils.isItemEqual(this.getStackInSlot(this.topStackSlot), reactorRecipe.getTopInput(), true, - true, true)) { - if (reactorRecipe.getBottomInput() != null) { - if (!ItemUtils.isItemEqual(this.getStackInSlot(this.bottomStackSlot), - reactorRecipe.getBottomInput(), true, true, true)) { - break; - } - } - if (this.canFitStack(reactorRecipe.getOutput(), this.outputStackSlot, true)) { - this.currentRecipe = reactorRecipe; - if (this.crafingTickTime != 0) { - this.finalTickTime = this.currentRecipe.getTickTime(); - this.neededPower = (int) this.currentRecipe.getStartEU(); - } - this.hasStartedCrafting = false; - this.crafingTickTime = 0; - this.finalTickTime = this.currentRecipe.getTickTime(); - this.neededPower = (int) this.currentRecipe.getStartEU(); - break; - } - } - } - } - } else { - if (this.inventory.hasChanged) { - if (!this.validateRecipe()) { - this.resetCrafter(); - return; - } - } - if (!this.hasStartedCrafting) { - if (this.canUseEnergy(this.currentRecipe.getStartEU() + 64)) { - this.useEnergy(this.currentRecipe.getStartEU()); - this.hasStartedCrafting = true; - } - } else { - if (this.crafingTickTime < this.currentRecipe.getTickTime()) { - if (this.currentRecipe.getEuTick() > 0) { // Power gen - this.addEnergy(this.currentRecipe.getEuTick()); // Waste - // power - // if it - // has - // no - // where - // to go - this.crafingTickTime++; - } else { // Power user - if (this.canUseEnergy(this.currentRecipe.getEuTick() * -1)) { - this.setEnergy(this.getEnergy() - this.currentRecipe.getEuTick() * -1); - this.crafingTickTime++; - } - } - } else { - if (this.canFitStack(this.currentRecipe.getOutput(), this.outputStackSlot, true)) { - if (this.getStackInSlot(this.outputStackSlot) == ItemStack.EMPTY) { - this.setInventorySlotContents(this.outputStackSlot, this.currentRecipe.getOutput().copy()); - } else { - this.decrStackSize(this.outputStackSlot, -this.currentRecipe.getOutput().getCount()); - } - this.decrStackSize(this.topStackSlot, this.currentRecipe.getTopInput().getCount()); - if (this.currentRecipe.getBottomInput() != ItemStack.EMPTY) { - this.decrStackSize(this.bottomStackSlot, this.currentRecipe.getBottomInput().getCount()); - } - this.resetCrafter(); - } - } + if (this.coilStatus == 0) { + this.resetCrafter(); + return; + } + + if (this.currentRecipe == null && this.inventory.hasChanged){ + this.crafingTickTime = 0; + for (final FusionReactorRecipe reactorRecipe : FusionReactorRecipeHelper.reactorRecipes) { + if (this.validateRecipe(reactorRecipe)){ + this.currentRecipe = reactorRecipe; + this.finalTickTime = this.currentRecipe.getTickTime(); + this.neededPower = (int) this.currentRecipe.getStartEU(); + this.hasStartedCrafting = false; + break; + } + } + } + else { + if (this.inventory.hasChanged) { + if (!this.validateRecipe(this.currentRecipe)) { + this.resetCrafter(); + return; + } + } + } + + if (this.currentRecipe != null) { + if (!this.hasStartedCrafting) { + // Ignition! + if (this.canUseEnergy(this.currentRecipe.getStartEU())) { + this.useEnergy(this.currentRecipe.getStartEU()); + this.hasStartedCrafting = true; + } + } + if (this.crafingTickTime < this.finalTickTime) { + this.crafingTickTime++; + // Power gen + if (this.currentRecipe.getEuTick() > 0) { + // Waste power if it has no where to go + this.addEnergy(this.currentRecipe.getEuTick()); + } else { // Power user + if (this.canUseEnergy(this.currentRecipe.getEuTick() * -1)) { + this.setEnergy(this.getEnergy() - this.currentRecipe.getEuTick() * -1); } } - } else { - if (this.currentRecipe != null) { - this.resetCrafter(); + } else if (this.crafingTickTime >= this.finalTickTime) { + if (this.canFitStack(this.currentRecipe.getOutput(), this.outputStackSlot, true)) { + if (this.getStackInSlot(this.outputStackSlot).isEmpty()) { + this.setInventorySlotContents(this.outputStackSlot, this.currentRecipe.getOutput().copy()); + } else { + this.decrStackSize(this.outputStackSlot, -this.currentRecipe.getOutput().getCount()); + } + this.decrStackSize(this.topStackSlot, this.currentRecipe.getTopInput().getCount()); + if (!this.currentRecipe.getBottomInput().isEmpty()) { + this.decrStackSize(this.bottomStackSlot, this.currentRecipe.getBottomInput().getCount()); + } + if (this.validateRecipe(this.currentRecipe)){ + this.crafingTickTime = 0; + } + else { + this.resetCrafter(); + } } } } @@ -263,15 +253,15 @@ public class TileFusionControlComputer extends TilePowerAcceptor implements IInv } } - private boolean validateRecipe() { - if (ItemUtils.isItemEqual(this.getStackInSlot(this.topStackSlot), this.currentRecipe.getTopInput(), true, true, true)) { - if (this.currentRecipe.getBottomInput() != null) { - if (!ItemUtils.isItemEqual(this.getStackInSlot(this.bottomStackSlot), this.currentRecipe.getBottomInput(), true, true, + private boolean validateRecipe(FusionReactorRecipe recipe) { + if (ItemUtils.isItemEqual(this.getStackInSlot(this.topStackSlot), recipe.getTopInput(), true, true, true)) { + if (recipe.getBottomInput() != null) { + if (!ItemUtils.isItemEqual(this.getStackInSlot(this.bottomStackSlot), recipe.getBottomInput(), true, true, true)) { return false; } } - if (this.canFitStack(this.currentRecipe.getOutput(), this.outputStackSlot, true)) { + if (this.canFitStack(recipe.getOutput(), this.outputStackSlot, true)) { return true; } } @@ -290,7 +280,7 @@ public class TileFusionControlComputer extends TilePowerAcceptor implements IInv if (stack.isEmpty()) { return true; } - if (this.inventory.getStackInSlot(slot) == ItemStack.EMPTY) { + if (this.inventory.getStackInSlot(slot).isEmpty()) { return true; } if (ItemUtils.isItemEqual(this.inventory.getStackInSlot(slot), stack, true, true, oreDic)) {