tcr/java/src/game/entity/attributes/Attribute.java

86 lines
2 KiB
Java
Raw Normal View History

2025-03-11 00:23:54 +01:00
package game.entity.attributes;
import java.util.Map;
import game.collect.Maps;
2025-03-16 17:40:47 +01:00
import game.util.ExtMath;
2025-03-11 00:23:54 +01:00
public class Attribute
{
private static final Map<String, Attribute> ATTRIBUTES = Maps.newHashMap();
private final String name;
private final String display;
private final double defValue;
private final double minValue;
private final double maxValue;
private final boolean shouldWatch;
public static Attribute getAttribute(String name) {
return ATTRIBUTES.get(name);
}
public Attribute(String name, String display, double def, double min, double max, boolean watch)
{
this.name = name;
this.display = display;
this.defValue = def;
if(name == null)
throw new IllegalArgumentException("Name cannot be null!");
this.minValue = min;
this.maxValue = max;
this.shouldWatch = watch;
if (min > max)
{
throw new IllegalArgumentException("Minimum value cannot be bigger than maximum value!");
}
else if (def < min)
{
throw new IllegalArgumentException("Default value cannot be lower than minimum value!");
}
else if (def > max)
{
throw new IllegalArgumentException("Default value cannot be bigger than maximum value!");
}
ATTRIBUTES.put(name, this);
}
public String getUnlocalizedName()
{
return this.name;
}
public String getDisplayName()
{
return this.display;
}
public double getDefaultValue()
{
return this.defValue;
}
public boolean getShouldWatch()
{
return this.shouldWatch;
}
public double clampValue(double value)
{
return ExtMath.clampd(value, this.minValue, this.maxValue);
}
public int hashCode()
{
return this.name.hashCode();
}
public boolean equals(Object obj)
{
return obj instanceof Attribute && this.name.equals(((Attribute)obj).getUnlocalizedName());
}
}