コード例 #1
0
  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;
  }
コード例 #2
0
  /**
   * 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;
  }