Java 类org.bukkit.craftbukkit.libs.com.google.gson.JsonElement 实例源码

项目:Ipsum    文件:FireworkEffectAdapter.java   
public static List<Color> toColorCollection(JsonElement json) {
    JsonArray array = json.getAsJsonArray();
    List<Color> ret = new ArrayList<Color>();

    Iterator<JsonElement> iter = array.iterator();
    while (iter.hasNext()) {
        JsonElement element = iter.next();
        ret.add(Color.fromRGB(element.getAsInt()));
    }

    return ret;
}
项目:Ipsum    文件:PotionEffectAdapter.java   
public static PotionEffect fromJson(JsonElement jsonElement) {
    if (jsonElement == null) return null;
    if (!jsonElement.isJsonObject()) return null;

    JsonObject json = jsonElement.getAsJsonObject();

    PotionEffectType pet = PotionEffectType.getById(json.get(POTION_EFFECT_ID).getAsInt());

    int duration = POTION_DURATION_DEFAULT;
    JsonElement durationElement = json.get(POTION_DURATION);
    if (durationElement != null) {
        duration = durationElement.getAsInt();
    }

    int amplifier = POTION_AMPLIFIER_DEFAULT;
    JsonElement amplifierElement = json.get(POTION_AMPLIFIER);
    if (amplifierElement != null) {
        amplifier = amplifierElement.getAsInt();
    }

    boolean ambient = POTION_AMBIENT_DEFAULT;
    JsonElement ambientElement = json.get(POTION_AMBIENT);
    if (ambientElement != null) {
        ambient = ambientElement.getAsBoolean();
    }

    return new PotionEffect(pet, duration, amplifier, ambient);
}
项目:Ipsum    文件:FireworkEffectAdapter.java   
public static FireworkEffect fromJson(JsonElement jsonElement) {
    if (jsonElement == null) return null;
    if (!jsonElement.isJsonObject()) return null;

    JsonObject json = jsonElement.getAsJsonObject();

    boolean flicker = FLICKER_DEFAULT;
    boolean trail = TRAIL_DEFAULT;
    List<Color> colors = COLORS_DEFAULT;
    List<Color> fadeColors = FADE_COLORS_DEFAULT;
    Type type = TYPE_DEFAULT;

    JsonElement element;

    element = json.get(FLICKER);
    if (element != null) {
        flicker = element.getAsBoolean();
    }

    element = json.get(TRAIL);
    if (element != null) {
        trail = element.getAsBoolean();
    }

    element = json.get(COLORS);
    if (element != null) {
        colors = toColorCollection(element);
    }

    element = json.get(FADE_COLORS);
    if (element != null) {
        fadeColors = toColorCollection(element);
    }

    element = json.get(TYPE);
    if (element != null) {
        type = Type.valueOf(element.getAsString());
    }

    FireworkEffect ret = FireworkEffect.builder()
            .flicker(flicker)
            .trail(trail)
            .withColor(colors)
            .withFade(fadeColors)
            .with(type)
            .build();

    return ret;
}