textrpg.builtin.actions.Get.java Source code

Java tutorial

Introduction

Here is the source code for textrpg.builtin.actions.Get.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.IActionR;

public class Get implements IActionR {

    @Override
    public JsonElement run(JsonObject params) {
        if (params.get("key") == null)
            return null;
        JsonArray root;
        if (!params.has("root"))
            root = new JsonArray();
        else
            root = params.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())
            return out.getAsJsonArray().get(params.get("key").getAsInt());
        else if (out.isJsonObject())
            return out.getAsJsonObject().get(params.get("key").getAsString());
        return null;
    }

}