diff options
author | Luck <git@lucko.me> | 2019-04-16 21:37:59 +0100 |
---|---|---|
committer | Luck <git@lucko.me> | 2019-04-16 21:37:59 +0100 |
commit | ecd4cec8545460a4fc4ca65b911c2503a00cd8e7 (patch) | |
tree | 62067383a1044abc3a09724e89c6e7c619e87ec0 /spark-velocity | |
parent | 8a61b404848ed8e3c27f06eb73239d37d4273240 (diff) | |
download | spark-ecd4cec8545460a4fc4ca65b911c2503a00cd8e7.tar.gz spark-ecd4cec8545460a4fc4ca65b911c2503a00cd8e7.tar.bz2 spark-ecd4cec8545460a4fc4ca65b911c2503a00cd8e7.zip |
Lots of refactoring, add tps command
Diffstat (limited to 'spark-velocity')
-rw-r--r-- | spark-velocity/src/main/java/me/lucko/spark/velocity/SparkVelocityPlugin.java | 151 |
1 files changed, 76 insertions, 75 deletions
diff --git a/spark-velocity/src/main/java/me/lucko/spark/velocity/SparkVelocityPlugin.java b/spark-velocity/src/main/java/me/lucko/spark/velocity/SparkVelocityPlugin.java index cf5ed79..fef48e7 100644 --- a/spark-velocity/src/main/java/me/lucko/spark/velocity/SparkVelocityPlugin.java +++ b/spark-velocity/src/main/java/me/lucko/spark/velocity/SparkVelocityPlugin.java @@ -21,26 +21,29 @@ package me.lucko.spark.velocity; import com.google.inject.Inject; +import com.velocitypowered.api.command.Command; import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.event.PostOrder; import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; +import com.velocitypowered.api.event.proxy.ProxyShutdownEvent; import com.velocitypowered.api.plugin.Plugin; import com.velocitypowered.api.plugin.annotation.DataDirectory; -import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.ProxyServer; - import me.lucko.spark.common.SparkPlatform; -import me.lucko.spark.sampler.ThreadDumper; -import me.lucko.spark.sampler.TickCounter; - -import net.kyori.text.Component; +import me.lucko.spark.common.SparkPlugin; +import me.lucko.spark.common.sampler.ThreadDumper; +import me.lucko.spark.common.sampler.TickCounter; import net.kyori.text.TextComponent; import net.kyori.text.event.ClickEvent; import net.kyori.text.format.TextColor; import net.kyori.text.serializer.ComponentSerializers; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.optional.qual.MaybePresent; import java.nio.file.Path; +import java.util.HashSet; +import java.util.Set; @Plugin( id = "spark", @@ -49,92 +52,90 @@ import java.nio.file.Path; description = "@desc@", authors = {"Luck", "sk89q"} ) -public class SparkVelocityPlugin { +public class SparkVelocityPlugin implements SparkPlugin<CommandSource>, Command { + + private final SparkPlatform<CommandSource> platform = new SparkPlatform<>(this); private final ProxyServer proxy; private final Path configDirectory; - private final SparkPlatform<CommandSource> sparkPlatform = new SparkPlatform<CommandSource>() { - @SuppressWarnings("deprecation") - private TextComponent colorize(String message) { - return ComponentSerializers.LEGACY.deserialize(message, '&'); - } + @Inject + public SparkVelocityPlugin(ProxyServer proxy, @DataDirectory Path configDirectory) { + this.proxy = proxy; + this.configDirectory = configDirectory; + } - private void broadcast(Component msg) { - SparkVelocityPlugin.this.proxy.getConsoleCommandSource().sendMessage(msg); - for (Player player : SparkVelocityPlugin.this.proxy.getAllPlayers()) { - if (player.hasPermission("spark")) { - player.sendMessage(msg); - } - } - } + @Subscribe(order = PostOrder.FIRST) + public void onEnable(ProxyInitializeEvent e) { + this.platform.enable(); + this.proxy.getCommandManager().register(this, "sparkv", "sparkvelocity"); + } - @Override - public String getVersion() { - return SparkVelocityPlugin.class.getAnnotation(Plugin.class).version(); - } + @Subscribe(order = PostOrder.LAST) + public void onDisable(ProxyShutdownEvent e) { + this.platform.disable(); + } - @Override - public Path getPluginFolder() { - return SparkVelocityPlugin.this.configDirectory; + @Override + public void execute(@MaybePresent CommandSource sender, @NonNull @MaybePresent String[] args) { + if (!sender.hasPermission("spark")) { + TextComponent msg = TextComponent.builder("You do not have permission to use this command.").color(TextColor.RED).build(); + sender.sendMessage(msg); + return; } - @Override - public String getLabel() { - return "sparkv"; - } + this.platform.executeCommand(sender, args); + } - @Override - public void sendMessage(CommandSource sender, String message) { - sender.sendMessage(colorize(message)); - } + @Override + public String getVersion() { + return SparkVelocityPlugin.class.getAnnotation(Plugin.class).version(); + } - @Override - public void sendMessage(String message) { - broadcast(colorize(message)); - } + @Override + public Path getPluginFolder() { + return this.configDirectory; + } - @Override - public void sendLink(String url) { - TextComponent msg = TextComponent.builder(url) - .color(TextColor.GRAY) - .clickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, url)) - .build(); - broadcast(msg); - } + @Override + public String getLabel() { + return "sparkv"; + } - @Override - public void runAsync(Runnable r) { - SparkVelocityPlugin.this.proxy.getScheduler().buildTask(SparkVelocityPlugin.this, r).schedule(); - } + @Override + public Set<CommandSource> getSenders() { + Set<CommandSource> senders = new HashSet<>(this.proxy.getAllPlayers()); + senders.add(this.proxy.getConsoleCommandSource()); + return senders; + } - @Override - public ThreadDumper getDefaultThreadDumper() { - return ThreadDumper.ALL; - } + @SuppressWarnings("deprecation") + @Override + public void sendMessage(CommandSource sender, String message) { + sender.sendMessage(ComponentSerializers.LEGACY.deserialize(message, '&')); + } - @Override - public TickCounter newTickCounter() { - throw new UnsupportedOperationException(); - } - }; + @Override + public void sendLink(CommandSource sender, String url) { + TextComponent msg = TextComponent.builder(url) + .color(TextColor.GRAY) + .clickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, url)) + .build(); + sender.sendMessage(msg); + } - @Inject - public SparkVelocityPlugin(ProxyServer proxy, @DataDirectory Path configDirectory) { - this.proxy = proxy; - this.configDirectory = configDirectory; + @Override + public void runAsync(Runnable r) { + this.proxy.getScheduler().buildTask(this, r).schedule(); } - @Subscribe(order = PostOrder.FIRST) - public void onEnable(ProxyInitializeEvent e) { - this.proxy.getCommandManager().register((sender, args) -> { - if (!sender.hasPermission("spark")) { - TextComponent msg = TextComponent.builder("You do not have permission to use this command.").color(TextColor.RED).build(); - sender.sendMessage(msg); - return; - } - - SparkVelocityPlugin.this.sparkPlatform.executeCommand(sender, args); - }, "sparkv", "sparkvelocity"); + @Override + public ThreadDumper getDefaultThreadDumper() { + return ThreadDumper.ALL; + } + + @Override + public TickCounter createTickCounter() { + return null; } } |