Example usage for com.google.gson JsonSyntaxException JsonSyntaxException

List of usage examples for com.google.gson JsonSyntaxException JsonSyntaxException

Introduction

In this page you can find the example usage for com.google.gson JsonSyntaxException JsonSyntaxException.

Prototype

public JsonSyntaxException(Throwable cause) 

Source Link

Document

Creates exception with the specified cause.

Usage

From source file:net.daporkchop.toobeetooteebot.text.JsonUtils.java

License:Open Source License

/**
 * Gets the JsonArray field on the JsonObject with the given name.
 *///from   ww  w . j a v a2 s .  c  o m
public static JsonArray getJsonArray(JsonObject json, String memberName) {
    if (json.has(memberName)) {
        return getJsonArray(json.get(memberName), memberName);
    } else {
        throw new JsonSyntaxException("Missing " + memberName + ", expected to find a JsonArray");
    }
}

From source file:net.daporkchop.toobeetooteebot.text.JsonUtils.java

License:Open Source License

public static <T> T deserializeClass(JsonElement json, String memberName, JsonDeserializationContext context,
        Class<? extends T> adapter) {
    if (json != null) {
        return (T) context.deserialize(json, adapter);
    } else {/*from  ww  w  .ja  v a  2  s . c  o m*/
        throw new JsonSyntaxException("Missing " + memberName);
    }
}

From source file:net.daporkchop.toobeetooteebot.text.JsonUtils.java

License:Open Source License

public static <T> T deserializeClass(JsonObject json, String memberName, JsonDeserializationContext context,
        Class<? extends T> adapter) {
    if (json.has(memberName)) {
        return deserializeClass(json.get(memberName), memberName, context, adapter);
    } else {//from w w w . jav a 2 s .c  o m
        throw new JsonSyntaxException("Missing " + memberName);
    }
}

From source file:net.minecraftforge.common.crafting.CraftingHelper.java

License:Open Source License

@Nonnull
public static Ingredient getIngredient(JsonElement json, JsonContext context) {
    if (json == null || json.isJsonNull())
        throw new JsonSyntaxException("Json cannot be null");
    if (context == null)
        throw new IllegalArgumentException("getIngredient Context cannot be null");

    if (json.isJsonArray()) {
        List<Ingredient> ingredients = Lists.newArrayList();
        List<Ingredient> vanilla = Lists.newArrayList();
        json.getAsJsonArray().forEach((ele) -> {
            Ingredient ing = CraftingHelper.getIngredient(ele, context);

            if (ing.getClass() == Ingredient.class) {
                //Vanilla, Due to how we read it splits each itemstack, so we pull out to re-merge later
                vanilla.add(ing);/*from   w w w  .  j  a v a 2 s.co m*/
            } else {
                ingredients.add(ing);
            }
        });

        if (!vanilla.isEmpty()) {
            ingredients.add(Ingredient.merge(vanilla));
        }

        if (ingredients.size() == 0)
            throw new JsonSyntaxException("Item array cannot be empty, at least one item must be defined");

        if (ingredients.size() == 1)
            return ingredients.get(0);

        return new CompoundIngredient(ingredients);
    }

    if (!json.isJsonObject())
        throw new JsonSyntaxException("Expcted ingredient to be a object or array of objects");

    JsonObject obj = (JsonObject) json;

    String type = context.appendModId(JsonUtils.getString(obj, "type", "minecraft:item"));
    if (type.isEmpty())
        throw new JsonSyntaxException("Ingredient type can not be an empty string");

    if (type.equals("minecraft:item")) {
        String item = JsonUtils.getString(obj, "item");
        if (item.startsWith("#")) {
            Ingredient constant = context.getConstant(item.substring(1));
            if (constant == null)
                throw new JsonSyntaxException("Ingredient referenced invalid constant: " + item);
            return constant;
        }
    }

    IIngredientFactory factory = ingredients.get(new ResourceLocation(type));
    if (factory == null)
        throw new JsonSyntaxException("Unknown ingredient type: " + type);

    return factory.parse(context, obj);
}

From source file:net.minecraftforge.common.crafting.CraftingHelper.java

License:Open Source License

public static ItemStack getItemStack(JsonObject json, JsonContext context) {
    String itemName = context.appendModId(JsonUtils.getString(json, "item"));

    Item item = ForgeRegistries.ITEMS.getValue(new ResourceLocation(itemName));

    if (item == null)
        throw new JsonSyntaxException("Unknown item '" + itemName + "'");

    if (item.getHasSubtypes() && !json.has("data"))
        throw new JsonParseException("Missing data for item '" + itemName + "'");

    if (json.has("nbt")) {
        // Lets hope this works? Needs test
        try {/*  w  ww  . j  a  va  2  s . c  o m*/
            JsonElement element = json.get("nbt");
            NBTTagCompound nbt;
            if (element.isJsonObject())
                nbt = JsonToNBT.getTagFromJson(GSON.toJson(element));
            else
                nbt = JsonToNBT.getTagFromJson(element.getAsString());

            NBTTagCompound tmp = new NBTTagCompound();
            if (nbt.hasKey("ForgeCaps")) {
                tmp.setTag("ForgeCaps", nbt.getTag("ForgeCaps"));
                nbt.removeTag("ForgeCaps");
            }

            tmp.setTag("tag", nbt);
            tmp.setString("id", itemName);
            tmp.setInteger("Count", JsonUtils.getInt(json, "count", 1));
            tmp.setInteger("Damage", JsonUtils.getInt(json, "data", 0));

            return new ItemStack(tmp);
        } catch (NBTException e) {
            throw new JsonSyntaxException("Invalid NBT Entry: " + e.toString());
        }
    }

    return new ItemStack(item, JsonUtils.getInt(json, "count", 1), JsonUtils.getInt(json, "data", 0));
}

From source file:net.minecraftforge.common.crafting.CraftingHelper.java

License:Open Source License

public static ItemStack getItemStackBasic(JsonObject json, JsonContext context) {
    String itemName = context.appendModId(JsonUtils.getString(json, "item"));

    Item item = ForgeRegistries.ITEMS.getValue(new ResourceLocation(itemName));

    if (item == null)
        throw new JsonSyntaxException("Unknown item '" + itemName + "'");

    if (item.getHasSubtypes() && !json.has("data"))
        throw new JsonParseException("Missing data for item '" + itemName + "'");

    return new ItemStack(item, 1, JsonUtils.getInt(json, "data", 0));
}

From source file:net.minecraftforge.common.crafting.CraftingHelper.java

License:Open Source License

public static ShapedPrimer parseShaped(Object... recipe) {
    ShapedPrimer ret = new ShapedPrimer();
    String shape = "";
    int idx = 0;/*from   w w w. j av  a 2 s .  c o m*/

    if (recipe[idx] instanceof Boolean) {
        ret.mirrored = (Boolean) recipe[idx];
        if (recipe[idx + 1] instanceof Object[])
            recipe = (Object[]) recipe[idx + 1];
        else
            idx = 1;
    }

    if (recipe[idx] instanceof String[]) {
        String[] parts = ((String[]) recipe[idx++]);

        for (String s : parts) {
            ret.width = s.length();
            shape += s;
        }

        ret.height = parts.length;
    } else {
        while (recipe[idx] instanceof String) {
            String s = (String) recipe[idx++];
            shape += s;
            ret.width = s.length();
            ret.height++;
        }
    }

    if (ret.width * ret.height != shape.length() || shape.length() == 0) {
        String err = "Invalid shaped recipe: ";
        for (Object tmp : recipe) {
            err += tmp + ", ";
        }
        throw new RuntimeException(err);
    }

    HashMap<Character, Ingredient> itemMap = Maps.newHashMap();
    itemMap.put(' ', Ingredient.EMPTY);

    for (; idx < recipe.length; idx += 2) {
        Character chr = (Character) recipe[idx];
        Object in = recipe[idx + 1];
        Ingredient ing = CraftingHelper.getIngredient(in);

        if (' ' == chr.charValue())
            throw new JsonSyntaxException("Invalid key entry: ' ' is a reserved symbol.");

        if (ing != null) {
            itemMap.put(chr, ing);
        } else {
            String err = "Invalid shaped ore recipe: ";
            for (Object tmp : recipe) {
                err += tmp + ", ";
            }
            throw new RuntimeException(err);
        }
    }

    ret.input = NonNullList.withSize(ret.width * ret.height, Ingredient.EMPTY);

    Set<Character> keys = Sets.newHashSet(itemMap.keySet());
    keys.remove(' ');

    int x = 0;
    for (char chr : shape.toCharArray()) {
        Ingredient ing = itemMap.get(chr);
        if (ing == null)
            throw new IllegalArgumentException(
                    "Pattern references symbol '" + chr + "' but it's not defined in the key");
        ret.input.set(x++, ing);
        keys.remove(chr);
    }

    if (!keys.isEmpty())
        throw new IllegalArgumentException("Key defines symbols that aren't used in pattern: " + keys);

    return ret;
}

From source file:net.minecraftforge.common.crafting.CraftingHelper.java

License:Open Source License

public static boolean processConditions(JsonArray conditions, JsonContext context) {
    for (int x = 0; x < conditions.size(); x++) {
        if (!conditions.get(x).isJsonObject())
            throw new JsonSyntaxException("Conditions must be an array of JsonObjects");

        JsonObject json = conditions.get(x).getAsJsonObject();
        BooleanSupplier cond = CraftingHelper.getCondition(json, context);
        if (!cond.getAsBoolean())
            return false;
    }//from  w w  w. j  a  v a  2s  . c  o m
    return true;
}

From source file:net.minecraftforge.common.crafting.CraftingHelper.java

License:Open Source License

public static BooleanSupplier getCondition(JsonObject json, JsonContext context) {
    ResourceLocation type = new ResourceLocation(context.appendModId(JsonUtils.getString(json, "type")));
    IConditionFactory factory = conditions.get(type);
    if (factory == null)
        throw new JsonSyntaxException("Unknown condition type: " + type.toString());
    return factory.parse(context, json);
}

From source file:net.minecraftforge.common.crafting.CraftingHelper.java

License:Open Source License

public static IRecipe getRecipe(JsonObject json, JsonContext context) {
    if (json == null || json.isJsonNull())
        throw new JsonSyntaxException("Json cannot be null");
    if (context == null)
        throw new IllegalArgumentException("getRecipe Context cannot be null");

    String type = context.appendModId(JsonUtils.getString(json, "type"));
    if (type.isEmpty())
        throw new JsonSyntaxException("Recipe type can not be an empty string");

    IRecipeFactory factory = recipes.get(new ResourceLocation(type));
    if (factory == null)
        throw new JsonSyntaxException("Unknown recipe type: " + type);

    return factory.parse(context, json);
}