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