Attempt at improving Potion erialization

This commit is contained in:
Olof Larsson 2016-03-14 18:06:54 +01:00
parent e7a1f75617
commit 54a83313b5

View File

@ -13,6 +13,7 @@ import com.google.common.collect.ImmutableBiMap;
import com.massivecraft.massivecore.xlib.gson.JsonElement; import com.massivecraft.massivecore.xlib.gson.JsonElement;
import com.massivecraft.massivecore.xlib.gson.JsonObject; import com.massivecraft.massivecore.xlib.gson.JsonObject;
@SuppressWarnings("deprecation")
public class ItemStackAdapterInner19 extends ItemStackAdapterInner18 public class ItemStackAdapterInner19 extends ItemStackAdapterInner18
{ {
// -------------------------------------------- // // -------------------------------------------- //
@ -20,7 +21,7 @@ public class ItemStackAdapterInner19 extends ItemStackAdapterInner18
// -------------------------------------------- // // -------------------------------------------- //
public static final String POTION = "potion"; public static final String POTION = "potion";
public static final PotionData DATA_NULL = new PotionData(PotionType.UNCRAFTABLE, false, false); public static final PotionData POTION_DEFAULT = new PotionData(PotionType.UNCRAFTABLE, false, false);
// -------------------------------------------- // // -------------------------------------------- //
// INSTANCE & CONSTRUCT // INSTANCE & CONSTRUCT
@ -42,110 +43,137 @@ public class ItemStackAdapterInner19 extends ItemStackAdapterInner18
// -------------------------------------------- // // -------------------------------------------- //
// SPECIFIC META: POTION // SPECIFIC META: POTION
// -------------------------------------------- // // -------------------------------------------- //
// In Minecraft 1.8 the damage value was used to store the potion effects.
// In Minecraft 1.9 the damage value is no longer used and the potion effect is stored by string instead.
//
// Sticking to the damage value for serialization is not feasible.
// Minecraft 1.9 adds new potion effects that did not exist in Minecraft 1.9 such as LUCK.
//
// Thus we upgrade the database from damage values to the new potion string where possible.
// Invalid old damage values that does not make any sense are left as is.
@Override @SuppressWarnings("deprecation") @Override
public void transferPotion(ItemStack stack, JsonObject json, boolean meta2json, PotionMeta meta) public void transferPotion(ItemStack stack, JsonObject json, boolean meta2json, PotionMeta meta)
{ {
super.transferPotion(stack, json, meta2json, meta); super.transferPotion(stack, json, meta2json, meta);
// Note: We serialize/deserialize PotionData if available, otherwise fall back to damage values
// NOTE: Damage values are NOT safe and thus should not be used to serialize data in 1.9.
if (meta2json) if (meta2json)
{ {
PotionData data = meta.getBasePotionData(); PotionData potionData = meta.getBasePotionData();
json.addProperty(POTION, fromBukkit(data)); if (potionData != null && ! potionData.equals(POTION_DEFAULT))
{
String potionString = toPotionString(potionData);
if (potionString != null)
{
json.addProperty(POTION, potionString);
}
}
} }
else else
{ {
JsonElement element = json.get(POTION); JsonElement potionElement = json.get(POTION);
if (element != null) if (potionElement != null)
{ {
meta.setBasePotionData(toBukkit(element.getAsString())); String potionString = potionElement.getAsString();
PotionData potionData = toPotionData(potionString);
if (potionData != null)
{
meta.setBasePotionData(potionData);
}
return; return;
} }
// NOTE: PotionData is initialized with UNCRAFTABLE. If is freshly initialized, try from damage values.
PotionData data = meta.getBasePotionData();
if ( ! data.equals(DATA_NULL)) return;
int damage = DEFAULT_DAMAGE;
JsonElement damageElement = json.get(DAMAGE); JsonElement damageElement = json.get(DAMAGE);
if (damageElement != null) damage = damageElement.getAsInt(); if (damageElement != null)
Potion.fromDamage(damage).apply(stack); {
int damage = damageElement.getAsInt();
PotionData potionData = toPotionData(damage);
if (potionData != null)
{
meta.setBasePotionData(potionData);
stack.setDurability((short) 0);
}
}
} }
} }
// -------------------------------------------- // // -------------------------------------------- //
// CRAFT POTION UTIL // POTION UTIL
// -------------------------------------------- // // -------------------------------------------- //
public static String fromBukkit(PotionData data) public static PotionData toPotionData(int damage)
{ {
if (data.isUpgraded()) return upgradeable.get(data.getType()); try
if (data.isExtended()) return extendable.get(data.getType()); {
return regular.get(data.getType()); Potion potion = Potion.fromDamage(damage);
PotionType type = potion.getType();
boolean extended = potion.hasExtendedDuration();
boolean upgraded = (potion.getLevel() >= 2);
return new PotionData(type, extended, upgraded);
}
catch (Exception e)
{
return null;
}
} }
public static PotionData toBukkit(String type) public static PotionData toPotionData(String potionString)
{ {
PotionType potionType = null; if (potionString == null) return null;
return POTION_ID_TO_DATA.get(potionString);
// Extended
potionType = extendable.inverse().get(type);
if (potionType != null) return new PotionData(potionType, true, false);
// Upgraded
potionType = upgradeable.inverse().get(type);
if (potionType != null) return new PotionData(potionType, false, true);
// Regular
return new PotionData(regular.inverse().get(type), false, false);
} }
private static final BiMap<PotionType, String> regular = ImmutableBiMap.<PotionType, String>builder() public static String toPotionString(PotionData potionData)
.put(PotionType.UNCRAFTABLE, "empty") {
.put(PotionType.WATER, "water") if (potionData == null) return null;
.put(PotionType.MUNDANE, "mundane") return POTION_ID_TO_DATA.inverse().get(potionData);
.put(PotionType.THICK, "thick") }
.put(PotionType.AWKWARD, "awkward")
.put(PotionType.NIGHT_VISION, "night_vision")
.put(PotionType.INVISIBILITY, "invisibility")
.put(PotionType.JUMP, "leaping")
.put(PotionType.FIRE_RESISTANCE, "fire_resistance")
.put(PotionType.SPEED, "swiftness")
.put(PotionType.SLOWNESS, "slowness")
.put(PotionType.WATER_BREATHING, "water_breathing")
.put(PotionType.INSTANT_HEAL, "healing")
.put(PotionType.INSTANT_DAMAGE, "harming")
.put(PotionType.POISON, "poison")
.put(PotionType.REGEN, "regeneration")
.put(PotionType.STRENGTH, "strength")
.put(PotionType.WEAKNESS, "weakness")
.put(PotionType.LUCK, "luck")
.build();
private static final BiMap<PotionType, String> upgradeable = ImmutableBiMap.<PotionType, String>builder() private static final BiMap<String, PotionData> POTION_ID_TO_DATA = ImmutableBiMap.<String, PotionData>builder()
.put(PotionType.JUMP, "strong_leaping") // REGULAR
.put(PotionType.SPEED, "strong_swiftness") .put("empty", new PotionData(PotionType.UNCRAFTABLE, false, false))
.put(PotionType.INSTANT_HEAL, "strong_healing") .put("water", new PotionData(PotionType.WATER, false, false))
.put(PotionType.INSTANT_DAMAGE, "strong_harming") .put("mundane", new PotionData(PotionType.MUNDANE, false, false))
.put(PotionType.POISON, "strong_poison") .put("thick", new PotionData(PotionType.THICK, false, false))
.put(PotionType.REGEN, "strong_regeneration") .put("awkward", new PotionData(PotionType.AWKWARD, false, false))
.put(PotionType.STRENGTH, "strong_strength") .put("night_vision", new PotionData(PotionType.NIGHT_VISION, false, false))
.build(); .put("invisibility", new PotionData(PotionType.INVISIBILITY, false, false))
.put("leaping", new PotionData(PotionType.JUMP, false, false))
.put("fire_resistance", new PotionData(PotionType.FIRE_RESISTANCE, false, false))
.put("swiftness", new PotionData(PotionType.SPEED, false, false))
.put("slowness", new PotionData(PotionType.SLOWNESS, false, false))
.put("water_breathing", new PotionData(PotionType.WATER_BREATHING, false, false))
.put("healing", new PotionData(PotionType.INSTANT_HEAL, false, false))
.put("harming", new PotionData(PotionType.INSTANT_DAMAGE, false, false))
.put("poison", new PotionData(PotionType.POISON, false, false))
.put("regeneration", new PotionData(PotionType.REGEN, false, false))
.put("strength", new PotionData(PotionType.STRENGTH, false, false))
.put("weakness", new PotionData(PotionType.WEAKNESS, false, false))
.put("luck", new PotionData(PotionType.LUCK, false, false))
private static final BiMap<PotionType, String> extendable = ImmutableBiMap.<PotionType, String>builder() // UPGRADABLE
.put(PotionType.NIGHT_VISION, "long_night_vision") .put("strong_leaping", new PotionData(PotionType.JUMP, false, true))
.put(PotionType.INVISIBILITY, "long_invisibility") .put("strong_swiftness", new PotionData(PotionType.SPEED, false, true))
.put(PotionType.JUMP, "long_leaping") .put("strong_healing", new PotionData(PotionType.INSTANT_HEAL, false, true))
.put(PotionType.FIRE_RESISTANCE, "long_fire_resistance") .put("strong_harming", new PotionData(PotionType.INSTANT_DAMAGE, false, true))
.put(PotionType.SPEED, "long_swiftness") .put("strong_poison", new PotionData(PotionType.POISON, false, true))
.put(PotionType.SLOWNESS, "long_slowness") .put("strong_regeneration", new PotionData(PotionType.REGEN, false, true))
.put(PotionType.WATER_BREATHING, "long_water_breathing") .put("strong_strength", new PotionData(PotionType.STRENGTH, false, true))
.put(PotionType.POISON, "long_poison")
.put(PotionType.REGEN, "long_regeneration") // EXTENDABLE
.put(PotionType.STRENGTH, "long_strength") .put("long_night_vision", new PotionData(PotionType.NIGHT_VISION, true, false))
.put(PotionType.WEAKNESS, "long_weakness") .put("long_invisibility", new PotionData(PotionType.INVISIBILITY, true, false))
.put("long_leaping", new PotionData(PotionType.JUMP, true, false))
.put("long_fire_resistance", new PotionData(PotionType.FIRE_RESISTANCE, true, false))
.put("long_swiftness", new PotionData(PotionType.SPEED, true, false))
.put("long_slowness", new PotionData(PotionType.SLOWNESS, true, false))
.put("long_water_breathing", new PotionData(PotionType.WATER_BREATHING, true, false))
.put("long_poison", new PotionData(PotionType.POISON, true, false))
.put("long_regeneration", new PotionData(PotionType.REGEN, true, false))
.put("long_strength", new PotionData(PotionType.STRENGTH, true, false))
.put("long_weakness", new PotionData(PotionType.WEAKNESS, true, false))
// BUILD
.build(); .build();
} }