Example #1
0
 public String prettyJson(String json)
 {
     IDictionary<string, object> obj = gson.fromJson<Dictionary<string, object>>(json, typeof(Dictionary<string, object>));
     return gson.toJson(obj);
 }
    public void EntityPlayerDatasToSave(EntityPlayer player)
    {
        PlayerDatas entityDatasToSave = new PlayerDatas();

        // Datas for identifying the player
        entityDatasToSave.setplayerDataToSaveName(player.getEntityName());
        entityDatasToSave.setplayerDataToSaveClass(player.getEntityClass());
        entityDatasToSave.setplayerDataToSaveRace(player.getEntityRace());
        entityDatasToSave.setplayerDataToSavePortrait(player.getEntityPortrait());
        entityDatasToSave.setplayerDataToSaveLevel(player.getEntityLevel());

        // Datas for checking if the player is drunk or not
        entityDatasToSave.setplayerDataToSaveAlcoholLevel(player.getEntityAlcoholLevel());
        entityDatasToSave.setplayerDataToSaveAlcoholResistance(player.getEntityAlcoholResistance());

        // Datas for positionning the player in the game world
        entityDatasToSave.setplayerDataToSaveMap(player.getEntityMap());
        entityDatasToSave.setplayerDataToSaveXPosition(player.getEntityXPosition());
        entityDatasToSave.setplayerDataToSaveYPosition(player.getEntityYPosition());

        // Datas for battle
        entityDatasToSave.setplayerDataToSaveAttack(player.getEntityAttack());
        entityDatasToSave.setplayerDataToSaveDefense(player.getEntityDefense());
        entityDatasToSave.setplayerDataToSaveCurrentHealth(player.getEntityCurrentHealth());
        entityDatasToSave.setplayerDataToSaveTotalHealth(player.getEntityTotalHealth());

        // Datas for experience status
        entityDatasToSave.setplayerDataToSaveCurrentXp(player.getEntityCurrentXp());
        entityDatasToSave.setplayerDataToSaveTotalXp(player.getEntityTotalXp());

        entityDatasToSave.setplayerDataToSaveGold(player.getEntityGold());

        // Data to get the player state (Drunk, sleeping...)
        entityDatasToSave.setplayerDataToSaveState(player.getEntityState());

        // --------------------------------------------------------------
        // | The method in comment below will only save non empty datas |
        // --------------------------------------------------------------
        // [START]
        //Gson gson = new GsonBuilder().setPrettyPrinting().create();
        //String jsonToSave = json.prettyPrint(entityDatasToSave);
        // [END]

        // ----------------------------------------------------------------------------------------
        // | The method in comment below will save all datas from a character even the empty ones |
        // ----------------------------------------------------------------------------------------
        // [START]
        Gson   gson       = new Gson();
        String jsonToSave = gson.toJson(entityDatasToSave);
        // [END]

        Json json = new Json();

        json.setOutputType(OutputType.json);

        // saving datas in a new JSON file with the name of the player character
        try
        {
            SaveGame.saveFile(entityDatasToSave.getplayerDataToSaveName(), jsonToSave, "character");
        }
        catch (IOException e)
        {
            Console.WriteLine("Error: error when creating character file");
            e.printStackTrace();
        }
    }