aboutsummaryrefslogtreecommitdiff
path: root/spark-sponge8/src/main/java/me/lucko
diff options
context:
space:
mode:
authorPhillip Schichtel <pschichtel@users.noreply.github.com>2021-04-05 11:22:23 +0200
committerGitHub <noreply@github.com>2021-04-05 10:22:23 +0100
commit75f1e4d0c20c38e7d65a7c558d96213145b682e6 (patch)
tree469ccf586e602b6c132adf05250440109f8f7910 /spark-sponge8/src/main/java/me/lucko
parenta3129bc834bf22b89f7ac545a960b89361b03459 (diff)
downloadspark-75f1e4d0c20c38e7d65a7c558d96213145b682e6.tar.gz
spark-75f1e4d0c20c38e7d65a7c558d96213145b682e6.tar.bz2
spark-75f1e4d0c20c38e7d65a7c558d96213145b682e6.zip
Sponge API8 (#110)
Diffstat (limited to 'spark-sponge8/src/main/java/me/lucko')
-rw-r--r--spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8CommandSender.java64
-rw-r--r--spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8PlatformInfo.java59
-rw-r--r--spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8SparkPlugin.java190
-rw-r--r--spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8TickHook.java63
4 files changed, 376 insertions, 0 deletions
diff --git a/spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8CommandSender.java b/spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8CommandSender.java
new file mode 100644
index 0000000..cc0778c
--- /dev/null
+++ b/spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8CommandSender.java
@@ -0,0 +1,64 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package me.lucko.spark.sponge;
+
+import me.lucko.spark.common.command.sender.AbstractCommandSender;
+
+import net.kyori.adventure.audience.Audience;
+import net.kyori.adventure.text.Component;
+import org.spongepowered.api.service.permission.Subject;
+
+
+import java.util.UUID;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+public class Sponge8CommandSender extends AbstractCommandSender<Audience> {
+ private final Subject subject;
+
+ public Sponge8CommandSender(Subject subject, Audience audience) {
+ super(audience);
+ this.subject = subject;
+ }
+
+ @Override
+ public String getName() {
+ return subject.friendlyIdentifier().orElse(subject.identifier());
+ }
+
+ @Override
+ public UUID getUniqueId() {
+ try {
+ return UUID.fromString(subject.identifier());
+ } catch (Exception e) {
+ return UUID.nameUUIDFromBytes(subject.identifier().getBytes(UTF_8));
+ }
+ }
+
+ @Override
+ public void sendMessage(Component message) {
+ delegate.sendMessage(message);
+ }
+
+ @Override
+ public boolean hasPermission(String permission) {
+ return subject.hasPermission(permission);
+ }
+}
diff --git a/spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8PlatformInfo.java b/spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8PlatformInfo.java
new file mode 100644
index 0000000..87f5b17
--- /dev/null
+++ b/spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8PlatformInfo.java
@@ -0,0 +1,59 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package me.lucko.spark.sponge;
+
+import me.lucko.spark.common.platform.AbstractPlatformInfo;
+
+import org.spongepowered.api.Game;
+import org.spongepowered.api.Platform;
+import org.spongepowered.plugin.metadata.PluginMetadata;
+
+public class Sponge8PlatformInfo extends AbstractPlatformInfo {
+ private final Game game;
+
+ public Sponge8PlatformInfo(Game game) {
+ this.game = game;
+ }
+
+ @Override
+ public Type getType() {
+ return Type.SERVER;
+ }
+
+ @Override
+ public String getName() {
+ return getMetadata().getName().orElse(getMetadata().getId());
+ }
+
+ @Override
+ public String getVersion() {
+ return getMetadata().getVersion();
+ }
+
+ private PluginMetadata getMetadata() {
+ return this.game.platform().container(Platform.Component.IMPLEMENTATION).getMetadata();
+ }
+
+ @Override
+ public String getMinecraftVersion() {
+ return this.game.platform().minecraftVersion().name();
+ }
+}
diff --git a/spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8SparkPlugin.java b/spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8SparkPlugin.java
new file mode 100644
index 0000000..aa05153
--- /dev/null
+++ b/spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8SparkPlugin.java
@@ -0,0 +1,190 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package me.lucko.spark.sponge;
+
+import com.google.inject.Inject;
+
+import me.lucko.spark.common.SparkPlatform;
+import me.lucko.spark.common.SparkPlugin;
+import me.lucko.spark.common.command.sender.CommandSender;
+import me.lucko.spark.common.platform.PlatformInfo;
+import me.lucko.spark.common.sampler.ThreadDumper;
+import me.lucko.spark.common.tick.TickHook;
+
+import net.kyori.adventure.text.Component;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.spongepowered.api.Game;
+import org.spongepowered.api.Server;
+import org.spongepowered.api.command.Command;
+import org.spongepowered.api.command.CommandCause;
+import org.spongepowered.api.command.CommandResult;
+import org.spongepowered.api.command.exception.CommandException;
+import org.spongepowered.api.command.parameter.ArgumentReader;
+import org.spongepowered.api.command.registrar.tree.CommandTreeNode;
+import org.spongepowered.api.config.ConfigDir;
+import org.spongepowered.api.event.Listener;
+import org.spongepowered.api.event.lifecycle.RegisterCommandEvent;
+import org.spongepowered.api.event.lifecycle.StartedEngineEvent;
+import org.spongepowered.api.event.lifecycle.StoppingEngineEvent;
+import org.spongepowered.plugin.PluginContainer;
+import org.spongepowered.plugin.jvm.Plugin;
+
+import java.nio.file.Path;
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.ExecutorService;
+import java.util.stream.Stream;
+
+@Plugin("spark")
+public class Sponge8SparkPlugin implements SparkPlugin {
+
+ private final PluginContainer pluginContainer;
+ private final Game game;
+ private final Path configDirectory;
+ private final ExecutorService asyncExecutor;
+
+ private SparkPlatform platform;
+ private final ThreadDumper.GameThread threadDumper = new ThreadDumper.GameThread();
+
+ @Inject
+ public Sponge8SparkPlugin(PluginContainer pluginContainer, Game game, @ConfigDir(sharedRoot = false) Path configDirectory) {
+
+ this.pluginContainer = pluginContainer;
+ this.game = game;
+ this.configDirectory = configDirectory;
+ this.asyncExecutor = game.asyncScheduler().createExecutor(pluginContainer);
+ }
+
+
+ @Listener
+ public void onRegisterCommands(final RegisterCommandEvent<Command.Raw> event) {
+ event.register(this.pluginContainer, new SparkCommand(this), pluginContainer.getMetadata().getId());
+ }
+
+ @Listener
+ public void onEnable(StartedEngineEvent<Server> event) {
+ this.platform = new SparkPlatform(this);
+ this.platform.enable();
+ }
+
+ @Listener
+ public void onDisable(StoppingEngineEvent<Server> event) {
+ this.platform.disable();
+ }
+
+ @Override
+ public String getVersion() {
+ return this.pluginContainer.getMetadata().getVersion();
+ }
+
+ @Override
+ public Path getPluginDirectory() {
+ return this.configDirectory;
+ }
+
+ @Override
+ public String getCommandName() {
+ return "spark";
+ }
+
+ @Override
+ public Stream<CommandSender> getCommandSenders() {
+ return Stream.concat(
+ this.game.server().onlinePlayers().stream(),
+ Stream.of(this.game.systemSubject())
+ ).map(s -> new Sponge8CommandSender(s, s));
+ }
+
+ @Override
+ public void executeAsync(Runnable task) {
+ this.asyncExecutor.execute(task);
+ }
+
+ @Override
+ public ThreadDumper getDefaultThreadDumper() {
+ return this.threadDumper.get();
+ }
+
+ @Override
+ public TickHook createTickHook() {
+ return new Sponge8TickHook(this.pluginContainer, this.game);
+ }
+
+ @Override
+ public PlatformInfo getPlatformInfo() {
+ return new Sponge8PlatformInfo(this.game);
+ }
+
+ private static final class SparkCommand implements Command.Raw {
+
+ private Sponge8SparkPlugin plugin;
+
+ public SparkCommand(Sponge8SparkPlugin plugin) {
+ this.plugin = plugin;
+ }
+
+ @Override
+ public CommandResult process(CommandCause cause, ArgumentReader.Mutable arguments) {
+ this.plugin.threadDumper.ensureSetup();
+ this.plugin.platform.executeCommand(getSenderFromCause(cause), arguments.input().split(" "));
+ return CommandResult.empty();
+ }
+
+ @Override
+ public List<String> suggestions(CommandCause cause, ArgumentReader.Mutable arguments) {
+ return this.plugin.platform.tabCompleteCommand(getSenderFromCause(cause), arguments.input().split(" "));
+ }
+
+ private static CommandSender getSenderFromCause(CommandCause cause) {
+ return new Sponge8CommandSender(cause.subject(), cause.audience());
+ }
+
+ @Override
+ public boolean canExecute(CommandCause cause) {
+ return this.plugin.platform.hasPermissionForAnyCommand(getSenderFromCause(cause));
+ }
+
+ @Override
+ public Optional<Component> shortDescription(CommandCause cause) {
+ return Optional.of(Component.text("Main spark plugin command"));
+ }
+
+ @Override
+ public Optional<Component> extendedDescription(CommandCause cause) {
+ return Optional.empty();
+ }
+
+ @Override
+ public Component usage(CommandCause cause) {
+ return Component.text("Run '/spark' to view usage.");
+ }
+
+ @Override
+ public CommandTreeNode.Root commandTree() {
+ return Command.Raw.super.commandTree();
+ }
+
+ @Override
+ public Optional<Component> help(@NonNull CommandCause cause) {
+ return Optional.of(Component.text("Run '/spark' to view usage."));
+ }
+ }
+}
diff --git a/spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8TickHook.java b/spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8TickHook.java
new file mode 100644
index 0000000..29698c9
--- /dev/null
+++ b/spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8TickHook.java
@@ -0,0 +1,63 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package me.lucko.spark.sponge;
+
+import me.lucko.spark.common.tick.AbstractTickHook;
+import me.lucko.spark.common.tick.TickHook;
+
+import org.spongepowered.api.Game;
+import org.spongepowered.api.scheduler.ScheduledTask;
+import org.spongepowered.api.scheduler.Task;
+import org.spongepowered.api.util.Ticks;
+import org.spongepowered.plugin.PluginContainer;
+
+public class Sponge8TickHook extends AbstractTickHook implements TickHook, Runnable {
+ private final PluginContainer plugin;
+ private final Game game;
+ private ScheduledTask task;
+
+ public Sponge8TickHook(PluginContainer plugin, Game game) {
+ this.plugin = plugin;
+ this.game = game;
+ }
+
+ @Override
+ public void run() {
+ onTick();
+ }
+
+ @Override
+ public void start() {
+ Task task = Task.builder()
+ .interval(Ticks.of(1))
+ .name("spark-ticker")
+ .plugin(plugin)
+ .execute(this)
+ .build();
+ this.task = game.server().scheduler().submit(task);
+ }
+
+ @Override
+ public void close() {
+ this.task.cancel();
+ }
+
+}