use of com.massivecraft.massivecore.xlib.gson.JsonPrimitive in project MassiveCore by MassiveCraft.
the class AdapterInventory method fromJson.
public static Inventory fromJson(JsonElement json) {
// If must be an object!
if (!json.isJsonObject())
return null;
JsonObject jsonInventory = json.getAsJsonObject();
// The return value
Inventory ret = null;
// These variables are used in loops and repetitive logic.
ItemStack itemStack = null;
JsonElement jsonItemStack = null;
// There must be a size entry!
if (!jsonInventory.has(SIZE))
return null;
JsonPrimitive jsonSize = jsonInventory.get(SIZE).getAsJsonPrimitive();
int size = 0;
// What size/type is it?
if (jsonSize.isString() && jsonSize.getAsString().equals(PLAYER)) {
// We use 36 here since it's the size of the player inventory (without armor)
size = SIZE_PLAYER_STORAGE;
// This is a PlayerInventory
ret = MixinInventory.get().createPlayerInventory();
PlayerInventory pret = (PlayerInventory) ret;
// Helmet
if (jsonInventory.has(HELMET)) {
itemStack = getItemStack(jsonInventory, HELMET);
pret.setHelmet(itemStack);
}
// Chestplate
if (jsonInventory.has(CHESTPLATE)) {
itemStack = getItemStack(jsonInventory, CHESTPLATE);
pret.setChestplate(itemStack);
}
// Leggings
if (jsonInventory.has(LEGGINGS)) {
jsonItemStack = jsonInventory.get(LEGGINGS);
itemStack = MassiveCore.gson.fromJson(jsonItemStack, ItemStack.class);
pret.setLeggings(itemStack);
}
// Boots
if (jsonInventory.has(BOOTS)) {
itemStack = getItemStack(jsonInventory, BOOTS);
pret.setBoots(itemStack);
}
// Shield (Minecraft 1.9)
if (jsonInventory.has(SHIELD) && INDEX_PLAYER_SHIELD < pret.getSize()) {
itemStack = getItemStack(jsonInventory, SHIELD);
pret.setItem(INDEX_PLAYER_SHIELD, itemStack);
}
} else {
// A custom size were specified
size = jsonSize.getAsInt();
// This is a "Custom" Inventory (content only).
ret = MixinInventory.get().createInventory(null, size, "");
}
// Now process content
for (int i = 0; i < size; i++) {
String stackIdx = String.valueOf(i);
itemStack = getItemStack(jsonInventory, stackIdx);
if (itemStack == null)
continue;
ret.setItem(i, itemStack);
}
return ret;
}
use of com.massivecraft.massivecore.xlib.gson.JsonPrimitive in project MassiveCore by MassiveCraft.
the class MigratorDataItemStack001StringId method migrateInner.
// -------------------------------------------- //
// CONVERSION
// -------------------------------------------- //
@Override
public void migrateInner(JsonObject json) {
// Get the id
JsonElement id = json.get("id");
if (id == null || id.isJsonNull())
return;
// The id is either a number or string, always a JsonPrimitive
JsonPrimitive primitive = id.getAsJsonPrimitive();
// Only convert if number
if (!primitive.isNumber())
return;
int typeId = primitive.getAsInt();
String name = id2name.get(typeId);
if (name == null)
throw new RuntimeException(String.valueOf(typeId));
JsonElement newValue = new JsonPrimitive(name);
json.add("id", newValue);
}
use of com.massivecraft.massivecore.xlib.gson.JsonPrimitive in project MassiveCore by MassiveCraft.
the class GsonEqualsChecker method primitiveEquals.
// The argument one must be JsonPrimitive, and can not be null.
// The argument twoObject may be anything, even null.
public static boolean primitiveEquals(JsonPrimitive one, Object twoObject) {
// Null check (one can't ever be null)
if (twoObject == null)
return false;
// Object identity speedup
if (one == twoObject)
return true;
// if twoObject is JsonObject or JsonArray we are not equal.
if (!(twoObject instanceof JsonPrimitive))
return false;
// Cast to JsonPrimitive
JsonPrimitive two = (JsonPrimitive) twoObject;
// Boolean check
if (one.isBoolean()) {
if (!two.isBoolean())
return false;
return one.getAsBoolean() == two.getAsBoolean();
}
// Number check
if (one.isNumber()) {
if (!two.isNumber())
return false;
Number oneNumber = one.getAsNumber();
Number twoNumber = two.getAsNumber();
boolean floating = isFloating(oneNumber);
if (floating) {
// Our epsilon is pretty big in order to see float and double as the same.
return Math.abs(oneNumber.doubleValue() - twoNumber.doubleValue()) < 0.0001D;
} else {
return oneNumber.longValue() == twoNumber.longValue();
}
}
// String check
if (one.isString()) {
if (!two.isString())
return false;
return one.getAsString().equals(two.getAsString());
}
throw new IllegalArgumentException("Unsupported value type for: " + one);
}
use of com.massivecraft.massivecore.xlib.gson.JsonPrimitive in project MassiveCore by MassiveCraft.
the class AdapterPolymorphic method deserialize.
@Override
public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (!json.isJsonObject()) {
throw new JsonParseException("A polymorph must be an object.");
}
JsonObject jsonObject = json.getAsJsonObject();
if (!jsonObject.has(TYPE)) {
throw new JsonParseException("A polymorph must be have a \"" + TYPE + "\" field.");
}
if (!jsonObject.has(VALUE)) {
throw new JsonParseException("A polymorph must be have a \"+VALUE+\" field.");
}
String type = ((JsonPrimitive) jsonObject.get(TYPE)).getAsString();
Class<?> typeClass = null;
try {
typeClass = Class.forName(type);
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new JsonParseException(e.getMessage());
}
return context.deserialize(jsonObject.get(VALUE), typeClass);
}
use of com.massivecraft.massivecore.xlib.gson.JsonPrimitive in project MassiveCore by MassiveCraft.
the class GsonMongoConverter method gson2MongoPrimitive.
public static Object gson2MongoPrimitive(JsonElement inElement) {
if (inElement.isJsonNull())
return null;
JsonPrimitive in = inElement.getAsJsonPrimitive();
if (in.isBoolean()) {
return in.getAsBoolean();
}
if (in.isNumber()) {
Number number = in.getAsNumber();
boolean floating;
if (number instanceof LazilyParsedNumber) {
floating = StringUtils.contains(number.toString(), '.');
} else {
floating = (number instanceof Double || number instanceof Float);
}
if (floating) {
return number.doubleValue();
} else {
return number.longValue();
}
}
if (in.isString()) {
return in.getAsString();
}
throw new IllegalArgumentException("Unsupported value type for: " + in);
}
Aggregations