.
This commit is contained in:
parent
855050c2d1
commit
77decae10c
74 changed files with 1190 additions and 861 deletions
29
core/configurator/BUILD
Normal file
29
core/configurator/BUILD
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
load("@rules_jvm_external//:defs.bzl", "artifact")
|
||||
load("@rules_java//java:defs.bzl", "java_binary", "java_library")
|
||||
|
||||
java_binary(
|
||||
name = "_plugin",
|
||||
srcs = glob(["main/java/**/*.java"]),
|
||||
create_executable = False,
|
||||
resources = glob(["main/resources/**"]),
|
||||
resource_strip_prefix = "core/configurator/main/resources",
|
||||
deps = [
|
||||
"//core/bukkit:api",
|
||||
artifact("io.papermc.paper:paper-api"),
|
||||
],
|
||||
)
|
||||
|
||||
java_library(
|
||||
name = "lib",
|
||||
visibility = ["//visibility:public"],
|
||||
exports = [":_plugin"],
|
||||
neverlink = True,
|
||||
)
|
||||
|
||||
genrule(
|
||||
name = "plugin",
|
||||
srcs = [":_plugin_deploy.jar"],
|
||||
outs = ["plugin.jar"],
|
||||
cmd = "cp $< $@",
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
34
core/configurator/README.md
Normal file
34
core/configurator/README.md
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
# configurator
|
||||
|
||||
This plugin/API allows developers to define configuration fields by name(or rather key) and data type, and have admins easily set
|
||||
the fields' values in-game using commands such as `/cfg key chat.messageDelay set 50 ms`.
|
||||
|
||||
<small>
|
||||
the key literal in `/cfg key` is required, because there are other modes like `/cfg check` that checks
|
||||
the configFile for uninitialized values.
|
||||
</small>
|
||||
|
||||
# Design
|
||||
|
||||
Plugins register configuration nodes by key. A key consists of a unique identifier and a type.
|
||||
The type handles (de)serialization and defines actions. Actions can be used on nodes.
|
||||
Nodes hold values.
|
||||
|
||||
The `/configFile` or `/cfg` command is used by admins to configure the plugin:
|
||||
A lobby plugin might register a node with the identifier `lobby.spawnLocation` and type `ConfigLocationType`,
|
||||
which gives us the command `/cfg key lobby.spawnLocation`.
|
||||
Let's say the ConfigLocationType defines an action to use the player's location as the value("pick-current")
|
||||
and an action to teleport the user to the defined location("teleport").
|
||||
Thus, we can do `/cfg key lobby.spawnLocation pick-current` or `/cfg key lobby.spawnLocation teleport`.
|
||||
Tab completion makes this pretty obvious.
|
||||
|
||||
## Lists
|
||||
|
||||
Now maybe we want to make our lobby have some launch pads(i.e. some pressure plates that launch the player).
|
||||
So, lists would be handy.
|
||||
For lists, we simply use a Node of type List<ConfigType of Element>, so for our launch pads, we could make a node
|
||||
with identifier "lobby.launchpads" and type `ConfigListType<ConfigLocationType>`.
|
||||
Now to add a launch pad, we can just `/cfg key lobby.launchpads append`, which creates a new uninitialized element
|
||||
in the list, followed by `/cfg key lobby.launchpads.0 pick-current` which actually sets the node's value.
|
||||
|
||||
The node `lobby.launchpads.0` is not a real registered node. It is a Sub-Node of the `lobby.launchpads` node.
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
package site.lab0x13.scrow.configurator;
|
||||
|
||||
import de.kentoj.scrow.bukkit.ScrowAPI;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import site.lab0x13.scrow.configurator.command.ConfiguratorCommand;
|
||||
import site.lab0x13.scrow.configurator.configfile.ConfigFile;
|
||||
import site.lab0x13.scrow.configurator.configfile.ConfigFileRegistry;
|
||||
import site.lab0x13.scrow.configurator.storable.impl.VectorStorable;
|
||||
import site.lab0x13.scrow.configurator.storable.impl.location.LocationStorable;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class ConfiguratorPlugin extends JavaPlugin {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(ConfiguratorPlugin.class);
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
ConfigFile hotPotatoConfig = ConfigFile.of(this.getDataPath().resolve("config.json"));
|
||||
ConfigFileRegistry.get().register(hotPotatoConfig);
|
||||
|
||||
var someVector = new VectorStorable();
|
||||
var someLocation = new LocationStorable();
|
||||
hotPotatoConfig.registerStorable("someVector", someVector);
|
||||
hotPotatoConfig.registerStorable("someLocation", someLocation);
|
||||
|
||||
try {
|
||||
hotPotatoConfig.load();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
ScrowAPI.commands().register(ConfiguratorCommand.rootLiteral(ConfigFileRegistry.get()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
try {
|
||||
for (var cfg : ConfigFileRegistry.get().allConfigs().values()) {
|
||||
log.info("saving {}...", cfg.path());
|
||||
cfg.save();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Failed saving config", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
package site.lab0x13.scrow.configurator.action;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import de.kentoj.scrow.bukkit.command.ScrowBukkitCC;
|
||||
import de.kentoj.scrowlib.command.type.JsonArgumentType;
|
||||
import site.lab0x13.scrow.commands.model.argument.Argument;
|
||||
import site.lab0x13.scrow.configurator.storable.Storable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public final class GlobalSetJsonAction implements StorableAction<Object, Storable<Object>> {
|
||||
|
||||
private final Argument<ScrowBukkitCC, JsonElement> valueArg =
|
||||
Argument.builder("jsonValue", new JsonArgumentType<ScrowBukkitCC>()).build();
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "set-json";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Argument<ScrowBukkitCC, ?>> arguments(Storable<Object> __) {
|
||||
return List.of(valueArg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean requiresValue() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(Storable<Object> storable, ScrowBukkitCC ctx) {
|
||||
try {
|
||||
storable.loadJson(ctx.getArg(valueArg));
|
||||
} catch (Exception ex) {
|
||||
ctx.sender().sendMessage(ctx.style().err("Failed serializing: " + ex.getMessage()));
|
||||
return;
|
||||
}
|
||||
ctx.sender().sendMessage(ctx.style().ok("Value set."));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
package site.lab0x13.scrow.configurator.action;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonElement;
|
||||
import de.kentoj.scrow.bukkit.command.ScrowBukkitCC;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.event.ClickEvent;
|
||||
import site.lab0x13.scrow.commands.model.argument.Argument;
|
||||
import site.lab0x13.scrow.configurator.storable.Storable;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public final class GlobalShowAction implements StorableAction<Object, Storable<Object>> {
|
||||
|
||||
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "show-json";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Argument<ScrowBukkitCC, ?>> arguments(Storable<Object> __) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean requiresValue() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(Storable<Object> storable, ScrowBukkitCC ctx) {
|
||||
var prettyJson = GSON.toJson(storable.toJson(), JsonElement.class);
|
||||
var component = Component.text(prettyJson + " [click to copy]")
|
||||
.clickEvent(ClickEvent.copyToClipboard(prettyJson));
|
||||
ctx.sender().sendMessage(ctx.style().ok(component));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
package site.lab0x13.scrow.configurator.action;
|
||||
|
||||
import de.kentoj.scrow.bukkit.command.ScrowBukkitCC;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.entity.Player;
|
||||
import site.lab0x13.scrow.commands.model.argument.Argument;
|
||||
import site.lab0x13.scrow.configurator.storable.Storable;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
public abstract class PickAction<V> implements StorableAction<Object, Storable<Object>> {
|
||||
|
||||
protected abstract CompletableFuture<V> pick(Player player);
|
||||
|
||||
@Override
|
||||
public List<Argument<ScrowBukkitCC, ?>> arguments(Storable<Object> __) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean requiresValue() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(Storable<Object> node, ScrowBukkitCC ctx) {
|
||||
pick(ctx.player())
|
||||
.orTimeout(10, TimeUnit.MINUTES)
|
||||
.whenComplete((picked, ex) -> {
|
||||
if (ex instanceof TimeoutException) {
|
||||
ctx.player().sendMessage(ctx.style().err("Timed out picking value."));
|
||||
return;
|
||||
}
|
||||
ctx.player().playSound(ctx.player().getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 0.5f, 0f);
|
||||
node.value(picked);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
package site.lab0x13.scrow.configurator.action;
|
||||
|
||||
import de.kentoj.scrow.bukkit.command.ScrowBukkitCC;
|
||||
import site.lab0x13.scrow.commands.model.argument.Argument;
|
||||
import site.lab0x13.scrow.configurator.storable.Storable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface StorableAction<V, T extends Storable<V>> {
|
||||
|
||||
String name();
|
||||
|
||||
List<Argument<ScrowBukkitCC, ?>> arguments(T node);
|
||||
|
||||
boolean requiresValue();
|
||||
|
||||
void execute(T storable, ScrowBukkitCC ctx);
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
package site.lab0x13.scrow.configurator.action;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public interface ValuePicker<T> {
|
||||
|
||||
CompletableFuture<T> pick(Player player);
|
||||
}
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
package site.lab0x13.scrow.configurator.command;
|
||||
|
||||
import de.kentoj.scrow.bukkit.command.ScrowBukkitCC;
|
||||
import site.lab0x13.scrow.commands.model.literal.Literal;
|
||||
import site.lab0x13.scrow.configurator.configfile.ConfigFile;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static java.util.Objects.*;
|
||||
|
||||
final class CheckLiteral {
|
||||
|
||||
private final Literal<ScrowBukkitCC> literal;
|
||||
private final ConfigFile configFile;
|
||||
|
||||
public CheckLiteral(ConfigFile configFile) {
|
||||
this.configFile = configFile;
|
||||
this.literal = Literal.<ScrowBukkitCC>builder("check")
|
||||
.withSyncExecutor(this::execute)
|
||||
.build();
|
||||
}
|
||||
|
||||
private void execute(ScrowBukkitCC ctx) {
|
||||
var badNodes = new AtomicInteger();
|
||||
configFile.allKeys().forEach(key -> {
|
||||
var error = requireNonNull(configFile.getStorable(key)).checkValue();
|
||||
if (error == null) return;
|
||||
ctx.sender().sendMessage(ctx.style().ok(key + ": " + error));
|
||||
});
|
||||
|
||||
if (badNodes.get() > 0)
|
||||
ctx.sender().sendMessage(ctx.style().err("Found " + badNodes + " errors."));
|
||||
else
|
||||
ctx.sender().sendMessage(ctx.style().ok("No errors."));
|
||||
}
|
||||
|
||||
public Literal<ScrowBukkitCC> literal() {
|
||||
return literal;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
package site.lab0x13.scrow.configurator.command;
|
||||
|
||||
import de.kentoj.scrow.bukkit.command.ScrowBukkitCC;
|
||||
import site.lab0x13.scrow.commands.model.literal.Literal;
|
||||
import site.lab0x13.scrow.configurator.configfile.ConfigFile;
|
||||
|
||||
public final class ConfigLiteral {
|
||||
private ConfigLiteral() {
|
||||
}
|
||||
|
||||
public static Literal<ScrowBukkitCC> literal(ConfigFile cfg) {
|
||||
return Literal.<ScrowBukkitCC>builder(cfg.path().normalize().toString())
|
||||
.withSubLiteral(new KeyLiteral(cfg).literal())
|
||||
.withSubLiteral(new CheckLiteral(cfg).literal())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
package site.lab0x13.scrow.configurator.command;
|
||||
|
||||
import de.kentoj.scrow.bukkit.command.ScrowBukkitCC;
|
||||
import de.kentoj.scrowlib.convention.ScrowMessageStyle;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import site.lab0x13.scrow.commands.model.literal.Literal;
|
||||
import site.lab0x13.scrow.commands.model.literal.LiteralMeta;
|
||||
import site.lab0x13.scrow.commands.model.literal.RootLiteral;
|
||||
import site.lab0x13.scrow.configurator.configfile.ConfigFileRegistry;
|
||||
|
||||
public final class ConfiguratorCommand {
|
||||
|
||||
private ConfiguratorCommand() {
|
||||
}
|
||||
|
||||
public static RootLiteral<ScrowBukkitCC> rootLiteral(ConfigFileRegistry registry) {
|
||||
return Literal.<ScrowBukkitCC>dynamic("configurator", "cfg")
|
||||
.withPermission("command.config")
|
||||
.withSubLiteralMetas(() -> registry.allConfigs().keySet().stream().map(LiteralMeta::of).toList())
|
||||
.withSubLiteralProvider(name -> {
|
||||
var configFile = registry.getConfigFile(name);
|
||||
if (configFile == null) return null;
|
||||
return ConfigLiteral.literal(configFile);
|
||||
})
|
||||
.asRootLiteral(new ScrowMessageStyle("Configurator", TextColor.color(0x9070C0)));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
package site.lab0x13.scrow.configurator.command;
|
||||
|
||||
import de.kentoj.scrow.bukkit.command.ScrowBukkitCC;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import site.lab0x13.scrow.commands.model.literal.Literal;
|
||||
import site.lab0x13.scrow.commands.model.literal.LiteralMeta;
|
||||
import site.lab0x13.scrow.configurator.configfile.ConfigFile;
|
||||
import site.lab0x13.scrow.configurator.storable.Storable;
|
||||
import site.lab0x13.scrow.configurator.action.StorableAction;
|
||||
|
||||
final class KeyLiteral {
|
||||
|
||||
private final Literal<ScrowBukkitCC> literal;
|
||||
private final ConfigFile configFile;
|
||||
|
||||
KeyLiteral(ConfigFile configFile) {
|
||||
this.configFile = configFile;
|
||||
literal = Literal.<ScrowBukkitCC>dynamic("key")
|
||||
.withSubLiteralMetas(() -> configFile.allKeys().stream().map(LiteralMeta::of).toList())
|
||||
.withSubLiteralProvider(this::keyLiteral)
|
||||
.build();
|
||||
}
|
||||
|
||||
private @Nullable Literal<ScrowBukkitCC> keyLiteral(String name) {
|
||||
var storable = (Storable<Object>) configFile.getStorable(name);
|
||||
if (storable == null) return null;
|
||||
|
||||
var literalBuilder = Literal.<ScrowBukkitCC>builder(name);
|
||||
for (var _action : storable.actions()) {
|
||||
var action = (StorableAction<Object, Storable<Object>>) _action;
|
||||
var actionLiteralBuilder = Literal.<ScrowBukkitCC>builder(action.name());
|
||||
action.arguments(storable).forEach(actionLiteralBuilder::withArgument);
|
||||
actionLiteralBuilder.withSyncExecutor(ctx -> {
|
||||
if (action.requiresValue() && storable.value() == null) {
|
||||
ctx.sender().sendMessage(ctx.style().err("No value set."));
|
||||
return;
|
||||
}
|
||||
action.execute(storable, ctx);
|
||||
});
|
||||
literalBuilder.withSubLiteral(actionLiteralBuilder.build());
|
||||
}
|
||||
return literalBuilder.build();
|
||||
}
|
||||
|
||||
public Literal<ScrowBukkitCC> literal() {
|
||||
return literal;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
package site.lab0x13.scrow.configurator.configfile;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import site.lab0x13.scrow.configurator.storable.Storable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface ConfigFile extends Storable<Map<String, Storable<?>>> {
|
||||
|
||||
static ConfigFile of(Path path) {
|
||||
return new ConfigFileImpl(path.normalize());
|
||||
}
|
||||
|
||||
@Nullable Storable<?> getStorable(String key);
|
||||
|
||||
void registerStorable(String key, Storable<?> storable);
|
||||
|
||||
List<String> allKeys();
|
||||
|
||||
void load() throws IOException;
|
||||
|
||||
void save() throws IOException;
|
||||
|
||||
Path path();
|
||||
}
|
||||
|
|
@ -0,0 +1,83 @@
|
|||
package site.lab0x13.scrow.configurator.configfile;
|
||||
|
||||
import com.google.gson.JsonParser;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import site.lab0x13.scrow.configurator.storable.MapStorable;
|
||||
import site.lab0x13.scrow.configurator.storable.Storable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
class ConfigFileImpl extends MapStorable<Storable<?>> implements ConfigFile {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(ConfigFileImpl.class);
|
||||
private final Path path;
|
||||
|
||||
ConfigFileImpl(Path path) {
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Storable<?> getStorable(String key) {
|
||||
var index = key.indexOf(':');
|
||||
if (index == -1)
|
||||
return getSubField(key);
|
||||
if (key.endsWith(":"))
|
||||
return null;
|
||||
|
||||
var fieldName = key.substring(0, index);
|
||||
Storable<?> storable = getSubField(fieldName);
|
||||
if (storable == null)
|
||||
return null;
|
||||
for (String subFieldName : key.substring(index + 1).split(":")) {
|
||||
if (subFieldName.isEmpty()) continue;
|
||||
storable = storable.getSubField(subFieldName);
|
||||
if (storable == null)
|
||||
return null;
|
||||
}
|
||||
return storable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerStorable(String key, Storable<?> storable) {
|
||||
registerEntry(key, (Storable<Object>) storable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> allKeys() {
|
||||
var res = new ArrayList<String>();
|
||||
subFieldNames().forEach(key -> addKeysRecursively(res, key, requireNonNull(getSubField(key))));
|
||||
return res;
|
||||
}
|
||||
|
||||
private void addKeysRecursively(List<String> result, String key, Storable<?> storable) {
|
||||
result.add(key);
|
||||
storable.subFieldNames().forEach(subKey ->
|
||||
addKeysRecursively(result, key + ":" + subKey, requireNonNull(storable.getSubField(subKey))));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() throws IOException {
|
||||
if (!Files.exists(this.path)) return;
|
||||
var json = Files.readString(this.path());
|
||||
this.loadJson(JsonParser.parseString(json));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save() throws IOException {
|
||||
Files.createDirectories(this.path().getParent());
|
||||
Files.writeString(this.path(), this.toJson().toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Path path() {
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
package site.lab0x13.scrow.configurator.configfile;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class ConfigFileRegistry {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(ConfigFileRegistry.class);
|
||||
private static final ConfigFileRegistry instance = new ConfigFileRegistry();
|
||||
|
||||
public static ConfigFileRegistry get() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
private final Map<String, ConfigFile> configFiles = new HashMap<>();
|
||||
|
||||
public void register(ConfigFile configFile) {
|
||||
var oldValue = configFiles.put(configFile.path().toString(), configFile);
|
||||
if (oldValue != null)
|
||||
log.warn("Overwriting old config with same path '{}'", configFile.path());
|
||||
}
|
||||
|
||||
public Map<String, ConfigFile> allConfigs() {
|
||||
return new HashMap<>(configFiles);
|
||||
}
|
||||
|
||||
public @Nullable ConfigFile getConfigFile(String path) {
|
||||
return configFiles.get(path);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
package site.lab0x13.scrow.configurator.storable;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public abstract class AbstractStorable<T> implements Storable<T> {
|
||||
private final List<ValueChangeSubscriber> subscribers = new ArrayList<>();
|
||||
@Nullable
|
||||
protected T value = null;
|
||||
|
||||
private final List<ValueRequirement<T>> requirements = new ArrayList<>();
|
||||
|
||||
protected void requireValue(Predicate<T> predicate, String error) {
|
||||
requirements.add(new ValueRequirement<>(predicate, error));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void subscribe(ValueChangeSubscriber subscriber) {
|
||||
subscribers.add(subscriber);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable T value() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void value(@Nullable T value) {
|
||||
this.value = value;
|
||||
subscribers.forEach(ValueChangeSubscriber::notifyValueChange);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String checkValue() {
|
||||
for (var requirement : requirements) {
|
||||
if (!requirement.predicate().test(value()))
|
||||
return requirement.error();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
package site.lab0x13.scrow.configurator.storable;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface Action {
|
||||
void apply();
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
package site.lab0x13.scrow.configurator.storable;
|
||||
|
||||
import site.lab0x13.scrow.configurator.storable.complex.ComplexStorable;
|
||||
import site.lab0x13.scrow.configurator.storable.complex.FieldReader;
|
||||
import site.lab0x13.scrow.configurator.storable.complex.FieldRegistry;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class MapStorable<V> extends ComplexStorable<Map<String, V>> {
|
||||
|
||||
public <S> void registerEntry(String name, Storable<S> storable) {
|
||||
fieldRegistry().registerField(name, storable, map -> (S) map.get(name));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void registerFields(FieldRegistry<Map<String, V>> registry) {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Map<String, V> construct(FieldReader<Map<String, V>> reader) {
|
||||
var res = new HashMap<String, V>();
|
||||
reader.subFieldNames().forEach(key -> res.put(key, reader.read(key)));
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
package site.lab0x13.scrow.configurator.storable;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import site.lab0x13.scrow.configurator.action.StorableAction;
|
||||
import site.lab0x13.scrow.configurator.action.GlobalSetJsonAction;
|
||||
import site.lab0x13.scrow.configurator.action.GlobalShowAction;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public interface Storable<T> {
|
||||
|
||||
@Nullable T value();
|
||||
|
||||
void value(@Nullable T value);
|
||||
|
||||
JsonElement toJson();
|
||||
|
||||
void loadJson(JsonElement json);
|
||||
|
||||
List<String> subFieldNames();
|
||||
|
||||
@Nullable Storable<?> getSubField(String name);
|
||||
|
||||
/**
|
||||
* @return error message or null if value is fine
|
||||
*/
|
||||
@Nullable String checkValue();
|
||||
|
||||
default List<StorableAction<?, ?>> actions() {
|
||||
return new ArrayList<>(List.of(
|
||||
new GlobalShowAction(),
|
||||
new GlobalSetJsonAction()
|
||||
));
|
||||
}
|
||||
|
||||
void subscribe(ValueChangeSubscriber subscriber);
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
package site.lab0x13.scrow.configurator.storable;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface ValueChangeSubscriber {
|
||||
|
||||
void notifyValueChange();
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package site.lab0x13.scrow.configurator.storable;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public record ValueRequirement<T>(
|
||||
Predicate<T> predicate,
|
||||
String error
|
||||
) {
|
||||
}
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
package site.lab0x13.scrow.configurator.storable;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public abstract class WrappedStorable<R, T> extends AbstractStorable<T> {
|
||||
|
||||
private final Storable<R> realStorable;
|
||||
private boolean skipUpdates = false;
|
||||
|
||||
public WrappedStorable(Storable<R> realStorable) {
|
||||
this.realStorable = realStorable;
|
||||
realStorable.subscribe(() -> {
|
||||
if (realStorable.value() == null)
|
||||
value(null);
|
||||
else
|
||||
value(wrap(realStorable.value()));
|
||||
});
|
||||
}
|
||||
|
||||
abstract protected T wrap(R real);
|
||||
|
||||
abstract protected R unwrap(T value);
|
||||
|
||||
@Override
|
||||
public void value(@Nullable T value) {
|
||||
if (skipUpdates)
|
||||
return;
|
||||
super.value(value);
|
||||
skipUpdates(() -> realStorable.value(value == null ? null : unwrap(value)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement toJson() {
|
||||
return realStorable.toJson();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadJson(JsonElement json) {
|
||||
realStorable.loadJson(json);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> subFieldNames() {
|
||||
return realStorable.subFieldNames();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Storable<?> getSubField(String name) {
|
||||
return realStorable.getSubField(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run action without updating the value
|
||||
*/
|
||||
private void skipUpdates(Action action) {
|
||||
try {
|
||||
skipUpdates = true;
|
||||
action.apply();
|
||||
} finally {
|
||||
skipUpdates = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
package site.lab0x13.scrow.configurator.storable.complex;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import site.lab0x13.scrow.configurator.storable.Storable;
|
||||
|
||||
public abstract class ComplexStorable<T> extends SubFieldedStorable<T> {
|
||||
|
||||
@Override
|
||||
protected @Nullable T defaultValue() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement toJson() {
|
||||
var obj = new JsonObject();
|
||||
fieldRegistry().subFields().forEach((subFieldName, subField) -> {
|
||||
var v = (Storable<Object>) subField.storable();
|
||||
obj.add(subFieldName, v.toJson());
|
||||
});
|
||||
return obj;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadJson(JsonElement json) {
|
||||
var obj = json.getAsJsonObject();
|
||||
skipUpdates(() -> fieldRegistry().subFields().forEach((subFieldName, subField) ->
|
||||
subField.storable().loadJson(obj.get(subFieldName))));
|
||||
updateValue();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
package site.lab0x13.scrow.configurator.storable.complex;
|
||||
|
||||
import site.lab0x13.scrow.configurator.storable.Storable;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public class Field<T, S> {
|
||||
private final Storable<S> storable;
|
||||
private final Function<T, S> getter;
|
||||
|
||||
public Field(Storable<S> storable, Function<T, S> getter) {
|
||||
this.storable = storable;
|
||||
this.getter = getter;
|
||||
}
|
||||
|
||||
public Storable<S> storable() {
|
||||
return storable;
|
||||
}
|
||||
|
||||
public Function<T, S> getter() {
|
||||
return getter;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
package site.lab0x13.scrow.configurator.storable.complex;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class FieldReader<T> {
|
||||
|
||||
private final FieldRegistry<T> fieldRegistry;
|
||||
|
||||
public FieldReader(FieldRegistry<T> fieldRegistry) {
|
||||
this.fieldRegistry = fieldRegistry;
|
||||
}
|
||||
|
||||
public <S> S read(String name) {
|
||||
var sf = fieldRegistry.get(name);
|
||||
if (sf == null) return null;
|
||||
return (S) sf.storable().value();
|
||||
}
|
||||
|
||||
public List<String> subFieldNames() {
|
||||
return new ArrayList<>(fieldRegistry.subFields().keySet());
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
package site.lab0x13.scrow.configurator.storable.complex;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import site.lab0x13.scrow.configurator.storable.Storable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class FieldRegistry<T> {
|
||||
|
||||
private final Map<String, Field<T, ?>> subFields = new HashMap<>();
|
||||
|
||||
// TODO move field stuff into own class where register field is one block and after that ready is called
|
||||
public <S> void registerField(String name, Storable<S> storable, Function<T, S> getter) {
|
||||
subFields.put(name, new Field<>(storable, getter));
|
||||
}
|
||||
|
||||
public Map<String, Field<T, ?>> subFields() {
|
||||
return subFields;
|
||||
}
|
||||
|
||||
public @Nullable Field<T, ?> get(String name) {
|
||||
return subFields.get(name);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,75 @@
|
|||
package site.lab0x13.scrow.configurator.storable.complex;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import site.lab0x13.scrow.configurator.storable.AbstractStorable;
|
||||
import site.lab0x13.scrow.configurator.storable.Action;
|
||||
import site.lab0x13.scrow.configurator.storable.Storable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class SubFieldedStorable<T> extends AbstractStorable<T> {
|
||||
|
||||
private final FieldRegistry<T> fieldRegistry = new FieldRegistry<>();
|
||||
private boolean skipUpdates = false;
|
||||
|
||||
public SubFieldedStorable() {
|
||||
this.registerFields(fieldRegistry);
|
||||
fieldRegistry.subFields().values().forEach(f -> f.storable().subscribe(this::updateValue));
|
||||
}
|
||||
|
||||
protected abstract void registerFields(FieldRegistry<T> registry);
|
||||
|
||||
protected abstract T construct(FieldReader<T> reader);
|
||||
|
||||
protected abstract @Nullable T defaultValue();
|
||||
|
||||
/**
|
||||
* Run action without updating the value
|
||||
*/
|
||||
protected void skipUpdates(Action action) {
|
||||
try {
|
||||
skipUpdates = true;
|
||||
action.apply();
|
||||
} finally {
|
||||
skipUpdates = false;
|
||||
}
|
||||
}
|
||||
|
||||
protected void updateValue() {
|
||||
if (skipUpdates)
|
||||
return;
|
||||
super.value(construct(new FieldReader<>(fieldRegistry)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void value(@Nullable T value) {
|
||||
if (value == null) {
|
||||
super.value(defaultValue());
|
||||
return;
|
||||
}
|
||||
super.value(value);
|
||||
|
||||
// reflect value change no sub fields
|
||||
skipUpdates(() -> fieldRegistry.subFields().values().forEach(_field -> {
|
||||
var field = (Field<T, Object>) _field;
|
||||
field.storable().value(field.getter().apply(value));
|
||||
}));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> subFieldNames() {
|
||||
return new ArrayList<>(fieldRegistry.subFields().keySet());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Storable<?> getSubField(String name) {
|
||||
var sf = fieldRegistry.subFields().get(name);
|
||||
if (sf == null) return null;
|
||||
return sf.storable();
|
||||
}
|
||||
|
||||
protected FieldRegistry<T> fieldRegistry() {
|
||||
return fieldRegistry;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
package site.lab0x13.scrow.configurator.storable.impl;
|
||||
|
||||
import org.bukkit.util.Vector;
|
||||
import site.lab0x13.scrow.configurator.storable.complex.ComplexStorable;
|
||||
import site.lab0x13.scrow.configurator.storable.complex.FieldReader;
|
||||
import site.lab0x13.scrow.configurator.storable.complex.FieldRegistry;
|
||||
import site.lab0x13.scrow.configurator.storable.primitive.DoubleStorable;
|
||||
|
||||
public class VectorStorable extends ComplexStorable<Vector> {
|
||||
|
||||
@Override
|
||||
protected void registerFields(FieldRegistry<Vector> registry) {
|
||||
registry.registerField("x", new DoubleStorable(), Vector::getX);
|
||||
registry.registerField("y", new DoubleStorable(), Vector::getY);
|
||||
registry.registerField("z", new DoubleStorable(), Vector::getZ);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Vector construct(FieldReader<Vector> reader) {
|
||||
return new Vector(
|
||||
reader.<Double>read("x"),
|
||||
reader.<Double>read("y"),
|
||||
reader.<Double>read("z")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
package site.lab0x13.scrow.configurator.storable.impl.list;
|
||||
|
||||
import de.kentoj.scrow.bukkit.command.ScrowBukkitCC;
|
||||
import site.lab0x13.scrow.commands.model.argument.Argument;
|
||||
import site.lab0x13.scrow.configurator.action.StorableAction;
|
||||
import site.lab0x13.scrow.configurator.storable.Storable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ListExpandAction<V extends Storable<?>> implements StorableAction<List<V>, ListStorable<V>> {
|
||||
@Override
|
||||
public String name() {
|
||||
return "expand";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Argument<ScrowBukkitCC, ?>> arguments(ListStorable<V> node) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean requiresValue() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(ListStorable<V> storable, ScrowBukkitCC ctx) {
|
||||
storable.expandList();
|
||||
ctx.sender().sendMessage(ctx.style().ok("Expanded list-storable."));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,82 @@
|
|||
package site.lab0x13.scrow.configurator.storable.impl.list;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import site.lab0x13.scrow.configurator.action.StorableAction;
|
||||
import site.lab0x13.scrow.configurator.storable.AbstractStorable;
|
||||
import site.lab0x13.scrow.configurator.storable.Storable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
public class ListStorable<S extends Storable<?>> extends AbstractStorable<List<S>> {
|
||||
|
||||
private final Supplier<S> newElementSupplier;
|
||||
private final List<StorableAction<?, ?>> actions;
|
||||
|
||||
public ListStorable(Supplier<S> newElementSupplier) {
|
||||
this.newElementSupplier = newElementSupplier;
|
||||
actions = super.actions();
|
||||
actions.add(new ListExpandAction<>());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<StorableAction<?, ?>> actions() {
|
||||
return actions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull List<S> value() {
|
||||
var v = super.value();
|
||||
if (v == null) {
|
||||
v = new ArrayList<>();
|
||||
value(v);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
/**
|
||||
* Expands the list with a default value storable
|
||||
*/
|
||||
public S expandList() {
|
||||
var newElement = newElementSupplier.get();
|
||||
requireNonNull(value()).add(newElement);
|
||||
return newElement;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement toJson() {
|
||||
var out = new JsonArray();
|
||||
requireNonNull(value()).forEach(storable -> out.add(storable.toJson()));
|
||||
return out;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadJson(JsonElement json) {
|
||||
var in = json.getAsJsonArray();
|
||||
in.forEach(jsonElement -> expandList().loadJson(jsonElement));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> subFieldNames() {
|
||||
var v = value();
|
||||
if (v == null) return Collections.emptyList();
|
||||
return IntStream.range(0, v.size())
|
||||
.mapToObj(Integer::toString)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Storable<?> getSubField(String name) {
|
||||
var v = value();
|
||||
if (v == null) return null;
|
||||
return v.get(Integer.parseInt(name));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package site.lab0x13.scrow.configurator.storable.impl.location;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
import site.lab0x13.scrow.configurator.action.PickAction;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
class LocationPickCurrentAction extends PickAction<Location> {
|
||||
@Override
|
||||
public String name() {
|
||||
return "pick-current";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CompletableFuture<Location> pick(Player player) {
|
||||
return CompletableFuture.completedFuture(player.getLocation());
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
package site.lab0x13.scrow.configurator.storable.impl.location;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import site.lab0x13.scrow.configurator.action.StorableAction;
|
||||
import site.lab0x13.scrow.configurator.storable.complex.ComplexStorable;
|
||||
import site.lab0x13.scrow.configurator.storable.primitive.DoubleStorable;
|
||||
import site.lab0x13.scrow.configurator.storable.primitive.FloatStorable;
|
||||
import site.lab0x13.scrow.configurator.storable.primitive.PrimitiveStorable;
|
||||
import site.lab0x13.scrow.configurator.storable.complex.FieldReader;
|
||||
import site.lab0x13.scrow.configurator.storable.complex.FieldRegistry;
|
||||
import site.lab0x13.scrow.configurator.storable.impl.world.WorldStorable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class LocationStorable extends ComplexStorable<Location> {
|
||||
|
||||
private final List<StorableAction<?, ?>> actions;
|
||||
|
||||
public LocationStorable() {
|
||||
actions = new ArrayList<>(super.actions());
|
||||
actions.add(new LocationPickCurrentAction());
|
||||
actions.add(new LocationTeleportAction());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void registerFields(FieldRegistry<Location> registry) {
|
||||
registry.registerField("world", new WorldStorable(), Location::getWorld);
|
||||
registry.registerField("x", new DoubleStorable(), Location::getX);
|
||||
registry.registerField("y", new DoubleStorable(), Location::getY);
|
||||
registry.registerField("z", new DoubleStorable(), Location::getZ);
|
||||
registry.registerField("yaw", new FloatStorable(), Location::getYaw);
|
||||
registry.registerField("pitch", new FloatStorable(), Location::getPitch);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<StorableAction<?, ?>> actions() {
|
||||
return actions;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Location construct(FieldReader<Location> reader) {
|
||||
return new Location(
|
||||
reader.read("world"),
|
||||
reader.read("x"),
|
||||
reader.read("y"),
|
||||
reader.read("z"),
|
||||
reader.read("yaw"),
|
||||
reader.read("pitch")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
package site.lab0x13.scrow.configurator.storable.impl.location;
|
||||
|
||||
import de.kentoj.scrow.bukkit.command.ScrowBukkitCC;
|
||||
import org.bukkit.Location;
|
||||
import site.lab0x13.scrow.commands.model.argument.Argument;
|
||||
import site.lab0x13.scrow.configurator.action.StorableAction;
|
||||
import site.lab0x13.scrow.configurator.storable.Storable;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
class LocationTeleportAction implements StorableAction<Location, Storable<Location>> {
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "teleport";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Argument<ScrowBukkitCC, ?>> arguments(Storable<Location> __) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean requiresValue() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(Storable<Location> node, ScrowBukkitCC ctx) {
|
||||
assert node.value() != null;
|
||||
ctx.player().teleport(node.value());
|
||||
ctx.player().sendMessage(ctx.style().ok("You've been teleported."));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
package site.lab0x13.scrow.configurator.storable.impl.world;
|
||||
|
||||
import de.kentoj.scrow.bukkit.command.ScrowBukkitCC;
|
||||
import org.bukkit.World;
|
||||
import site.lab0x13.scrow.commands.bukkit.type.WorldArgumentType;
|
||||
import site.lab0x13.scrow.commands.model.argument.Argument;
|
||||
import site.lab0x13.scrow.configurator.action.StorableAction;
|
||||
import site.lab0x13.scrow.configurator.storable.Storable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
class SetWorldAction implements StorableAction<World, Storable<World>> {
|
||||
|
||||
private final Argument<ScrowBukkitCC, World> arg =
|
||||
Argument.builder("world", new WorldArgumentType<ScrowBukkitCC>()).build();
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "set";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Argument<ScrowBukkitCC, ?>> arguments(Storable<World> __) {
|
||||
return List.of(arg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean requiresValue() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(Storable<World> node, ScrowBukkitCC ctx) {
|
||||
World world = ctx.getArg(arg);
|
||||
node.value(world);
|
||||
ctx.sender().sendMessage(ctx.style().ok("Set world to " + world.getName() + "(" + world.getUID() + ")"));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
package site.lab0x13.scrow.configurator.storable.impl.world;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
import site.lab0x13.scrow.configurator.action.StorableAction;
|
||||
import site.lab0x13.scrow.configurator.storable.WrappedStorable;
|
||||
import site.lab0x13.scrow.configurator.storable.primitive.StringStorable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class WorldStorable extends WrappedStorable<String, World> {
|
||||
|
||||
private final List<StorableAction<?, ?>> actions;
|
||||
|
||||
public WorldStorable() {
|
||||
super(new StringStorable());
|
||||
actions = new ArrayList<>(super.actions());
|
||||
actions.add(new SetWorldAction());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<StorableAction<?, ?>> actions() {
|
||||
return actions;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected World wrap(String real) {
|
||||
return Bukkit.getWorld(real);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String unwrap(World value) {
|
||||
return value.getName();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
package site.lab0x13.scrow.configurator.storable.primitive;
|
||||
|
||||
import com.google.gson.JsonPrimitive;
|
||||
|
||||
public class BooleanStorable extends PrimitiveStorable<Boolean>{
|
||||
public BooleanStorable() {
|
||||
super(false, JsonPrimitive::new, JsonPrimitive::getAsBoolean);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
package site.lab0x13.scrow.configurator.storable.primitive;
|
||||
|
||||
import com.google.gson.JsonPrimitive;
|
||||
|
||||
public class ByteStorable extends PrimitiveStorable<Byte>{
|
||||
public ByteStorable() {
|
||||
super((byte)0, JsonPrimitive::new, JsonPrimitive::getAsByte);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
package site.lab0x13.scrow.configurator.storable.primitive;
|
||||
|
||||
import com.google.gson.JsonPrimitive;
|
||||
|
||||
public class DoubleStorable extends PrimitiveStorable<Double>{
|
||||
public DoubleStorable() {
|
||||
super(0D, JsonPrimitive::new, JsonPrimitive::getAsDouble);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
package site.lab0x13.scrow.configurator.storable.primitive;
|
||||
|
||||
import com.google.gson.JsonPrimitive;
|
||||
|
||||
public class FloatStorable extends PrimitiveStorable<Float>{
|
||||
public FloatStorable() {
|
||||
super(0F, JsonPrimitive::new, JsonPrimitive::getAsFloat);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
package site.lab0x13.scrow.configurator.storable.primitive;
|
||||
|
||||
import com.google.gson.JsonPrimitive;
|
||||
|
||||
public class IntStorable extends PrimitiveStorable<Integer>{
|
||||
public IntStorable() {
|
||||
super(0, JsonPrimitive::new, JsonPrimitive::getAsInt);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
package site.lab0x13.scrow.configurator.storable.primitive;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonNull;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import site.lab0x13.scrow.configurator.storable.AbstractStorable;
|
||||
import site.lab0x13.scrow.configurator.storable.Storable;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class PrimitiveStorable<V> extends AbstractStorable<V> {
|
||||
|
||||
private final V defaultValue;
|
||||
private final Function<V, JsonPrimitive> serializer;
|
||||
private final Function<JsonPrimitive, V> deserializer;
|
||||
|
||||
public PrimitiveStorable(V defaultValue, Function<V, JsonPrimitive> serializer, Function<JsonPrimitive, V> deserializer) {
|
||||
this.defaultValue = defaultValue;
|
||||
this.serializer = serializer;
|
||||
this.deserializer = deserializer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement toJson() {
|
||||
if (value() == null)
|
||||
return JsonNull.INSTANCE;
|
||||
return serializer.apply(value());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadJson(JsonElement json) {
|
||||
if (json.isJsonNull())
|
||||
value(defaultValue);
|
||||
else
|
||||
value(deserializer.apply(json.getAsJsonPrimitive()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void value(@Nullable V value) {
|
||||
super.value(value == null ? defaultValue : value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> subFieldNames() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Storable<?> getSubField(String name) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
package site.lab0x13.scrow.configurator.storable.primitive;
|
||||
|
||||
import com.google.gson.JsonPrimitive;
|
||||
|
||||
public class StringStorable extends PrimitiveStorable<String>{
|
||||
public StringStorable() {
|
||||
super("", JsonPrimitive::new, JsonPrimitive::getAsString);
|
||||
}
|
||||
}
|
||||
|
||||
6
core/configurator/main/resources/plugin.yml
Normal file
6
core/configurator/main/resources/plugin.yml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
name: "Configurator"
|
||||
version: "0"
|
||||
depend: [ "CoreBukkit" ]
|
||||
main: "site.lab0x13.scrow.configurator.ConfiguratorPlugin"
|
||||
api-version: "26.2"
|
||||
author: "Kento2"
|
||||
Loading…
Add table
Add a link
Reference in a new issue