us.myles.ViaVersion.util.GsonUtil Java Examples

The following examples show how to use us.myles.ViaVersion.util.GsonUtil. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VRPlatform.java    From ViaFabric with MIT License 6 votes vote down vote up
@Override
public JsonObject getDump() {
    JsonObject platformSpecific = new JsonObject();
    List<PluginInfo> mods = new ArrayList<>();
    for (ModContainer mod : FabricLoader.getInstance().getAllMods()) {
        mods.add(new PluginInfo(true,
                mod.getMetadata().getId() + " (" + mod.getMetadata().getName() + ")",
                mod.getMetadata().getVersion().getFriendlyString(),
                null,
                mod.getMetadata().getAuthors().stream()
                        .map(info -> info.getName()
                                + (info.getContact().asMap().isEmpty() ? "" : " " + info.getContact().asMap()))
                        .collect(Collectors.toList())
        ));
    }

    platformSpecific.add("mods", GsonUtil.getGson().toJsonTree(mods));
    return platformSpecific;
}
 
Example #2
Source File: BungeePlugin.java    From ViaVersion with MIT License 6 votes vote down vote up
@Override
public JsonObject getDump() {
    JsonObject platformSpecific = new JsonObject();

    List<PluginInfo> plugins = new ArrayList<>();
    for (Plugin p : ProxyServer.getInstance().getPluginManager().getPlugins())
        plugins.add(new PluginInfo(
                true,
                p.getDescription().getName(),
                p.getDescription().getVersion(),
                p.getDescription().getMain(),
                Collections.singletonList(p.getDescription().getAuthor())
        ));

    platformSpecific.add("plugins", GsonUtil.getGson().toJsonTree(plugins));
    platformSpecific.add("servers", GsonUtil.getGson().toJsonTree(ProtocolDetectorService.getDetectedIds()));
    return platformSpecific;
}
 
Example #3
Source File: SpongePlugin.java    From ViaVersion with MIT License 6 votes vote down vote up
@Override
public JsonObject getDump() {
    JsonObject platformSpecific = new JsonObject();

    List<PluginInfo> plugins = new ArrayList<>();
    for (PluginContainer p : game.getPluginManager().getPlugins()) {
        plugins.add(new PluginInfo(
                true,
                p.getName(),
                p.getVersion().orElse("Unknown Version"),
                p.getInstance().isPresent() ? p.getInstance().get().getClass().getCanonicalName() : "Unknown",
                p.getAuthors()
        ));
    }
    platformSpecific.add("plugins", GsonUtil.getGson().toJsonTree(plugins));

    return platformSpecific;
}
 
Example #4
Source File: VelocityPlugin.java    From ViaVersion with MIT License 6 votes vote down vote up
@Override
public JsonObject getDump() {
    JsonObject extra = new JsonObject();
    List<PluginInfo> plugins = new ArrayList<>();
    for (PluginContainer p : PROXY.getPluginManager().getPlugins()) {
        plugins.add(new PluginInfo(
                true,
                p.getDescription().getName().orElse(p.getDescription().getId()),
                p.getDescription().getVersion().orElse("Unknown Version"),
                p.getInstance().isPresent() ? p.getInstance().get().getClass().getCanonicalName() : "Unknown",
                p.getDescription().getAuthors()
        ));
    }
    extra.add("plugins", GsonUtil.getGson().toJsonTree(plugins));
    extra.add("servers", GsonUtil.getGson().toJsonTree(ProtocolDetectorService.getDetectedIds()));
    return extra;
}
 
Example #5
Source File: MappingDataLoader.java    From ViaVersion with MIT License 6 votes vote down vote up
/**
 * Loads the file from the bundled resources. Uses the cache if enabled.
 *
 * @param cacheIfEnabled whether loaded files should be cached
 */
public static JsonObject loadData(String name, boolean cacheIfEnabled) {
    if (cacheJsonMappings) {
        JsonObject cached = MAPPINGS_CACHE.get(name);
        if (cached != null) {
            return cached;
        }
    }

    InputStream stream = getResource(name);
    InputStreamReader reader = new InputStreamReader(stream);
    try {
        JsonObject object = GsonUtil.getGson().fromJson(reader, JsonObject.class);
        if (cacheIfEnabled && cacheJsonMappings) {
            MAPPINGS_CACHE.put(name, object);
        }
        return object;
    } finally {
        try {
            reader.close();
        } catch (IOException ignored) {
            // Ignored
        }
    }
}
 
Example #6
Source File: Protocol1_9To1_8.java    From ViaVersion with MIT License 6 votes vote down vote up
public static JsonElement fixJson(String line) {
    if (line == null || line.equalsIgnoreCase("null")) {
        line = "{\"text\":\"\"}";
    } else {
        if ((!line.startsWith("\"") || !line.endsWith("\"")) && (!line.startsWith("{") || !line.endsWith("}"))) {
            return constructJson(line);
        }
        if (line.startsWith("\"") && line.endsWith("\"")) {
            line = "{\"text\":" + line + "}";
        }
    }
    try {
        return GsonUtil.getGson().fromJson(line, JsonObject.class);
    } catch (Exception e) {
        if (Via.getConfig().isForceJsonTransform()) {
            return constructJson(line);
        } else {
            Via.getPlatform().getLogger().warning("Invalid JSON String: \"" + line + "\" Please report this issue to the ViaVersion Github: " + e.getMessage());
            return GsonUtil.getGson().fromJson("{\"text\":\"\"}", JsonObject.class);
        }
    }
}
 
Example #7
Source File: RecipeData.java    From ViaVersion with MIT License 6 votes vote down vote up
public static void init() {
    InputStream stream = MappingData.class.getClassLoader()
            .getResourceAsStream("assets/viaversion/data/itemrecipes1_12_2to1_13.json");
    InputStreamReader reader = new InputStreamReader(stream);
    try {
        recipes = GsonUtil.getGson().fromJson(
                reader,
                new TypeToken<Map<String, Recipe>>() {
                }.getType()
        );
    } finally {
        try {
            reader.close();
        } catch (IOException ignored) {
            // Ignored
        }
    }
}
 
Example #8
Source File: ViaVersionPlugin.java    From ViaVersion with MIT License 5 votes vote down vote up
@Override
public JsonObject getDump() {
    JsonObject platformSpecific = new JsonObject();

    List<PluginInfo> plugins = new ArrayList<>();
    for (Plugin p : Bukkit.getPluginManager().getPlugins())
        plugins.add(new PluginInfo(p.isEnabled(), p.getDescription().getName(), p.getDescription().getVersion(), p.getDescription().getMain(), p.getDescription().getAuthors()));

    platformSpecific.add("plugins", GsonUtil.getGson().toJsonTree(plugins));
    // TODO more? ProtocolLib things etc?

    return platformSpecific;
}
 
Example #9
Source File: CommandBlockHandler.java    From ViaVersion with MIT License 5 votes vote down vote up
@Override
public int transform(UserConnection user, CompoundTag tag) {
    Tag name = tag.get("CustomName");
    if (name instanceof StringTag) {
        ((StringTag) name).setValue(ChatRewriter.legacyTextToJson(((StringTag) name).getValue()).toString());
    }
    Tag out = tag.get("LastOutput");
    if (out instanceof StringTag) {
        JsonElement value = GsonUtil.getJsonParser().parse(((StringTag) out).getValue());
        ChatRewriter.processTranslate(value);
        ((StringTag) out).setValue(value.toString());
    }
    return -1;
}
 
Example #10
Source File: VBMappingDataLoader.java    From ViaBackwards with MIT License 5 votes vote down vote up
public static JsonObject loadData(String name) {
    InputStream stream = VBMappingDataLoader.class.getClassLoader().getResourceAsStream("assets/viabackwards/data/" + name);
    try (InputStreamReader reader = new InputStreamReader(stream)) {
        return GsonUtil.getGson().fromJson(reader, JsonObject.class);
    } catch (IOException e) {
        return null;
    }
}
 
Example #11
Source File: ComponentRewriter.java    From ViaVersion with MIT License 4 votes vote down vote up
public JsonElement processText(String value) {
    JsonElement root = GsonUtil.getJsonParser().parse(value);
    processText(root);
    return root;
}
 
Example #12
Source File: ComponentType.java    From ViaVersion with MIT License 4 votes vote down vote up
@Override
public JsonElement read(ByteBuf buffer) throws Exception {
    return GsonUtil.getJsonParser().parse(STRING_TAG.read(buffer));
}
 
Example #13
Source File: MappingData.java    From ViaVersion with MIT License 4 votes vote down vote up
public static void init() {
    Via.getPlatform().getLogger().info("Loading 1.12.2 -> 1.13 mappings...");
    JsonObject mapping1_12 = MappingDataLoader.loadData("mapping-1.12.json", true);
    JsonObject mapping1_13 = MappingDataLoader.loadData("mapping-1.13.json", true);

    oldToNewItems.defaultReturnValue(-1);
    blockMappings = new BlockMappingsShortArray(mapping1_12.getAsJsonObject("blocks"), mapping1_13.getAsJsonObject("blocks"));
    MappingDataLoader.mapIdentifiers(oldToNewItems, mapping1_12.getAsJsonObject("items"), mapping1_13.getAsJsonObject("items"));
    loadTags(blockTags, mapping1_13.getAsJsonObject("block_tags"));
    loadTags(itemTags, mapping1_13.getAsJsonObject("item_tags"));
    loadTags(fluidTags, mapping1_13.getAsJsonObject("fluid_tags"));

    loadEnchantments(oldEnchantmentsIds, mapping1_12.getAsJsonObject("enchantments"));
    enchantmentMappings = new Mappings(72, mapping1_12.getAsJsonObject("enchantments"), mapping1_13.getAsJsonObject("enchantments"));
    soundMappings = new Mappings(662, mapping1_12.getAsJsonArray("sounds"), mapping1_13.getAsJsonArray("sounds"));

    JsonObject object = MappingDataLoader.loadFromDataDir("channelmappings-1.13.json");
    if (object != null) {
        for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
            String oldChannel = entry.getKey();
            String newChannel = entry.getValue().getAsString();
            if (!isValid1_13Channel(newChannel)) {
                Via.getPlatform().getLogger().warning("Channel '" + newChannel + "' is not a valid 1.13 plugin channel, please check your configuration!");
                continue;
            }
            channelMappings.put(oldChannel, newChannel);
        }
    }

    Map<String, String> translateData = GsonUtil.getGson().fromJson(
            new InputStreamReader(MappingData.class.getClassLoader().getResourceAsStream("assets/viaversion/data/mapping-lang-1.12-1.13.json")),
            new TypeToken<Map<String, String>>() {
            }.getType());
    try {
        String[] lines;
        try (Reader reader = new InputStreamReader(MappingData.class.getClassLoader()
                .getResourceAsStream("mojang-translations/en_US.properties"), StandardCharsets.UTF_8)) {
            lines = CharStreams.toString(reader).split("\n");
        }
        for (String line : lines) {
            if (line.isEmpty()) continue;

            String[] keyAndTranslation = line.split("=", 2);
            if (keyAndTranslation.length != 2) continue;

            String key = keyAndTranslation[0];
            if (!translateData.containsKey(key)) {
                String translation = keyAndTranslation[1].replaceAll("%(\\d\\$)?d", "%$1s");
                mojangTranslation.put(key, translation);
            } else {
                String dataValue = translateData.get(key);
                if (dataValue != null) {
                    translateMapping.put(key, dataValue);
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}