From 1180c16f451a433317afaf486fb420c140c0d389 Mon Sep 17 00:00:00 2001 From: Nedelosk Date: Fri, 8 Sep 2017 22:43:24 +0200 Subject: [PATCH] Improve Climate Sources --- gradle.properties | 2 +- .../api/climate/ClimateStateType.java | 15 +- .../forestry/api/climate/ClimateType.java | 12 - .../forestry/api/climate/IClimateState.java | 14 +- .../forestry/api/climate/IClimateStates.java | 8 - .../core/climate/AbsentClimateState.java | 18 +- .../forestry/core/climate/ClimateInfo.java | 8 +- .../forestry/core/climate/ClimateManager.java | 2 +- .../forestry/core/climate/ClimateSource.java | 206 -------------- .../forestry/core/climate/ClimateState.java | 88 ++---- .../forestry/core/climate/ClimateStates.java | 57 ++-- .../java/forestry/core/config/Config.java | 4 +- .../api/climate/IClimateSource.java | 15 +- .../api/climate/IClimateSourceContainer.java | 6 +- .../greenhouse/climate/ClimateContainer.java | 22 +- .../greenhouse/climate/ClimateSource.java | 257 ++++++++++++++++++ .../climate/ClimateSourceCircuitable.java | 2 - .../climate/ClimateSourceClimatiser.java | 22 +- .../climate/ClimateSourceHygroregulator.java | 10 +- .../climate/ClimateSourceMode.java | 13 +- .../climate/ClimateSourceType.java | 4 +- .../climate/ClimateSourceWindow.java | 11 +- .../modifiers/ClimateSourceModifier.java | 96 ++----- .../greenhouse/gui/GuiGreenhouse.java | 2 +- .../gui/widgets/WidgetClimateBar.java | 2 +- .../items/ItemGreenhouseScreen.java | 18 +- .../multiblock/GreenhouseController.java | 5 +- .../greenhouse/tiles/TileClimatiser.java | 8 +- .../tiles/TileGreenhouseWindow.java | 11 +- 29 files changed, 461 insertions(+), 477 deletions(-) delete mode 100644 src/main/java/forestry/core/climate/ClimateSource.java create mode 100644 src/main/java/forestry/greenhouse/climate/ClimateSource.java rename src/main/java/forestry/{core => greenhouse}/climate/ClimateSourceMode.java (72%) rename src/main/java/forestry/{core => greenhouse}/climate/ClimateSourceType.java (98%) diff --git a/gradle.properties b/gradle.properties index d6c820eff..73787c2d9 100755 --- a/gradle.properties +++ b/gradle.properties @@ -11,4 +11,4 @@ ic2_version=2.8.24-ex112 version_major=5 version_minor=5 -version_patch=2 +version_patch=3 diff --git a/src/main/java/forestry/api/climate/ClimateStateType.java b/src/main/java/forestry/api/climate/ClimateStateType.java index 5a85af827..91d2fd591 100644 --- a/src/main/java/forestry/api/climate/ClimateStateType.java +++ b/src/main/java/forestry/api/climate/ClimateStateType.java @@ -5,21 +5,20 @@ ******************************************************************************/ package forestry.api.climate; -import java.util.function.Function; - import net.minecraft.util.math.MathHelper; public enum ClimateStateType { - IMMUTABLE(0.0F, 2.0F), - MUTABLE(0.0F, 2.0F), - CHANGE(-3.0F, 3.0F); + DEFAULT(0.0F, 2.0F), + EXTENDED(-3.0F, 3.0F); - public final Function bounds; - public final Float boundDown, boundUp; + public final float boundDown, boundUp; ClimateStateType(Float boundDown, Float boundUp) { - this.bounds = v -> MathHelper.clamp(v, boundDown, boundUp); this.boundDown = boundDown; this.boundUp = boundUp; } + + public float clamp(float value) { + return MathHelper.clamp(value, boundDown, boundUp); + } } diff --git a/src/main/java/forestry/api/climate/ClimateType.java b/src/main/java/forestry/api/climate/ClimateType.java index ca821c5ff..b5d142d32 100644 --- a/src/main/java/forestry/api/climate/ClimateType.java +++ b/src/main/java/forestry/api/climate/ClimateType.java @@ -7,21 +7,9 @@ package forestry.api.climate; import java.util.Locale; -import net.minecraft.client.renderer.texture.TextureAtlasSprite; - -import net.minecraftforge.fml.relauncher.Side; -import net.minecraftforge.fml.relauncher.SideOnly; - -import forestry.api.core.ForestryAPI; - public enum ClimateType { TEMPERATURE, HUMIDITY; - @SideOnly(Side.CLIENT) - public TextureAtlasSprite getSprite() { - return ForestryAPI.textureManager.getDefault("misc/" + getName()); - } - public String getName(){ return name().toLowerCase(Locale.ENGLISH); } diff --git a/src/main/java/forestry/api/climate/IClimateState.java b/src/main/java/forestry/api/climate/IClimateState.java index d532a549c..61ffcdd29 100644 --- a/src/main/java/forestry/api/climate/IClimateState.java +++ b/src/main/java/forestry/api/climate/IClimateState.java @@ -11,8 +11,8 @@ import forestry.api.core.INbtWritable; /** * A {@link IClimateState} is used to store and handle temperature and humidity. - * - * The range of the value of this two goes from 0.0F to 2.0F. And is oriented on the values of {@link Biome#getTemperature()} and {@link Biome#getRainfall()}. + * + * The values are oriented on the values of {@link Biome#getTemperature()} and {@link Biome#getRainfall()}. */ public interface IClimateState extends INbtWritable { @@ -25,18 +25,18 @@ public interface IClimateState extends INbtWritable { IClimateState addHumidity(float humidity); IClimateState add(IClimateState state); - + + IClimateState scale(double factor); + IClimateState remove(IClimateState state); default float get(ClimateType type){ return type == ClimateType.HUMIDITY ? getHumidity() : getTemperature(); } - IClimateState toState(ClimateStateType type); + IClimateState copy(ClimateStateType type); - IClimateState setTemperature(float temperature); - - IClimateState setHumidity(float humidity); + IClimateState copy(); boolean isPresent(); diff --git a/src/main/java/forestry/api/climate/IClimateStates.java b/src/main/java/forestry/api/climate/IClimateStates.java index e98f3ebb9..45c024c25 100644 --- a/src/main/java/forestry/api/climate/IClimateStates.java +++ b/src/main/java/forestry/api/climate/IClimateStates.java @@ -15,16 +15,8 @@ public interface IClimateStates { IClimateState create(float temperature, float humidity, ClimateStateType type); - default IClimateState create(float temperature, float humidity){ - return create(temperature, humidity, ClimateStateType.MUTABLE); - } - IClimateState create(IClimateState climateState, ClimateStateType type); - default IClimateState create(IClimateState climateState){ - return create(climateState, ClimateStateType.MUTABLE); - } - IClimateState absent(); IClimateState min(); diff --git a/src/main/java/forestry/core/climate/AbsentClimateState.java b/src/main/java/forestry/core/climate/AbsentClimateState.java index 4c5f99096..5456010a5 100644 --- a/src/main/java/forestry/core/climate/AbsentClimateState.java +++ b/src/main/java/forestry/core/climate/AbsentClimateState.java @@ -31,17 +31,12 @@ public final class AbsentClimateState implements IClimateState { } @Override - public IClimateState toState(ClimateStateType type) { + public IClimateState copy(ClimateStateType type) { return this; } @Override - public IClimateState setHumidity(float humidity) { - return this; - } - - @Override - public IClimateState setTemperature(float temperature) { + public IClimateState copy() { return this; } @@ -60,6 +55,11 @@ public final class AbsentClimateState implements IClimateState { return this; } + @Override + public IClimateState scale(double factor) { + return this; + } + @Override public IClimateState remove(IClimateState state){ return this; @@ -72,7 +72,7 @@ public final class AbsentClimateState implements IClimateState { @Override public ClimateStateType getType() { - return ClimateStateType.IMMUTABLE; + return ClimateStateType.DEFAULT; } @Override @@ -91,7 +91,7 @@ public final class AbsentClimateState implements IClimateState { return false; } IClimateState otherState = (IClimateState) obj; - return otherState.getTemperature() == Float.NaN && otherState.getHumidity() == Float.NaN; + return Float.isNaN(otherState.getTemperature()) && Float.isNaN(otherState.getHumidity()); } @Override diff --git a/src/main/java/forestry/core/climate/ClimateInfo.java b/src/main/java/forestry/core/climate/ClimateInfo.java index c491c5f4b..445113912 100644 --- a/src/main/java/forestry/core/climate/ClimateInfo.java +++ b/src/main/java/forestry/core/climate/ClimateInfo.java @@ -20,13 +20,13 @@ public class ClimateInfo implements IClimateInfo { private final float humidity; public ClimateInfo(float temperature, float humidity) { - this.temperature = ClimateStateType.IMMUTABLE.bounds.apply(temperature); - this.humidity = ClimateStateType.IMMUTABLE.bounds.apply(humidity); + this.temperature = ClimateStateType.DEFAULT.clamp(temperature); + this.humidity = ClimateStateType.DEFAULT.clamp(humidity); } public ClimateInfo(IClimateState climateState) { - this.temperature = climateState.toState(ClimateStateType.IMMUTABLE).getTemperature(); - this.humidity = climateState.toState(ClimateStateType.IMMUTABLE).getHumidity(); + this.temperature = climateState.copy(ClimateStateType.DEFAULT).getTemperature(); + this.humidity = climateState.copy(ClimateStateType.DEFAULT).getHumidity(); } @Override diff --git a/src/main/java/forestry/core/climate/ClimateManager.java b/src/main/java/forestry/core/climate/ClimateManager.java index 43a785a85..7ada3a766 100644 --- a/src/main/java/forestry/core/climate/ClimateManager.java +++ b/src/main/java/forestry/core/climate/ClimateManager.java @@ -58,7 +58,7 @@ public class ClimateManager implements IClimateManager { private IClimateState getBiomeState(Biome biome) { if (!BIOME_STATES.containsKey(biome)) { - BIOME_STATES.put(biome, ClimateStates.immutableOf(biome.getTemperature(), biome.getRainfall())); + BIOME_STATES.put(biome, ClimateStates.of(biome.getTemperature(), biome.getRainfall())); } return BIOME_STATES.get(biome); } diff --git a/src/main/java/forestry/core/climate/ClimateSource.java b/src/main/java/forestry/core/climate/ClimateSource.java deleted file mode 100644 index 898f8b308..000000000 --- a/src/main/java/forestry/core/climate/ClimateSource.java +++ /dev/null @@ -1,206 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2011-2014 SirSengir. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the GNU Lesser Public License v3 - * which accompanies this distribution, and is available at - * http://www.gnu.org/licenses/lgpl-3.0.txt - * - * Various Contributors including, but not limited to: - * SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges - ******************************************************************************/ -package forestry.core.climate; - -import javax.annotation.Nullable; - -import net.minecraft.world.World; - -import forestry.api.climate.ClimateType; -import forestry.api.climate.IClimateState; -import forestry.greenhouse.api.climate.IClimateContainer; -import forestry.greenhouse.api.climate.IClimateSource; -import forestry.greenhouse.api.climate.IClimateSourceOwner; -import forestry.greenhouse.api.greenhouse.GreenhouseManager; - -public abstract class ClimateSource implements IClimateSource { - - protected O owner; - protected float change; - protected final float range; - protected final ClimateSourceType sourceType; - protected ClimateSourceMode temperatureMode; - protected ClimateSourceMode humidityMode; - protected IClimateContainer container; - protected boolean addedToManager; - protected boolean isActive; - - public ClimateSource(float change, float range, ClimateSourceType sourceType) { - this.change = change; - this.range = range; - this.sourceType = sourceType; - this.temperatureMode = ClimateSourceMode.NONE; - this.humidityMode = ClimateSourceMode.NONE; - } - - public void setHumidityMode(ClimateSourceMode humidityMode) { - this.humidityMode = humidityMode; - } - - public void setTemperatureMode(ClimateSourceMode temperatureMode) { - this.temperatureMode = temperatureMode; - } - - public void setOwner(O owner) { - this.owner = owner; - } - - @Override - public float getBoundaryModifier(ClimateType type, boolean boundaryUp) { - if(type == ClimateType.HUMIDITY){ - if(humidityMode == ClimateSourceMode.POSITIVE && boundaryUp){ - return getRange(ClimateType.HUMIDITY); - }else if(humidityMode == ClimateSourceMode.NEGATIVE && !boundaryUp){ - return getRange(ClimateType.HUMIDITY); - } - }else { - if(temperatureMode == ClimateSourceMode.POSITIVE && boundaryUp){ - return getRange(ClimateType.TEMPERATURE); - }else if(temperatureMode == ClimateSourceMode.NEGATIVE && !boundaryUp){ - return getRange(ClimateType.TEMPERATURE); - } - } - return 0; - } - - protected float getRange(ClimateType type){ - return range; - } - - protected float getChange(ClimateType type) { - return change; - } - - @Override - public boolean isActive() { - return isActive; - } - - @Override - public boolean affectClimateType(ClimateType type) { - return sourceType.affectClimateType(type); - } - - @Override - public IClimateSourceOwner getOwner() { - return owner; - } - - @Override - public void onAdded(IClimateContainer container) { - this.container = container; - } - - @Override - public void onRemoved(IClimateContainer container) { - this.container = null; - } - - public void update() { - if(!addedToManager){ - onLoad(); - } - } - - public void onLoad() { - World world = owner.getWorldObj(); - if(!addedToManager && !world.isRemote) { - GreenhouseManager.climateManager.addSource(owner); - addedToManager = true; - } - - } - - public void invalidate() { - this.onChunkUnload(); - } - - public void onChunkUnload() { - World world = owner.getWorldObj(); - if(addedToManager &&!world.isRemote) { - GreenhouseManager.climateManager.removeSource(owner); - addedToManager = false; - } - - } - - @Override - public IClimateState work(IClimateState state, IClimateState target) { - beforeWork(); - ClimateSourceType validType = getValidType(state, target); - if(validType == null){ - isNotValid(); - isActive = false; - return AbsentClimateState.INSTANCE; - } - if(!canWork(state, target)){ - isActive = false; - return AbsentClimateState.INSTANCE; - } - removeResources(state, target); - isActive = true; - return getChange(validType, state, target); - } - - protected void isNotValid(){ - - } - - protected void beforeWork(){ - } - - /** - * @param state the {@link IClimateState} that the source has to work on. - * @param target the by the {@link IClimateContainer} targeted {@link IClimateState}. - * - * @return true if the source can work, false if it can not. - */ - protected abstract boolean canWork(IClimateState state, IClimateState target); - - protected abstract void removeResources(IClimateState state, IClimateState target); - - protected abstract IClimateState getChange(ClimateSourceType type, IClimateState state, IClimateState target); - - @Nullable - private ClimateSourceType getValidType(IClimateState state, IClimateState target){ - boolean canChangeHumidity = false; - boolean canChangeTemperature = false; - if(sourceType.canChangeHumidity()){ - if(canChange(state.getHumidity(), target.getHumidity(), humidityMode)){ - canChangeHumidity = true; - } - } - if(sourceType.canChangeTemperature()){ - if(canChange(state.getTemperature(), target.getTemperature(), temperatureMode)){ - canChangeTemperature = true; - } - } - if(canChangeHumidity){ - if(canChangeTemperature){ - return ClimateSourceType.BOTH; - } - return ClimateSourceType.HUMIDITY; - }else if(canChangeTemperature){ - return ClimateSourceType.TEMPERATURE; - } - return null; - } - - private boolean canChange(float value, float target, ClimateSourceMode mode){ - if(mode == ClimateSourceMode.POSITIVE && value < target){ - return true; - }else if(mode == ClimateSourceMode.NEGATIVE && value > target){ - return true; - } - return false; - } - -} diff --git a/src/main/java/forestry/core/climate/ClimateState.java b/src/main/java/forestry/core/climate/ClimateState.java index 91744cd05..b54b85d1b 100644 --- a/src/main/java/forestry/core/climate/ClimateState.java +++ b/src/main/java/forestry/core/climate/ClimateState.java @@ -12,8 +12,6 @@ package forestry.core.climate; import com.google.common.base.MoreObjects; -import java.util.function.Function; - import net.minecraft.nbt.NBTTagCompound; import forestry.api.climate.ClimateStateType; @@ -23,9 +21,9 @@ import forestry.api.climate.IClimateState; class ClimateState implements IClimateState, IClimateInfo { // The minimum climate state. - public static final ClimateState MIN = new ClimateState(0.0F, 0.0F, ClimateStateType.IMMUTABLE); + public static final ClimateState MIN = new ClimateState(0.0F, 0.0F, ClimateStateType.DEFAULT); // The maximum climate state. - public static final ClimateState MAX = new ClimateState(2.0F, 2.0F, ClimateStateType.IMMUTABLE); + public static final ClimateState MAX = new ClimateState(2.0F, 2.0F, ClimateStateType.DEFAULT); public static final String TEMPERATURE_NBT_KEY = "TEMP"; public static final String HUMIDITY_NBT_KEY = "HUMID"; @@ -33,7 +31,6 @@ class ClimateState implements IClimateState, IClimateInfo { public static final String ABSENT_NBT_KEY = "ABSENT"; protected final ClimateStateType type; - protected final Function bounds; protected float temperature; protected float humidity; @@ -43,20 +40,17 @@ class ClimateState implements IClimateState, IClimateInfo { public ClimateState(float temperature, float humidity, ClimateStateType type) { this.type = type; - this.bounds = type.bounds; - this.temperature = bounds.apply(temperature); - this.humidity = bounds.apply(humidity); + this.temperature = type.clamp(temperature); + this.humidity = type.clamp(humidity); } public ClimateState(NBTTagCompound compound, ClimateStateType type) { this.type = type; - this.bounds = type.bounds; readFromNBT(compound); } public ClimateState(NBTTagCompound compound) { this.type = ClimateStateType.values()[compound.getByte(TYPE_NBT_KEY)]; - this.bounds = type.bounds; readFromNBT(compound); } @@ -70,87 +64,43 @@ class ClimateState implements IClimateState, IClimateInfo { } public void readFromNBT(NBTTagCompound compound) { - this.temperature = bounds.apply(compound.getFloat(TEMPERATURE_NBT_KEY)); - this.humidity = bounds.apply(compound.getFloat(HUMIDITY_NBT_KEY)); + this.temperature = type.clamp(compound.getFloat(TEMPERATURE_NBT_KEY)); + this.humidity = type.clamp(compound.getFloat(HUMIDITY_NBT_KEY)); } @Override - public IClimateState setHumidity(float humidity) { - if(type == ClimateStateType.IMMUTABLE){ - return ClimateStates.immutableOf(getTemperature(), humidity); - } - this.humidity = bounds.apply(humidity); - if(!isPresent()){ - return AbsentClimateState.INSTANCE; - } - return this; + public IClimateState copy(ClimateStateType type) { + return new ClimateState(this, type); } @Override - public IClimateState setTemperature(float temperature) { - if(type == ClimateStateType.IMMUTABLE){ - return ClimateStates.immutableOf(temperature, getHumidity()); - } - this.temperature = bounds.apply(temperature); - if(!isPresent()){ - return AbsentClimateState.INSTANCE; - } - return this; - } - - @Override - public IClimateState toState(ClimateStateType type) { + public IClimateState copy() { return new ClimateState(this, type); } @Override public IClimateState addTemperature(float temperature){ - if (type == ClimateStateType.IMMUTABLE) { - return ClimateStates.immutableOf(this.temperature + temperature, humidity); - } - this.temperature = bounds.apply(this.temperature + temperature); - if(!isPresent()){ - return AbsentClimateState.INSTANCE; - } - return this; + return ClimateStates.of(this.temperature + temperature, humidity, type); } @Override public IClimateState addHumidity(float humidity){ - if (type == ClimateStateType.IMMUTABLE) { - return ClimateStates.immutableOf(temperature, this.humidity + humidity); - } - this.humidity= bounds.apply(this.humidity + humidity); - if(!isPresent()){ - return AbsentClimateState.INSTANCE; - } - return this; + return ClimateStates.of(temperature, this.humidity + humidity, type); } @Override public IClimateState add(IClimateState state){ - if (type == ClimateStateType.IMMUTABLE) { - return ClimateStates.immutableOf(this.temperature + state.getTemperature(), this.humidity + state.getHumidity()); - } - addTemperature(state.getTemperature()); - addHumidity(state.getHumidity()); - if(!isPresent()){ - return AbsentClimateState.INSTANCE; - } - return this; + return ClimateStates.of(this.temperature + state.getTemperature(), this.humidity + state.getHumidity(), type); } - + + @Override + public IClimateState scale(double factor) { + return ClimateStates.of((float) (this.temperature * factor), (float) (this.humidity * factor), type); + } + @Override public IClimateState remove(IClimateState state){ - if (type == ClimateStateType.IMMUTABLE) { - return ClimateStates.immutableOf(this.temperature - state.getTemperature(), this.humidity - state.getHumidity()); - } - addTemperature(-state.getTemperature()); - addHumidity(-state.getHumidity()); - if(!isPresent()){ - return AbsentClimateState.INSTANCE; - } - return this; + return ClimateStates.of(this.temperature - state.getTemperature(), this.humidity - state.getHumidity(), type); } @Override diff --git a/src/main/java/forestry/core/climate/ClimateStates.java b/src/main/java/forestry/core/climate/ClimateStates.java index 12219b5c5..b33cb3e93 100644 --- a/src/main/java/forestry/core/climate/ClimateStates.java +++ b/src/main/java/forestry/core/climate/ClimateStates.java @@ -15,54 +15,63 @@ import net.minecraft.nbt.NBTTagCompound; import forestry.api.climate.ClimateStateType; import forestry.api.climate.IClimateState; import forestry.api.climate.IClimateStates; +import forestry.greenhouse.climate.modifiers.ClimateSourceModifier; public final class ClimateStates implements IClimateStates { public static final ClimateStates INSTANCE = new ClimateStates(); + public static final IClimateState ZERO = of(0.0F, 0.0F); private ClimateStates() { } public static IClimateState of(float temperature, float humidity, ClimateStateType type) { - IClimateState state = INSTANCE.create(temperature, humidity, type); - if (!state.isPresent()) { - return INSTANCE.absent(); - } - return state; + return INSTANCE.create(temperature, humidity, type); } public static IClimateState of(float temperature, float humidity) { - IClimateState state = INSTANCE.create(temperature, humidity, ClimateStateType.MUTABLE); - if (!state.isPresent()) { - return INSTANCE.absent(); - } - return state; + return INSTANCE.create(temperature, humidity, ClimateStateType.DEFAULT); } - public static IClimateState immutableOf(float temperature, float humidity) { - IClimateState state = INSTANCE.create(temperature, humidity, ClimateStateType.IMMUTABLE); - if (!state.isPresent()) { - return INSTANCE.absent(); - } - return state; + public static IClimateState extendedOf(float temperature, float humidity) { + return INSTANCE.create(temperature, humidity, ClimateStateType.EXTENDED); } - public static IClimateState changeOf(float temperature, float humidity) { - IClimateState state = INSTANCE.create(temperature, humidity, ClimateStateType.CHANGE); - if (!state.isPresent()) { - return INSTANCE.absent(); - } - return state; + public static IClimateState extendedZero() { + return INSTANCE.create(ZERO, ClimateStateType.EXTENDED); + } + + public static boolean isNearTarget(IClimateState state, IClimateState target) { + return target.getHumidity() - ClimateSourceModifier.CLIMATE_CHANGE < state.getHumidity() + && target.getHumidity() + ClimateSourceModifier.CLIMATE_CHANGE > state.getHumidity() + && target.getTemperature() - ClimateSourceModifier.CLIMATE_CHANGE < state.getTemperature() + && target.getTemperature() + ClimateSourceModifier.CLIMATE_CHANGE > state.getTemperature(); + } + + public static boolean isZero(IClimateState state) { + return state.getHumidity() == ZERO.getHumidity() && state.getTemperature() == ZERO.getTemperature(); + } + + public static boolean isNearZero(IClimateState state) { + return isNearTarget(state, ZERO); } @Override public IClimateState create(IClimateState climateState, ClimateStateType type) { - return new ClimateState(climateState, type); + IClimateState state = new ClimateState(climateState, type); + if (!state.isPresent()) { + return absent(); + } + return state; } @Override public IClimateState create(float temperature, float humidity, ClimateStateType type) { - return new ClimateState(temperature, humidity, type); + IClimateState state = new ClimateState(temperature, humidity, type); + if (!state.isPresent()) { + return absent(); + } + return state; } @Override diff --git a/src/main/java/forestry/core/config/Config.java b/src/main/java/forestry/core/config/Config.java index 8cb7adbb3..f1d2c665f 100755 --- a/src/main/java/forestry/core/config/Config.java +++ b/src/main/java/forestry/core/config/Config.java @@ -65,8 +65,8 @@ public class Config { //Greenhouse public static int climateSourceRange = 36; + public static float climateSourceEnergyModifier = 1.5F; public static int greenhouseSize = 4; - public static boolean showClimateTable = true; // Genetics public static boolean pollinateVanillaTrees = true; @@ -258,8 +258,8 @@ public class Config { enableMagicalCropsSupport = configCommon.getBooleanLocalized("tweaks.farms", "magicalcrops", enableMagicalCropsSupport); climateSourceRange = configCommon.getIntLocalized("tweaks.greenhouse", "range", climateSourceRange, 9, 270); + climateSourceEnergyModifier = configCommon.getFloatLocalized("tweaks.greenhouse", "energy", climateSourceEnergyModifier, 0.0F, 15.0F); greenhouseSize = configCommon.getIntLocalized("tweaks.greenhouse", "size", greenhouseSize, 1, 5); - showClimateTable = configCommon.getBooleanLocalized("tweaks.greenhouse", "table", showClimateTable); String[] availableStructures = new String[]{"alveary3x3", "farm3x3", "farm3x4", "farm3x5", "farm4x4", "farm5x5"}; String[] disabledStructureArray = disabledStructures.toArray(new String[disabledStructures.size()]); diff --git a/src/main/java/forestry/greenhouse/api/climate/IClimateSource.java b/src/main/java/forestry/greenhouse/api/climate/IClimateSource.java index fac8557ee..cd5d7b351 100644 --- a/src/main/java/forestry/greenhouse/api/climate/IClimateSource.java +++ b/src/main/java/forestry/greenhouse/api/climate/IClimateSource.java @@ -9,6 +9,8 @@ import javax.annotation.Nullable; import forestry.api.climate.ClimateType; import forestry.api.climate.IClimateState; +import forestry.api.core.INbtReadable; +import forestry.api.core.INbtWritable; /** * A climate source is stored in a {@link IClimateSourceOwner}. It is used by the {@link IClimateSourceContainer}s to change the climate of a {@link IClimateContainer}. @@ -16,7 +18,7 @@ import forestry.api.climate.IClimateState; *

* In forestry it is used in a part of a {@link IClimateModifier}. */ -public interface IClimateSource { +public interface IClimateSource extends INbtWritable, INbtReadable { /** * The owner of this source. @@ -35,10 +37,10 @@ public interface IClimateSource { boolean affectClimateType(ClimateType type); /** - * @param state the {@link IClimateState} that the source has to work on. - * @param target the by the {@link IClimateContainer} targeted {@link IClimateState}. + * @param previousState the {@link IClimateState} that the source has to work on. + * @param targetState the by the {@link IClimateSourceContainer} targeted {@link IClimateState}. */ - IClimateState work(IClimateState state, IClimateState target); + IClimateState work(IClimateState previousState, IClimateState targetState, IClimateState currentState, final double sizeModifier, final boolean canWork); /** * @return true if source has changed the climate at the last work cicle. @@ -55,4 +57,9 @@ public interface IClimateSource { */ void onRemoved(IClimateContainer container); + /** + * @return A copy of the current state of this source. + */ + IClimateState getState(); + } diff --git a/src/main/java/forestry/greenhouse/api/climate/IClimateSourceContainer.java b/src/main/java/forestry/greenhouse/api/climate/IClimateSourceContainer.java index 5fc0c10c6..533dae82d 100644 --- a/src/main/java/forestry/greenhouse/api/climate/IClimateSourceContainer.java +++ b/src/main/java/forestry/greenhouse/api/climate/IClimateSourceContainer.java @@ -39,10 +39,12 @@ public interface IClimateSourceContainer { */ IClimateState getBoundaryDown(); + double getSizeModifier(); + /** - * Calculates the range of all {@link IClimateSource}s together. + * Calculates the up and down boundary and the size modifier. */ - void recalculateBoundaries(double sizeModifier); + void recalculateBoundaries(); /** * @return True if the container allows the {@link IClimateSource}s to work. diff --git a/src/main/java/forestry/greenhouse/climate/ClimateContainer.java b/src/main/java/forestry/greenhouse/climate/ClimateContainer.java index f399764aa..d95dd42da 100644 --- a/src/main/java/forestry/greenhouse/climate/ClimateContainer.java +++ b/src/main/java/forestry/greenhouse/climate/ClimateContainer.java @@ -31,6 +31,7 @@ import forestry.api.core.ForestryAPI; import forestry.api.greenhouse.IClimateHousing; import forestry.core.climate.AbsentClimateState; import forestry.core.climate.ClimateStates; +import forestry.core.config.Config; import forestry.core.network.IStreamable; import forestry.core.network.PacketBufferForestry; import forestry.core.network.packets.PacketUpdateClimate; @@ -49,6 +50,7 @@ public class ClimateContainer implements IClimateContainer, IStreamable { protected IClimateState targetedState; protected IClimateState boundaryUp; protected IClimateState boundaryDown; + protected double sizeModifier; private NBTTagCompound modifierData; /** @@ -62,12 +64,13 @@ public class ClimateContainer implements IClimateContainer, IStreamable { this.parent = parent; this.sources = new HashSet<>(); this.delay = 20; - this.state = parent.getDefaultClimate().toState(ClimateStateType.MUTABLE); + this.state = parent.getDefaultClimate().copy(); this.modifierData = new NBTTagCompound(); this.boundaryUp = ClimateStates.INSTANCE.min(); this.boundaryDown = ClimateStates.INSTANCE.min(); this.targetedState = AbsentClimateState.INSTANCE; this.canWork = canWork; + this.sizeModifier = 1.0D; } @Override @@ -78,21 +81,26 @@ public class ClimateContainer implements IClimateContainer, IStreamable { @Override public void updateClimate(int ticks) { if (ticks % getTickDelay() == 0) { - IClimateState oldState = state.toState(ClimateStateType.IMMUTABLE); - state = parent.getDefaultClimate().toState(ClimateStateType.CHANGE); + IClimateState oldState = state.copy(ClimateStateType.DEFAULT); + state = parent.getDefaultClimate().copy(ClimateStateType.EXTENDED); for (IClimateModifier modifier : GreenhouseClimateManager.getInstance().getModifiers()) { - state = modifier.modifyTarget(this, state, oldState, modifierData).toState(ClimateStateType.CHANGE); + state = modifier.modifyTarget(this, state, oldState, modifierData).copy(ClimateStateType.EXTENDED); } - state = state.toState(ClimateStateType.MUTABLE); if (!state.equals(oldState)) { BlockPos coordinates = parent.getCoordinates(); NetworkUtil.sendNetworkPacket(new PacketUpdateClimate(coordinates, this), coordinates, parent.getWorldObj()); } } } - + @Override - public void recalculateBoundaries(double sizeModifier){ + public double getSizeModifier() { + return sizeModifier; + } + + @Override + public void recalculateBoundaries() { + sizeModifier = Math.max(parent.getSize() / Config.climateSourceRange, 1.0D); float temperatureBoundaryUp = 0.0F; float humidityBoundaryUp = 0.0F; float temperatureBoundaryDown = 0.0F; diff --git a/src/main/java/forestry/greenhouse/climate/ClimateSource.java b/src/main/java/forestry/greenhouse/climate/ClimateSource.java new file mode 100644 index 000000000..2862a1db6 --- /dev/null +++ b/src/main/java/forestry/greenhouse/climate/ClimateSource.java @@ -0,0 +1,257 @@ +/******************************************************************************* + * Copyright (c) 2011-2014 SirSengir. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Lesser Public License v3 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/lgpl-3.0.txt + * + * Various Contributors including, but not limited to: + * SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges + ******************************************************************************/ +package forestry.greenhouse.climate; + +import javax.annotation.Nullable; + +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.world.World; + +import forestry.api.climate.ClimateStateType; +import forestry.api.climate.ClimateType; +import forestry.api.climate.IClimateState; +import forestry.core.climate.ClimateStates; +import forestry.core.utils.Log; +import forestry.greenhouse.api.climate.IClimateContainer; +import forestry.greenhouse.api.climate.IClimateSource; +import forestry.greenhouse.api.climate.IClimateSourceOwner; +import forestry.greenhouse.api.greenhouse.GreenhouseManager; + +public abstract class ClimateSource implements IClimateSource { + + protected final float range; + protected final ClimateSourceType sourceType; + protected O owner; + private IClimateState state; + protected float change; + protected ClimateSourceMode temperatureMode; + protected ClimateSourceMode humidityMode; + protected IClimateContainer container; + protected boolean addedToManager; + protected boolean isActive; + + public ClimateSource(float change, float range, ClimateSourceType sourceType) { + this.change = change; + this.range = range; + this.sourceType = sourceType; + this.temperatureMode = ClimateSourceMode.NONE; + this.humidityMode = ClimateSourceMode.NONE; + this.state = ClimateStates.INSTANCE.absent(); + } + + public void setHumidityMode(ClimateSourceMode humidityMode) { + this.humidityMode = humidityMode; + } + + public void setTemperatureMode(ClimateSourceMode temperatureMode) { + this.temperatureMode = temperatureMode; + } + + public void setOwner(O owner) { + this.owner = owner; + } + + @Override + public float getBoundaryModifier(ClimateType type, boolean boundaryUp) { + if(type == ClimateType.HUMIDITY){ + if(humidityMode == ClimateSourceMode.POSITIVE && boundaryUp){ + return getRange(ClimateType.HUMIDITY); + }else if(humidityMode == ClimateSourceMode.NEGATIVE && !boundaryUp){ + return getRange(ClimateType.HUMIDITY); + } + }else { + if(temperatureMode == ClimateSourceMode.POSITIVE && boundaryUp){ + return getRange(ClimateType.TEMPERATURE); + }else if(temperatureMode == ClimateSourceMode.NEGATIVE && !boundaryUp){ + return getRange(ClimateType.TEMPERATURE); + } + } + return 0; + } + + protected float getRange(ClimateType type){ + return range; + } + + protected float getChange(ClimateType type) { + return change; + } + + @Override + public boolean isActive() { + return isActive; + } + + @Override + public boolean affectClimateType(ClimateType type) { + return sourceType.affectClimateType(type); + } + + @Override + public IClimateSourceOwner getOwner() { + return owner; + } + + @Override + public void onAdded(IClimateContainer container) { + this.container = container; + } + + @Override + public void onRemoved(IClimateContainer container) { + this.container = null; + } + + public void update() { + if(!addedToManager){ + onLoad(); + } + } + + public void onLoad() { + World world = owner.getWorldObj(); + if(!addedToManager && !world.isRemote) { + GreenhouseManager.climateManager.addSource(owner); + addedToManager = true; + } + + } + + public void invalidate() { + this.onChunkUnload(); + } + + public void onChunkUnload() { + World world = owner.getWorldObj(); + if(addedToManager &&!world.isRemote) { + GreenhouseManager.climateManager.removeSource(owner); + addedToManager = false; + } + } + + @Override + public final IClimateState work(IClimateState previousState, IClimateState targetState, IClimateState currentState, final double sizeModifier, final boolean canWork) { + IClimateState state = ClimateStates.INSTANCE.create(getState(), ClimateStateType.EXTENDED); + IClimateState change = ClimateStates.extendedZero(); + IClimateState defaultState = container.getParent().getDefaultClimate(); + ClimateSourceType validType = getWorkType(currentState, targetState); + ClimateSourceType oppositeType = getOppositeWorkType(currentState, defaultState); + beforeWork(); + boolean work = canWork(state, oppositeType); + //Test if the source can work and test if the owner has enough resources to work. + if (!canWork || !work && oppositeType != null) { + isActive = false; + isNotValid(); + if (ClimateStates.isZero(state)) { + return change; + } else if (ClimateStates.isNearZero(state)) { + setState(change); + return change; + } else if (ClimateStates.isNearTarget(currentState, targetState)) { + return change; + } + //If the state is not already zero, remove one change state from the state. + change = getChange(oppositeType, defaultState, currentState); + change = ClimateStates.INSTANCE.create(-change.getTemperature(), -change.getHumidity(), ClimateStateType.EXTENDED); + } else if (validType == null && oppositeType != null) { + //Remove the resources if the owner has enough resources and the state is not the default state. + removeResources(state, oppositeType); + } else { + change = getChange(validType, targetState, previousState); + IClimateState changedState = state.add(change.scale(1 / sizeModifier)); + boolean couldWork = canWork(changedState, oppositeType); + //Test if the owner could work with the changed state. If he can remove the resources for the changed state, if not only remove the resources for the old state. + removeResources(couldWork ? changedState : state, oppositeType); + if (!couldWork) { + change = ClimateStates.extendedZero(); + } + } + state.add(change.scale(1 / sizeModifier)); + if (ClimateStates.isZero(state) || ClimateStates.isNearZero(state)) { + state = ClimateStates.extendedZero(); + } + if (false) { + state = ClimateStates.extendedZero(); + } + if (!state.isPresent()) { + state = ClimateStates.extendedZero(); + Log.error("Failed to update a climate source. Please report this to the authors of the mod. Previous={}, Target={}, Current={}, Size={}", previousState, targetState, currentState, sizeModifier); + } + setState(state); + return change; + } + + protected void isNotValid(){ + + } + + protected void beforeWork(){ + } + + /** + * @return true if the source can work, false if it can not. + */ + protected abstract boolean canWork(IClimateState currentState, ClimateSourceType oppositeType); + + protected abstract void removeResources(IClimateState currentState, ClimateSourceType oppositeType); + + protected abstract IClimateState getChange(@Nullable ClimateSourceType type, IClimateState target, IClimateState currentState); + + @Nullable + protected ClimateSourceType getOppositeWorkType(IClimateState state, IClimateState target) { + boolean canChangeHumidity = sourceType.canChangeHumidity() && canChange(state.getHumidity(), target.getHumidity(), humidityMode.getOpposite()); + boolean canChangeTemperature = sourceType.canChangeTemperature() && canChange(state.getTemperature(), target.getTemperature(), temperatureMode.getOpposite()); + return canChangeHumidity ? canChangeTemperature ? ClimateSourceType.BOTH : ClimateSourceType.HUMIDITY : canChangeTemperature ? ClimateSourceType.TEMPERATURE : null; + } + + @Nullable + protected ClimateSourceType getWorkType(IClimateState state, IClimateState target) { + boolean canChangeHumidity = sourceType.canChangeHumidity() && canChange(state.getHumidity(), target.getHumidity(), humidityMode); + boolean canChangeTemperature = sourceType.canChangeTemperature() && canChange(state.getTemperature(), target.getTemperature(), temperatureMode); + return canChangeHumidity ? canChangeTemperature ? ClimateSourceType.BOTH : ClimateSourceType.HUMIDITY : canChangeTemperature ? ClimateSourceType.TEMPERATURE : null; + } + + private boolean canChange(float value, float target, ClimateSourceMode mode){ + if(mode == ClimateSourceMode.POSITIVE && value < target){ + return true; + }else if(mode == ClimateSourceMode.NEGATIVE && value > target){ + return true; + } + return false; + } + + @Override + public IClimateState getState() { + return state; + } + + protected void setState(IClimateState state) { + this.state = state; + } + + @Override + public NBTTagCompound writeToNBT(NBTTagCompound nbt) { + NBTTagCompound sourceData = new NBTTagCompound(); + state.writeToNBT(sourceData); + nbt.setTag("Source", sourceData); + return nbt; + } + + @Override + public void readFromNBT(NBTTagCompound nbt) { + NBTTagCompound sourceData = nbt.getCompoundTag("Source"); + if (sourceData.hasNoTags()) { + return; + } + state = ClimateStates.INSTANCE.create(sourceData, ClimateStateType.EXTENDED); + } + +} diff --git a/src/main/java/forestry/greenhouse/climate/ClimateSourceCircuitable.java b/src/main/java/forestry/greenhouse/climate/ClimateSourceCircuitable.java index f872c51cf..566ed34e7 100644 --- a/src/main/java/forestry/greenhouse/climate/ClimateSourceCircuitable.java +++ b/src/main/java/forestry/greenhouse/climate/ClimateSourceCircuitable.java @@ -11,8 +11,6 @@ package forestry.greenhouse.climate; import forestry.api.climate.ClimateType; -import forestry.core.climate.ClimateSource; -import forestry.core.climate.ClimateSourceType; import forestry.greenhouse.api.climate.IClimateSourceCircuitable; import forestry.greenhouse.api.climate.IClimateSourceOwner; diff --git a/src/main/java/forestry/greenhouse/climate/ClimateSourceClimatiser.java b/src/main/java/forestry/greenhouse/climate/ClimateSourceClimatiser.java index 47fdded8e..77b58addd 100644 --- a/src/main/java/forestry/greenhouse/climate/ClimateSourceClimatiser.java +++ b/src/main/java/forestry/greenhouse/climate/ClimateSourceClimatiser.java @@ -14,9 +14,8 @@ import forestry.api.climate.ClimateType; import forestry.api.climate.IClimateState; import forestry.api.core.IErrorLogic; import forestry.api.greenhouse.IClimateHousing; -import forestry.core.climate.ClimateSourceMode; -import forestry.core.climate.ClimateSourceType; import forestry.core.climate.ClimateStates; +import forestry.core.config.Config; import forestry.core.errors.EnumErrorCode; import forestry.energy.EnergyManager; import forestry.greenhouse.api.climate.IClimateContainer; @@ -25,7 +24,7 @@ import forestry.greenhouse.tiles.TileClimatiser; public class ClimateSourceClimatiser extends ClimateSourceCircuitable { - protected static final int ENERGY_PER_OPERATION = 150; + protected static final int ENERGY_PER_OPERATION = 75; public ClimateSourceClimatiser(ClimateSourceType type, float change, float range) { super(change, range, type); @@ -48,14 +47,14 @@ public class ClimateSourceClimatiser extends ClimateSo } @Override - public boolean canWork(IClimateState state, IClimateState target) { + public boolean canWork(IClimateState currentState, ClimateSourceType oppositeType) { IClimateHousing region = container.getParent(); if (region instanceof IGreenhouseControllerInternal) { IGreenhouseControllerInternal controller = (IGreenhouseControllerInternal) region; IErrorLogic errorLogic = owner.getErrorLogic(); EnergyManager energyManager = controller.getEnergyManager(); - if (energyManager.extractEnergy(ENERGY_PER_OPERATION, true) > 0) { + if (energyManager.extractEnergy((int) (ENERGY_PER_OPERATION * getEnergyModifier(currentState, oppositeType)), true) > 0) { owner.setActive(true); errorLogic.setCondition(false, EnumErrorCode.NO_POWER); return true; @@ -72,19 +71,24 @@ public class ClimateSourceClimatiser extends ClimateSo } @Override - protected void removeResources(IClimateState state, IClimateState target) { + protected void removeResources(IClimateState currentState, ClimateSourceType oppositeType) { IClimateHousing region = container.getParent(); if (region instanceof IGreenhouseControllerInternal) { IGreenhouseControllerInternal controller = (IGreenhouseControllerInternal) region; EnergyManager energyManager = controller.getEnergyManager(); - energyManager.extractEnergy(ENERGY_PER_OPERATION, false); + energyManager.extractEnergy((int) (ENERGY_PER_OPERATION * getEnergyModifier(currentState, oppositeType)), false); } } + protected float getEnergyModifier(IClimateState currentState, ClimateSourceType oppositeType) { + float change = oppositeType.canChangeTemperature() ? currentState.getTemperature() : oppositeType.canChangeHumidity() ? currentState.getHumidity() : 0.0F; + return 1.0F + change * Config.climateSourceEnergyModifier; + } + @Override - protected IClimateState getChange(ClimateSourceType type, IClimateState state, IClimateState target) { + protected IClimateState getChange(ClimateSourceType type, IClimateState target, IClimateState currentState) { float temperature = 0.0F; float humidity = 0.0F; if (type.canChangeHumidity()) { @@ -93,7 +97,7 @@ public class ClimateSourceClimatiser extends ClimateSo if (type.canChangeTemperature()) { temperature += getChange(ClimateType.TEMPERATURE); } - return ClimateStates.changeOf(temperature, humidity); + return ClimateStates.extendedOf(temperature, humidity); } } diff --git a/src/main/java/forestry/greenhouse/climate/ClimateSourceHygroregulator.java b/src/main/java/forestry/greenhouse/climate/ClimateSourceHygroregulator.java index 3261901fa..2baf8698d 100644 --- a/src/main/java/forestry/greenhouse/climate/ClimateSourceHygroregulator.java +++ b/src/main/java/forestry/greenhouse/climate/ClimateSourceHygroregulator.java @@ -15,8 +15,6 @@ import net.minecraftforge.fluids.FluidStack; import forestry.api.climate.ClimateType; import forestry.api.climate.IClimateState; import forestry.api.core.IErrorLogic; -import forestry.core.climate.ClimateSourceMode; -import forestry.core.climate.ClimateSourceType; import forestry.core.climate.ClimateStates; import forestry.core.errors.EnumErrorCode; import forestry.core.fluids.FilteredTank; @@ -59,7 +57,7 @@ public class ClimateSourceHygroregulator extends ClimateSourceCircuitable { } @Override - public boolean canWork(IClimateState state, IClimateState target) { + public boolean canWork(IClimateState currentState, ClimateSourceType oppositeType) { return owner.getMode() == WindowMode.OPEN; } @Override - protected void removeResources(IClimateState state, IClimateState target) { + protected void removeResources(IClimateState currentState, ClimateSourceType oppositeType) { } @Override - protected IClimateState getChange(ClimateSourceType type, IClimateState state, IClimateState target) { + protected IClimateState getChange(ClimateSourceType type, IClimateState target, IClimateState currentState) { float temperature = 0.0F; float humidity = 0.0F; if (type.canChangeHumidity()) { @@ -72,7 +69,7 @@ public class ClimateSourceWindow extends ClimateSource { temperature += change; } } - return ClimateStates.changeOf(temperature, humidity); + return ClimateStates.extendedOf(temperature, humidity); } } diff --git a/src/main/java/forestry/greenhouse/climate/modifiers/ClimateSourceModifier.java b/src/main/java/forestry/greenhouse/climate/modifiers/ClimateSourceModifier.java index 247541319..03e7bda65 100644 --- a/src/main/java/forestry/greenhouse/climate/modifiers/ClimateSourceModifier.java +++ b/src/main/java/forestry/greenhouse/climate/modifiers/ClimateSourceModifier.java @@ -22,9 +22,7 @@ import net.minecraftforge.fml.relauncher.SideOnly; import forestry.api.climate.ClimateStateType; import forestry.api.climate.ClimateType; import forestry.api.climate.IClimateState; -import forestry.api.greenhouse.IClimateHousing; import forestry.core.climate.ClimateStates; -import forestry.core.config.Config; import forestry.core.utils.StringUtil; import forestry.core.utils.Translator; import forestry.greenhouse.PluginGreenhouse; @@ -36,7 +34,7 @@ public class ClimateSourceModifier implements IClimateModifier { public static final float CLIMATE_CHANGE = 0.01F; @Override - public IClimateState modifyTarget(IClimateContainer container, IClimateState newState, IClimateState oldState, NBTTagCompound data) { + public IClimateState modifyTarget(IClimateContainer container, IClimateState newState, IClimateState previousState, NBTTagCompound data) { Collection sources = container.getClimateSources(); if (sources.isEmpty()) { data.removeTag("rangeUp"); @@ -44,82 +42,41 @@ public class ClimateSourceModifier implements IClimateModifier { data.removeTag("change"); return newState; } - IClimateHousing housing = container.getParent(); - double sizeModifier = housing.getSize() / Config.climateSourceRange; - sizeModifier = Math.max(sizeModifier, 1.0D); + container.recalculateBoundaries(); - IClimateState changeState = ClimateStates.INSTANCE.create(data.getCompoundTag("change"), ClimateStateType.CHANGE); - if (!container.canWork()) { - IClimateState defaultState = container.getParent().getDefaultClimate(); - float defaultTemperature = defaultState.getTemperature(); - float defaultHumidity = defaultState.getHumidity(); - float temperature = changeState.getTemperature(); - float humidity = changeState.getHumidity(); - if (temperature != defaultTemperature) { - if (temperature > defaultTemperature) { - changeState = changeState.addTemperature((float) -Math.min(CLIMATE_CHANGE / sizeModifier, temperature - defaultTemperature)); - } else { - changeState = changeState.addTemperature((float) Math.min(CLIMATE_CHANGE / sizeModifier, defaultTemperature - temperature)); - } - } - if (humidity != defaultHumidity) { - if (humidity > defaultHumidity) { - changeState = changeState.addHumidity((float) -Math.min(CLIMATE_CHANGE / sizeModifier, humidity - defaultHumidity)); - } else { - changeState = changeState.addHumidity((float) Math.min(CLIMATE_CHANGE / sizeModifier, defaultHumidity - humidity)); - } - } - } else { - container.recalculateBoundaries(sizeModifier); + IClimateState boundaryUp = container.getBoundaryUp(); + IClimateState boundaryDown = container.getBoundaryDown(); - IClimateState boundaryUp = container.getBoundaryUp(); - IClimateState boundaryDown = container.getBoundaryDown(); + //Send the boundaries to the client + data.setTag("rangeUp", boundaryUp.writeToNBT(new NBTTagCompound())); + data.setTag("rangeDown", boundaryDown.writeToNBT(new NBTTagCompound())); - data.setTag("rangeUp", boundaryUp.writeToNBT(new NBTTagCompound())); - data.setTag("rangeDown", boundaryDown.writeToNBT(new NBTTagCompound())); + IClimateState targetedState = container.getTargetedState(); + if (!targetedState.isPresent()) { + return newState; + } + IClimateState target = getTargetOrBound(previousState, container.getBoundaryDown(), container.getBoundaryUp(), targetedState); + IClimateState changeState = ClimateStates.extendedZero(); - IClimateState targetedState = container.getTargetedState(); - if (!targetedState.isPresent()) { - return newState; - } - int workedSources = 0; - IClimateState target = getTargetOrBound(oldState, container.getBoundaryDown(), container.getBoundaryUp(), targetedState); + for (IClimateSource source : container.getClimateSources()) { + newState = newState.add(source.getState()); + } - for (IClimateSource source : container.getClimateSources()) { - IClimateState state = source.work(oldState, target); - if (state.isPresent()) { - boolean hasChange = false; - double temperatureChange = state.getTemperature(); - double humidityChange = state.getHumidity(); - if (temperatureChange != 0) { - temperatureChange /= sizeModifier; - hasChange = true; - } - if (humidityChange != 0) { - humidityChange /= sizeModifier; - hasChange = true; - } - if (hasChange) { - changeState = changeState.addTemperature((float) temperatureChange); - changeState = changeState.addHumidity((float) humidityChange); - workedSources++; - } - } - } - if (workedSources == 0) { - return newState.add(changeState); - } + for (IClimateSource source : container.getClimateSources()) { + IClimateState state = source.work(previousState, target, newState, container.getSizeModifier(), container.canWork()); + changeState = changeState.add(source.getState()); + newState = newState.add(state); } data.setTag("change", changeState.writeToNBT(new NBTTagCompound())); - return newState.add(changeState); + return newState; } @Override @SideOnly(Side.CLIENT) public void addInformation(IClimateContainer container, NBTTagCompound nbtData, ClimateType type, List lines) { - IClimateState rangeDown = ClimateStates.INSTANCE.create(nbtData.getCompoundTag("rangeDown"), ClimateStateType.MUTABLE); - IClimateState rangeUp = ClimateStates.INSTANCE.create(nbtData.getCompoundTag("rangeUp"), ClimateStateType.MUTABLE); - IClimateState change = ClimateStates.INSTANCE.create(nbtData.getCompoundTag("change"), ClimateStateType.MUTABLE); + IClimateState rangeDown = ClimateStates.INSTANCE.create(nbtData.getCompoundTag("rangeDown"), ClimateStateType.DEFAULT); + IClimateState rangeUp = ClimateStates.INSTANCE.create(nbtData.getCompoundTag("rangeUp"), ClimateStateType.DEFAULT); + IClimateState change = ClimateStates.INSTANCE.create(nbtData.getCompoundTag("change"), ClimateStateType.EXTENDED); if (type == ClimateType.HUMIDITY) { lines.add(Translator.translateToLocalFormatted("for.gui.modifier.sources.range.up", StringUtil.floatAsPercent(rangeUp.getHumidity()))); lines.add(Translator.translateToLocalFormatted("for.gui.modifier.sources.range.down", StringUtil.floatAsPercent(rangeDown.getHumidity()))); @@ -146,6 +103,9 @@ public class ClimateSourceModifier implements IClimateModifier { return -1; } + /** + * @return The target if it is within the bounds and the bounds if it is above or below the bounds. + */ private IClimateState getTargetOrBound(IClimateState climateState, IClimateState boundaryDown, IClimateState boundaryUp, IClimateState targetedState) { float temperature = climateState.getTemperature(); float humidity = climateState.getHumidity(); @@ -161,7 +121,7 @@ public class ClimateSourceModifier implements IClimateModifier { } else if (targetHumidity < humidity) { humidity = Math.max(targetHumidity, boundaryDown.getHumidity()); } - return ClimateStates.immutableOf(temperature, humidity); + return ClimateStates.of(temperature, humidity); } @Override diff --git a/src/main/java/forestry/greenhouse/gui/GuiGreenhouse.java b/src/main/java/forestry/greenhouse/gui/GuiGreenhouse.java index 8887a32b4..3f36927e5 100755 --- a/src/main/java/forestry/greenhouse/gui/GuiGreenhouse.java +++ b/src/main/java/forestry/greenhouse/gui/GuiGreenhouse.java @@ -72,7 +72,7 @@ public class GuiGreenhouse extends GuiForestryTitled { public void setClimate(IClimateContainer container, float temp, float hum) { temperaturePanel.setValue(temp); humidityPanel.setValue(hum); - container.setTargetedState(ClimateStates.immutableOf(temp, hum)); + container.setTargetedState(ClimateStates.of(temp, hum)); } @Override diff --git a/src/main/java/forestry/greenhouse/gui/widgets/WidgetClimateBar.java b/src/main/java/forestry/greenhouse/gui/widgets/WidgetClimateBar.java index 5710d9596..2e4be85fb 100644 --- a/src/main/java/forestry/greenhouse/gui/widgets/WidgetClimateBar.java +++ b/src/main/java/forestry/greenhouse/gui/widgets/WidgetClimateBar.java @@ -105,7 +105,7 @@ public class WidgetClimateBar extends Widget { String spriteName; EnumClimate(String spriteName, Biome biome) { - climateState = ClimateStates.immutableOf(biome.getTemperature(), biome.getRainfall()); + climateState = ClimateStates.of(biome.getTemperature(), biome.getRainfall()); this.spriteName = spriteName; } diff --git a/src/main/java/forestry/greenhouse/items/ItemGreenhouseScreen.java b/src/main/java/forestry/greenhouse/items/ItemGreenhouseScreen.java index 117bf5b2e..b75bd1ad0 100644 --- a/src/main/java/forestry/greenhouse/items/ItemGreenhouseScreen.java +++ b/src/main/java/forestry/greenhouse/items/ItemGreenhouseScreen.java @@ -26,6 +26,7 @@ import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumHand; import net.minecraft.util.math.BlockPos; import net.minecraft.util.text.TextComponentTranslation; +import net.minecraft.util.text.TextFormatting; import net.minecraft.world.World; import net.minecraftforge.fml.relauncher.Side; @@ -39,6 +40,7 @@ import forestry.core.items.IColoredItem; import forestry.core.items.ItemForestry; import forestry.core.multiblock.MultiblockUtil; import forestry.core.utils.ClimateUtil; +import forestry.core.utils.StringUtil; import forestry.core.utils.Translator; import forestry.greenhouse.PluginGreenhouse; import forestry.greenhouse.api.climate.IClimateContainer; @@ -85,7 +87,7 @@ public class ItemGreenhouseScreen extends ItemForestry implements IColoredItem { public static boolean isValid(ItemStack stack, World world) { BlockPos pos = getGreenhousePos(stack); boolean isValid = true; - if (pos == null || !world.isBlockLoaded(pos)) { + if (pos == null || world == null || !world.isBlockLoaded(pos)) { isValid = false; } else { IGreenhouseControllerInternal controller = MultiblockUtil.getController(world, pos, IGreenhouseComponent.class); @@ -165,7 +167,19 @@ public class ItemGreenhouseScreen extends ItemForestry implements IColoredItem { public void addInformation(ItemStack stack, @Nullable World world, List tooltip, ITooltipFlag flag) { super.addInformation(stack, world, tooltip, flag); boolean previewModeActive = isPreviewModeActive(stack); - tooltip.add(Translator.translateToLocal(Translator.translateToLocal("for.greenhouse_screen.mode.name") + ": " + (previewModeActive ? "Active" : "Inactive"))); + String previewMode = Translator.translateToLocal(previewModeActive ? "for.greenhouse_screen.mode.active" : "for.greenhouse_screen.mode.inactive"); + tooltip.add(Translator.translateToLocal(Translator.translateToLocalFormatted("for.greenhouse_screen.mode", previewMode))); + boolean isValid = isValid(stack, world); + BlockPos pos = getGreenhousePos(stack); + String state = isValid ? Translator.translateToLocalFormatted("for.greenhouse_screen.state.linked", pos.getX(), pos.getY(), pos.getZ()) : Translator.translateToLocal("for.greenhouse_screen.state.fail"); + tooltip.add(Translator.translateToLocalFormatted("for.greenhouse_screen.state", state)); + if (!isValid) { + return; + } + IGreenhouseControllerInternal controller = MultiblockUtil.getController(world, pos, IGreenhouseComponent.class); + IClimateState climateState = controller.getClimateContainer().getState(); + tooltip.add(Translator.translateToLocalFormatted("for.greenhouse_screen.temperature", TextFormatting.GOLD + StringUtil.floatAsPercent(climateState.getTemperature()))); + tooltip.add(Translator.translateToLocalFormatted("for.greenhouse_screen.humidity", TextFormatting.BLUE + StringUtil.floatAsPercent(climateState.getHumidity()))); } @Override diff --git a/src/main/java/forestry/greenhouse/multiblock/GreenhouseController.java b/src/main/java/forestry/greenhouse/multiblock/GreenhouseController.java index a8035cb73..90cc80857 100644 --- a/src/main/java/forestry/greenhouse/multiblock/GreenhouseController.java +++ b/src/main/java/forestry/greenhouse/multiblock/GreenhouseController.java @@ -26,7 +26,6 @@ import net.minecraft.world.biome.Biome; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; -import forestry.api.climate.ClimateStateType; import forestry.api.climate.IClimateState; import forestry.api.core.EnumHumidity; import forestry.api.core.EnumTemperature; @@ -392,7 +391,7 @@ public class GreenhouseController extends RectangularMultiblockControllerBase im limits = createLimits(); if (!climateContainer.getTargetedState().isPresent()) { IClimateState defaultClimate = getDefaultClimate(); - climateContainer.setState(defaultClimate.toState(ClimateStateType.MUTABLE)); + climateContainer.setState(defaultClimate.copy()); climateContainer.setTargetedState(defaultClimate); } @@ -460,7 +459,7 @@ public class GreenhouseController extends RectangularMultiblockControllerBase im biomes++; } } - defaultState = ClimateStates.immutableOf((float) (temperature / biomes), (float) (humidity / biomes)); + defaultState = ClimateStates.of((float) (temperature / biomes), (float) (humidity / biomes)); } /* RectangularMultiblockControllerBase */ diff --git a/src/main/java/forestry/greenhouse/tiles/TileClimatiser.java b/src/main/java/forestry/greenhouse/tiles/TileClimatiser.java index d2e209b86..b3455ee4c 100644 --- a/src/main/java/forestry/greenhouse/tiles/TileClimatiser.java +++ b/src/main/java/forestry/greenhouse/tiles/TileClimatiser.java @@ -30,8 +30,6 @@ import forestry.api.core.ICamouflageHandler; import forestry.api.core.ICamouflagedTile; import forestry.apiculture.network.packets.PacketActiveUpdate; import forestry.core.circuits.ISocketable; -import forestry.core.climate.ClimateSource; -import forestry.core.climate.ClimateSourceType; import forestry.core.inventory.InventoryAdapter; import forestry.core.network.IStreamableGui; import forestry.core.network.PacketBufferForestry; @@ -43,7 +41,9 @@ import forestry.greenhouse.api.climate.IClimateSource; import forestry.greenhouse.api.climate.IClimateSourceOwner; import forestry.greenhouse.blocks.BlockClimatiser; import forestry.greenhouse.camouflage.CamouflageHandlerType; +import forestry.greenhouse.climate.ClimateSource; import forestry.greenhouse.climate.ClimateSourceClimatiser; +import forestry.greenhouse.climate.ClimateSourceType; import forestry.greenhouse.gui.ContainerClimatiser; import forestry.greenhouse.gui.GuiClimatiser; import forestry.greenhouse.multiblock.GreenhouseController; @@ -164,6 +164,8 @@ public class TileClimatiser extends TileForestry implements IActivatable, IStrea chipset.onLoad(this); } } + + source.readFromNBT(data); } @Override @@ -177,6 +179,8 @@ public class TileClimatiser extends TileForestry implements IActivatable, IStrea data.setBoolean("Active", active); sockets.writeToNBT(data); + + source.writeToNBT(data); return data; } diff --git a/src/main/java/forestry/greenhouse/tiles/TileGreenhouseWindow.java b/src/main/java/forestry/greenhouse/tiles/TileGreenhouseWindow.java index a6fb3d018..495e018c2 100644 --- a/src/main/java/forestry/greenhouse/tiles/TileGreenhouseWindow.java +++ b/src/main/java/forestry/greenhouse/tiles/TileGreenhouseWindow.java @@ -116,6 +116,7 @@ public class TileGreenhouseWindow extends TileEntity implements IActivatable, IC if (data.hasKey("Glass")) { glass = data.getString("Glass"); } + source.readFromNBT(data); } @Override @@ -126,19 +127,13 @@ public class TileGreenhouseWindow extends TileEntity implements IActivatable, IC if (glass != null) { data.setString("Glass", glass); } + source.writeToNBT(data); return super.writeToNBT(data); } @Override public NBTTagCompound getUpdateTag() { - NBTTagCompound data = super.getUpdateTag(); - if (mode != null) { - data.setShort("mode", (short) mode.ordinal()); - } - if (glass != null) { - data.setString("Glass", glass); - } - return data; + return writeToNBT(new NBTTagCompound()); } @Override