arleypadua / PKHeX.Everywhere

Cross platform tools for interacting with Pokemon save files. The web version runs everywhere and the CLI works with Mac OS, Linux and Windows
https://pkhex-web.github.io
MIT License
29 stars 2 forks source link

Export to JSON #54

Closed YourBr0ther closed 2 months ago

YourBr0ther commented 2 months ago

Could you possible make it where you can export the pokemon as a json file? I have been looking everywhere for something that might do this. It would be so helpful if I could export a single pokemon to a json file to be fed into something I'm working on. I'm trying to create it myself, but I do not have the experience in this area or knowledge

arleypadua commented 2 months ago

I guess that for the roadmap of the standard web UI , exporting as a json, is a bit to specific, but that is definitely possible by writing plugins.

This is a feature that's not documented yet, but you can use the following steps to create one:

1 - Create a C# project 2 - Reference the PKHeX.Web.Plugins nuget package 3 - Add a plugin hook on the Pokemon edit page

public class ExportPokemonToJson : IPokemonEditAction
{
    public string Description => "Exports the Pokémon's data as JSON to the clipboard";
    public string Label => "Export as JSON";

    public Task<Outcome> OnActionRequested(Pokemon pokemon)
    {
        var pokemonData = new
        {
            Species = pokemon.Species.Name,
            Level = pokemon.Level,
            IsShiny = pokemon.IsShiny,
            Gender = pokemon.Gender.ToString()
        };

        var json = JsonSerializer.Serialize(pokemonData);

        return Outcome.CopyToClipboard(json).Completed();
    }
}

4 - Add a manifest

public class JsonOperationsPlugin : Settings
{
    public JsonOperationsPlugin() : base(Manifest)
    {
        EnabledByDefault<ExportPokemonToJson>();
    }

    private static readonly PlugInManifest Manifest = new PlugInManifest(
        "JsonOperations",
        "Provides JSON export functionality for Pokémon data"
    );
}

The dll generated out of this project would be then compatible with the web UI and because the hook is implemented with the interface IPokemonEditAction, the action will be available in the Pokemon edit page