From 66f0da433ef99649bc6d27190de5de7a4e323c64 Mon Sep 17 00:00:00 2001 From: Nedelosk Date: Tue, 29 Aug 2017 19:08:26 +0200 Subject: [PATCH] Fix Targeted state --- .../core/climate/ClimateContainer.java | 27 ++++++++++++++----- .../forestry/core/errors/EnumErrorCode.java | 4 +-- .../MultiblockEventHandlerClient.java | 2 +- .../greenhouse/GreenhouseEventHandler.java | 8 +++--- .../api/climate/IClimateContainer.java | 3 ++- .../modifiers/ClimateSourceModifier.java | 6 ++++- .../greenhouse/gui/GuiGreenhouse.java | 22 ++++++++------- .../gui/widgets/WidgetClimatePanel.java | 7 ++++- .../multiblock/FakeGreenhouseController.java | 1 - .../multiblock/GreenhouseController.java | 8 +++++- .../storage/GreenhouseProviderServer.java | 6 ++++- 11 files changed, 65 insertions(+), 29 deletions(-) diff --git a/src/main/java/forestry/core/climate/ClimateContainer.java b/src/main/java/forestry/core/climate/ClimateContainer.java index 840889787..9715ac650 100644 --- a/src/main/java/forestry/core/climate/ClimateContainer.java +++ b/src/main/java/forestry/core/climate/ClimateContainer.java @@ -62,7 +62,6 @@ public class ClimateContainer implements IClimateContainer, IStreamable { this.listeners = new ClimateContainerListeners(); this.sources = new HashSet<>(); this.delay = 20; - this.targetedState = parent.getDefaultClimate(); this.state = parent.getDefaultClimate().toMutable(); this.modifierData = new NBTTagCompound(); this.boundaryUp = ImmutableClimateState.MIN; @@ -165,7 +164,9 @@ public class ClimateContainer implements IClimateContainer, IStreamable { @Override public NBTTagCompound writeToNBT(NBTTagCompound nbt) { state.writeToNBT(nbt); - nbt.setTag("Target", targetedState.writeToNBT(new NBTTagCompound())); + if(targetedState != null) { + nbt.setTag("Target", targetedState.writeToNBT(new NBTTagCompound())); + } nbt.setTag("modifierData", modifierData); return nbt; } @@ -173,7 +174,9 @@ public class ClimateContainer implements IClimateContainer, IStreamable { @Override public void readFromNBT(NBTTagCompound nbt) { state.readFromNBT(nbt); - targetedState = new ImmutableClimateState(nbt.getCompoundTag("Target")); + if(nbt.hasKey("Target")) { + targetedState = new ImmutableClimateState(nbt.getCompoundTag("Target")); + } modifierData = nbt.getCompoundTag("modifierData"); } @@ -183,6 +186,7 @@ public class ClimateContainer implements IClimateContainer, IStreamable { } @Override + @Nullable public ImmutableClimateState getTargetedState() { return targetedState; } @@ -234,8 +238,13 @@ public class ClimateContainer implements IClimateContainer, IStreamable { data.writeFloat(boundaryUp.getHumidity()); data.writeFloat(boundaryDown.getTemperature()); data.writeFloat(boundaryDown.getHumidity()); - data.writeFloat(targetedState.getTemperature()); - data.writeFloat(targetedState.getHumidity()); + if(targetedState != null) { + data.writeBoolean(true); + data.writeFloat(targetedState.getTemperature()); + data.writeFloat(targetedState.getHumidity()); + }else{ + data.writeBoolean(false); + } data.writeCompoundTag(modifierData); } @@ -245,7 +254,11 @@ public class ClimateContainer implements IClimateContainer, IStreamable { state.setHumidity(data.readFloat()); boundaryUp = new ImmutableClimateState(data.readFloat(), data.readFloat()); boundaryDown = new ImmutableClimateState(data.readFloat(), data.readFloat()); - targetedState = new ImmutableClimateState(data.readFloat(), data.readFloat()); + if(data.readBoolean()) { + targetedState = new ImmutableClimateState(data.readFloat(), data.readFloat()); + }else{ + targetedState = null; + } modifierData = data.readCompoundTag(); } @@ -276,7 +289,7 @@ public class ClimateContainer implements IClimateContainer, IStreamable { } IClimateContainer container = (IClimateContainer) obj; IClimateHousing parent = container.getParent(); - if(parent == null || this.parent == null){ + if(parent.getCoordinates() == null || this.parent.getCoordinates() == null){ return false; } return this.parent.getCoordinates().equals(parent.getCoordinates()); diff --git a/src/main/java/forestry/core/errors/EnumErrorCode.java b/src/main/java/forestry/core/errors/EnumErrorCode.java index ce1d1fb1a..c1f15b848 100644 --- a/src/main/java/forestry/core/errors/EnumErrorCode.java +++ b/src/main/java/forestry/core/errors/EnumErrorCode.java @@ -86,8 +86,8 @@ public enum EnumErrorCode implements IErrorState { // Greenhouse NOT_LOADED("not_loaded"), // One chunk of the greenhouse is not loaded. - NOT_CLOSED("not_closed"), // The greenhouse is not closed. - TOO_LARGE("too_large") // The greenhouse is too large. + NOT_CLOSED("not_closed"), // The roof of the greenhouse is not closed. + TOO_LARGE("too_large") //The greenhouse is too large for its controller or it is not closed at a position. ; private final String name; diff --git a/src/main/java/forestry/core/multiblock/MultiblockEventHandlerClient.java b/src/main/java/forestry/core/multiblock/MultiblockEventHandlerClient.java index 91c7dea1b..58162d00c 100755 --- a/src/main/java/forestry/core/multiblock/MultiblockEventHandlerClient.java +++ b/src/main/java/forestry/core/multiblock/MultiblockEventHandlerClient.java @@ -90,7 +90,7 @@ public class MultiblockEventHandlerClient { GlStateManager.popMatrix(); } }catch(Exception e){ - Log.error("Failed to render the position of a multiblock exeption.", e); + Log.error("Failed to render the position of a multiblock exception.", e); } } } diff --git a/src/main/java/forestry/greenhouse/GreenhouseEventHandler.java b/src/main/java/forestry/greenhouse/GreenhouseEventHandler.java index be817805e..69511703a 100644 --- a/src/main/java/forestry/greenhouse/GreenhouseEventHandler.java +++ b/src/main/java/forestry/greenhouse/GreenhouseEventHandler.java @@ -96,15 +96,15 @@ public class GreenhouseEventHandler { GlStateManager.glLineWidth(2.5F); IGreenhouseLimits limits = provider.getUsedLimits(); if (limits != null) { - BlockPos offset = provider.getCenterPos(); Position2D minEdge = limits.getMinimumCoordinates(); Position2D maxEdge = limits.getMaximumCoordinates(); AxisAlignedBB greenhouseBB = new AxisAlignedBB(minEdge.getX(), limits.getDepth(), minEdge.getZ(), maxEdge.getX() + 1, limits.getHeight() + 1, maxEdge.getZ() + 1); - AxisAlignedBB testBlockBB = Block.FULL_BLOCK_AABB; - testBlockBB = testBlockBB.offset(offset); - RenderGlobal.drawSelectionBoundingBox(testBlockBB, 0.0F, 0.0F, 0.0F, 0.5F); RenderGlobal.drawSelectionBoundingBox(greenhouseBB, 0.0F, 0.0F, 0.0F, 0.5F); } + //Draw center position + BlockPos offset = provider.getCenterPos(); + AxisAlignedBB testBlockBB = Block.FULL_BLOCK_AABB.offset(offset); + RenderGlobal.drawSelectionBoundingBox(testBlockBB, 0.0F, 0.0F, 0.0F, 0.5F); GlStateManager.enableTexture2D(); GlStateManager.disableBlend(); GlStateManager.popMatrix(); diff --git a/src/main/java/forestry/greenhouse/api/climate/IClimateContainer.java b/src/main/java/forestry/greenhouse/api/climate/IClimateContainer.java index 482f1b6b9..bdf1c3b13 100644 --- a/src/main/java/forestry/greenhouse/api/climate/IClimateContainer.java +++ b/src/main/java/forestry/greenhouse/api/climate/IClimateContainer.java @@ -54,8 +54,9 @@ public interface IClimateContainer extends INbtReadable, INbtWritable, IClimateS Collection getListeners(); /** - * @return the state that the container targets to get his climate state to. + * @return only null if the controller never was assembled. */ + @Nullable ImmutableClimateState getTargetedState(); void setTargetedState(ImmutableClimateState state); diff --git a/src/main/java/forestry/greenhouse/climate/modifiers/ClimateSourceModifier.java b/src/main/java/forestry/greenhouse/climate/modifiers/ClimateSourceModifier.java index d6f3fa94a..b266a079b 100644 --- a/src/main/java/forestry/greenhouse/climate/modifiers/ClimateSourceModifier.java +++ b/src/main/java/forestry/greenhouse/climate/modifiers/ClimateSourceModifier.java @@ -54,8 +54,12 @@ public class ClimateSourceModifier extends BlankClimateModifier { data.setTag("rangeUp", boundaryUp.writeToNBT(new NBTTagCompound())); data.setTag("rangeDown", boundaryDown.writeToNBT(new NBTTagCompound())); + ImmutableClimateState targetedState = container.getTargetedState(); + if(targetedState == null){ + return newState; + } int workedSources = 0; - ImmutableClimateState target = getTargetOrBound(oldState, container.getBoundaryDown(), container.getBoundaryUp(), container.getTargetedState()); + ImmutableClimateState target = getTargetOrBound(oldState, container.getBoundaryDown(), container.getBoundaryUp(), targetedState); ClimateChange changeState = new ClimateChange(data.getCompoundTag("change")); for (IClimateSource source : container.getClimateSources()) { diff --git a/src/main/java/forestry/greenhouse/gui/GuiGreenhouse.java b/src/main/java/forestry/greenhouse/gui/GuiGreenhouse.java index 8658df9d9..2a29a735f 100755 --- a/src/main/java/forestry/greenhouse/gui/GuiGreenhouse.java +++ b/src/main/java/forestry/greenhouse/gui/GuiGreenhouse.java @@ -16,16 +16,16 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.math.BlockPos; import forestry.api.climate.ClimateType; -import forestry.greenhouse.api.climate.IClimateContainer; -import forestry.greenhouse.api.climate.IClimateData; import forestry.api.climate.ImmutableClimateState; import forestry.core.config.Constants; import forestry.core.gui.GuiForestryTitled; import forestry.core.gui.TextLayoutHelper; import forestry.core.utils.NetworkUtil; +import forestry.greenhouse.api.climate.IClimateContainer; +import forestry.greenhouse.api.climate.IClimateData; import forestry.greenhouse.gui.widgets.WidgetCamouflageTab; -import forestry.greenhouse.gui.widgets.WidgetClimatePanel; import forestry.greenhouse.gui.widgets.WidgetClimateBar; +import forestry.greenhouse.gui.widgets.WidgetClimatePanel; import forestry.greenhouse.multiblock.IGreenhouseControllerInternal; import forestry.greenhouse.network.packets.PacketSelectClimateTargeted; import forestry.greenhouse.tiles.TileGreenhouse; @@ -53,17 +53,21 @@ public class GuiGreenhouse extends GuiForestryTitled { //Add the camouflage tab widgetManager.add(new WidgetCamouflageTab(widgetManager, xSize / 4 - WidgetCamouflageTab.WIDTH / 2, -WidgetCamouflageTab.HEIGHT, controller, tile)); - widgetManager.add(new WidgetClimateBar(widgetManager, xSize / 3 + WidgetClimateBar.WIDTH / 3, -WidgetClimateBar.HEIGHT)); + if(container.getTargetedState() != null) { + widgetManager.add(new WidgetClimateBar(widgetManager, xSize / 3 + WidgetClimateBar.WIDTH / 3, -WidgetClimateBar.HEIGHT)); + } widgetManager.add(temperaturePanel = new WidgetClimatePanel(widgetManager, this, 9, 18, ClimateType.TEMPERATURE, data)); widgetManager.add(humidityPanel = new WidgetClimatePanel(widgetManager, this, 102, 18, ClimateType.HUMIDITY, data)); } public void sendNetworkUpdate() { - BlockPos pos = controller.getCoordinates(); - float temp = temperaturePanel.parseValue(); - float hum = humidityPanel.parseValue(); - setClimate(container, temp, hum); - NetworkUtil.sendToServer(new PacketSelectClimateTargeted(pos, container.getTargetedState())); + if(container.getTargetedState() != null) { + BlockPos pos = controller.getCoordinates(); + float temp = temperaturePanel.parseValue(); + float hum = humidityPanel.parseValue(); + setClimate(container, temp, hum); + NetworkUtil.sendToServer(new PacketSelectClimateTargeted(pos, container.getTargetedState())); + } } public void setClimate(IClimateContainer container, float temp, float hum) { diff --git a/src/main/java/forestry/greenhouse/gui/widgets/WidgetClimatePanel.java b/src/main/java/forestry/greenhouse/gui/widgets/WidgetClimatePanel.java index 352fc1259..d4a454ba1 100644 --- a/src/main/java/forestry/greenhouse/gui/widgets/WidgetClimatePanel.java +++ b/src/main/java/forestry/greenhouse/gui/widgets/WidgetClimatePanel.java @@ -67,7 +67,12 @@ public class WidgetClimatePanel extends Widget { textField = new GuiTextField(0, Minecraft.getMinecraft().fontRenderer, xPos + width / 2 - textFieldLength / 2, yPos + 14, textFieldLength, 10); textField.setValidator(NUMBER_FILTER); textField.setEnableBackgroundDrawing(false); - textField.setText(Float.toString(gui.container.getTargetedState().get(type))); + if(gui.container.getTargetedState() != null) { + textField.setText(Float.toString(gui.container.getTargetedState().get(type))); + }else{ + textField.setVisible(false); + textField.setEnabled(false); + } table = new Table(); for (Map.Entry entry : data.getData(type).entrySet()) { table.addValueEntry(entry.getKey(), StringUtil.floatAsPercent(entry.getValue())); diff --git a/src/main/java/forestry/greenhouse/multiblock/FakeGreenhouseController.java b/src/main/java/forestry/greenhouse/multiblock/FakeGreenhouseController.java index ac92cdb30..fbfbc17e3 100644 --- a/src/main/java/forestry/greenhouse/multiblock/FakeGreenhouseController.java +++ b/src/main/java/forestry/greenhouse/multiblock/FakeGreenhouseController.java @@ -108,6 +108,5 @@ public class FakeGreenhouseController extends FakeMultiblockController implement @Override public void setCenterCoordinates(BlockPos cordinates) { - } } diff --git a/src/main/java/forestry/greenhouse/multiblock/GreenhouseController.java b/src/main/java/forestry/greenhouse/multiblock/GreenhouseController.java index 4aff028d1..430f5a029 100644 --- a/src/main/java/forestry/greenhouse/multiblock/GreenhouseController.java +++ b/src/main/java/forestry/greenhouse/multiblock/GreenhouseController.java @@ -242,6 +242,7 @@ public class GreenhouseController extends RectangularMultiblockControllerBase im @Override protected void onMachinePaused() { provider.clear(true); + provider.getStorage().removeProviderFromChunks(); this.centerPos = BlockPos.ORIGIN; } @@ -250,6 +251,7 @@ public class GreenhouseController extends RectangularMultiblockControllerBase im super.onMachineDisassembled(); provider.clear(false); + provider.getStorage().removeProviderFromChunks(); this.centerPos = BlockPos.ORIGIN; @@ -395,7 +397,11 @@ public class GreenhouseController extends RectangularMultiblockControllerBase im createDefaultState(); limits = createLimits(); - climateContainer.setState(getDefaultClimate().toMutable()); + if(climateContainer.getTargetedState() == null) { + ImmutableClimateState defaultClimate = getDefaultClimate(); + climateContainer.setState(defaultClimate.toMutable()); + climateContainer.setTargetedState(defaultClimate); + } provider.init(centerPos, limits); assembleTickCount = getTickCount(); diff --git a/src/main/java/forestry/greenhouse/multiblock/blocks/storage/GreenhouseProviderServer.java b/src/main/java/forestry/greenhouse/multiblock/blocks/storage/GreenhouseProviderServer.java index 4549dae02..9bf702766 100644 --- a/src/main/java/forestry/greenhouse/multiblock/blocks/storage/GreenhouseProviderServer.java +++ b/src/main/java/forestry/greenhouse/multiblock/blocks/storage/GreenhouseProviderServer.java @@ -153,7 +153,7 @@ public class GreenhouseProviderServer extends GreenhouseProvider { || maxSize.getZ() < position.getZ() || minSize.getX() > position.getX() || minSize.getZ() > position.getZ()) { - return EnumErrorCode.NOT_CLOSED; + return EnumErrorCode.TOO_LARGE; } if (!world.isBlockLoaded(position)) { @@ -256,6 +256,10 @@ public class GreenhouseProviderServer extends GreenhouseProvider { return GreenhouseState.UNLOADED_CHUNK; } if(errorLogic.hasErrors()){ + //Remove the state NOT_CLOSED if the logic has the state TOO_LARGE because the state NOT_CLOSED can be caused by the TOO_LARGE state + if(errorLogic.getErrorStates().contains(EnumErrorCode.TOO_LARGE)){ + errorLogic.setCondition(false, EnumErrorCode.NOT_CLOSED); + } return GreenhouseState.OPEN; } this.size = height + depth + storage.getBlockCount();