Search in sources :

Example 1 with JsonPrimitive

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;
}
Also used : JsonPrimitive(com.massivecraft.massivecore.xlib.gson.JsonPrimitive) JsonElement(com.massivecraft.massivecore.xlib.gson.JsonElement) JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject) PlayerInventory(org.bukkit.inventory.PlayerInventory) DataItemStack(com.massivecraft.massivecore.item.DataItemStack) ItemStack(org.bukkit.inventory.ItemStack) Inventory(org.bukkit.inventory.Inventory) MixinInventory(com.massivecraft.massivecore.mixin.MixinInventory) PlayerInventory(org.bukkit.inventory.PlayerInventory)

Example 2 with JsonPrimitive

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);
}
Also used : JsonPrimitive(com.massivecraft.massivecore.xlib.gson.JsonPrimitive) JsonElement(com.massivecraft.massivecore.xlib.gson.JsonElement)

Example 3 with JsonPrimitive

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);
}
Also used : LazilyParsedNumber(com.massivecraft.massivecore.xlib.gson.internal.LazilyParsedNumber) JsonPrimitive(com.massivecraft.massivecore.xlib.gson.JsonPrimitive)

Example 4 with JsonPrimitive

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);
}
Also used : JsonPrimitive(com.massivecraft.massivecore.xlib.gson.JsonPrimitive) JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject) JsonParseException(com.massivecraft.massivecore.xlib.gson.JsonParseException)

Example 5 with JsonPrimitive

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);
}
Also used : LazilyParsedNumber(com.massivecraft.massivecore.xlib.gson.internal.LazilyParsedNumber) JsonPrimitive(com.massivecraft.massivecore.xlib.gson.JsonPrimitive) LazilyParsedNumber(com.massivecraft.massivecore.xlib.gson.internal.LazilyParsedNumber)

Aggregations

JsonPrimitive (com.massivecraft.massivecore.xlib.gson.JsonPrimitive)5 JsonElement (com.massivecraft.massivecore.xlib.gson.JsonElement)2 JsonObject (com.massivecraft.massivecore.xlib.gson.JsonObject)2 LazilyParsedNumber (com.massivecraft.massivecore.xlib.gson.internal.LazilyParsedNumber)2 DataItemStack (com.massivecraft.massivecore.item.DataItemStack)1 MixinInventory (com.massivecraft.massivecore.mixin.MixinInventory)1 JsonParseException (com.massivecraft.massivecore.xlib.gson.JsonParseException)1 Inventory (org.bukkit.inventory.Inventory)1 ItemStack (org.bukkit.inventory.ItemStack)1 PlayerInventory (org.bukkit.inventory.PlayerInventory)1