Skip to content

Commit

Permalink
Fix Targeted state
Browse files Browse the repository at this point in the history
  • Loading branch information
Nedelosk committed Aug 29, 2017
1 parent d8c8aec commit 66f0da4
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 29 deletions.
27 changes: 20 additions & 7 deletions src/main/java/forestry/core/climate/ClimateContainer.java
Expand Up @@ -62,7 +62,6 @@ public ClimateContainer(IClimateHousing parent) {
this.listeners = new ClimateContainerListeners(); this.listeners = new ClimateContainerListeners();
this.sources = new HashSet<>(); this.sources = new HashSet<>();
this.delay = 20; this.delay = 20;
this.targetedState = parent.getDefaultClimate();
this.state = parent.getDefaultClimate().toMutable(); this.state = parent.getDefaultClimate().toMutable();
this.modifierData = new NBTTagCompound(); this.modifierData = new NBTTagCompound();
this.boundaryUp = ImmutableClimateState.MIN; this.boundaryUp = ImmutableClimateState.MIN;
Expand Down Expand Up @@ -165,15 +164,19 @@ public IClimateState getBoundaryUp() {
@Override @Override
public NBTTagCompound writeToNBT(NBTTagCompound nbt) { public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
state.writeToNBT(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); nbt.setTag("modifierData", modifierData);
return nbt; return nbt;
} }


@Override @Override
public void readFromNBT(NBTTagCompound nbt) { public void readFromNBT(NBTTagCompound nbt) {
state.readFromNBT(nbt); state.readFromNBT(nbt);
targetedState = new ImmutableClimateState(nbt.getCompoundTag("Target")); if(nbt.hasKey("Target")) {
targetedState = new ImmutableClimateState(nbt.getCompoundTag("Target"));
}
modifierData = nbt.getCompoundTag("modifierData"); modifierData = nbt.getCompoundTag("modifierData");
} }


Expand All @@ -183,6 +186,7 @@ public void setTargetedState(ImmutableClimateState state) {
} }


@Override @Override
@Nullable
public ImmutableClimateState getTargetedState() { public ImmutableClimateState getTargetedState() {
return targetedState; return targetedState;
} }
Expand Down Expand Up @@ -234,8 +238,13 @@ public void writeData(PacketBufferForestry data) {
data.writeFloat(boundaryUp.getHumidity()); data.writeFloat(boundaryUp.getHumidity());
data.writeFloat(boundaryDown.getTemperature()); data.writeFloat(boundaryDown.getTemperature());
data.writeFloat(boundaryDown.getHumidity()); data.writeFloat(boundaryDown.getHumidity());
data.writeFloat(targetedState.getTemperature()); if(targetedState != null) {
data.writeFloat(targetedState.getHumidity()); data.writeBoolean(true);
data.writeFloat(targetedState.getTemperature());
data.writeFloat(targetedState.getHumidity());
}else{
data.writeBoolean(false);
}
data.writeCompoundTag(modifierData); data.writeCompoundTag(modifierData);
} }


Expand All @@ -245,7 +254,11 @@ public void readData(PacketBufferForestry data) throws IOException {
state.setHumidity(data.readFloat()); state.setHumidity(data.readFloat());
boundaryUp = new ImmutableClimateState(data.readFloat(), data.readFloat()); boundaryUp = new ImmutableClimateState(data.readFloat(), data.readFloat());
boundaryDown = 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(); modifierData = data.readCompoundTag();
} }


Expand Down Expand Up @@ -276,7 +289,7 @@ public boolean equals(Object obj) {
} }
IClimateContainer container = (IClimateContainer) obj; IClimateContainer container = (IClimateContainer) obj;
IClimateHousing parent = container.getParent(); IClimateHousing parent = container.getParent();
if(parent == null || this.parent == null){ if(parent.getCoordinates() == null || this.parent.getCoordinates() == null){
return false; return false;
} }
return this.parent.getCoordinates().equals(parent.getCoordinates()); return this.parent.getCoordinates().equals(parent.getCoordinates());
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/forestry/core/errors/EnumErrorCode.java
Expand Up @@ -86,8 +86,8 @@ public enum EnumErrorCode implements IErrorState {


// Greenhouse // Greenhouse
NOT_LOADED("not_loaded"), // One chunk of the greenhouse is not loaded. NOT_LOADED("not_loaded"), // One chunk of the greenhouse is not loaded.
NOT_CLOSED("not_closed"), // The greenhouse is not closed. NOT_CLOSED("not_closed"), // The roof of the greenhouse is not closed.
TOO_LARGE("too_large") // The greenhouse is too large. TOO_LARGE("too_large") //The greenhouse is too large for its controller or it is not closed at a position.
; ;


private final String name; private final String name;
Expand Down
Expand Up @@ -90,7 +90,7 @@ public void onWorldRenderLast(RenderWorldLastEvent event){
GlStateManager.popMatrix(); GlStateManager.popMatrix();
} }
}catch(Exception e){ }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);
} }
} }
} }
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/forestry/greenhouse/GreenhouseEventHandler.java
Expand Up @@ -96,15 +96,15 @@ public void onWorldRenderLast(RenderWorldLastEvent event) {
GlStateManager.glLineWidth(2.5F); GlStateManager.glLineWidth(2.5F);
IGreenhouseLimits limits = provider.getUsedLimits(); IGreenhouseLimits limits = provider.getUsedLimits();
if (limits != null) { if (limits != null) {
BlockPos offset = provider.getCenterPos();
Position2D minEdge = limits.getMinimumCoordinates(); Position2D minEdge = limits.getMinimumCoordinates();
Position2D maxEdge = limits.getMaximumCoordinates(); Position2D maxEdge = limits.getMaximumCoordinates();
AxisAlignedBB greenhouseBB = new AxisAlignedBB(minEdge.getX(), limits.getDepth(), minEdge.getZ(), maxEdge.getX() + 1, limits.getHeight() + 1, maxEdge.getZ() + 1); 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); 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.enableTexture2D();
GlStateManager.disableBlend(); GlStateManager.disableBlend();
GlStateManager.popMatrix(); GlStateManager.popMatrix();
Expand Down
Expand Up @@ -54,8 +54,9 @@ public interface IClimateContainer extends INbtReadable, INbtWritable, IClimateS
Collection<IClimateContainerListener> getListeners(); Collection<IClimateContainerListener> 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(); ImmutableClimateState getTargetedState();


void setTargetedState(ImmutableClimateState state); void setTargetedState(ImmutableClimateState state);
Expand Down
Expand Up @@ -54,8 +54,12 @@ public IClimateState modifyTarget(IClimateContainer container, IClimateState new
data.setTag("rangeUp", boundaryUp.writeToNBT(new NBTTagCompound())); data.setTag("rangeUp", boundaryUp.writeToNBT(new NBTTagCompound()));
data.setTag("rangeDown", boundaryDown.writeToNBT(new NBTTagCompound())); data.setTag("rangeDown", boundaryDown.writeToNBT(new NBTTagCompound()));


ImmutableClimateState targetedState = container.getTargetedState();
if(targetedState == null){
return newState;
}
int workedSources = 0; 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")); ClimateChange changeState = new ClimateChange(data.getCompoundTag("change"));


for (IClimateSource source : container.getClimateSources()) { for (IClimateSource source : container.getClimateSources()) {
Expand Down
22 changes: 13 additions & 9 deletions src/main/java/forestry/greenhouse/gui/GuiGreenhouse.java
Expand Up @@ -16,16 +16,16 @@
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;


import forestry.api.climate.ClimateType; import forestry.api.climate.ClimateType;
import forestry.greenhouse.api.climate.IClimateContainer;
import forestry.greenhouse.api.climate.IClimateData;
import forestry.api.climate.ImmutableClimateState; import forestry.api.climate.ImmutableClimateState;
import forestry.core.config.Constants; import forestry.core.config.Constants;
import forestry.core.gui.GuiForestryTitled; import forestry.core.gui.GuiForestryTitled;
import forestry.core.gui.TextLayoutHelper; import forestry.core.gui.TextLayoutHelper;
import forestry.core.utils.NetworkUtil; 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.WidgetCamouflageTab;
import forestry.greenhouse.gui.widgets.WidgetClimatePanel;
import forestry.greenhouse.gui.widgets.WidgetClimateBar; import forestry.greenhouse.gui.widgets.WidgetClimateBar;
import forestry.greenhouse.gui.widgets.WidgetClimatePanel;
import forestry.greenhouse.multiblock.IGreenhouseControllerInternal; import forestry.greenhouse.multiblock.IGreenhouseControllerInternal;
import forestry.greenhouse.network.packets.PacketSelectClimateTargeted; import forestry.greenhouse.network.packets.PacketSelectClimateTargeted;
import forestry.greenhouse.tiles.TileGreenhouse; import forestry.greenhouse.tiles.TileGreenhouse;
Expand Down Expand Up @@ -53,17 +53,21 @@ public GuiGreenhouse(EntityPlayer player, TileGreenhouse tile) {


//Add the camouflage tab //Add the camouflage tab
widgetManager.add(new WidgetCamouflageTab(widgetManager, xSize / 4 - WidgetCamouflageTab.WIDTH / 2, -WidgetCamouflageTab.HEIGHT, controller, tile)); 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(temperaturePanel = new WidgetClimatePanel(widgetManager, this, 9, 18, ClimateType.TEMPERATURE, data));
widgetManager.add(humidityPanel = new WidgetClimatePanel(widgetManager, this, 102, 18, ClimateType.HUMIDITY, data)); widgetManager.add(humidityPanel = new WidgetClimatePanel(widgetManager, this, 102, 18, ClimateType.HUMIDITY, data));
} }


public void sendNetworkUpdate() { public void sendNetworkUpdate() {
BlockPos pos = controller.getCoordinates(); if(container.getTargetedState() != null) {
float temp = temperaturePanel.parseValue(); BlockPos pos = controller.getCoordinates();
float hum = humidityPanel.parseValue(); float temp = temperaturePanel.parseValue();
setClimate(container, temp, hum); float hum = humidityPanel.parseValue();
NetworkUtil.sendToServer(new PacketSelectClimateTargeted(pos, container.getTargetedState())); setClimate(container, temp, hum);
NetworkUtil.sendToServer(new PacketSelectClimateTargeted(pos, container.getTargetedState()));
}
} }


public void setClimate(IClimateContainer container, float temp, float hum) { public void setClimate(IClimateContainer container, float temp, float hum) {
Expand Down
Expand Up @@ -67,7 +67,12 @@ public WidgetClimatePanel(WidgetManager manager, GuiGreenhouse gui, int xPos, in
textField = new GuiTextField(0, Minecraft.getMinecraft().fontRenderer, xPos + width / 2 - textFieldLength / 2, yPos + 14, textFieldLength, 10); textField = new GuiTextField(0, Minecraft.getMinecraft().fontRenderer, xPos + width / 2 - textFieldLength / 2, yPos + 14, textFieldLength, 10);
textField.setValidator(NUMBER_FILTER); textField.setValidator(NUMBER_FILTER);
textField.setEnableBackgroundDrawing(false); 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(); table = new Table();
for (Map.Entry<String, Float> entry : data.getData(type).entrySet()) { for (Map.Entry<String, Float> entry : data.getData(type).entrySet()) {
table.addValueEntry(entry.getKey(), StringUtil.floatAsPercent(entry.getValue())); table.addValueEntry(entry.getKey(), StringUtil.floatAsPercent(entry.getValue()));
Expand Down
Expand Up @@ -108,6 +108,5 @@ public BlockPos getCenterCoordinates() {


@Override @Override
public void setCenterCoordinates(BlockPos cordinates) { public void setCenterCoordinates(BlockPos cordinates) {

} }
} }
Expand Up @@ -242,6 +242,7 @@ protected void onMachineRestored() {
@Override @Override
protected void onMachinePaused() { protected void onMachinePaused() {
provider.clear(true); provider.clear(true);
provider.getStorage().removeProviderFromChunks();
this.centerPos = BlockPos.ORIGIN; this.centerPos = BlockPos.ORIGIN;
} }


Expand All @@ -250,6 +251,7 @@ protected void onMachineDisassembled() {
super.onMachineDisassembled(); super.onMachineDisassembled();


provider.clear(false); provider.clear(false);
provider.getStorage().removeProviderFromChunks();


this.centerPos = BlockPos.ORIGIN; this.centerPos = BlockPos.ORIGIN;


Expand Down Expand Up @@ -395,7 +397,11 @@ protected void onMachineAssembled() {


createDefaultState(); createDefaultState();
limits = createLimits(); limits = createLimits();
climateContainer.setState(getDefaultClimate().toMutable()); if(climateContainer.getTargetedState() == null) {
ImmutableClimateState defaultClimate = getDefaultClimate();
climateContainer.setState(defaultClimate.toMutable());
climateContainer.setTargetedState(defaultClimate);
}


provider.init(centerPos, limits); provider.init(centerPos, limits);
assembleTickCount = getTickCount(); assembleTickCount = getTickCount();
Expand Down
Expand Up @@ -153,7 +153,7 @@ public IErrorState checkPosition(BlockPos position) {
|| maxSize.getZ() < position.getZ() || maxSize.getZ() < position.getZ()
|| minSize.getX() > position.getX() || minSize.getX() > position.getX()
|| minSize.getZ() > position.getZ()) { || minSize.getZ() > position.getZ()) {
return EnumErrorCode.NOT_CLOSED; return EnumErrorCode.TOO_LARGE;
} }


if (!world.isBlockLoaded(position)) { if (!world.isBlockLoaded(position)) {
Expand Down Expand Up @@ -256,6 +256,10 @@ private GreenhouseState checkBlocks(Collection<IGreenhouseBlock> blocks) {
return GreenhouseState.UNLOADED_CHUNK; return GreenhouseState.UNLOADED_CHUNK;
} }
if(errorLogic.hasErrors()){ 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; return GreenhouseState.OPEN;
} }
this.size = height + depth + storage.getBlockCount(); this.size = height + depth + storage.getBlockCount();
Expand Down

0 comments on commit 66f0da4

Please sign in to comment.