Class VariableStorage

java.lang.Object
io.github.syst3ms.skriptparser.variables.VariableStorage
All Implemented Interfaces:
Closeable, AutoCloseable

public abstract class VariableStorage extends Object implements Closeable
A variable storage is holds the means and methods of storing variables.

This is usually some sort of database, and could be as simply as a text file.

Must contain a constructor of just SkriptLogger.

  • Field Details

    • closed

      protected volatile boolean closed
      Whether this variable storage has been closed.
    • name

      protected final String name
      The name of the database used in the configurations.
    • gson

      protected final com.google.gson.Gson gson
    • changesQueue

      protected final LinkedBlockingQueue<SerializedVariable> changesQueue
  • Constructor Details

    • VariableStorage

      protected VariableStorage(SkriptLogger logger, @NotNull @NotNull String name)
      Creates a new variable storage with the given name. Gson will be handled.
      Parameters:
      logger - the logger to print logs to.
      name - the name.
    • VariableStorage

      protected VariableStorage(SkriptLogger logger, @NotNull @NotNull com.google.gson.Gson gson, @NotNull @NotNull String name)
      Creates a new variable storage with the given names.
      Parameters:
      logger - the logger to print logs to.
      gson - the gson that controls the serialization of the json elements.
      name - the name.
  • Method Details

    • getConfigurationValue

      @Nullable protected @Nullable String getConfigurationValue(FileSection section, String key)
      Gets the string value at the given key of the given section node.
      Parameters:
      section - the file section.
      key - the key node.
      Returns:
      the value, or null if the value was invalid, or not found.
    • getConfigurationValue

      @Nullable protected <T> T getConfigurationValue(FileSection section, String key, Class<T> classType)
      Gets the value at the given key of the given section node, parsed with the given class type.
      Type Parameters:
      T - the class type generic.
      Parameters:
      section - the file section.
      key - the key node.
      classType - the class type.
      Returns:
      the parsed value, or null if the value was invalid, or not found.
    • loadConfiguration

      public final boolean loadConfiguration(Config.ConfigSection section)
      Loads the configuration for this variable storage from the given section node.
      Parameters:
      section - the section node.
      Returns:
      whether the loading succeeded.
    • load

      protected abstract boolean load(Config.ConfigSection section)
      Loads configurations and should start loading variables too.
      Returns:
      Whether the database could be loaded successfully, i.e. whether the configuration is correct and all variables could be loaded.
    • loadVariable

      protected void loadVariable(String name, SerializedVariable variable)
    • loadVariable

      protected void loadVariable(String name, @NotNull @NotNull String type, @NotNull @NotNull com.google.gson.JsonElement value)
      Loads a variable into Skript ram. Call this inside load(ConfigSection)
      Parameters:
      name - the name of the variable.
      type - the type of the variable.
      value - the serialized value of the variable.
    • allLoaded

      protected abstract void allLoaded()
      Called after all storages have been loaded, and variables have been redistributed if settings have changed. This should commit the first transaction.
    • requiresFile

      protected abstract boolean requiresFile()
      Checks if this storage requires a file for storing data, like SQLite.
      Returns:
      if this storage needs a file.
    • getFile

      @Nullable protected abstract @Nullable File getFile(String fileName)
      Gets the file needed for this variable storage from the given file name.

      Will only be called if requiresFile() is true.

      Parameters:
      fileName - the given file name.
      Returns:
      the File object.
    • serialize

      public <T> SerializedVariable serialize(String name, @Nullable T value)
      Creates a SerializedVariable from the given variable name and value. Can be overriden to add custom encryption in your implemented VariableStorage. Call super.
      Parameters:
      name - the variable name.
      value - the variable value.
      Returns:
      the serialized variable.
    • deserialize

      protected Object deserialize(@NotNull @NotNull String typeName, @NotNull @NotNull com.google.gson.JsonElement value)
      Used by loadVariable(String, String, JsonElement). You don't need to use this method, but if you need to read the Object, this method allows for deserialization.
      Parameters:
      typeName - The name of the type.
      value - The value that represents a object.
      Returns:
      The Object after deserialization, not present if not possible to deserialize due to missing serializer on Type.
    • clearChangesQueue

      protected void clearChangesQueue()
    • save

      protected abstract boolean save(String name, @Nullable @Nullable String type, @Nullable @Nullable com.google.gson.JsonElement value)
      Saves a variable.

      type and value are both null if this call is to delete the variable.

      Parameters:
      name - the name of the variable.
      type - the type of the variable.
      value - the serialized value of the variable.
      Returns:
      Whether the variable was saved.