2014-07-14 04:05:27 +02:00
|
|
|
package com.pahimar.ee3.api;
|
2014-04-29 03:24:31 +02:00
|
|
|
|
2014-09-12 22:11:18 +02:00
|
|
|
import com.google.gson.*;
|
2014-08-29 22:25:31 +02:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
2014-09-12 22:11:18 +02:00
|
|
|
import java.lang.reflect.Type;
|
|
|
|
|
|
|
|
public final class EnergyValue implements Comparable<EnergyValue>, JsonDeserializer<EnergyValue>, JsonSerializer<EnergyValue>
|
2014-04-29 03:24:31 +02:00
|
|
|
{
|
2014-09-12 22:11:18 +02:00
|
|
|
private static final Gson jsonSerializer = (new GsonBuilder()).registerTypeAdapter(EnergyValue.class, new EnergyValue()).create();
|
2014-10-03 21:55:22 +02:00
|
|
|
private float energyValue;
|
2014-07-10 22:05:14 +02:00
|
|
|
private final EnergyType energyType;
|
2014-04-29 03:24:31 +02:00
|
|
|
|
2014-09-12 22:11:18 +02:00
|
|
|
public EnergyValue()
|
|
|
|
{
|
|
|
|
this(0f, EnergyType.UNKNOWN);
|
|
|
|
}
|
|
|
|
|
2014-07-10 22:05:14 +02:00
|
|
|
public EnergyValue(int energyValue)
|
2014-04-29 03:24:31 +02:00
|
|
|
{
|
2014-07-10 22:05:14 +02:00
|
|
|
this((float) energyValue);
|
2014-04-29 03:24:31 +02:00
|
|
|
}
|
|
|
|
|
2014-09-09 21:59:07 +02:00
|
|
|
public EnergyValue(double energyValue)
|
|
|
|
{
|
|
|
|
this((float) energyValue);
|
|
|
|
}
|
|
|
|
|
2014-07-10 22:05:14 +02:00
|
|
|
public EnergyValue(float energyValue)
|
2014-04-29 03:24:31 +02:00
|
|
|
{
|
2014-07-10 22:05:14 +02:00
|
|
|
this(energyValue, EnergyType.DEFAULT);
|
2014-04-29 03:24:31 +02:00
|
|
|
}
|
|
|
|
|
2014-07-10 22:05:14 +02:00
|
|
|
public EnergyValue(float energyValue, EnergyType energyType)
|
2014-04-29 03:24:31 +02:00
|
|
|
{
|
2014-07-10 22:05:14 +02:00
|
|
|
this.energyValue = energyValue;
|
|
|
|
this.energyType = energyType;
|
2014-07-04 21:18:10 +02:00
|
|
|
}
|
|
|
|
|
2014-07-10 22:05:14 +02:00
|
|
|
public EnergyValue(int energyValue, EnergyType energyType)
|
2014-07-04 21:18:10 +02:00
|
|
|
{
|
2014-07-10 22:05:14 +02:00
|
|
|
this((float) energyValue, energyType);
|
2014-07-04 21:18:10 +02:00
|
|
|
}
|
|
|
|
|
2014-07-10 22:05:14 +02:00
|
|
|
@Override
|
|
|
|
public boolean equals(Object object)
|
2014-07-04 21:18:10 +02:00
|
|
|
{
|
2014-07-10 22:05:14 +02:00
|
|
|
return object instanceof EnergyValue && (compareTo((EnergyValue) object) == 0);
|
2014-04-29 03:24:31 +02:00
|
|
|
}
|
|
|
|
|
2014-07-10 22:05:14 +02:00
|
|
|
@Override
|
|
|
|
public String toString()
|
2014-04-29 03:24:31 +02:00
|
|
|
{
|
2014-07-10 22:05:14 +02:00
|
|
|
return String.format(" %s@%s ", energyValue, energyType);
|
2014-04-29 03:24:31 +02:00
|
|
|
}
|
|
|
|
|
2014-07-10 22:05:14 +02:00
|
|
|
@Override
|
|
|
|
public int compareTo(EnergyValue energyValue)
|
2014-04-29 03:24:31 +02:00
|
|
|
{
|
2014-07-10 22:05:14 +02:00
|
|
|
if (energyValue != null)
|
2014-04-29 03:24:31 +02:00
|
|
|
{
|
2014-07-10 22:05:14 +02:00
|
|
|
if (this.energyType == energyValue.getEnergyType())
|
2014-04-29 03:24:31 +02:00
|
|
|
{
|
2014-07-10 22:05:14 +02:00
|
|
|
return Float.compare(this.energyValue, energyValue.getEnergyValue());
|
2014-04-29 03:24:31 +02:00
|
|
|
}
|
2014-07-10 22:05:14 +02:00
|
|
|
else
|
2014-04-29 03:24:31 +02:00
|
|
|
{
|
2014-07-10 22:05:14 +02:00
|
|
|
return (this.energyType.ordinal() - energyValue.getEnergyType().ordinal());
|
2014-04-29 03:24:31 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-10 22:05:14 +02:00
|
|
|
else
|
2014-04-29 03:24:31 +02:00
|
|
|
{
|
2014-07-10 22:05:14 +02:00
|
|
|
return -1;
|
2014-04-29 03:24:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-10 22:05:14 +02:00
|
|
|
public EnergyType getEnergyType()
|
2014-04-29 03:24:31 +02:00
|
|
|
{
|
2014-07-10 22:05:14 +02:00
|
|
|
return this.energyType;
|
2014-04-29 03:24:31 +02:00
|
|
|
}
|
|
|
|
|
2014-07-10 22:05:14 +02:00
|
|
|
public float getEnergyValue()
|
2014-04-29 03:24:31 +02:00
|
|
|
{
|
2014-07-10 22:05:14 +02:00
|
|
|
return this.energyValue;
|
2014-04-29 03:24:31 +02:00
|
|
|
}
|
2014-10-03 21:55:22 +02:00
|
|
|
|
|
|
|
public void add(EnergyValue energyValue)
|
|
|
|
{
|
|
|
|
if (this.energyType == energyValue.energyType && energyValue.energyValue > 0f)
|
|
|
|
{
|
|
|
|
this.energyValue += energyValue.energyValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void subtract(EnergyValue energyValue)
|
|
|
|
{
|
|
|
|
if (this.energyType == energyValue.energyType && (this.energyValue - energyValue.energyValue) >= 0f)
|
|
|
|
{
|
|
|
|
this.energyValue -= energyValue.energyValue;
|
|
|
|
}
|
|
|
|
}
|
2014-04-29 03:24:31 +02:00
|
|
|
|
2014-08-29 22:25:31 +02:00
|
|
|
public NBTTagCompound writeToNBT(NBTTagCompound nbtTagCompound)
|
|
|
|
{
|
|
|
|
nbtTagCompound.setFloat("energyValue", energyValue);
|
|
|
|
nbtTagCompound.setInteger("energyType", energyType.ordinal());
|
|
|
|
return nbtTagCompound;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static NBTTagCompound writeEnergyValueToNBT(EnergyValue energyValue)
|
|
|
|
{
|
|
|
|
NBTTagCompound nbtTagCompound = new NBTTagCompound();
|
|
|
|
energyValue.writeToNBT(nbtTagCompound);
|
|
|
|
return nbtTagCompound;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static EnergyValue loadEnergyValueFromNBT(NBTTagCompound nbtTagCompound)
|
|
|
|
{
|
|
|
|
if (nbtTagCompound.hasKey("energyValue") && nbtTagCompound.hasKey("energyType"))
|
|
|
|
{
|
|
|
|
float energyValue = nbtTagCompound.getFloat("energyValue");
|
|
|
|
EnergyType energyType = EnergyType.getEnergyTypeFromOrdinal(nbtTagCompound.getInteger("energyType"));
|
|
|
|
|
|
|
|
return new EnergyValue(energyValue, energyType);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-09-12 22:11:18 +02:00
|
|
|
/**
|
|
|
|
* Deserializes an EmcValue object from the given serialized json String
|
|
|
|
*
|
|
|
|
* @param jsonEnergyValue Json encoded String representing a EmcValue object
|
|
|
|
* @return The EmcValue that was encoded as json, or null if a valid EmcValue could not be decoded from given String
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
public static EnergyValue createFromJson(String jsonEnergyValue)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return jsonSerializer.fromJson(jsonEnergyValue, EnergyValue.class);
|
|
|
|
}
|
|
|
|
catch (JsonSyntaxException exception)
|
|
|
|
{
|
|
|
|
exception.printStackTrace();
|
|
|
|
}
|
|
|
|
catch (JsonParseException exception)
|
|
|
|
{
|
|
|
|
exception.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns this EmcValue as a json serialized String
|
|
|
|
*
|
|
|
|
* @return Json serialized String of this EmcValue
|
|
|
|
*/
|
|
|
|
public String toJson()
|
|
|
|
{
|
|
|
|
return jsonSerializer.toJson(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gson invokes this call-back method during deserialization when it encounters a field of the
|
|
|
|
* specified type.
|
|
|
|
* <p>In the implementation of this call-back method, you should consider invoking
|
|
|
|
* {@link com.google.gson.JsonDeserializationContext#deserialize(com.google.gson.JsonElement, java.lang.reflect.Type)} method to create objects
|
|
|
|
* for any non-trivial field of the returned object. However, you should never invoke it on the
|
|
|
|
* the same type passing {@code jsonElement} since that will cause an infinite loop (Gson will call your
|
|
|
|
* call-back method again).
|
|
|
|
*
|
|
|
|
* @param jsonElement The Json data being deserialized
|
|
|
|
* @param typeOfT The type of the Object to deserialize to
|
|
|
|
* @param context
|
|
|
|
* @return a deserialized object of the specified type typeOfT which is a subclass of {@code T}
|
|
|
|
* @throws com.google.gson.JsonParseException if jsonElement is not in the expected format of {@code typeofT}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public EnergyValue deserialize(JsonElement jsonElement, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
|
|
|
|
{
|
|
|
|
JsonObject jsonEnergyValue = (JsonObject) jsonElement;
|
|
|
|
|
|
|
|
if (jsonEnergyValue.get("type") != null && jsonEnergyValue.get("type").isJsonPrimitive() && jsonEnergyValue.get("value") != null && jsonEnergyValue.get("value").isJsonPrimitive())
|
|
|
|
{
|
|
|
|
EnergyType energyType = EnergyType.getEnergyTypeFromOrdinal(jsonEnergyValue.get("type").getAsInt());
|
|
|
|
float energyValue = jsonEnergyValue.get("value").getAsFloat();
|
|
|
|
|
|
|
|
if (Float.compare(energyValue, 0f) > 0)
|
|
|
|
{
|
|
|
|
return new EnergyValue(energyValue, energyType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gson invokes this call-back method during serialization when it encounters a field of the
|
|
|
|
* specified type.
|
|
|
|
* <p/>
|
|
|
|
* <p>In the implementation of this call-back method, you should consider invoking
|
|
|
|
* {@link com.google.gson.JsonSerializationContext#serialize(Object, java.lang.reflect.Type)} method to create JsonElements for any
|
|
|
|
* non-trivial field of the {@code energyValueObject} object. However, you should never invoke it on the
|
|
|
|
* {@code energyValueObject} object itself since that will cause an infinite loop (Gson will call your
|
|
|
|
* call-back method again).</p>
|
|
|
|
*
|
|
|
|
* @param energyValueObject the object that needs to be converted to Json.
|
|
|
|
* @param typeOfSrc the actual type (fully genericized version) of the source object.
|
|
|
|
* @param context
|
|
|
|
* @return a JsonElement corresponding to the specified object.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public JsonElement serialize(EnergyValue energyValueObject, Type typeOfSrc, JsonSerializationContext context)
|
|
|
|
{
|
|
|
|
JsonObject jsonEnergyValue = new JsonObject();
|
|
|
|
|
|
|
|
jsonEnergyValue.addProperty("type", energyValueObject.energyType.ordinal());
|
|
|
|
jsonEnergyValue.addProperty("value", energyValueObject.energyValue);
|
|
|
|
|
|
|
|
return jsonEnergyValue;
|
|
|
|
}
|
|
|
|
|
2014-07-10 22:05:14 +02:00
|
|
|
public static enum EnergyType
|
2014-04-29 03:24:31 +02:00
|
|
|
{
|
2014-08-29 22:25:31 +02:00
|
|
|
UNKNOWN, CORPOREAL, KINETIC, TEMPORAL, ESSENTIA, AMORPHOUS, VOID, OMNI;
|
2014-07-04 21:18:10 +02:00
|
|
|
|
2014-07-10 22:05:14 +02:00
|
|
|
public static final EnergyType DEFAULT = EnergyType.CORPOREAL;
|
2014-08-29 22:25:31 +02:00
|
|
|
|
|
|
|
public static EnergyType getEnergyTypeFromOrdinal(int ordinal)
|
|
|
|
{
|
|
|
|
if (ordinal == CORPOREAL.ordinal())
|
|
|
|
{
|
|
|
|
return CORPOREAL;
|
|
|
|
}
|
|
|
|
else if (ordinal == KINETIC.ordinal())
|
|
|
|
{
|
|
|
|
return KINETIC;
|
|
|
|
}
|
|
|
|
else if (ordinal == TEMPORAL.ordinal())
|
|
|
|
{
|
|
|
|
return TEMPORAL;
|
|
|
|
}
|
|
|
|
else if (ordinal == ESSENTIA.ordinal())
|
|
|
|
{
|
|
|
|
return ESSENTIA;
|
|
|
|
}
|
|
|
|
else if (ordinal == AMORPHOUS.ordinal())
|
|
|
|
{
|
|
|
|
return AMORPHOUS;
|
|
|
|
}
|
|
|
|
else if (ordinal == VOID.ordinal())
|
|
|
|
{
|
|
|
|
return VOID;
|
|
|
|
}
|
|
|
|
else if (ordinal == OMNI.ordinal())
|
|
|
|
{
|
|
|
|
return OMNI;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
2014-04-29 03:24:31 +02:00
|
|
|
}
|
|
|
|
}
|