From 4090fcf490c68debf96482b68f6ad14afbf67b67 Mon Sep 17 00:00:00 2001 From: Aidan Brady Date: Fri, 1 Aug 2014 00:55:24 -0400 Subject: [PATCH] Reactor now auto-forms --- .../mekanism/common/block/BlockMachine.java | 4 +- .../common/tile/TileEntityBasicBlock.java | 5 + .../common/tile/TileEntityElectricBlock.java | 21 +++- .../tile/TileEntitySalinationBlock.java | 6 +- .../tile/TileEntitySalinationController.java | 10 +- .../client/gui/GuiReactorController.java | 25 +---- .../generators/client/gui/GuiReactorFuel.java | 2 +- .../generators/common/FusionReactor.java | 44 +++++--- .../common/block/BlockGenerator.java | 4 +- .../generators/common/block/BlockReactor.java | 32 +++++- .../tile/reactor/TileEntityReactorBlock.java | 102 +++++++++++++++--- .../reactor/TileEntityReactorController.java | 30 +++--- .../TileEntityReactorLaserFocusMatrix.java | 2 + .../tile/reactor/TileEntityReactorPort.java | 5 + 14 files changed, 212 insertions(+), 80 deletions(-) diff --git a/src/main/java/mekanism/common/block/BlockMachine.java b/src/main/java/mekanism/common/block/BlockMachine.java index b7c00e5af..0ef75b0a1 100644 --- a/src/main/java/mekanism/common/block/BlockMachine.java +++ b/src/main/java/mekanism/common/block/BlockMachine.java @@ -1146,9 +1146,9 @@ public class BlockMachine extends BlockContainer implements ISpecialBounds, IPer if(!world.isRemote) { - if(tileEntity instanceof TileEntityElectricBlock && MekanismUtils.useIC2()) + if(tileEntity instanceof TileEntityBasicBlock) { - ((TileEntityElectricBlock)tileEntity).register(); + ((TileEntityBasicBlock)tileEntity).onAdded(); } } } diff --git a/src/main/java/mekanism/common/tile/TileEntityBasicBlock.java b/src/main/java/mekanism/common/tile/TileEntityBasicBlock.java index 882b84736..00a79a3c9 100644 --- a/src/main/java/mekanism/common/tile/TileEntityBasicBlock.java +++ b/src/main/java/mekanism/common/tile/TileEntityBasicBlock.java @@ -239,4 +239,9 @@ public abstract class TileEntityBasicBlock extends TileEntity implements IWrench } } } + + public void onAdded() + { + + } } diff --git a/src/main/java/mekanism/common/tile/TileEntityElectricBlock.java b/src/main/java/mekanism/common/tile/TileEntityElectricBlock.java index 9d14d7ec8..a3e7a2f7a 100644 --- a/src/main/java/mekanism/common/tile/TileEntityElectricBlock.java +++ b/src/main/java/mekanism/common/tile/TileEntityElectricBlock.java @@ -104,7 +104,9 @@ public abstract class TileEntityElectricBlock extends TileEntityContainerBlock i public void onUpdate() { if(MekanismUtils.useBuildCraft()) + { reconfigure(); + } } public EnumSet getOutputtingSides() @@ -157,12 +159,25 @@ public abstract class TileEntityElectricBlock extends TileEntityContainerBlock i data.add(getEnergy()); return data; } + + @Override + public void onAdded() + { + super.onAdded(); + + if(MekanismUtils.useIC2()) + { + register(); + } + } @Override public void onChunkUnload() { if(MekanismUtils.useIC2()) + { deregister(); + } super.onChunkUnload(); } @@ -173,7 +188,9 @@ public abstract class TileEntityElectricBlock extends TileEntityContainerBlock i super.invalidate(); if(MekanismUtils.useIC2()) + { deregister(); + } } @Override @@ -184,7 +201,9 @@ public abstract class TileEntityElectricBlock extends TileEntityContainerBlock i electricityStored = nbtTags.getDouble("electricityStored"); if(MekanismUtils.useBuildCraft()) + { reconfigure(); + } } @Override @@ -210,7 +229,7 @@ public abstract class TileEntityElectricBlock extends TileEntityContainerBlock i @Method(modid = "BuildCraftAPI|power") protected void reconfigure() { - powerHandler.configure(0, (float)((getMaxEnergy()-getEnergy())* general.TO_BC), 0, (float)(getMaxEnergy()* general.TO_BC)); + powerHandler.configure(0, (float)((getMaxEnergy()-getEnergy())*general.TO_BC), 0, (float)(getMaxEnergy()*general.TO_BC)); } @Override diff --git a/src/main/java/mekanism/common/tile/TileEntitySalinationBlock.java b/src/main/java/mekanism/common/tile/TileEntitySalinationBlock.java index 478a3d3fd..0def9fe13 100644 --- a/src/main/java/mekanism/common/tile/TileEntitySalinationBlock.java +++ b/src/main/java/mekanism/common/tile/TileEntitySalinationBlock.java @@ -47,7 +47,7 @@ public class TileEntitySalinationBlock extends TileEntityContainerBlock if(master != null) { - master.refresh(true); + master.refresh(); } } @@ -60,7 +60,7 @@ public class TileEntitySalinationBlock extends TileEntityContainerBlock { if(master != null) { - master.refresh(true); + master.refresh(); } else { if(!(this instanceof TileEntitySalinationController)) @@ -69,7 +69,7 @@ public class TileEntitySalinationBlock extends TileEntityContainerBlock if(found != null) { - found.refresh(true); + found.refresh(); } } } diff --git a/src/main/java/mekanism/common/tile/TileEntitySalinationController.java b/src/main/java/mekanism/common/tile/TileEntitySalinationController.java index 5b67926fb..b2ae76c50 100644 --- a/src/main/java/mekanism/common/tile/TileEntitySalinationController.java +++ b/src/main/java/mekanism/common/tile/TileEntitySalinationController.java @@ -78,7 +78,7 @@ public class TileEntitySalinationController extends TileEntitySalinationBlock if(ticker == 5) { - refresh(true); + refresh(); } updateTemperature(); @@ -123,7 +123,7 @@ public class TileEntitySalinationController extends TileEntitySalinationBlock { super.onChunkUnload(); - refresh(true); + refresh(); } @Override @@ -131,14 +131,14 @@ public class TileEntitySalinationController extends TileEntitySalinationBlock { super.onNeighborChange(block); - refresh(true); + refresh(); } - protected void refresh(boolean canCreate) + protected void refresh() { if(!worldObj.isRemote) { - if((structured || canCreate) && !updatedThisTick) + if(!updatedThisTick) { boolean prev = structured; diff --git a/src/main/java/mekanism/generators/client/gui/GuiReactorController.java b/src/main/java/mekanism/generators/client/gui/GuiReactorController.java index 52ff33b7b..fd4503ac5 100644 --- a/src/main/java/mekanism/generators/client/gui/GuiReactorController.java +++ b/src/main/java/mekanism/generators/client/gui/GuiReactorController.java @@ -53,12 +53,12 @@ public class GuiReactorController extends GuiMekanism super.drawGuiContainerForegroundLayer(mouseX, mouseY); fontRendererObj.drawString(tileEntity.getInventoryName(), 6, 6, 0x404040); + if(tileEntity.getActive()) { fontRendererObj.drawString(MekanismUtils.localize("container.reactor.formed"), 8, 16, 0x404040); } - else - { + else { fontRendererObj.drawString(MekanismUtils.localize("container.reactor.notFormed"), 8, 16, 0x404040); } } @@ -74,25 +74,4 @@ public class GuiReactorController extends GuiMekanism super.drawGuiContainerBackgroundLayer(partialTick, mouseX, mouseY); } - - @Override - protected void mouseClicked(int x, int y, int button) - { - super.mouseClicked(x, y, button); - - if(button == 0) - { - int xAxis = (x - (width - xSize) / 2); - int yAxis = (y - (height - ySize) / 2); - - if(xAxis >= 48 && xAxis <= 128 && yAxis >= 5 && yAxis <= 17) - { - ArrayList data = new ArrayList(); - data.add(0); - - Mekanism.packetHandler.sendToServer(new TileEntityMessage(Coord4D.get(tileEntity), data)); - SoundHandler.playSound("gui.button.press"); - } - } - } } diff --git a/src/main/java/mekanism/generators/client/gui/GuiReactorFuel.java b/src/main/java/mekanism/generators/client/gui/GuiReactorFuel.java index c79e448d5..bc1e5fb5d 100644 --- a/src/main/java/mekanism/generators/client/gui/GuiReactorFuel.java +++ b/src/main/java/mekanism/generators/client/gui/GuiReactorFuel.java @@ -161,7 +161,7 @@ public class GuiReactorFuel extends GuiMekanism int toUse = Math.max(0, Integer.parseInt(injectionRateField.getText())); ArrayList data = new ArrayList(); - data.add(1); + data.add(0); data.add(toUse); Mekanism.packetHandler.sendToServer(new TileEntityMessage(Coord4D.get(tileEntity), data)); diff --git a/src/main/java/mekanism/generators/common/FusionReactor.java b/src/main/java/mekanism/generators/common/FusionReactor.java index 769052038..aaf1ad48f 100644 --- a/src/main/java/mekanism/generators/common/FusionReactor.java +++ b/src/main/java/mekanism/generators/common/FusionReactor.java @@ -15,9 +15,9 @@ import mekanism.api.reactor.IFusionReactor; import mekanism.api.reactor.INeutronCapture; import mekanism.api.reactor.IReactorBlock; import mekanism.common.Mekanism; +import mekanism.common.network.PacketTileEntity.TileEntityMessage; import mekanism.generators.common.item.ItemHohlraum; import mekanism.generators.common.tile.reactor.TileEntityReactorController; - import net.minecraft.block.Block; import net.minecraft.item.ItemCoal; import net.minecraft.tileentity.TileEntity; @@ -26,7 +26,6 @@ import net.minecraftforge.fluids.FluidContainerRegistry; import net.minecraftforge.fluids.FluidRegistry; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.FluidTank; - import static java.lang.Math.min; import static java.lang.Math.max; @@ -64,6 +63,8 @@ public class FusionReactor implements IFusionReactor public boolean burning = false; public boolean activelyCooled = true; + + public boolean updatedThisTick; public boolean formed = false; @@ -90,9 +91,12 @@ public class FusionReactor implements IFusionReactor { lastPlasmaTemperature = plasmaTemperature; lastCaseTemperature = caseTemperature; + return; } + updatedThisTick = false; + //Only thermal transfer happens unless we're hot enough to burn. if(plasmaTemperature >= burnTemperature) { @@ -106,9 +110,7 @@ public class FusionReactor implements IFusionReactor if(burning) { injectFuel(); - - int fuelBurned = burnFuel(); - neutronFlux(fuelBurned); + neutronFlux(burnFuel()); } } else { @@ -284,7 +286,7 @@ public class FusionReactor implements IFusionReactor public void unformMultiblock() { - for(IReactorBlock block: reactorBlocks) + for(IReactorBlock block : reactorBlocks) { block.setReactor(null); } @@ -294,12 +296,19 @@ public class FusionReactor implements IFusionReactor reactorBlocks.clear(); neutronCaptors.clear(); formed = false; + + if(!controller.getWorldObj().isRemote) + { + Mekanism.packetHandler.sendToDimension(new TileEntityMessage(Coord4D.get(controller), controller.getNetworkedData(new ArrayList())), controller.getWorldObj().provider.dimensionId); + } } @Override public void formMultiblock() { - Mekanism.logger.trace("Attempting to form multiblock"); + updatedThisTick = true; + + Mekanism.logger.info("Attempting to form multiblock"); Coord4D controllerPosition = Coord4D.get(controller); Coord4D centreOfReactor = controllerPosition.getFromSide(ForgeDirection.DOWN, 2); @@ -308,32 +317,38 @@ public class FusionReactor implements IFusionReactor reactorBlocks.add(controller); - Mekanism.logger.trace("Centre at " + centreOfReactor.toString()); + Mekanism.logger.info("Centre at " + centreOfReactor.toString()); if(!createFrame(centreOfReactor)) { unformMultiblock(); - Mekanism.logger.trace("Reactor failed: Frame not complete."); + Mekanism.logger.info("Reactor failed: Frame not complete."); return; } - Mekanism.logger.trace("Frame valid"); + Mekanism.logger.info("Frame valid"); if(!addSides(centreOfReactor)) { unformMultiblock(); - Mekanism.logger.trace("Reactor failed: Sides not complete."); + Mekanism.logger.info("Reactor failed: Sides not complete."); return; } - Mekanism.logger.trace("Side Blocks Valid"); + Mekanism.logger.info("Side Blocks Valid"); if(!centreIsClear(centreOfReactor)) { unformMultiblock(); - Mekanism.logger.trace("Blocks in chamber."); + Mekanism.logger.info("Blocks in chamber."); return; } - Mekanism.logger.trace("Centre is clear"); + Mekanism.logger.info("Centre is clear"); + formed = true; + + if(!controller.getWorldObj().isRemote) + { + Mekanism.packetHandler.sendToDimension(new TileEntityMessage(Coord4D.get(controller), controller.getNetworkedData(new ArrayList())), controller.getWorldObj().provider.dimensionId); + } } public boolean createFrame(Coord4D centre) @@ -387,6 +402,7 @@ public class FusionReactor implements IFusionReactor { reactorBlocks.add((IReactorBlock)tile); ((IReactorBlock)tile).setReactor(this); + if(tile instanceof INeutronCapture) { neutronCaptors.add((INeutronCapture)tile); diff --git a/src/main/java/mekanism/generators/common/block/BlockGenerator.java b/src/main/java/mekanism/generators/common/block/BlockGenerator.java index 1420b06be..189793b9c 100644 --- a/src/main/java/mekanism/generators/common/block/BlockGenerator.java +++ b/src/main/java/mekanism/generators/common/block/BlockGenerator.java @@ -511,9 +511,9 @@ public class BlockGenerator extends BlockContainer implements ISpecialBounds if(!world.isRemote) { - if(tileEntity != null && MekanismUtils.useIC2()) + if(tileEntity instanceof TileEntityBasicBlock) { - ((TileEntityElectricBlock)tileEntity).register(); + ((TileEntityBasicBlock)tileEntity).onAdded(); } } } diff --git a/src/main/java/mekanism/generators/common/block/BlockReactor.java b/src/main/java/mekanism/generators/common/block/BlockReactor.java index e753eafb3..59df057cb 100644 --- a/src/main/java/mekanism/generators/common/block/BlockReactor.java +++ b/src/main/java/mekanism/generators/common/block/BlockReactor.java @@ -5,6 +5,7 @@ import java.util.Random; import mekanism.common.ItemAttacher; import mekanism.common.Mekanism; +import mekanism.common.tile.TileEntityBasicBlock; import mekanism.common.tile.TileEntityElectricBlock; import mekanism.common.util.MekanismUtils; import mekanism.generators.common.GeneratorsBlocks; @@ -123,6 +124,20 @@ public class BlockReactor extends BlockContainer return i; } + @Override + public void onNeighborBlockChange(World world, int x, int y, int z, Block block) + { + if(!world.isRemote) + { + TileEntity tileEntity = world.getTileEntity(x, y, z); + + if(tileEntity instanceof TileEntityBasicBlock) + { + ((TileEntityBasicBlock)tileEntity).onNeighborChange(block); + } + } + } + @Override public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer entityplayer, int facing, float playerX, float playerY, float playerZ) { @@ -181,9 +196,6 @@ public class BlockReactor extends BlockContainer { entityplayer.openGui(MekanismGenerators.instance, ReactorBlockType.get(this, metadata).guiId, world, x, y, z); } - else { - ((TileEntityReactorController)tileEntity).formMultiblock(); - } return true; } @@ -246,6 +258,20 @@ public class BlockReactor extends BlockContainer { return false; } + + @Override + public void onBlockAdded(World world, int x, int y, int z) + { + TileEntity tileEntity = world.getTileEntity(x, y, z); + + if(!world.isRemote) + { + if(tileEntity instanceof TileEntityBasicBlock) + { + ((TileEntityBasicBlock)tileEntity).onAdded(); + } + } + } /*This method is not used, metadata manipulation is required to create a Tile Entity.*/ @Override diff --git a/src/main/java/mekanism/generators/common/tile/reactor/TileEntityReactorBlock.java b/src/main/java/mekanism/generators/common/tile/reactor/TileEntityReactorBlock.java index 30deb1878..53a2ef470 100644 --- a/src/main/java/mekanism/generators/common/tile/reactor/TileEntityReactorBlock.java +++ b/src/main/java/mekanism/generators/common/tile/reactor/TileEntityReactorBlock.java @@ -1,18 +1,23 @@ package mekanism.generators.common.tile.reactor; +import java.util.ArrayList; import java.util.EnumSet; +import java.util.HashSet; +import java.util.Set; +import mekanism.api.Coord4D; import mekanism.api.reactor.IFusionReactor; import mekanism.api.reactor.IReactorBlock; +import mekanism.common.Mekanism; +import mekanism.common.network.PacketTileEntity.TileEntityMessage; import mekanism.common.tile.TileEntityElectricBlock; - import net.minecraft.item.ItemStack; -import net.minecraft.tileentity.TileEntity; import net.minecraftforge.common.util.ForgeDirection; public abstract class TileEntityReactorBlock extends TileEntityElectricBlock implements IReactorBlock { public IFusionReactor fusionReactor; + public boolean changed; public TileEntityReactorBlock() @@ -46,6 +51,8 @@ public abstract class TileEntityReactorBlock extends TileEntityElectricBlock imp @Override public void invalidate() { + super.invalidate(); + if(getReactor() != null) { getReactor().formMultiblock(); @@ -55,32 +62,99 @@ public abstract class TileEntityReactorBlock extends TileEntityElectricBlock imp @Override public void onUpdate() { + super.onUpdate(); + if(changed) { - worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord, zCoord, getBlockType()); changed = false; } } @Override - public double transferEnergyToAcceptor(ForgeDirection side, double energy) - { - return 0; - } - - @Override - public boolean canReceiveEnergy(ForgeDirection side) - { - return false; - } - public EnumSet getOutputtingSides() { return EnumSet.noneOf(ForgeDirection.class); } + @Override protected EnumSet getConsumingSides() { return EnumSet.noneOf(ForgeDirection.class); } + + @Override + public void onChunkUnload() + { + super.onChunkUnload(); + + if(!(this instanceof TileEntityReactorController) && getReactor() != null) + { + getReactor().formMultiblock(); + } + } + + @Override + public void onAdded() + { + super.onAdded(); + + if(!worldObj.isRemote) + { + if(getReactor() != null) + { + getReactor().formMultiblock(); + } + else { + if(!(this instanceof TileEntityReactorController)) + { + TileEntityReactorController found = new ControllerFinder().find(); + + if(found != null && (found.getReactor() == null || !found.getReactor().isFormed())) + { + found.formMultiblock(); + } + } + } + } + } + + public class ControllerFinder + { + public TileEntityReactorController found; + + public Set iterated = new HashSet(); + + public void loop(Coord4D pos) + { + if(iterated.size() > 512 || found != null) + { + return; + } + + iterated.add(pos); + + for(ForgeDirection side : ForgeDirection.VALID_DIRECTIONS) + { + Coord4D coord = pos.getFromSide(side); + + if(!iterated.contains(coord) && coord.getTileEntity(worldObj) instanceof TileEntityReactorBlock) + { + if(coord.getTileEntity(worldObj) instanceof TileEntityReactorController) + { + found = (TileEntityReactorController)coord.getTileEntity(worldObj); + return; + } + + loop(coord); + } + } + } + + public TileEntityReactorController find() + { + loop(Coord4D.get(TileEntityReactorBlock.this)); + + return found; + } + } } diff --git a/src/main/java/mekanism/generators/common/tile/reactor/TileEntityReactorController.java b/src/main/java/mekanism/generators/common/tile/reactor/TileEntityReactorController.java index d390b9dd2..ada8e9c2a 100644 --- a/src/main/java/mekanism/generators/common/tile/reactor/TileEntityReactorController.java +++ b/src/main/java/mekanism/generators/common/tile/reactor/TileEntityReactorController.java @@ -11,7 +11,7 @@ import mekanism.common.Mekanism; import mekanism.common.network.PacketTileEntity.TileEntityMessage; import mekanism.common.util.MekanismUtils; import mekanism.generators.common.FusionReactor; - +import net.minecraft.block.Block; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.AxisAlignedBB; @@ -19,10 +19,8 @@ import net.minecraftforge.fluids.FluidContainerRegistry; import net.minecraftforge.fluids.FluidRegistry; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.FluidTank; - import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; - import io.netty.buffer.ByteBuf; public class TileEntityReactorController extends TileEntityReactorBlock implements IActiveState @@ -41,8 +39,6 @@ public class TileEntityReactorController extends TileEntityReactorBlock implemen public AxisAlignedBB box; - public boolean tryForm = false; - public double clientTemp = 0; public boolean clientBurning = false; @@ -108,12 +104,26 @@ public class TileEntityReactorController extends TileEntityReactorBlock implemen clientTemp = getReactor().getPlasmaTemp(); } } - else if(tryForm && !worldObj.isRemote) + else if(ticker == 5 && !worldObj.isRemote) { formMultiblock(); - tryForm = false; } - + } + + @Override + public void onChunkUnload() + { + super.onChunkUnload(); + + formMultiblock(); + } + + @Override + public void onAdded() + { + super.onAdded(); + + formMultiblock(); } @Override @@ -153,7 +163,6 @@ public class TileEntityReactorController extends TileEntityReactorBlock implemen if(formed) { - tryForm = true; setReactor(new FusionReactor(this)); getReactor().setPlasmaTemp(tag.getDouble("plasmaTemp")); getReactor().setCaseTemp(tag.getDouble("caseTemp")); @@ -201,9 +210,6 @@ public class TileEntityReactorController extends TileEntityReactorBlock implemen switch(type) { case 0: - formMultiblock(); - break; - case 1: if(getReactor() != null) getReactor().setInjectionRate(dataStream.readInt()); break; } diff --git a/src/main/java/mekanism/generators/common/tile/reactor/TileEntityReactorLaserFocusMatrix.java b/src/main/java/mekanism/generators/common/tile/reactor/TileEntityReactorLaserFocusMatrix.java index 8fd162d35..441344837 100644 --- a/src/main/java/mekanism/generators/common/tile/reactor/TileEntityReactorLaserFocusMatrix.java +++ b/src/main/java/mekanism/generators/common/tile/reactor/TileEntityReactorLaserFocusMatrix.java @@ -17,7 +17,9 @@ public class TileEntityReactorLaserFocusMatrix extends TileEntityReactorBlock im public void receiveLaserEnergy(double energy, ForgeDirection side) { if(getReactor() != null) + { getReactor().addTemperatureFromEnergyInput(energy); + } } @Override diff --git a/src/main/java/mekanism/generators/common/tile/reactor/TileEntityReactorPort.java b/src/main/java/mekanism/generators/common/tile/reactor/TileEntityReactorPort.java index d67d9ec33..ac0bd335b 100644 --- a/src/main/java/mekanism/generators/common/tile/reactor/TileEntityReactorPort.java +++ b/src/main/java/mekanism/generators/common/tile/reactor/TileEntityReactorPort.java @@ -37,6 +37,11 @@ public class TileEntityReactorPort extends TileEntityReactorBlock implements IFl @Override public void onUpdate() { + if(changed) + { + worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord, zCoord, getBlockType()); + } + super.onUpdate(); CableUtils.emit(this);