From ecd4cec8545460a4fc4ca65b911c2503a00cd8e7 Mon Sep 17 00:00:00 2001 From: Luck Date: Tue, 16 Apr 2019 21:37:59 +0100 Subject: Lots of refactoring, add tps command --- .../me/lucko/spark/sponge/SparkSpongePlugin.java | 192 +++++++++++---------- .../me/lucko/spark/sponge/SpongeTickCounter.java | 18 +- 2 files changed, 105 insertions(+), 105 deletions(-) (limited to 'spark-sponge/src/main') diff --git a/spark-sponge/src/main/java/me/lucko/spark/sponge/SparkSpongePlugin.java b/spark-sponge/src/main/java/me/lucko/spark/sponge/SparkSpongePlugin.java index 2abaf3f..6d0c56a 100644 --- a/spark-sponge/src/main/java/me/lucko/spark/sponge/SparkSpongePlugin.java +++ b/spark-sponge/src/main/java/me/lucko/spark/sponge/SparkSpongePlugin.java @@ -21,19 +21,18 @@ package me.lucko.spark.sponge; import com.google.inject.Inject; - import me.lucko.spark.common.SparkPlatform; -import me.lucko.spark.sampler.ThreadDumper; -import me.lucko.spark.sampler.TickCounter; - +import me.lucko.spark.common.SparkPlugin; +import me.lucko.spark.common.sampler.ThreadDumper; +import me.lucko.spark.common.sampler.TickCounter; import org.spongepowered.api.Game; import org.spongepowered.api.command.CommandCallable; import org.spongepowered.api.command.CommandResult; import org.spongepowered.api.command.CommandSource; import org.spongepowered.api.config.ConfigDir; -import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.event.Listener; import org.spongepowered.api.event.game.state.GameStartedServerEvent; +import org.spongepowered.api.event.game.state.GameStoppingServerEvent; import org.spongepowered.api.plugin.Dependency; import org.spongepowered.api.plugin.Plugin; import org.spongepowered.api.scheduler.AsynchronousExecutor; @@ -45,14 +44,15 @@ import org.spongepowered.api.text.serializer.TextSerializers; import org.spongepowered.api.world.Location; import org.spongepowered.api.world.World; +import javax.annotation.Nullable; import java.net.MalformedURLException; import java.net.URL; import java.nio.file.Path; import java.util.Collections; +import java.util.HashSet; import java.util.List; import java.util.Optional; - -import javax.annotation.Nullable; +import java.util.Set; @Plugin( id = "spark", @@ -65,80 +65,13 @@ import javax.annotation.Nullable; @Dependency(id = "spongeapi") } ) -public class SparkSpongePlugin implements CommandCallable { +public class SparkSpongePlugin implements SparkPlugin { private final Game game; private final Path configDirectory; private final SpongeExecutorService asyncExecutor; - private final SparkPlatform sparkPlatform = new SparkPlatform() { - private Text colorize(String message) { - return TextSerializers.FORMATTING_CODE.deserialize(message); - } - - private void broadcast(Text msg) { - SparkSpongePlugin.this.game.getServer().getConsole().sendMessage(msg); - for (Player player : SparkSpongePlugin.this.game.getServer().getOnlinePlayers()) { - if (player.hasPermission("spark")) { - player.sendMessage(msg); - } - } - } - - @Override - public String getVersion() { - return SparkSpongePlugin.class.getAnnotation(Plugin.class).version(); - } - - @Override - public Path getPluginFolder() { - return SparkSpongePlugin.this.configDirectory; - } - - @Override - public String getLabel() { - return "spark"; - } - - @Override - public void sendMessage(CommandSource sender, String message) { - sender.sendMessage(colorize(message)); - } - - @Override - public void sendMessage(String message) { - Text msg = colorize(message); - broadcast(msg); - } - - @Override - public void sendLink(String url) { - try { - Text msg = Text.builder(url) - .color(TextColors.GRAY) - .onClick(TextActions.openUrl(new URL(url))) - .build(); - broadcast(msg); - } catch (MalformedURLException e) { - e.printStackTrace(); - } - } - - @Override - public void runAsync(Runnable r) { - SparkSpongePlugin.this.asyncExecutor.execute(r); - } - - @Override - public ThreadDumper getDefaultThreadDumper() { - return new ThreadDumper.Specific(new long[]{Thread.currentThread().getId()}); - } - - @Override - public TickCounter newTickCounter() { - return new SpongeTickCounter(SparkSpongePlugin.this); - } - }; + private final SparkPlatform platform = new SparkPlatform<>(this); @Inject public SparkSpongePlugin(Game game, @ConfigDir(sharedRoot = false) Path configDirectory, @AsynchronousExecutor SpongeExecutorService asyncExecutor) { @@ -148,43 +81,112 @@ public class SparkSpongePlugin implements CommandCallable { } @Listener - public void onServerStart(GameStartedServerEvent event) { - this.game.getCommandManager().register(this, this, "spark"); + public void onEnable(GameStartedServerEvent event) { + this.platform.enable(); + this.game.getCommandManager().register(this, new SparkCommand(this), "spark"); + } + + @Listener + public void onDisable(GameStoppingServerEvent event) { + this.platform.disable(); } @Override - public CommandResult process(CommandSource source, String arguments) { - if (!testPermission(source)) { - source.sendMessage(Text.builder("You do not have permission to use this command.").color(TextColors.RED).build()); - return CommandResult.empty(); - } + public String getVersion() { + return SparkSpongePlugin.class.getAnnotation(Plugin.class).version(); + } - this.sparkPlatform.executeCommand(source, arguments.split(" ")); - return CommandResult.empty(); + @Override + public Path getPluginFolder() { + return this.configDirectory; } @Override - public List getSuggestions(CommandSource source, String arguments, @Nullable Location targetPosition) { - return Collections.emptyList(); + public String getLabel() { + return "spark"; } @Override - public boolean testPermission(CommandSource source) { - return source.hasPermission("spark"); + public Set getSenders() { + Set senders = new HashSet<>(this.game.getServer().getOnlinePlayers()); + senders.add(this.game.getServer().getConsole()); + return senders; } @Override - public Optional getShortDescription(CommandSource source) { - return Optional.of(Text.of("Main spark plugin command")); + public void sendMessage(CommandSource sender, String message) { + sender.sendMessage(TextSerializers.FORMATTING_CODE.deserialize(message)); + } + + @Override + public void sendLink(CommandSource sender, String url) { + try { + Text msg = Text.builder(url) + .color(TextColors.GRAY) + .onClick(TextActions.openUrl(new URL(url))) + .build(); + sender.sendMessage(msg); + } catch (MalformedURLException e) { + e.printStackTrace(); + } } @Override - public Optional getHelp(CommandSource source) { - return Optional.of(Text.of("Run '/spark' to view usage.")); + public void runAsync(Runnable r) { + this.asyncExecutor.execute(r); } @Override - public Text getUsage(CommandSource source) { - return Text.of("Run '/spark' to view usage."); + public ThreadDumper getDefaultThreadDumper() { + return new ThreadDumper.Specific(new long[]{Thread.currentThread().getId()}); + } + + @Override + public TickCounter createTickCounter() { + return new SpongeTickCounter(this); + } + + private static final class SparkCommand implements CommandCallable { + private final SparkSpongePlugin plugin; + + private SparkCommand(SparkSpongePlugin plugin) { + this.plugin = plugin; + } + + @Override + public CommandResult process(CommandSource source, String arguments) { + if (!testPermission(source)) { + source.sendMessage(Text.builder("You do not have permission to use this command.").color(TextColors.RED).build()); + return CommandResult.empty(); + } + + this.plugin.platform.executeCommand(source, arguments.split(" ")); + return CommandResult.empty(); + } + + @Override + public List getSuggestions(CommandSource source, String arguments, @Nullable Location targetPosition) { + return Collections.emptyList(); + } + + @Override + public boolean testPermission(CommandSource source) { + return source.hasPermission("spark"); + } + + @Override + public Optional getShortDescription(CommandSource source) { + return Optional.of(Text.of("Main spark plugin command")); + } + + @Override + public Optional getHelp(CommandSource source) { + return Optional.of(Text.of("Run '/spark' to view usage.")); + } + + @Override + public Text getUsage(CommandSource source) { + return Text.of("Run '/spark' to view usage."); + } } } diff --git a/spark-sponge/src/main/java/me/lucko/spark/sponge/SpongeTickCounter.java b/spark-sponge/src/main/java/me/lucko/spark/sponge/SpongeTickCounter.java index 14b30b8..187f301 100644 --- a/spark-sponge/src/main/java/me/lucko/spark/sponge/SpongeTickCounter.java +++ b/spark-sponge/src/main/java/me/lucko/spark/sponge/SpongeTickCounter.java @@ -20,18 +20,16 @@ package me.lucko.spark.sponge; -import me.lucko.spark.sampler.TickCounter; - -import org.spongepowered.api.scheduler.Task; +import me.lucko.spark.common.sampler.TickCounter; import java.util.HashSet; import java.util.Set; public class SpongeTickCounter implements TickCounter, Runnable { private final SparkSpongePlugin plugin; - private Task task; + private org.spongepowered.api.scheduler.Task task; - private final Set tasks = new HashSet<>(); + private final Set tasks = new HashSet<>(); private int tick = 0; public SpongeTickCounter(SparkSpongePlugin plugin) { @@ -40,15 +38,15 @@ public class SpongeTickCounter implements TickCounter, Runnable { @Override public void run() { - for (Runnable r : this.tasks){ - r.run(); + for (TickTask r : this.tasks){ + r.onTick(this); } this.tick++; } @Override public void start() { - this.task = Task.builder().intervalTicks(1).name("spark-ticker").execute(this).submit(this.plugin); + this.task = org.spongepowered.api.scheduler.Task.builder().intervalTicks(1).name("spark-ticker").execute(this).submit(this.plugin); } @Override @@ -62,12 +60,12 @@ public class SpongeTickCounter implements TickCounter, Runnable { } @Override - public void addTickTask(Runnable runnable) { + public void addTickTask(TickTask runnable) { this.tasks.add(runnable); } @Override - public void removeTickTask(Runnable runnable) { + public void removeTickTask(TickTask runnable) { this.tasks.remove(runnable); } } -- cgit