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;
|
2015-02-06 05:06:53 +01:00
|
|
|
import net.minecraft.util.ChatComponentText;
|
|
|
|
import net.minecraft.util.IChatComponent;
|
2014-08-29 22:25:31 +02:00
|
|
|
|
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-04-29 03:24:31 +02:00
|
|
|
|
2014-09-12 22:11:18 +02:00
|
|
|
public EnergyValue()
|
|
|
|
{
|
2015-03-22 21:52:48 +01:00
|
|
|
this(0f);
|
2014-09-12 22:11:18 +02:00
|
|
|
}
|
|
|
|
|
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 = energyValue;
|
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
|
|
|
{
|
2015-03-22 21:52:48 +01:00
|
|
|
return String.format("%s", energyValue);
|
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
|
|
|
{
|
2015-03-22 21:52:48 +01: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 -1;
|
2014-04-29 03:24:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-22 21:52:48 +01:00
|
|
|
public float getEnergyValue()
|
2014-04-29 03:24:31 +02:00
|
|
|
{
|
2015-03-22 21:52:48 +01:00
|
|
|
return this.energyValue;
|
2014-04-29 03:24:31 +02:00
|
|
|
}
|
|
|
|
|
2015-03-22 21:52:48 +01:00
|
|
|
public void add(int energyValue)
|
2014-04-29 03:24:31 +02:00
|
|
|
{
|
2015-03-22 21:52:48 +01:00
|
|
|
this.add((float) energyValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void add(float energyValue)
|
|
|
|
{
|
|
|
|
if (energyValue >= 0f)
|
|
|
|
{
|
|
|
|
this.energyValue += energyValue;
|
|
|
|
}
|
2014-04-29 03:24:31 +02:00
|
|
|
}
|
2014-10-03 21:55:22 +02:00
|
|
|
|
|
|
|
public void add(EnergyValue energyValue)
|
|
|
|
{
|
2015-03-22 21:52:48 +01:00
|
|
|
if (energyValue != null && energyValue.energyValue >= 0f)
|
2014-10-03 21:55:22 +02:00
|
|
|
{
|
|
|
|
this.energyValue += energyValue.energyValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-22 21:52:48 +01:00
|
|
|
public void subtract(int energyValue)
|
|
|
|
{
|
|
|
|
this.subtract((float) energyValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void subtract(float energyValue)
|
|
|
|
{
|
|
|
|
if (this.energyValue - energyValue >= 0f)
|
|
|
|
{
|
|
|
|
this.energyValue -= energyValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-03 21:55:22 +02:00
|
|
|
public void subtract(EnergyValue energyValue)
|
|
|
|
{
|
2015-03-22 21:52:48 +01:00
|
|
|
if (energyValue != null && (this.energyValue - energyValue.energyValue) >= 0f)
|
2014-10-03 21:55:22 +02:00
|
|
|
{
|
|
|
|
this.energyValue -= energyValue.energyValue;
|
|
|
|
}
|
|
|
|
}
|
2015-02-06 05:06:53 +01:00
|
|
|
|
|
|
|
public IChatComponent getChatComponent()
|
|
|
|
{
|
|
|
|
return new ChatComponentText("" + this.getEnergyValue());
|
|
|
|
}
|
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);
|
|
|
|
return nbtTagCompound;
|
|
|
|
}
|
|
|
|
|
2015-03-22 21:52:48 +01:00
|
|
|
public void readFromNBT(NBTTagCompound nbtTagCompound)
|
|
|
|
{
|
|
|
|
if (nbtTagCompound.hasKey("energyValue"))
|
|
|
|
{
|
|
|
|
this.energyValue = nbtTagCompound.getFloat("energyValue");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-29 22:25:31 +02:00
|
|
|
public static NBTTagCompound writeEnergyValueToNBT(EnergyValue energyValue)
|
|
|
|
{
|
|
|
|
NBTTagCompound nbtTagCompound = new NBTTagCompound();
|
|
|
|
energyValue.writeToNBT(nbtTagCompound);
|
|
|
|
return nbtTagCompound;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static EnergyValue loadEnergyValueFromNBT(NBTTagCompound nbtTagCompound)
|
|
|
|
{
|
2015-03-22 21:52:48 +01:00
|
|
|
if (nbtTagCompound.hasKey("energyValue"))
|
2014-08-29 22:25:31 +02:00
|
|
|
{
|
|
|
|
float energyValue = nbtTagCompound.getFloat("energyValue");
|
|
|
|
|
2015-03-22 21:52:48 +01:00
|
|
|
return new EnergyValue(energyValue);
|
2014-08-29 22:25:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2015-03-22 21:52:48 +01:00
|
|
|
if (jsonEnergyValue.get("value") != null && jsonEnergyValue.get("value").isJsonPrimitive())
|
2014-09-12 22:11:18 +02:00
|
|
|
{
|
|
|
|
float energyValue = jsonEnergyValue.get("value").getAsFloat();
|
|
|
|
|
2015-03-22 21:52:48 +01:00
|
|
|
if (Float.compare(energyValue, 0f) >= 0)
|
2014-09-12 22:11:18 +02:00
|
|
|
{
|
2015-03-22 21:52:48 +01:00
|
|
|
return new EnergyValue(energyValue);
|
2014-09-12 22:11:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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("value", energyValueObject.energyValue);
|
|
|
|
|
|
|
|
return jsonEnergyValue;
|
|
|
|
}
|
2014-04-29 03:24:31 +02:00
|
|
|
}
|