List of usage examples for com.google.gson JsonParseException JsonParseException
public JsonParseException(Throwable cause)
From source file:net.kyori.text.serializer.gson.BlockNbtComponentPosSerializer.java
License:MIT License
@Override public BlockNbtComponent.Pos deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { final String string = json.getAsString(); final Matcher localMatch = LOCAL_PATTERN.matcher(string); if (localMatch.matches()) { return BlockNbtComponent.LocalPos.of(Double.parseDouble(localMatch.group(1)), Double.parseDouble(localMatch.group(3)), Double.parseDouble(localMatch.group(5))); }/*from w w w. j av a 2 s . co m*/ final Matcher worldMatch = WORLD_PATTERN.matcher(string); if (worldMatch.matches()) { return BlockNbtComponent.WorldPos.of(deserializeCoordinate(worldMatch.group(1), worldMatch.group(2)), deserializeCoordinate(worldMatch.group(3), worldMatch.group(4)), deserializeCoordinate(worldMatch.group(5), worldMatch.group(6))); } throw new JsonParseException("Don't know how to turn " + string + " into a Position"); }
From source file:net.kyori.text.serializer.gson.GsonComponentSerializer.java
License:MIT License
private BuildableComponent<?, ?> deserialize0(final JsonElement element, final JsonDeserializationContext context) throws JsonParseException { if (element.isJsonPrimitive()) { return TextComponent.of(element.getAsString()); } else if (element.isJsonArray()) { ComponentBuilder<?, ?> parent = null; for (final JsonElement childElement : element.getAsJsonArray()) { final BuildableComponent<?, ?> child = this.deserialize0(childElement, context); if (parent == null) { parent = child.toBuilder(); } else { parent.append(child);/* ww w. j av a 2s . co m*/ } } if (parent == null) { throw new JsonParseException("Don't know how to turn " + element + " into a Component"); } return parent.build(); } else if (!element.isJsonObject()) { throw new JsonParseException("Don't know how to turn " + element + " into a Component"); } final JsonObject object = element.getAsJsonObject(); final ComponentBuilder<?, ?> component; if (object.has(TEXT)) { component = TextComponent.builder(object.get(TEXT).getAsString()); } else if (object.has(TRANSLATE)) { final String key = object.get(TRANSLATE).getAsString(); if (!object.has(TRANSLATE_WITH)) { component = TranslatableComponent.builder(key); } else { final JsonArray with = object.getAsJsonArray(TRANSLATE_WITH); final List<Component> args = new ArrayList<>(with.size()); for (int i = 0, size = with.size(); i < size; i++) { final JsonElement argElement = with.get(i); args.add(this.deserialize0(argElement, context)); } component = TranslatableComponent.builder(key).args(args); } } else if (object.has(SCORE)) { final JsonObject score = object.getAsJsonObject(SCORE); if (!score.has(SCORE_NAME) || !score.has(SCORE_OBJECTIVE)) { throw new JsonParseException( "A score component requires a " + SCORE_NAME + " and " + SCORE_OBJECTIVE); } // score components can have a value sometimes, let's grab it if (score.has(SCORE_VALUE)) { component = ScoreComponent.builder().name(score.get(SCORE_NAME).getAsString()) .objective(score.get(SCORE_OBJECTIVE).getAsString()) .value(score.get(SCORE_VALUE).getAsString()); } else { component = ScoreComponent.builder().name(score.get(SCORE_NAME).getAsString()) .objective(score.get(SCORE_OBJECTIVE).getAsString()); } } else if (object.has(SELECTOR)) { component = SelectorComponent.builder().pattern(object.get(SELECTOR).getAsString()); } else if (object.has(KEYBIND)) { component = KeybindComponent.builder().keybind(object.get(KEYBIND).getAsString()); } else if (object.has(NBT)) { final String nbt = object.get(NBT).getAsString(); final boolean interpret = object.has(NBT_INTERPRET) && object.getAsJsonPrimitive(NBT_INTERPRET).getAsBoolean(); if (object.has(NBT_BLOCK)) { final BlockNbtComponent.Pos position = context.deserialize(object.get(NBT_BLOCK), BlockNbtComponent.Pos.class); component = BlockNbtComponent.builder().nbtPath(nbt).interpret(interpret).pos(position); } else if (object.has(NBT_ENTITY)) { component = EntityNbtComponent.builder().nbtPath(nbt).interpret(interpret) .selector(object.get(NBT_ENTITY).getAsString()); } else { throw notSureHowToDeserialize(element); } } else { throw notSureHowToDeserialize(element); } if (object.has(EXTRA)) { final JsonArray extra = object.getAsJsonArray(EXTRA); for (int i = 0, size = extra.size(); i < size; i++) { final JsonElement extraElement = extra.get(i); component.append(this.deserialize0(extraElement, context)); } } final Style style = context.deserialize(element, Style.class); if (!style.isEmpty()) { component.style(style); } return component.build(); }
From source file:net.kyori.text.serializer.gson.GsonComponentSerializer.java
License:MIT License
private static JsonParseException notSureHowToDeserialize(final JsonElement element) { return new JsonParseException("Don't know how to turn " + element + " into a Component"); }
From source file:net.kyori.text.serializer.gson.NameMapSerializer.java
License:MIT License
@Override public T deserialize(final JsonElement json, final Type type, final JsonDeserializationContext context) throws JsonParseException { final String string = json.getAsString(); return this.map.get(string) .orElseThrow(() -> new JsonParseException("invalid " + this.name + ": " + string)); }
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 {//from w w w . jav a 2 s . com 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
private static void init() { conditions.clear();/*w w w.j a v a 2 s .c o m*/ ingredients.clear(); recipes.clear(); registerC("forge:mod_loaded", (context, json) -> { String modid = JsonUtils.getString(json, "modid"); return () -> Loader.isModLoaded(modid); }); registerC("minecraft:item_exists", (context, json) -> { String itemName = context.appendModId(JsonUtils.getString(json, "item")); return () -> ForgeRegistries.ITEMS.containsKey(new ResourceLocation(itemName)); }); registerC("forge:not", (context, json) -> { BooleanSupplier child = CraftingHelper.getCondition(JsonUtils.getJsonObject(json, "value"), context); return () -> !child.getAsBoolean(); }); registerC("forge:or", (context, json) -> { JsonArray values = JsonUtils.getJsonArray(json, "values"); List<BooleanSupplier> children = Lists.newArrayList(); for (JsonElement j : values) { if (!j.isJsonObject()) throw new JsonSyntaxException("Or condition values must be an array of JsonObjects"); children.add(CraftingHelper.getCondition(j.getAsJsonObject(), context)); } return () -> children.stream().anyMatch(BooleanSupplier::getAsBoolean); }); registerC("forge:and", (context, json) -> { JsonArray values = JsonUtils.getJsonArray(json, "values"); List<BooleanSupplier> children = Lists.newArrayList(); for (JsonElement j : values) { if (!j.isJsonObject()) throw new JsonSyntaxException("And condition values must be an array of JsonObjects"); children.add(CraftingHelper.getCondition(j.getAsJsonObject(), context)); } return () -> children.stream().allMatch(c -> c.getAsBoolean()); }); registerC("forge:false", (context, json) -> { return () -> false; }); registerR("minecraft:crafting_shaped", (context, json) -> { String group = JsonUtils.getString(json, "group", ""); //if (!group.isEmpty() && group.indexOf(':') == -1) // group = context.getModId() + ":" + group; Map<Character, Ingredient> ingMap = Maps.newHashMap(); for (Entry<String, JsonElement> entry : JsonUtils.getJsonObject(json, "key").entrySet()) { if (entry.getKey().length() != 1) throw new JsonSyntaxException("Invalid key entry: '" + entry.getKey() + "' is an invalid symbol (must be 1 character only)."); if (" ".equals(entry.getKey())) throw new JsonSyntaxException("Invalid key entry: ' ' is a reserved symbol."); ingMap.put(entry.getKey().toCharArray()[0], CraftingHelper.getIngredient(entry.getValue(), context)); } ingMap.put(' ', Ingredient.EMPTY); JsonArray patternJ = JsonUtils.getJsonArray(json, "pattern"); if (patternJ.size() == 0) throw new JsonSyntaxException("Invalid pattern: empty pattern not allowed"); if (patternJ.size() > 3) throw new JsonSyntaxException("Invalid pattern: too many rows, 3 is maximum"); String[] pattern = new String[patternJ.size()]; for (int x = 0; x < pattern.length; ++x) { String line = JsonUtils.getString(patternJ.get(x), "pattern[" + x + "]"); if (line.length() > 3) throw new JsonSyntaxException("Invalid pattern: too many columns, 3 is maximum"); if (x > 0 && pattern[0].length() != line.length()) throw new JsonSyntaxException("Invalid pattern: each row must be the same width"); pattern[x] = line; } NonNullList<Ingredient> input = NonNullList.withSize(pattern[0].length() * pattern.length, Ingredient.EMPTY); Set<Character> keys = Sets.newHashSet(ingMap.keySet()); keys.remove(' '); int x = 0; for (String line : pattern) { for (char chr : line.toCharArray()) { Ingredient ing = ingMap.get(chr); if (ing == null) throw new JsonSyntaxException( "Pattern references symbol '" + chr + "' but it's not defined in the key"); input.set(x++, ing); keys.remove(chr); } } if (!keys.isEmpty()) throw new JsonSyntaxException("Key defines symbols that aren't used in pattern: " + keys); ItemStack result = CraftingHelper.getItemStack(JsonUtils.getJsonObject(json, "result"), context); return new ShapedRecipes(group, pattern[0].length(), pattern.length, input, result); }); registerR("minecraft:crafting_shapeless", (context, json) -> { String group = JsonUtils.getString(json, "group", ""); NonNullList<Ingredient> ings = NonNullList.create(); for (JsonElement ele : JsonUtils.getJsonArray(json, "ingredients")) ings.add(CraftingHelper.getIngredient(ele, context)); if (ings.isEmpty()) throw new JsonParseException("No ingredients for shapeless recipe"); if (ings.size() > 9) throw new JsonParseException("Too many ingredients for shapeless recipe"); ItemStack itemstack = CraftingHelper.getItemStack(JsonUtils.getJsonObject(json, "result"), context); return new ShapelessRecipes(group, itemstack, ings); }); registerR("forge:ore_shaped", ShapedOreRecipe::factory); registerR("forge:ore_shapeless", ShapelessOreRecipe::factory); registerI("minecraft:item", (context, json) -> Ingredient.fromStacks(CraftingHelper.getItemStackBasic(json, context))); registerI("minecraft:empty", (context, json) -> Ingredient.EMPTY); registerI("minecraft:item_nbt", (context, json) -> new IngredientNBT(CraftingHelper.getItemStack(json, context))); registerI("forge:ore_dict", (context, json) -> new OreIngredient(JsonUtils.getString(json, "ore"))); }
From source file:net.minecraftforge.common.ForgeHooks.java
License:Open Source License
private static LootTableContext getLootTableContext() { LootTableContext ctx = lootContext.get().peek(); if (ctx == null) throw new JsonParseException("Invalid call stack, could to grab json context!"); // Show I throw this? Do we care about custom deserializers outside the manager? return ctx;// www .j av a 2 s.co m }
From source file:net.minecraftforge.common.ForgeHooks.java
License:Open Source License
public static String readPoolName(JsonObject json) { LootTableContext ctx = ForgeHooks.getLootTableContext(); ctx.resetPoolCtx();/*from w ww. j a v a2 s. c om*/ if (json.has("name")) return JsonUtils.getString(json, "name"); if (ctx.custom) return "custom#" + json.hashCode(); //We don't care about custom ones modders shouldn't be editing them! ctx.poolCount++; if (!ctx.vanilla) throw new JsonParseException("Loot Table \"" + ctx.name.toString() + "\" Missing `name` entry for pool #" + (ctx.poolCount - 1)); return ctx.poolCount == 1 ? "main" : "pool" + (ctx.poolCount - 1); }
From source file:net.minecraftforge.common.model.animation.AnimationStateMachine.java
License:Open Source License
/** * post-loading initialization hook.//from w ww .j av a2s. com */ void initialize() { if (parameters == null) { throw new JsonParseException("Animation State Machine should contain \"parameters\" key."); } if (clips == null) { throw new JsonParseException("Animation State Machine should contain \"clips\" key."); } if (states == null) { throw new JsonParseException("Animation State Machine should contain \"states\" key."); } if (transitions == null) { throw new JsonParseException("Animation State Machine should contain \"transitions\" key."); } shouldHandleSpecialEvents = true; lastPollTime = Float.NEGATIVE_INFINITY; // setting the starting state IClip state = clips.get(startState); if (!clips.containsKey(startState) || !states.contains(startState)) { throw new IllegalStateException("unknown state: " + startState); } currentStateName = startState; currentState = state; }