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

项目:Ipsum    文件:FireworkEffectAdapter.java   
public static JsonObject toJson(FireworkEffect fireworkEffect) {
    if (fireworkEffect == null) return null;

    JsonObject ret = new JsonObject();

    ret.addProperty(FLICKER, fireworkEffect.hasFlicker());
    ret.addProperty(TRAIL, fireworkEffect.hasTrail());
    ret.add(COLORS, fromColorCollection(fireworkEffect.getColors()));
    ret.add(FADE_COLORS, fromColorCollection(fireworkEffect.getFadeColors()));
    ret.addProperty(TYPE, fireworkEffect.getType().name());

    return ret;
}
项目:Ipsum    文件:PotionEffectAdapter.java   
public static JsonObject toJson(PotionEffect potionEffect) {
    if (potionEffect == null) return null;

    JsonObject ret = new JsonObject();

    ret.addProperty(POTION_EFFECT_ID, potionEffect.getType().getId());
    ret.addProperty(POTION_DURATION, potionEffect.getDuration());
    ret.addProperty(POTION_AMPLIFIER, potionEffect.getAmplifier());
    ret.addProperty(POTION_AMBIENT, potionEffect.isAmbient());

    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    文件:BaseComponentSerializer.java   
protected void serialize(JsonObject object, BaseComponent component, JsonSerializationContext context) {
    boolean first = false;
    if (ComponentSerializer.serializedComponents.get() == null) {
        first = true;
        ComponentSerializer.serializedComponents.set(new HashSet<BaseComponent>());
    }
    try {
        Preconditions.checkArgument(!ComponentSerializer.serializedComponents.get().contains(component), "Component loop");
        ComponentSerializer.serializedComponents.get().add(component);
        if (component.getColorRaw() != null) {
            object.addProperty("color", component.getColorRaw().name());
        }
        if (component.isBoldRaw() != null) {
            object.addProperty("bold", component.isBoldRaw());
        }
        if (component.isItalicRaw() != null) {
            object.addProperty("italic", component.isItalicRaw());
        }
        if (component.isUnderlinedRaw() != null) {
            object.addProperty("underlined", component.isUnderlinedRaw());
        }
        if (component.isStrikethroughRaw() != null) {
            object.addProperty("strikethrough", component.isStrikethroughRaw());
        }
        if (component.isObfuscatedRaw() != null) {
            object.addProperty("obfuscated", component.isObfuscatedRaw());
        }

        if (component.getExtra() != null) {
            object.add("extra", context.serialize(component.getExtra()));
        }

        // Events
        if (component.getClickEvent() != null) {
            JsonObject clickEvent = new JsonObject();
            clickEvent.addProperty("action", component.getClickEvent().getAction().toString().toLowerCase());
            clickEvent.addProperty("value", component.getClickEvent().getValue());
            object.add("clickEvent", clickEvent);
        }
        if (component.getHoverEvent() != null) {
            JsonObject hoverEvent = new JsonObject();
            hoverEvent.addProperty("action", component.getHoverEvent().getAction().toString().toLowerCase());
            hoverEvent.add("value", context.serialize(component.getHoverEvent().getValue()));
            object.add("hoverEvent", hoverEvent);
        }
    } finally {
        ComponentSerializer.serializedComponents.get().remove(component);
        if (first) {
            ComponentSerializer.serializedComponents.set(null);
        }
    }
}
项目: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;
}