textrpg.builtin.actions.Edit.java Source code

Java tutorial

Introduction

Here is the source code for textrpg.builtin.actions.Edit.java

Source

/*
 *    Copyright 2017 MrYurihi Redstone
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package textrpg.builtin.actions;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import textrpg.Game;
import textrpg.mod.action.IAction;

public class Edit implements IAction {

    @Override
    public void run(JsonObject parameters) {
        System.out.println("Edit: " + parameters.toString());
        if (parameters.get("key") == null)
            return;
        JsonArray root;
        if (!parameters.has("root"))
            root = new JsonArray();
        else
            root = parameters.get("root").getAsJsonArray();
        JsonElement out = Game.playerdata.get("variables");

        for (JsonElement j : root) {
            if (out.isJsonArray())
                out = out.getAsJsonArray().get(j.getAsInt());
            else if (out.isJsonObject())
                out = out.getAsJsonObject().get(j.getAsString());
        }

        if (out.isJsonArray())
            out.getAsJsonArray().set(parameters.get("key").getAsInt(), parameters.get("value"));
        else if (out.isJsonObject())
            out.getAsJsonObject().add(parameters.get("key").getAsString(), parameters.get("value"));
    }
}