Skip to content

Instantly share code, notes, and snippets.

@Lanse505
Created March 20, 2020 23:24
Show Gist options
  • Save Lanse505/dbb25c9032d574f8b91749d6c0e320a5 to your computer and use it in GitHub Desktop.
Save Lanse505/dbb25c9032d574f8b91749d6c0e320a5 to your computer and use it in GitHub Desktop.
public static JsonElement serializeEffectInstance(EffectInstance instance) {
final JsonObject object = new JsonObject();
object.addProperty("effect", instance.getPotion().getRegistryName().toString());
object.addProperty("duration", instance.getDuration());
final JsonObject propertiesElement = new JsonObject();
if (instance.getAmplifier() > 0) {
propertiesElement.addProperty("amplifier", instance.getAmplifier());
propertiesElement.addProperty("ambient", instance.isAmbient());
propertiesElement.addProperty("showParticles", instance.doesShowParticles());
propertiesElement.addProperty("showIcon", instance.isShowIcon());
}
object.add("properties", propertiesElement);
return object;
}
@SuppressWarnings({"unchecked", "rawtypes"})
public static EffectInstance deserializeEffectInstance (JsonObject json) {
// Read the effect from the forge registry.
final Effect effect = getPotion(json, "effect");
final int duration = json.get("duration").getAsInt();
if (json.has("properties")) {
final JsonElement propertiesElement = json.get("properties");
if (propertiesElement.isJsonObject()) {
final JsonObject properties = propertiesElement.getAsJsonObject();
if (properties.has("amplifier")) {
final int amplifier = properties.getAsJsonObject("amplifier").getAsInt();
if (properties.has("ambient")) {
final boolean ambient = properties.getAsJsonObject("ambient").getAsBoolean();
if (properties.has("showParticles")) {
final boolean showParticles = properties.getAsJsonObject("showParticles").getAsBoolean();
if (properties.has("showIcon")) {
final boolean showIcon = properties.getAsJsonObject("showIcon").getAsBoolean();
return new EffectInstance(effect, duration, amplifier, ambient, showParticles, showIcon);
}
return new EffectInstance(effect, duration, amplifier, ambient, showParticles);
} else {
throw new JsonSyntaxException("EffectInstance requires both Value for 'ambient' and Value for 'showParticles'");
}
}
return new EffectInstance(effect, duration, amplifier);
}
}
}
return new EffectInstance(effect, duration);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment