<commit_before>package de.craften.plugins.rpgplus.scripting.api.storage;

import de.craften.plugins.rpgplus.RpgPlus;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.OneArgFunction;
import org.luaj.vm2.lib.TwoArgFunction;

/**
 * Lua module for storage.
 */
public class Storage extends LuaTable {
    final de.craften.plugins.rpgplus.components.storage.Storage storage = RpgPlus.getPlugin(RpgPlus.class).getStorage();

    public Storage() {
        set("get", new TwoArgFunction() {
            @Override
            public LuaValue call(LuaValue key, LuaValue defaultValue) {
                return LuaValueConverter.convert(storage.get(key.checkjstring(), defaultValue.isnil() ? null : defaultValue.checkjstring()));
            }
        });

        set("set", new TwoArgFunction() {
            @Override
            public LuaValue call(LuaValue key, LuaValue value) {
                storage.set(key.checkjstring(), key.checkjstring());
                return LuaValue.NIL;
            }
        });

        set("of", new OneArgFunction() {
            @Override
            public LuaValue call(LuaValue player) {
                return new PlayerTable(Bukkit.getServer().getPlayer(player.checkjstring()));
            }
        });
    }

    private class PlayerTable extends LuaTable {
        public PlayerTable(final OfflinePlayer player) {
            set("get", new TwoArgFunction() {
                @Override
                public LuaValue call(LuaValue key, LuaValue defaultValue) {
                    return LuaValue.valueOf(storage.get(player, key.checkjstring(), defaultValue.checkjstring()));
                }
            });

            set("set", new TwoArgFunction() {
                @Override
                public LuaValue call(LuaValue key, LuaValue value) {
                    storage.set(player, key.checkjstring(), value.checkjstring());
                    return LuaValue.NIL;
                }
            });
        }
    }
}<commit_msg>Allow saving tables into the storage (unpacks the values).<commit_after>package de.craften.plugins.rpgplus.scripting.api.storage;

import de.craften.plugins.rpgplus.RpgPlus;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.OneArgFunction;
import org.luaj.vm2.lib.TwoArgFunction;

import java.util.Map;

/**
 * Lua module for storage.
 */
public class Storage extends LuaTable {
    final de.craften.plugins.rpgplus.components.storage.Storage storage = RpgPlus.getPlugin(RpgPlus.class).getStorage();

    public Storage() {
        set("get", new TwoArgFunction() {
            @Override
            public LuaValue call(LuaValue key, LuaValue defaultValue) {
                return LuaValueConverter.convert(storage.get(key.checkjstring(), defaultValue.isnil() ? null : defaultValue.checkjstring()));
            }
        });

        set("set", new TwoArgFunction() {
            @Override
            public LuaValue call(LuaValue key, LuaValue value) {
                if (value.istable()) {
                    for (Map.Entry<String, String> entry : LuaValueConverter.convertFromTable(value.checktable()).entrySet()) {
                        storage.set(key.checkjstring() + "." + entry.getKey(), entry.getValue());
                    }
                } else {
                    storage.set(key.checkjstring(), key.checkjstring());
                }
                return LuaValue.NIL;
            }
        });

        set("of", new OneArgFunction() {
            @Override
            public LuaValue call(LuaValue player) {
                return new PlayerTable(Bukkit.getServer().getPlayer(player.checkjstring()));
            }
        });
    }

    private class PlayerTable extends LuaTable {
        public PlayerTable(final OfflinePlayer player) {
            set("get", new TwoArgFunction() {
                @Override
                public LuaValue call(LuaValue key, LuaValue defaultValue) {
                    return LuaValue.valueOf(storage.get(player, key.checkjstring(), defaultValue.checkjstring()));
                }
            });

            set("set", new TwoArgFunction() {
                @Override
                public LuaValue call(LuaValue key, LuaValue value) {
                    if (value.istable()) {
                        for (Map.Entry<String, String> entry : LuaValueConverter.convertFromTable(value.checktable()).entrySet()) {
                            storage.set(key.checkjstring() + "." + entry.getKey(), entry.getValue());
                        }
                    } else {
                        storage.set(player, key.checkjstring(), value.checkjstring());
                    }
                    return LuaValue.NIL;
                }
            });
        }
    }
}
{'fraction_non_alphanumeric': 0.11151811151811152, 'fraction_numerical': 0.001628001628001628, 'mean_word_length': 1.9343283582089552, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6442458', 'n_tokens_mistral': 1376, 'n_tokens_neox': 1323, 'n_words': 250}