Improve Climate State

This commit is contained in:
Nedelosk committed 2017-08-31 16:29:46 +02:00
1 parent 66f0da433e
commit c03fa0e955
37 files changed
+612 -572

No files matched your search

@@ -1,124 +0,0 @@
/*******************************************************************************
* Copyright 2011-2014 SirSengir
*
* This work (the API) is licensed under the "MIT" License, see LICENSE.txt for details.
******************************************************************************/
package forestry.api.climate;
import com.google.common.base.MoreObjects;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.math.MathHelper;
import forestry.api.core.INbtReadable;
import static forestry.api.climate.ImmutableClimateState.MAX;
import static forestry.api.climate.ImmutableClimateState.MIN;
/**
* A {@link IClimateState} that has a range from 0.0F to 2.0F.
*/
public class ClimateState implements IClimateState, INbtReadable{
private static final String TEMPERATURE_NBT_KEY = "TEMP";
private static final String HUMIDITY_NBT_KEY = "HUMID";
protected float temperature;
protected float humidity;
public ClimateState(float temperature, float humidity) {
addTemperature(temperature);
addHumidity(humidity);
}
public ClimateState(NBTTagCompound compound) {
readFromNBT(compound);
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
compound.setFloat(TEMPERATURE_NBT_KEY, temperature);
compound.setFloat(HUMIDITY_NBT_KEY, humidity);
return compound;
}
@Override
public void readFromNBT(NBTTagCompound compound) {
this.temperature = compound.getFloat(TEMPERATURE_NBT_KEY);
this.humidity = compound.getFloat(HUMIDITY_NBT_KEY);
}
public void setTemperature(float temperature) {
this.temperature = MathHelper.clamp(temperature, MIN.temperature, MAX.temperature);
}
public void setHumidity(float humidity) {
this.humidity = MathHelper.clamp(humidity, MIN.humidity, MAX.humidity);
}
@Override
public ClimateState addTemperature(float temperature){
setTemperature(this.temperature + temperature);
return this;
}
@Override
public ClimateState addHumidity(float humidity){
setHumidity(this.humidity + humidity);
return this;
}
@Override
public ClimateState add(IClimateState state){
addTemperature(state.getTemperature());
addHumidity(state.getHumidity());
return this;
}
@Override
public ClimateState remove(IClimateState state){
addTemperature(-state.getTemperature());
addHumidity(-state.getHumidity());
return this;
}
@Override
public float getTemperature() {
return temperature;
}
@Override
public float getHumidity() {
return humidity;
}
@Override
public ImmutableClimateState toImmutable(){
return new ImmutableClimateState(temperature, humidity);
}
@Override
public ClimateState toMutable(){
return this;
}
@Override
public boolean equals(Object obj) {
if(!(obj instanceof IClimateState)){
return false;
}
IClimateState otherState = (IClimateState) obj;
return otherState.getTemperature() == temperature && otherState.getHumidity() == humidity;
}
@Override
public int hashCode() {
return Float.hashCode(temperature) + Float.hashCode(humidity);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("temperature", temperature).add("humidity", humidity).toString();
}
}
@@ -0,0 +1,17 @@
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(-2.0F, 2.0F);
public final Function<Float, Float> bounds;
ClimateStateType(Float boundDown, Float boundUp) {
this.bounds = v -> MathHelper.clamp(v, boundDown, boundUp);
}
}
@@ -0,0 +1,11 @@
package forestry.api.climate;
/**
* @deprecated Use IClimateState
*/
@Deprecated
public interface IClimateInfo {
float getTemperature();
float getHumidity();
}
@@ -9,7 +9,19 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
public interface IClimateManager {
/**
* @deprecated Use {@link IClimateStates#create(float, float)}
*/
@Deprecated
IClimateInfo createInfo(float temperature, float humidity);
/**
* @deprecated Use {@link #getClimateState(World, BlockPos)}
*/
@Deprecated
IClimateInfo getInfo(World world, BlockPos pos);
/**
* Gets the current state of a container at this position or setSettings one with the datas from the biome.
* @since 5.3.4
@@ -18,8 +30,9 @@ public interface IClimateManager {
/**
* Creates a climate state with the help of the biome on this position.
* @since 5.3.4
*/
ImmutableClimateState getBiomeState(World world, BlockPos pos);
IClimateState getBiomeState(World world, BlockPos pos);
/**
* @return Create a climate manager.
@@ -13,17 +13,15 @@ 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()}.
*
* Default inmplementations are: {@link ImmutableClimateState}, {@link ClimateState}.
*/
public interface IClimateState extends INbtWritable {
float getTemperature();
float getHumidity();
IClimateState addTemperature(float temperature);
IClimateState addHumidity(float humidity);
IClimateState add(IClimateState state);
@@ -33,9 +31,19 @@ public interface IClimateState extends INbtWritable {
default float get(ClimateType type){
return type == ClimateType.HUMIDITY ? getHumidity() : getTemperature();
}
IClimateState toImmutable();
ImmutableClimateState toImmutable();
ClimateState toMutable();
IClimateState toMutable();
IClimateState toChange();
IClimateState setTemperature(float temperature);
IClimateState setHumidity(float humidity);
boolean isPresent();
ClimateStateType getType();
}
@@ -0,0 +1,28 @@
package forestry.api.climate;
import net.minecraft.nbt.NBTTagCompound;
public interface IClimateStates {
IClimateState create(NBTTagCompound compound);
IClimateState create(NBTTagCompound compound, ClimateStateType type);
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();
IClimateState max();
}
@@ -1,113 +0,0 @@
/*******************************************************************************
* Copyright 2011-2014 SirSengir
*
* This work (the API) is licensed under the "MIT" License, see LICENSE.txt for details.
******************************************************************************/
package forestry.api.climate;
import com.google.common.base.MoreObjects;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.math.MathHelper;
import forestry.api.core.INbtWritable;
/**
* A {@link IClimateState} that has a range from 0.0F to 2.0F and can not be changed.
*/
public class ImmutableClimateState implements IClimateState, INbtWritable{
// The minimum climate state.
public static final ImmutableClimateState MIN = new ImmutableClimateState(0.0F, 0.0F);
// The climate state of a plain biome.
public static final ImmutableClimateState PLAIN = new ImmutableClimateState(0.8F, 0.4F);
// The maximum climate state.
public static final ImmutableClimateState MAX = new ImmutableClimateState(2.0F, 2.0F);
private static final String TEMPERATURE_NBT_KEY = "TEMP";
private static final String HUMIDITY_NBT_KEY = "HUMID";
protected final float temperature;
protected final float humidity;
public ImmutableClimateState(float temperature, float humidity) {
this.temperature = MathHelper.clamp(temperature, 0.0F, 2.0F);
this.humidity = MathHelper.clamp(humidity, 0.0F, 2.0F);
}
public ImmutableClimateState(NBTTagCompound compound) {
float temperature = compound.getFloat(TEMPERATURE_NBT_KEY);
float humidity = compound.getFloat(HUMIDITY_NBT_KEY);
this.temperature = MathHelper.clamp(temperature, 0.0F, 2.0F);
this.humidity = MathHelper.clamp(humidity, 0.0F, 2.0F);
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
compound.setFloat(TEMPERATURE_NBT_KEY, temperature);
compound.setFloat(HUMIDITY_NBT_KEY, humidity);
return compound;
}
@Override
public float getTemperature() {
return temperature;
}
@Override
public float getHumidity() {
return humidity;
}
@Override
public ImmutableClimateState addTemperature(float temperature){
return new ImmutableClimateState(this.temperature + temperature, humidity);
}
@Override
public ImmutableClimateState addHumidity(float humidity){
return new ImmutableClimateState(this.temperature, this.humidity + humidity);
}
@Override
public ImmutableClimateState remove(IClimateState state) {
return new ImmutableClimateState(temperature - state.getTemperature(), humidity - state.getHumidity());
}
@Override
public ImmutableClimateState add(IClimateState state) {
return new ImmutableClimateState(temperature + state.getTemperature(), humidity + state.getHumidity());
}
@Override
public ImmutableClimateState toImmutable(){
return this;
}
@Override
public ClimateState toMutable(){
return new ClimateState(temperature, humidity);
}
@Override
public boolean equals(Object obj) {
if(!(obj instanceof IClimateState)){
return false;
}
IClimateState otherState = (IClimateState) obj;
return otherState.getTemperature() == temperature && otherState.getHumidity() == humidity;
}
@Override
public int hashCode() {
return Float.hashCode(temperature) + Float.hashCode(humidity);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("temperature", temperature).add("humidity", humidity).toString();
}
}
@@ -10,6 +10,7 @@ import java.util.Set;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import forestry.api.climate.IClimateStates;
import forestry.greenhouse.api.climate.IClimateFactory;
import forestry.api.climate.IClimateManager;
import forestry.api.farming.IFarmRegistry;
@@ -57,6 +58,8 @@ public class ForestryAPI {
*/
public static IClimateFactory climateFactory;
public static IClimateStates states;
/**
* A registry for register fertilizers, {@link IFarmabeles}s and {@link ISimpleFarmLogic}s
*/
@@ -8,7 +8,7 @@ package forestry.api.greenhouse;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import forestry.api.climate.ImmutableClimateState;
import forestry.api.climate.IClimateState;
public interface IClimateHousing {
@@ -22,7 +22,7 @@ public interface IClimateHousing {
/**
* @return The default climate state. It is calculated out of all biomedata that this region contains.
*/
ImmutableClimateState getDefaultClimate();
IClimateState getDefaultClimate();
/**
* Must not be named "getWorld" to avoid SpecialSource issue https://github.com/md-5/SpecialSource/issues/12
@@ -0,0 +1,106 @@
package forestry.core.climate;
import com.google.common.base.MoreObjects;
import javax.annotation.concurrent.Immutable;
import net.minecraft.nbt.NBTTagCompound;
import forestry.api.climate.ClimateStateType;
import forestry.api.climate.IClimateState;
@Immutable
public final class AbsentClimateState implements IClimateState {
public static final AbsentClimateState INSTANCE = new AbsentClimateState();
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
compound.setBoolean(ClimateState.ABSENT_NBT_KEY, true);
return compound;
}
@Override
public IClimateState toImmutable() {
return this;
}
@Override
public IClimateState toMutable() {
return this;
}
@Override
public IClimateState toChange() {
return this;
}
@Override
public IClimateState setHumidity(float humidity) {
return this;
}
@Override
public IClimateState setTemperature(float temperature) {
return this;
}
@Override
public IClimateState addTemperature(float temperature){
return this;
}
@Override
public IClimateState addHumidity(float humidity){
return this;
}
@Override
public IClimateState add(IClimateState state){
return this;
}
@Override
public IClimateState remove(IClimateState state){
return this;
}
@Override
public boolean isPresent() {
return false;
}
@Override
public ClimateStateType getType() {
return ClimateStateType.IMMUTABLE;
}
@Override
public float getTemperature() {
return Float.NaN;
}
@Override
public float getHumidity() {
return Float.NaN;
}
@Override
public boolean equals(Object obj) {
if(!(obj instanceof IClimateState)){
return false;
}
IClimateState otherState = (IClimateState) obj;
return otherState.getTemperature() == Float.NaN && otherState.getHumidity() == Float.NaN;
}
@Override
public int hashCode() {
return Float.hashCode(Float.NaN) * 31 + Float.hashCode(Float.NaN);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("temperature", Float.NaN).add("humidity", Float.NaN).toString();
}
}
@@ -10,7 +10,6 @@
******************************************************************************/
package forestry.core.climate;
import javax.annotation.Nullable;
import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
@@ -25,10 +24,12 @@ import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import forestry.api.climate.ClimateState;
import forestry.api.climate.ClimateStateType;
import forestry.api.climate.ClimateType;
import forestry.api.climate.IClimateState;
import forestry.api.climate.ImmutableClimateState;
import forestry.api.climate.IClimateStates;
import forestry.api.core.ForestryAPI;
import forestry.api.greenhouse.IClimateHousing;
import forestry.core.network.IStreamable;
import forestry.core.network.PacketBufferForestry;
import forestry.core.network.packets.PacketUpdateClimate;
@@ -36,7 +37,6 @@ import forestry.core.utils.NetworkUtil;
import forestry.greenhouse.api.climate.IClimateContainer;
import forestry.greenhouse.api.climate.IClimateContainerListener;
import forestry.greenhouse.api.climate.IClimateData;
import forestry.api.greenhouse.IClimateHousing;
import forestry.greenhouse.api.climate.IClimateModifier;
import forestry.greenhouse.api.climate.IClimateSource;
import forestry.greenhouse.climate.GreenhouseClimateManager;
@@ -48,8 +48,8 @@ public class ClimateContainer implements IClimateContainer, IStreamable {
protected final ClimateContainerListeners listeners;
protected final Set<IClimateSource> sources;
private int delay;
protected ClimateState state;
protected ImmutableClimateState targetedState;
protected IClimateState state;
protected IClimateState targetedState;
protected IClimateState boundaryUp;
protected IClimateState boundaryDown;
private NBTTagCompound modifierData;
@@ -64,8 +64,8 @@ public class ClimateContainer implements IClimateContainer, IStreamable {
this.delay = 20;
this.state = parent.getDefaultClimate().toMutable();
this.modifierData = new NBTTagCompound();
this.boundaryUp = ImmutableClimateState.MIN;
this.boundaryDown = ImmutableClimateState.MIN;
this.boundaryUp = ClimateState.MIN;
this.boundaryDown = ClimateState.MIN;
}
public ClimateContainer(IClimateHousing parent, NBTTagCompound nbtTag) {
@@ -84,7 +84,7 @@ public class ClimateContainer implements IClimateContainer, IStreamable {
if(!listeners.isClosed(this)) {
returnClimateToDefault();
}else{
ImmutableClimateState oldState = state.toImmutable();
IClimateState oldState = state.toImmutable();
state = parent.getDefaultClimate().toMutable();
for(IClimateModifier modifier : GreenhouseClimateManager.getInstance().getModifiers()){
state = modifier.modifyTarget(this, state, oldState, modifierData).toMutable();
@@ -98,7 +98,7 @@ public class ClimateContainer implements IClimateContainer, IStreamable {
}
protected void returnClimateToDefault(){
ImmutableClimateState defaultState = parent.getDefaultClimate();
IClimateState defaultState = parent.getDefaultClimate();
float biomeTemperature = defaultState.getTemperature();
float biomeHumidity = defaultState.getHumidity();
float temperature = state.getTemperature();
@@ -147,8 +147,8 @@ public class ClimateContainer implements IClimateContainer, IStreamable {
if(humidityBoundaryDown != 0){
humidityBoundaryDown/=sizeModifier;
}
boundaryUp = parent.getDefaultClimate().add(new ClimateState(temperatureBoundaryUp, humidityBoundaryUp));
boundaryDown = parent.getDefaultClimate().remove(new ClimateState(temperatureBoundaryDown, humidityBoundaryDown));
boundaryUp = parent.getDefaultClimate().add(new ClimateState(temperatureBoundaryUp, humidityBoundaryUp, ClimateStateType.MUTABLE));
boundaryDown = parent.getDefaultClimate().remove(new ClimateState(temperatureBoundaryDown, humidityBoundaryDown, ClimateStateType.MUTABLE));
}
@Override
@@ -173,21 +173,20 @@ public class ClimateContainer implements IClimateContainer, IStreamable {
@Override
public void readFromNBT(NBTTagCompound nbt) {
state.readFromNBT(nbt);
state = ForestryAPI.states.create(nbt);
if(nbt.hasKey("Target")) {
targetedState = new ImmutableClimateState(nbt.getCompoundTag("Target"));
targetedState = new ClimateState(nbt.getCompoundTag("Target"), ClimateStateType.IMMUTABLE);
}
modifierData = nbt.getCompoundTag("modifierData");
}
@Override
public void setTargetedState(ImmutableClimateState state) {
public void setTargetedState(IClimateState state) {
this.targetedState = state;
}
@Override
@Nullable
public ImmutableClimateState getTargetedState() {
public IClimateState getTargetedState() {
return targetedState;
}
@@ -202,7 +201,7 @@ public class ClimateContainer implements IClimateContainer, IStreamable {
return delay;
}
public void setState(ClimateState state) {
public void setState(IClimateState state) {
this.state = state;
}
@@ -238,7 +237,7 @@ public class ClimateContainer implements IClimateContainer, IStreamable {
data.writeFloat(boundaryUp.getHumidity());
data.writeFloat(boundaryDown.getTemperature());
data.writeFloat(boundaryDown.getHumidity());
if(targetedState != null) {
if(targetedState.isPresent()) {
data.writeBoolean(true);
data.writeFloat(targetedState.getTemperature());
data.writeFloat(targetedState.getHumidity());
@@ -250,20 +249,21 @@ public class ClimateContainer implements IClimateContainer, IStreamable {
@Override
public void readData(PacketBufferForestry data) throws IOException {
state.setTemperature(data.readFloat());
state.setHumidity(data.readFloat());
boundaryUp = new ImmutableClimateState(data.readFloat(), data.readFloat());
boundaryDown = new ImmutableClimateState(data.readFloat(), data.readFloat());
state = state.setTemperature(data.readFloat());
state = state.setHumidity(data.readFloat());
IClimateStates states = ForestryAPI.states;
boundaryUp = states.create(data.readFloat(), data.readFloat(), ClimateStateType.IMMUTABLE);
boundaryDown = states.create(data.readFloat(), data.readFloat(), ClimateStateType.IMMUTABLE);
if(data.readBoolean()) {
targetedState = new ImmutableClimateState(data.readFloat(), data.readFloat());
targetedState = states.create(data.readFloat(), data.readFloat(), ClimateStateType.IMMUTABLE);
}else{
targetedState = null;
targetedState = states.absent();
}
modifierData = data.readCompoundTag();
}
@Override
public ClimateState getState() {
public IClimateState getState() {
return state;
}
@@ -300,7 +300,6 @@ public class ClimateContainer implements IClimateContainer, IStreamable {
return parent.getCoordinates().hashCode();
}
@Nullable
@Override
@SideOnly(Side.CLIENT)
public IClimateData getData() {
@@ -17,10 +17,11 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.biome.Biome;
import forestry.api.climate.ClimateStateType;
import forestry.api.climate.IClimateInfo;
import forestry.api.climate.IClimateManager;
import forestry.api.climate.IClimateProvider;
import forestry.api.climate.IClimateState;
import forestry.api.climate.ImmutableClimateState;
import forestry.core.DefaultClimateProvider;
import forestry.core.utils.World2ObjectMap;
@@ -28,7 +29,7 @@ public class ClimateManager implements IClimateManager {
private static final ClimateManager INSTANCE = new ClimateManager();
private static final Map<Biome, ImmutableClimateState> BIOME_STATES = new HashMap<>();
private static final Map<Biome, ClimateState> BIOME_STATES = new HashMap<>();
private final World2ObjectMap<ClimateWorldManager> managers;
@@ -39,16 +40,26 @@ public class ClimateManager implements IClimateManager {
public static ClimateManager getInstance(){
return INSTANCE;
}
@Override
public ImmutableClimateState getBiomeState(World world, BlockPos pos) {
public IClimateInfo createInfo(float temperature, float humidity) {
return new ClimateState(temperature, humidity, ClimateStateType.IMMUTABLE);
}
@Override
public IClimateInfo getInfo(World world, BlockPos pos) {
return getBiomeState(world, pos);
}
@Override
public ClimateState getBiomeState(World world, BlockPos pos) {
Biome biome = world.getBiome(pos);
return getBiomeState(biome);
}
private ImmutableClimateState getBiomeState(Biome biome) {
private ClimateState getBiomeState(Biome biome) {
if (!BIOME_STATES.containsKey(biome)) {
BIOME_STATES.put(biome, new ImmutableClimateState(biome.getTemperature(), biome.getRainfall()));
BIOME_STATES.put(biome, new ClimateState(biome.getTemperature(), biome.getRainfall(), ClimateStateType.IMMUTABLE));
}
return BIOME_STATES.get(biome);
}
@@ -16,12 +16,10 @@ import net.minecraft.world.World;
import forestry.api.climate.ClimateType;
import forestry.api.climate.IClimateState;
import forestry.api.climate.ImmutableClimateState;
import forestry.greenhouse.api.greenhouse.GreenhouseManager;
import forestry.greenhouse.api.climate.ClimateChange;
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<O extends IClimateSourceOwner> implements IClimateSource {
@@ -133,17 +131,17 @@ public abstract class ClimateSource<O extends IClimateSourceOwner> implements IC
}
@Override
public ClimateChange work(IClimateState state, ImmutableClimateState target) {
public IClimateState work(IClimateState state, IClimateState target) {
beforeWork();
ClimateSourceType validType = getValidType(state, target);
if(validType == null){
isNotValid();
isActive = false;
return ClimateChange.ORIGIN;
return AbsentClimateState.INSTANCE;
}
if(!canWork(state, target)){
isActive = false;
return ClimateChange.ORIGIN;
return AbsentClimateState.INSTANCE;
}
removeResources(state, target);
isActive = true;
@@ -163,14 +161,14 @@ public abstract class ClimateSource<O extends IClimateSourceOwner> implements IC
*
* @return true if the source can work, false if it can not.
*/
protected abstract boolean canWork(IClimateState state, ImmutableClimateState target);
protected abstract boolean canWork(IClimateState state, IClimateState target);
protected abstract void removeResources(IClimateState state, ImmutableClimateState target);
protected abstract void removeResources(IClimateState state, IClimateState target);
protected abstract ClimateChange getChange(ClimateSourceType type, IClimateState state, ImmutableClimateState target);
protected abstract IClimateState getChange(ClimateSourceType type, IClimateState state, IClimateState target);
@Nullable
private ClimateSourceType getValidType(IClimateState state, ImmutableClimateState target){
private ClimateSourceType getValidType(IClimateState state, IClimateState target){
boolean canChangeHumidity = false;
boolean canChangeTemperature = false;
if(sourceType.canChangeHumidity()){
@@ -0,0 +1,187 @@
/*******************************************************************************
* Copyright 2011-2014 SirSengir
*
* This work (the API) is licensed under the "MIT" License, see LICENSE.txt for details.
******************************************************************************/
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;
import forestry.api.climate.IClimateInfo;
import forestry.api.climate.IClimateState;
public class ClimateState implements IClimateState, IClimateInfo {
// The minimum climate state.
public static final ClimateState MIN = new ClimateState(0.0F, 0.0F, ClimateStateType.IMMUTABLE);
// The maximum climate state.
public static final ClimateState MAX = new ClimateState(2.0F, 2.0F, ClimateStateType.IMMUTABLE);
public static final String TEMPERATURE_NBT_KEY = "TEMP";
public static final String HUMIDITY_NBT_KEY = "HUMID";
public static final String TYPE_NBT_KEY = "TYPE";
public static final String ABSENT_NBT_KEY = "ABSENT";
protected final ClimateStateType type;
protected final Function<Float, Float> bounds;
protected float temperature;
protected float humidity;
public ClimateState(IClimateState climateState, ClimateStateType type) {
this(climateState.getTemperature(), climateState.getHumidity(), type);
}
public ClimateState(float temperature, float humidity, ClimateStateType type) {
addTemperature(temperature);
addHumidity(humidity);
this.type = type;
this.bounds = type.bounds;
}
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);
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
compound.setFloat(TEMPERATURE_NBT_KEY, temperature);
compound.setFloat(HUMIDITY_NBT_KEY, humidity);
compound.setByte(TYPE_NBT_KEY, (byte)type.ordinal());
compound.setBoolean(ABSENT_NBT_KEY, !isPresent());
return compound;
}
public void readFromNBT(NBTTagCompound compound) {
this.temperature = compound.getFloat(TEMPERATURE_NBT_KEY);
this.humidity = compound.getFloat(HUMIDITY_NBT_KEY);
}
@Override
public IClimateState setHumidity(float humidity) {
if(type == ClimateStateType.IMMUTABLE){
return new ClimateState(getTemperature(), humidity, ClimateStateType.IMMUTABLE);
}
this.humidity = bounds.apply(humidity);
if(!isPresent()){
return AbsentClimateState.INSTANCE;
}
return this;
}
@Override
public IClimateState setTemperature(float temperature) {
if(type == ClimateStateType.IMMUTABLE){
return new ClimateState(temperature, getHumidity(), ClimateStateType.IMMUTABLE);
}
this.temperature = bounds.apply(temperature);
if(!isPresent()){
return AbsentClimateState.INSTANCE;
}
return this;
}
@Override
public IClimateState toImmutable() {
return new ClimateState(this, ClimateStateType.IMMUTABLE);
}
@Override
public IClimateState toMutable() {
return new ClimateState(this, ClimateStateType.MUTABLE);
}
@Override
public IClimateState toChange() {
return new ClimateState(this, ClimateStateType.CHANGE);
}
@Override
public IClimateState addTemperature(float temperature){
this.temperature=+ temperature;
if(!isPresent()){
return AbsentClimateState.INSTANCE;
}
return this;
}
@Override
public IClimateState addHumidity(float humidity){
this.humidity=+ humidity;
if(!isPresent()){
return AbsentClimateState.INSTANCE;
}
return this;
}
@Override
public IClimateState add(IClimateState state){
addTemperature(state.getTemperature());
addHumidity(state.getHumidity());
if(!isPresent()){
return AbsentClimateState.INSTANCE;
}
return this;
}
@Override
public IClimateState remove(IClimateState state){
addTemperature(-state.getTemperature());
addHumidity(-state.getHumidity());
if(!isPresent()){
return AbsentClimateState.INSTANCE;
}
return this;
}
@Override
public boolean isPresent() {
return !Float.isNaN(temperature) && !Float.isNaN(humidity);
}
@Override
public ClimateStateType getType() {
return type;
}
@Override
public float getTemperature() {
return temperature;
}
@Override
public float getHumidity() {
return humidity;
}
@Override
public boolean equals(Object obj) {
if(!(obj instanceof IClimateState)){
return false;
}
IClimateState otherState = (IClimateState) obj;
return otherState.getTemperature() == temperature && otherState.getHumidity() == humidity;
}
@Override
public int hashCode() {
return Float.hashCode(temperature) * 31 + Float.hashCode(humidity);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("temperature", temperature).add("humidity", humidity).toString();
}
}
@@ -0,0 +1,51 @@
package forestry.core.climate;
import net.minecraft.nbt.NBTTagCompound;
import forestry.api.climate.ClimateStateType;
import forestry.api.climate.IClimateState;
import forestry.api.climate.IClimateStates;
public class ClimateStates implements IClimateStates {
@Override
public IClimateState create(IClimateState climateState, ClimateStateType type) {
return new ClimateState(climateState, type);
}
@Override
public IClimateState create(float temperature, float humidity, ClimateStateType type) {
return new ClimateState(temperature, humidity, type);
}
@Override
public IClimateState create(NBTTagCompound compound, ClimateStateType type) {
if(compound.getBoolean(ClimateState.ABSENT_NBT_KEY)){
return AbsentClimateState.INSTANCE;
}
return new ClimateState(compound, type);
}
@Override
public IClimateState create(NBTTagCompound compound) {
if(compound.getBoolean(ClimateState.ABSENT_NBT_KEY)){
return AbsentClimateState.INSTANCE;
}
return new ClimateState(compound);
}
@Override
public IClimateState absent() {
return AbsentClimateState.INSTANCE;
}
@Override
public IClimateState min() {
return ClimateState.MIN;
}
@Override
public IClimateState max() {
return ClimateState.MAX;
}
}
@@ -15,6 +15,10 @@ import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import forestry.api.climate.ClimateStateType;
import forestry.api.climate.IClimateState;
import forestry.core.climate.AbsentClimateState;
import forestry.core.climate.ClimateState;
import forestry.greenhouse.api.greenhouse.Position2D;
import io.netty.buffer.ByteBuf;
@@ -176,4 +180,24 @@ public class PacketBufferForestry extends PacketBuffer {
public interface IStreamableFactory<T extends IStreamable> {
T create(PacketBufferForestry data) throws IOException;
}
public void writeClimateState(IClimateState climateState){
if(climateState.isPresent()) {
writeBoolean(true);
writeByte(climateState.getType().ordinal());
writeFloat(climateState.getTemperature());
writeFloat(climateState.getHumidity());
}else{
writeBoolean(false);
}
}
public IClimateState readClimateState(){
if(readBoolean()){
ClimateStateType type = ClimateStateType.values()[readByte()];
return new ClimateState(readFloat(), readFloat(), type);
}else{
return AbsentClimateState.INSTANCE;
}
}
}
@@ -17,7 +17,6 @@ import java.util.Set;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import forestry.api.climate.ClimateState;
import forestry.api.climate.IClimateState;
import forestry.api.core.EnumHumidity;
import forestry.api.core.EnumTemperature;
@@ -230,42 +229,6 @@ public class ClimateUtil implements IClimateHelper {
}
}
public static boolean addHumidity(ClimateState state, IClimateState targetedState, float value){
float humidity = state.getHumidity();
float targetedHumidity = targetedState.getHumidity();
if(Float.isNaN(humidity)){
state.setHumidity(humidity = 0);
}
if (humidity != targetedHumidity) {
if (humidity > targetedHumidity) {
value=Math.min(value, humidity - targetedHumidity);
} else {
value=Math.min(value, targetedHumidity - humidity);
}
state.addHumidity(value);
return true;
}
return false;
}
public static boolean addTemperature(ClimateState state, IClimateState targetedState, float value){
float temperature = state.getTemperature();
float targetedTemperature = targetedState.getTemperature();
if(Float.isNaN(temperature)){
state.setTemperature(temperature = 0);
}
if (temperature != targetedTemperature) {
if (temperature > targetedTemperature) {
value=Math.min(value, temperature - targetedTemperature);
} else {
value=Math.min(value, targetedTemperature - temperature);
}
state.addTemperature(value);
return true;
}
return false;
}
public static float getTemperature(World world, BlockPos pos){
IClimateState state = ForestryAPI.climateManager.getClimateState(world, pos);
return state.getTemperature();
@@ -1,27 +0,0 @@
/*******************************************************************************
* Copyright 2011-2014 SirSengir
*
* This work (the API) is licensed under the "MIT" License, see LICENSE.txt for details.
******************************************************************************/
package forestry.greenhouse.api.climate;
import net.minecraft.nbt.NBTTagCompound;
import forestry.api.climate.IClimateState;
import forestry.api.climate.ImmutableClimateState;
public class BlankClimateModifier implements IClimateModifier {
@Override
public IClimateState modifyTarget(IClimateContainer container, IClimateState newState, ImmutableClimateState oldState, NBTTagCompound data) {
return newState;
}
@Override
public void addData(IClimateContainer container, IClimateState climateState, NBTTagCompound nbtData, IClimateData data) {
}
@Override
public int getPriority() {
return 0;
}
}
@@ -1,117 +0,0 @@
/*******************************************************************************
* Copyright 2011-2014 SirSengir
*
* This work (the API) is licensed under the "MIT" License, see LICENSE.txt for details.
******************************************************************************/
package forestry.greenhouse.api.climate;
import com.google.common.base.MoreObjects;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.math.MathHelper;
import forestry.api.climate.ClimateState;
import forestry.api.climate.IClimateState;
import forestry.api.climate.ImmutableClimateState;
import forestry.api.core.INbtReadable;
/**
* A {@link IClimateState} that has a range from 0.0F to 2.0F and can be positiv an negativ.
*
*
* It is mainly used by {@link IClimateSource} to change the {@link IClimateState} of the {@link IClimateContainer}.
*/
public class ClimateChange implements IClimateState, INbtReadable{
private static final String TEMPERATURE_NBT_KEY = "TEMP";
private static final String HUMIDITY_NBT_KEY = "HUMID";
public static final ClimateChange ORIGIN = new ClimateChange(0.0F, 0.0F);
public float temperature;
public float humidity;
public ClimateChange(float temperature, float humidity) {
addTemperature(temperature);
addHumidity(humidity);
}
public ClimateChange(NBTTagCompound compound) {
readFromNBT(compound);
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
compound.setFloat(TEMPERATURE_NBT_KEY, temperature);
compound.setFloat(HUMIDITY_NBT_KEY, humidity);
return compound;
}
@Override
public void readFromNBT(NBTTagCompound compound) {
this.temperature = compound.getFloat(TEMPERATURE_NBT_KEY);
this.humidity = compound.getFloat(HUMIDITY_NBT_KEY);
}
@Override
public float getTemperature() {
return temperature;
}
@Override
public float getHumidity() {
return humidity;
}
public void setTemperature(float temperature) {
this.temperature = 0;
addTemperature(temperature);
}
public void setHumidity(float humidity) {
this.humidity = 0;
addHumidity(humidity);
}
@Override
public ClimateChange add(IClimateState state){
addTemperature(state.getTemperature());
addHumidity(state.getHumidity());
return this;
}
@Override
public ClimateChange remove(IClimateState state){
addTemperature(-state.getTemperature());
addHumidity(-state.getHumidity());
return this;
}
@Override
public ClimateChange addTemperature(float temperature){
this.temperature = MathHelper.clamp(this.temperature + temperature, -2.0F, 2.0F);
return this;
}
@Override
public ClimateChange addHumidity(float humidity){
this.humidity = MathHelper.clamp(this.humidity + humidity, -2.0F, 2.0F);
return this;
}
@Override
public ImmutableClimateState toImmutable() {
return new ImmutableClimateState(temperature, humidity);
}
@Override
public ClimateState toMutable() {
return new ClimateState(temperature, humidity);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("temperature", temperature).add("humidity", humidity).toString();
}
}
@@ -5,7 +5,6 @@
******************************************************************************/
package forestry.greenhouse.api.climate;
import javax.annotation.Nullable;
import java.util.Collection;
import net.minecraft.world.World;
@@ -14,7 +13,6 @@ import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import forestry.api.climate.IClimateState;
import forestry.api.climate.ImmutableClimateState;
import forestry.api.core.INbtReadable;
import forestry.api.core.INbtWritable;
import forestry.api.greenhouse.IClimateHousing;
@@ -54,14 +52,12 @@ public interface IClimateContainer extends INbtReadable, INbtWritable, IClimateS
Collection<IClimateContainerListener> getListeners();
/**
* @return only null if the controller never was assembled.
* @return only not present if the controller never was assembled.
*/
@Nullable
ImmutableClimateState getTargetedState();
IClimateState getTargetedState();
void setTargetedState(ImmutableClimateState state);
void setTargetedState(IClimateState state);
@Nullable
@SideOnly(Side.CLIENT)
IClimateData getData();
@@ -9,8 +9,6 @@ public interface IClimateContainerListener {
/**
* Test if the container is closed.
*
* @return true to let the container slowly set his climate state back to the default climate state.
*/
boolean isClosed(IClimateContainer container);
@@ -11,7 +11,6 @@ import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import forestry.api.climate.IClimateState;
import forestry.api.climate.ImmutableClimateState;
/**
* This modifier is used by {@link IClimateContainer}s.
@@ -27,14 +26,19 @@ public interface IClimateModifier {
* @param data A {@link NBTTagCompound} that can be used to save custom data or to send it to the client to add it to the {@link IClimateData}.
* @return a modify newState or the newState.
*/
IClimateState modifyTarget(IClimateContainer container, IClimateState newState, ImmutableClimateState oldState, NBTTagCompound data);
default IClimateState modifyTarget(IClimateContainer container, IClimateState newState, IClimateState oldState, NBTTagCompound data){
return newState;
}
@SideOnly(Side.CLIENT)
void addData(IClimateContainer container, IClimateState climateState, NBTTagCompound nbtData, IClimateData data);
default void addData(IClimateContainer container, IClimateState climateState, NBTTagCompound nbtData, IClimateData data){
}
/**
* @return The priority of this modifier. The modifier with the highest priority is called first and the modifier with the lowest at last.
*/
int getPriority();
default int getPriority() {
return 0;
}
}
@@ -9,7 +9,6 @@ import javax.annotation.Nullable;
import forestry.api.climate.ClimateType;
import forestry.api.climate.IClimateState;
import forestry.api.climate.ImmutableClimateState;
/**
* 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}.
@@ -40,7 +39,7 @@ public interface IClimateSource {
* @param target the by the {@link IClimateContainer} targeted {@link IClimateState}.
*
*/
ClimateChange work(IClimateState state, ImmutableClimateState target);
IClimateState work(IClimateState state, IClimateState target);
/**
* @return true if source has changed the climate at the last work cicle.
@@ -8,7 +8,7 @@ package forestry.greenhouse.api.climate;
import forestry.api.circuits.ICircuit;
import forestry.api.climate.ClimateType;
public interface IClimateSourceCircuitable extends IClimateSource {
public interface IClimateSourceCircuitable extends IClimateSource {
/**
* Called by the {@link IClimateSourceOwner} if its {@link ICircuit} changes.
*/
@@ -10,17 +10,17 @@
******************************************************************************/
package forestry.greenhouse.climate;
import forestry.greenhouse.api.climate.ClimateChange;
import forestry.api.climate.ClimateStateType;
import forestry.api.climate.ClimateType;
import forestry.greenhouse.api.climate.IClimateContainer;
import forestry.api.greenhouse.IClimateHousing;
import forestry.api.climate.IClimateState;
import forestry.api.climate.ImmutableClimateState;
import forestry.api.core.IErrorLogic;
import forestry.api.greenhouse.IClimateHousing;
import forestry.core.climate.ClimateSourceMode;
import forestry.core.climate.ClimateSourceType;
import forestry.core.climate.ClimateState;
import forestry.core.errors.EnumErrorCode;
import forestry.energy.EnergyManager;
import forestry.greenhouse.api.climate.IClimateContainer;
import forestry.greenhouse.multiblock.IGreenhouseControllerInternal;
import forestry.greenhouse.tiles.TileClimatiser;
@@ -49,7 +49,7 @@ public class ClimateSourceClimatiser<O extends TileClimatiser> extends ClimateSo
}
@Override
public boolean canWork(IClimateState state, ImmutableClimateState target) {
public boolean canWork(IClimateState state, IClimateState target) {
IClimateHousing region = container.getParent();
if (region instanceof IGreenhouseControllerInternal) {
IGreenhouseControllerInternal controller = (IGreenhouseControllerInternal) region;
@@ -73,7 +73,7 @@ public class ClimateSourceClimatiser<O extends TileClimatiser> extends ClimateSo
}
@Override
protected void removeResources(IClimateState state, ImmutableClimateState target) {
protected void removeResources(IClimateState state, IClimateState target) {
IClimateHousing region = container.getParent();
if (region instanceof IGreenhouseControllerInternal) {
IGreenhouseControllerInternal controller = (IGreenhouseControllerInternal) region;
@@ -85,7 +85,7 @@ public class ClimateSourceClimatiser<O extends TileClimatiser> extends ClimateSo
}
@Override
protected ClimateChange getChange(ClimateSourceType type, IClimateState state, ImmutableClimateState target) {
protected IClimateState getChange(ClimateSourceType type, IClimateState state, IClimateState target) {
float temperature = 0.0F;
float humidity = 0.0F;
if (type.canChangeHumidity()) {
@@ -94,7 +94,7 @@ public class ClimateSourceClimatiser<O extends TileClimatiser> extends ClimateSo
if (type.canChangeTemperature()) {
temperature += getChange(ClimateType.TEMPERATURE);
}
return new ClimateChange(temperature, humidity);
return new ClimateState(temperature, humidity, ClimateStateType.CHANGE);
}
}
@@ -12,13 +12,13 @@ package forestry.greenhouse.climate;
import net.minecraftforge.fluids.FluidStack;
import forestry.greenhouse.api.climate.ClimateChange;
import forestry.api.climate.ClimateStateType;
import forestry.api.climate.ClimateType;
import forestry.api.climate.IClimateState;
import forestry.api.climate.ImmutableClimateState;
import forestry.api.core.IErrorLogic;
import forestry.core.climate.ClimateSourceMode;
import forestry.core.climate.ClimateSourceType;
import forestry.core.climate.ClimateState;
import forestry.core.errors.EnumErrorCode;
import forestry.core.fluids.FilteredTank;
import forestry.core.recipes.HygroregulatorRecipe;
@@ -60,7 +60,7 @@ public class ClimateSourceHygroregulator extends ClimateSourceCircuitable<TileHy
}
@Override
public boolean canWork(IClimateState state, ImmutableClimateState target) {
public boolean canWork(IClimateState state, IClimateState target) {
createRecipe();
FilteredTank liquidTank = owner.getLiquidTank();
IErrorLogic errorLogic = owner.getErrorLogic();
@@ -73,13 +73,13 @@ public class ClimateSourceHygroregulator extends ClimateSourceCircuitable<TileHy
}
@Override
protected void removeResources(IClimateState state, ImmutableClimateState target) {
protected void removeResources(IClimateState state, IClimateState target) {
FilteredTank liquidTank = owner.getLiquidTank();
liquidTank.drainInternal(currentRecipe.liquid.amount, true);
}
@Override
protected ClimateChange getChange(ClimateSourceType type, IClimateState state, ImmutableClimateState target) {
protected IClimateState getChange(ClimateSourceType type, IClimateState state, IClimateState target) {
float temperature = 0.0F;
float humidity = 0.0F;
if (type.canChangeHumidity()) {
@@ -88,7 +88,7 @@ public class ClimateSourceHygroregulator extends ClimateSourceCircuitable<TileHy
if (type.canChangeTemperature()) {
temperature += currentRecipe.tempChange * getChangeMultiplier(ClimateType.TEMPERATURE);
}
return new ClimateChange(temperature, humidity);
return new ClimateState(temperature, humidity, ClimateStateType.CHANGE);
}
private void createRecipe() {
@@ -10,13 +10,13 @@
******************************************************************************/
package forestry.greenhouse.climate;
import forestry.greenhouse.api.climate.ClimateChange;
import forestry.api.climate.ClimateStateType;
import forestry.api.climate.IClimateState;
import forestry.api.climate.ImmutableClimateState;
import forestry.api.core.ForestryAPI;
import forestry.core.climate.ClimateSource;
import forestry.core.climate.ClimateSourceMode;
import forestry.core.climate.ClimateSourceType;
import forestry.core.climate.ClimateState;
import forestry.greenhouse.tiles.TileGreenhouseWindow;
import forestry.greenhouse.tiles.TileGreenhouseWindow.WindowMode;
@@ -47,16 +47,16 @@ public class ClimateSourceWindow extends ClimateSource<TileGreenhouseWindow> {
}
@Override
public boolean canWork(IClimateState state, ImmutableClimateState target) {
public boolean canWork(IClimateState state, IClimateState target) {
return owner.getMode() == WindowMode.OPEN;
}
@Override
protected void removeResources(IClimateState state, ImmutableClimateState target) {
protected void removeResources(IClimateState state, IClimateState target) {
}
@Override
protected ClimateChange getChange(ClimateSourceType type, IClimateState state, ImmutableClimateState target) {
protected IClimateState getChange(ClimateSourceType type, IClimateState state, IClimateState target) {
float temperature = 0.0F;
float humidity = 0.0F;
if (type.canChangeHumidity()) {
@@ -73,7 +73,7 @@ public class ClimateSourceWindow extends ClimateSource<TileGreenhouseWindow> {
temperature += change;
}
}
return new ClimateChange(temperature, humidity);
return new ClimateState(temperature, humidity, ClimateStateType.CHANGE);
}
}
@@ -17,21 +17,20 @@ import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import forestry.greenhouse.api.climate.BlankClimateModifier;
import forestry.api.climate.ClimateType;
import forestry.api.climate.IClimateState;
import forestry.api.greenhouse.IClimateHousing;
import forestry.core.utils.Translator;
import forestry.greenhouse.api.climate.IClimateContainer;
import forestry.greenhouse.api.climate.IClimateData;
import forestry.api.greenhouse.IClimateHousing;
import forestry.api.climate.IClimateState;
import forestry.api.climate.ImmutableClimateState;
import forestry.core.utils.Translator;
import forestry.greenhouse.api.climate.IClimateModifier;
public class AltitudeModifier extends BlankClimateModifier {
public class AltitudeModifier implements IClimateModifier {
private static final float TEMPERATURE_CHANGE = 0.01F;
@Override
public IClimateState modifyTarget(IClimateContainer container, IClimateState newState, ImmutableClimateState oldState, NBTTagCompound data) {
public IClimateState modifyTarget(IClimateContainer container, IClimateState newState, IClimateState oldState, NBTTagCompound data) {
World world = container.getWorld();
IClimateHousing parent = container.getParent();
if (world.provider.isSurfaceWorld()) {
@@ -17,23 +17,22 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import forestry.greenhouse.api.climate.BlankClimateModifier;
import forestry.greenhouse.api.climate.ClimateChange;
import forestry.api.climate.ClimateState;
import forestry.api.climate.ClimateStateType;
import forestry.api.climate.ClimateType;
import forestry.greenhouse.api.climate.IClimateContainer;
import forestry.greenhouse.api.climate.IClimateData;
import forestry.api.greenhouse.IClimateHousing;
import forestry.greenhouse.api.climate.IClimateSource;
import forestry.api.climate.IClimateState;
import forestry.api.climate.ImmutableClimateState;
import forestry.api.greenhouse.IClimateHousing;
import forestry.core.climate.ClimateState;
import forestry.core.config.Config;
import forestry.core.utils.Translator;
import forestry.greenhouse.api.climate.IClimateContainer;
import forestry.greenhouse.api.climate.IClimateData;
import forestry.greenhouse.api.climate.IClimateModifier;
import forestry.greenhouse.api.climate.IClimateSource;
public class ClimateSourceModifier extends BlankClimateModifier {
public class ClimateSourceModifier implements IClimateModifier {
@Override
public IClimateState modifyTarget(IClimateContainer container, IClimateState newState, ImmutableClimateState oldState, NBTTagCompound data) {
public IClimateState modifyTarget(IClimateContainer container, IClimateState newState, IClimateState oldState, NBTTagCompound data) {
Collection<IClimateSource> sources = container.getClimateSources();
if (sources.isEmpty()) {
data.removeTag("rangeUp");
@@ -54,17 +53,17 @@ 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){
IClimateState targetedState = container.getTargetedState();
if(!targetedState.isPresent()){
return newState;
}
int workedSources = 0;
ImmutableClimateState target = getTargetOrBound(oldState, container.getBoundaryDown(), container.getBoundaryUp(), targetedState);
ClimateChange changeState = new ClimateChange(data.getCompoundTag("change"));
IClimateState target = getTargetOrBound(oldState, container.getBoundaryDown(), container.getBoundaryUp(), targetedState);
IClimateState changeState = new ClimateState(data.getCompoundTag("change"), ClimateStateType.CHANGE);
for (IClimateSource source : container.getClimateSources()) {
ClimateChange state = source.work(oldState, target);
if (state != null) {
IClimateState state = source.work(oldState, target);
if (state.isPresent()) {
boolean hasChange = false;
double temperatureChange = state.getTemperature();
double humidityChange = state.getHumidity();
@@ -93,9 +92,9 @@ public class ClimateSourceModifier extends BlankClimateModifier {
@Override
@SideOnly(Side.CLIENT)
public void addData(IClimateContainer container, IClimateState climateState, NBTTagCompound nbtData, IClimateData data) {
IClimateState rangeDown = new ClimateState(nbtData.getCompoundTag("rangeDown"));
IClimateState rangeUp = new ClimateState(nbtData.getCompoundTag("rangeUp"));
IClimateState change = new ClimateState(nbtData.getCompoundTag("change"));
IClimateState rangeDown = new ClimateState(nbtData.getCompoundTag("rangeDown"), ClimateStateType.MUTABLE);
IClimateState rangeUp = new ClimateState(nbtData.getCompoundTag("rangeUp"), ClimateStateType.MUTABLE);
IClimateState change = new ClimateState(nbtData.getCompoundTag("change"), ClimateStateType.MUTABLE);
data.addData(ClimateType.HUMIDITY, Translator.translateToLocal("for.gui.modifier.sources.range.up"), rangeUp.getHumidity())
.addData(ClimateType.HUMIDITY, Translator.translateToLocal("for.gui.modifier.sources.range.down"), rangeDown.getHumidity())
@@ -111,7 +110,7 @@ public class ClimateSourceModifier extends BlankClimateModifier {
return -1;
}
private ImmutableClimateState getTargetOrBound(IClimateState climateState, IClimateState boundaryDown, IClimateState boundaryUp, IClimateState targetedState) {
private IClimateState getTargetOrBound(IClimateState climateState, IClimateState boundaryDown, IClimateState boundaryUp, IClimateState targetedState) {
float temperature = climateState.getTemperature();
float humidity = climateState.getHumidity();
float targetTemperature = targetedState.getTemperature();
@@ -130,6 +129,6 @@ public class ClimateSourceModifier extends BlankClimateModifier {
} else if (targetHumidity < humidity) {
humidity = Math.max(targetHumidity, humidityBoundaryDown);
}
return new ImmutableClimateState(temperature, humidity);
return new ClimateState(temperature, humidity, ClimateStateType.IMMUTABLE);
}
}
@@ -18,15 +18,14 @@ import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import forestry.greenhouse.api.climate.BlankClimateModifier;
import forestry.api.climate.ClimateType;
import forestry.api.climate.IClimateState;
import forestry.core.utils.Translator;
import forestry.greenhouse.api.climate.IClimateContainer;
import forestry.greenhouse.api.climate.IClimateData;
import forestry.api.climate.IClimateState;
import forestry.api.climate.ImmutableClimateState;
import forestry.core.utils.Translator;
import forestry.greenhouse.api.climate.IClimateModifier;
public class TimeModifier extends BlankClimateModifier {
public class TimeModifier implements IClimateModifier {
private static final float TEMPERATURE_CHANGE = 0.01F;
@@ -47,7 +46,7 @@ public class TimeModifier extends BlankClimateModifier {
}
@Override
public IClimateState modifyTarget(IClimateContainer container, IClimateState newState, ImmutableClimateState oldState, NBTTagCompound data) {
public IClimateState modifyTarget(IClimateContainer container, IClimateState newState, IClimateState oldState, NBTTagCompound data) {
World world = container.getWorld();
float lightRatio = calculateLightRatio(world);
float temperatureChange = lightRatio * TEMPERATURE_CHANGE;
@@ -16,15 +16,14 @@ import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import forestry.greenhouse.api.climate.BlankClimateModifier;
import forestry.api.climate.ClimateType;
import forestry.api.climate.IClimateState;
import forestry.core.utils.Translator;
import forestry.greenhouse.api.climate.IClimateContainer;
import forestry.greenhouse.api.climate.IClimateData;
import forestry.api.climate.IClimateState;
import forestry.api.climate.ImmutableClimateState;
import forestry.core.utils.Translator;
import forestry.greenhouse.api.climate.IClimateModifier;
public class WeatherModifier extends BlankClimateModifier {
public class WeatherModifier implements IClimateModifier {
private static final int RAIN_TIME = 600;
private static final float RAIN_HUMIDITY_CHANGE = 0.15F;
@@ -32,7 +31,7 @@ public class WeatherModifier extends BlankClimateModifier {
private static final int SNOW_MODIFIER = 2;
@Override
public IClimateState modifyTarget(IClimateContainer container, IClimateState newState, ImmutableClimateState oldState, NBTTagCompound data) {
public IClimateState modifyTarget(IClimateContainer container, IClimateState newState, IClimateState oldState, NBTTagCompound data) {
World world = container.getWorld();
IClimateState defaultClimate = container.getParent().getDefaultClimate();
int rainTime = data.getInteger("raintime");
@@ -15,8 +15,10 @@ import java.io.IOException;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.math.BlockPos;
import forestry.api.climate.ClimateStateType;
import forestry.api.climate.ClimateType;
import forestry.api.climate.ImmutableClimateState;
import forestry.api.climate.IClimateState;
import forestry.core.climate.ClimateState;
import forestry.core.config.Constants;
import forestry.core.gui.GuiForestryTitled;
import forestry.core.gui.TextLayoutHelper;
@@ -53,7 +55,7 @@ public class GuiGreenhouse extends GuiForestryTitled<ContainerGreenhouse> {
//Add the camouflage tab
widgetManager.add(new WidgetCamouflageTab(widgetManager, xSize / 4 - WidgetCamouflageTab.WIDTH / 2, -WidgetCamouflageTab.HEIGHT, controller, tile));
if(container.getTargetedState() != null) {
if(container.getTargetedState().isPresent()) {
widgetManager.add(new WidgetClimateBar(widgetManager, xSize / 3 + WidgetClimateBar.WIDTH / 3, -WidgetClimateBar.HEIGHT));
}
widgetManager.add(temperaturePanel = new WidgetClimatePanel(widgetManager, this, 9, 18, ClimateType.TEMPERATURE, data));
@@ -61,19 +63,20 @@ public class GuiGreenhouse extends GuiForestryTitled<ContainerGreenhouse> {
}
public void sendNetworkUpdate() {
if(container.getTargetedState() != null) {
IClimateState climateState = container.getTargetedState();
if(climateState.isPresent()) {
BlockPos pos = controller.getCoordinates();
float temp = temperaturePanel.parseValue();
float hum = humidityPanel.parseValue();
setClimate(container, temp, hum);
NetworkUtil.sendToServer(new PacketSelectClimateTargeted(pos, container.getTargetedState()));
NetworkUtil.sendToServer(new PacketSelectClimateTargeted(pos, climateState));
}
}
public void setClimate(IClimateContainer container, float temp, float hum) {
temperaturePanel.setValue(temp);
humidityPanel.setValue(hum);
container.setTargetedState(new ImmutableClimateState(temp, hum));
container.setTargetedState(new ClimateState(temp, hum, ClimateStateType.IMMUTABLE));
}
@Override
@@ -23,13 +23,14 @@ import net.minecraft.client.renderer.GlStateManager;
import org.lwjgl.input.Keyboard;
import forestry.api.climate.ClimateType;
import forestry.greenhouse.api.climate.IClimateData;
import forestry.api.climate.IClimateState;
import forestry.core.gui.tables.Table;
import forestry.core.gui.widgets.Widget;
import forestry.core.gui.widgets.WidgetManager;
import forestry.core.render.ColourProperties;
import forestry.core.utils.StringUtil;
import forestry.core.utils.Translator;
import forestry.greenhouse.api.climate.IClimateData;
import forestry.greenhouse.gui.GuiGreenhouse;
public class WidgetClimatePanel extends Widget {
@@ -67,8 +68,9 @@ 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);
if(gui.container.getTargetedState() != null) {
textField.setText(Float.toString(gui.container.getTargetedState().get(type)));
IClimateState climateState = gui.container.getTargetedState();
if(climateState.isPresent()) {
textField.setText(Float.toString(climateState.get(type)));
}else{
textField.setVisible(false);
textField.setEnabled(false);
@@ -17,13 +17,14 @@ import java.util.Set;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import forestry.greenhouse.api.climate.IClimateContainer;
import forestry.api.climate.ImmutableClimateState;
import forestry.greenhouse.api.greenhouse.IGreenhouseLimits;
import forestry.greenhouse.api.greenhouse.IGreenhouseProvider;
import forestry.api.climate.IClimateState;
import forestry.api.multiblock.IGreenhouseComponent.Listener;
import forestry.core.climate.AbsentClimateState;
import forestry.core.multiblock.FakeMultiblockController;
import forestry.energy.EnergyManager;
import forestry.greenhouse.api.climate.IClimateContainer;
import forestry.greenhouse.api.greenhouse.IGreenhouseLimits;
import forestry.greenhouse.api.greenhouse.IGreenhouseProvider;
public class FakeGreenhouseController extends FakeMultiblockController implements IGreenhouseControllerInternal {
public static final FakeGreenhouseController instance = new FakeGreenhouseController();
@@ -81,8 +82,8 @@ public class FakeGreenhouseController extends FakeMultiblockController implement
}
@Override
public ImmutableClimateState getDefaultClimate() {
return null;
public IClimateState getDefaultClimate() {
return AbsentClimateState.INSTANCE;
}
@Override
@@ -26,7 +26,8 @@ import net.minecraft.world.biome.Biome;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import forestry.api.climate.ImmutableClimateState;
import forestry.api.climate.ClimateStateType;
import forestry.api.climate.IClimateState;
import forestry.api.core.EnumHumidity;
import forestry.api.core.EnumTemperature;
import forestry.api.core.ICamouflagedTile;
@@ -39,6 +40,7 @@ import forestry.api.multiblock.IGreenhouseComponent.Listener;
import forestry.api.multiblock.IMultiblockComponent;
import forestry.core.PluginCore;
import forestry.core.climate.ClimateContainer;
import forestry.core.climate.ClimateState;
import forestry.core.config.Config;
import forestry.core.multiblock.IMultiblockControllerInternal;
import forestry.core.multiblock.MultiblockValidationException;
@@ -86,7 +88,7 @@ public class GreenhouseController extends RectangularMultiblockControllerBase im
private ItemStack camouflage;
//Climate
private ClimateContainer climateContainer;
private ImmutableClimateState defaultState = ImmutableClimateState.MIN;
private IClimateState defaultState = ClimateState.MIN;
private BlockPos centerPos;
private int assembleTickCount;
@SideOnly(Side.CLIENT)
@@ -173,7 +175,7 @@ public class GreenhouseController extends RectangularMultiblockControllerBase im
}
@Override
public ImmutableClimateState getDefaultClimate() {
public IClimateState getDefaultClimate() {
return defaultState;
}
@@ -397,8 +399,8 @@ public class GreenhouseController extends RectangularMultiblockControllerBase im
createDefaultState();
limits = createLimits();
if(climateContainer.getTargetedState() == null) {
ImmutableClimateState defaultClimate = getDefaultClimate();
if(!climateContainer.getTargetedState().isPresent()) {
IClimateState defaultClimate = getDefaultClimate();
climateContainer.setState(defaultClimate.toMutable());
climateContainer.setTargetedState(defaultClimate);
}
@@ -467,7 +469,7 @@ public class GreenhouseController extends RectangularMultiblockControllerBase im
biomes++;
}
}
defaultState = new ImmutableClimateState((float) (temperature / biomes), (float) (humidity / biomes));
defaultState = new ClimateState((float) (temperature / biomes), (float) (humidity / biomes), ClimateStateType.IMMUTABLE);
}
/* RectangularMultiblockControllerBase */
@@ -15,9 +15,10 @@ import java.io.IOException;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.util.math.BlockPos;
import forestry.api.climate.ClimateStateType;
import forestry.api.climate.IClimateState;
import forestry.api.climate.ImmutableClimateState;
import forestry.api.multiblock.IGreenhouseComponent;
import forestry.core.climate.ClimateState;
import forestry.core.multiblock.MultiblockUtil;
import forestry.core.network.ForestryPacket;
import forestry.core.network.IForestryPacketHandlerServer;
@@ -58,7 +59,7 @@ public class PacketSelectClimateTargeted extends ForestryPacket implements IFore
IGreenhouseControllerInternal controller = MultiblockUtil.getController(player.world, pos, IGreenhouseComponent.class);
if (controller != null) {
IClimateContainer container = controller.getClimateContainer();
container.setTargetedState(new ImmutableClimateState(temperature, humidity));
container.setTargetedState(new ClimateState(temperature, humidity, ClimateStateType.IMMUTABLE));
}
}
}
@@ -22,7 +22,7 @@ import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import forestry.api.climate.ImmutableClimateState;
import forestry.api.climate.IClimateState;
import forestry.api.core.EnumHumidity;
import forestry.api.core.EnumTemperature;
import forestry.api.core.ICamouflageHandler;
@@ -192,7 +192,7 @@ public abstract class TileGreenhouse extends MultiblockTileEntityForestry<Multib
}
@Override
public ImmutableClimateState getDefaultClimate() {
public IClimateState getDefaultClimate() {
return getMultiblockLogic().getController().getDefaultClimate();
}