/* * Copyright (c) 2018-2020 bartimaeusnek 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 bartworks.common.tileentities.multis; import static bartworks.util.BWTooltipReference.MULTIBLOCK_ADDED_BY_BARTIMAEUSNEK_VIA_BARTWORKS; import static gregtech.api.enums.GTValues.VN; import java.lang.reflect.Field; import java.util.Arrays; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.fluids.Fluid; import net.minecraftforge.fluids.FluidRegistry; import bartworks.common.configs.Configuration; import gregtech.api.enums.GTValues; import gregtech.api.enums.ItemList; import gregtech.api.enums.Materials; import gregtech.api.interfaces.metatileentity.IMetaTileEntity; import gregtech.api.interfaces.tileentity.IGregTechTileEntity; import gregtech.api.metatileentity.implementations.MTEHatchInput; import gregtech.api.util.GTModHandler; import gregtech.api.util.GTUtility; import gregtech.api.util.MultiblockTooltipBuilder; import gregtech.common.tileentities.machines.multi.MTEDrillerBase; import ic2.core.block.reactor.tileentity.TileEntityNuclearReactorElectric; public class MTEDeepEarthHeatingPump extends MTEDrillerBase { private static float nulearHeatMod = 2f; private byte mMode; private byte mTier; public MTEDeepEarthHeatingPump(int aID, int tier, String aName, String aNameRegional) { super(aID, aName, aNameRegional); this.mTier = (byte) tier; } public MTEDeepEarthHeatingPump(String aName, byte mTier) { super(aName); this.mTier = mTier; } @Override @SuppressWarnings("rawtypes") public void onConfigLoad() { try { Class c = TileEntityNuclearReactorElectric.class; Field f = c.getDeclaredField("huOutputModifier"); f.setAccessible(true); MTEDeepEarthHeatingPump.nulearHeatMod = f.getFloat(f); } catch (SecurityException | IllegalArgumentException | ExceptionInInitializerError | NullPointerException | IllegalAccessException | NoSuchFieldException e) { e.printStackTrace(); } super.onConfigLoad(); } @Override public void saveNBTData(NBTTagCompound aNBT) { aNBT.setByte("mTier", this.mTier); aNBT.setByte("mMode", this.mMode); super.saveNBTData(aNBT); } @Override public void loadNBTData(NBTTagCompound aNBT) { this.mTier = aNBT.getByte("mTier"); this.mMode = aNBT.getByte("mMode"); super.loadNBTData(aNBT); } @Override public IMetaTileEntity newMetaEntity(IGregTechTileEntity iGregTechTileEntity) { return new MTEDeepEarthHeatingPump(this.mName, this.mTier); } @Override protected MultiblockTooltipBuilder createTooltip() { MultiblockTooltipBuilder tt = new MultiblockTooltipBuilder(); String casings = this.getCasingBlockItem() .get(0) .getDisplayName(); tt.addMachineType("Geothermal Heat Pump") .addInfo("Consumes " + GTValues.V[this.mTier + 2] + "EU/t") .addInfo("Has 4 Modes, use the Screwdriver to change them:"); if (Configuration.multiblocks.DEHPDirectSteam) { tt.addInfo("0 Idle, 1 Steam, 2 Superheated Steam (requires Distilled Water), 3 Retract") .addInfo("Explodes when it runs out of Water/Distilled Water") .addInfo( "Converts " + (long) (this.mTier * 1200 * 20) + "L/s Water(minus 10% per Maintenance Problem) to Steam") .addInfo( "Converts " + (long) (this.mTier * 600 * 20) + "L/s Distilled Water(minus 10% per Maintenance Problem) to SuperheatedSteam"); } else { tt.addInfo("0 Idle, 1 & 2 Coolant Heating Mode (no Difference between them), 3 Retract") .addInfo("Explodes when it runs out of Coolant") .addInfo( "Heats up " + (long) (this.mTier * 24 * (double) MTEDeepEarthHeatingPump.nulearHeatMod) * 20 + "L/s Coolant(minus 10% per Maintenance Problem)"); } tt.addSeparator() .beginStructureBlock(3, 7, 3, false) .addController("Front bottom") .addOtherStructurePart(casings, "form the 3x1x3 Base") .addOtherStructurePart(casings, "1x3x1 pillar above the center of the base (2 minimum total)") .addOtherStructurePart( this.getFrameMaterial().mName + " Frame Boxes", "Each pillar's side and 1x3x1 on top") .addEnergyHatch(VN[this.getMinTier()] + "+, Any base casing") .addMaintenanceHatch("Any base casing") .addInputBus("Mining Pipes, optional, any base casing") .addInputHatch("Any base casing") .addOutputHatch("Any base casing") .toolTipFinisher(MULTIBLOCK_ADDED_BY_BARTIMAEUSNEK_VIA_BARTWORKS); return tt; } @Override protected ItemList getCasingBlockItem() { return ItemList.Casing_HeatProof; } @Override protected Materials getFrameMaterial() { return Materials.Tungsten; } @Override protected int getCasingTextureIndex() { return 11; } @Override protected int getMinTier() { return 2 + this.mTier; } @Override protected boolean checkHatches() { return !this.mMaintenanceHatches.isEmpty() && !this.mOutputHatches.isEmpty() && !this.mInputHatches.isEmpty(); } private long getFluidFromHatches(Fluid f) { long ret = 0; for (MTEHatchInput ih : this.mInputHatches) { if (ih.getFluid() .getFluid() .equals(f)) ret += ih.getFluidAmount(); } return ret; } private long getWaterFromHatches(boolean onlyDistilled) { Fluid toConsume1 = FluidRegistry.WATER; Fluid toConsume2 = GTModHandler.getDistilledWater(1L) .getFluid(); if (onlyDistilled) toConsume1 = toConsume2; long ret = 0; for (MTEHatchInput ih : this.mInputHatches) { if (ih.getFluid() .getFluid() .equals(toConsume1) || ih.getFluid() .getFluid() .equals(toConsume2)) ret += ih.getFluidAmount(); } return ret; } @Override protected boolean workingUpward(ItemStack aStack, int xDrill, int yDrill, int zDrill, int xPipe, int zPipe, int yHead, int oldYHead) { if (this.mMode != 3) { this.isPickingPipes = false; try { Field workState = this.getClass() .getField("workState"); workState.setInt(this, 0); } catch (NoSuchFieldError | NoSuchFieldException | IllegalAccessException ignored) {} return true; } return super.workingUpward(aStack, xDrill, yDrill, zDrill, xPipe, zPipe, yHead, oldYHead); } @Override public void onScrewdriverRightClick(ForgeDirection side, EntityPlayer aPlayer, float aX, float aY, float aZ) { if (this.getBaseMetaTileEntity() .getWorld().isRemote) return; ++this.mMode; if (this.mMode >= 4) this.mMode = 0; GTUtility.sendChatToPlayer(aPlayer, "Mode: " + this.mMode); super.onScrewdriverRightClick(side, aPlayer, aX, aY, aZ); } @Override protected boolean workingDownward(ItemStack aStack, int xDrill, int yDrill, int zDrill, int xPipe, int zPipe, int yHead, int oldYHead) { if (this.mMode == 3) { this.isPickingPipes = true; try { Field workState = this.getClass() .getSuperclass() .getDeclaredField("workState"); workState.setInt(this, 2); } catch (NoSuchFieldError | NoSuchFieldException | IllegalAccessException ignored) {} return true; } if (this.tryLowerPipeState(false) == 0) { return true; } if (this.waitForPipes()) { return false; } if (this.mMode == 0) { this.mMode = 1; } if (Configuration.multiblocks.DEHPDirectSteam) { if (this.mMode == 1) { long steamProduced = this.mTier * 600 * 2L * this.mEfficiency / 10000L; long waterConsume = (steamProduced + 160) / 160; if (this.getWaterFromHatches(false) - waterConsume > 0) { this.consumeFluid(FluidRegistry.WATER, waterConsume); this.addOutput(GTModHandler.getSteam(steamProduced)); } else { this.explodeMultiblock(); return false; } } else if (this.mMode == 2) { long steamProduced = this.mTier * 300 * 2L * this.mEfficiency / 10000L; long waterConsume = (steamProduced + 160) / 160; if (this.getWaterFromHatches(true) - waterConsume > 0) { this.consumeFluid( GTModHandler.getDistilledWater(1) .getFluid(), waterConsume); this.addOutput(FluidRegistry.getFluidStack("ic2superheatedsteam", (int) steamProduced)); } else { this.explodeMultiblock(); return false; } } } else if (this.mMode == 1 || this.mMode == 2) { long coolantConverted = (long) (this.mTier * 24 * (double) MTEDeepEarthHeatingPump.nulearHeatMod * this.mEfficiency / 10000L); if (this.getFluidFromHatches(FluidRegistry.getFluid("ic2coolant")) - coolantConverted > 0) { this.consumeFluid(FluidRegistry.getFluid("ic2coolant"), coolantConverted); this.addOutput(FluidRegistry.getFluidStack("ic2hotcoolant", (int) coolantConverted)); } else { this.explodeMultiblock(); return false; } } return true; } private boolean consumeFluid(Fluid fluid, long ammount) { if (ammount > Integer.MAX_VALUE) { int[] tmp = new int[(int) (ammount / Integer.MAX_VALUE)]; Arrays.fill(tmp, (int) (ammount / Integer.MAX_VALUE)); for (int i = 0; i < tmp.length; i++) { for (MTEHatchInput ih : this.mInputHatches) { if (fluid.equals(FluidRegistry.WATER) ? ih.getFluid() .getFluid() .equals(fluid) || ih.getFluid() .getFluid() .equals( GTModHandler.getDistilledWater(1) .getFluid()) : ih.getFluid() .getFluid() .equals(fluid)) tmp[i] -= ih.drain((int) ammount, true).amount; if (tmp[i] <= 0) break; } } return tmp[tmp.length - 1] <= 0; } long tmp = ammount; for (MTEHatchInput ih : this.mInputHatches) { if (fluid.equals(FluidRegistry.WATER) ? ih.getFluid() .getFluid() .equals(fluid) || ih.getFluid() .getFluid() .equals( GTModHandler.getDistilledWater(1) .getFluid()) : ih.getFluid() .getFluid() .equals(fluid)) tmp -= ih.drain((int) ammount, true).amount; if (tmp <= 0) return true; } return false; } @Override protected void setElectricityStats() { try { this.mEUt = this.isPickingPipes ? 60 : Math.toIntExact(GTValues.V[this.getMinTier()]); } catch (ArithmeticException e) { e.printStackTrace(); this.mEUt = Integer.MAX_VALUE - 7; } this.mProgresstime = 0; this.mMaxProgresstime = 1; this.mEfficiency = this.getCurrentEfficiency(null); this.mEfficiencyIncrease = 10000; } }