diff options
Diffstat (limited to 'sponge/src/main/java/me')
-rw-r--r-- | sponge/src/main/java/me/lucko/spark/sponge/SparkSpongePlugin.java | 37 | ||||
-rw-r--r-- | sponge/src/main/java/me/lucko/spark/sponge/SpongeTickCounter.java | 54 |
2 files changed, 85 insertions, 6 deletions
diff --git a/sponge/src/main/java/me/lucko/spark/sponge/SparkSpongePlugin.java b/sponge/src/main/java/me/lucko/spark/sponge/SparkSpongePlugin.java index 9149cb0..b77b13c 100644 --- a/sponge/src/main/java/me/lucko/spark/sponge/SparkSpongePlugin.java +++ b/sponge/src/main/java/me/lucko/spark/sponge/SparkSpongePlugin.java @@ -4,18 +4,20 @@ import com.google.inject.Inject; import me.lucko.spark.common.CommandHandler; import me.lucko.spark.profiler.ThreadDumper; +import me.lucko.spark.profiler.TickCounter; import me.lucko.spark.sponge.utils.PomData; import org.spongepowered.api.Game; +import org.spongepowered.api.Sponge; import org.spongepowered.api.command.CommandCallable; import org.spongepowered.api.command.CommandResult; import org.spongepowered.api.command.CommandSource; +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.plugin.Plugin; import org.spongepowered.api.scheduler.AsynchronousExecutor; import org.spongepowered.api.scheduler.SpongeExecutorService; -import org.spongepowered.api.text.LiteralText; import org.spongepowered.api.text.Text; import org.spongepowered.api.text.action.TextActions; import org.spongepowered.api.text.format.TextColors; @@ -41,20 +43,38 @@ import javax.annotation.Nullable; public class SparkSpongePlugin implements CommandCallable { private final CommandHandler<CommandSource> commandHandler = new CommandHandler<CommandSource>() { + private Text colorize(String message) { + return TextSerializers.FORMATTING_CODE.deserialize(message); + } + + private void broadcast(Text msg) { + Sponge.getServer().getConsole().sendMessage(msg); + for (Player player : Sponge.getServer().getOnlinePlayers()) { + if (player.hasPermission("spark.profiler")) { + player.sendMessage(msg); + } + } + } + @Override protected void sendMessage(CommandSource sender, String message) { - sender.sendMessage(TextSerializers.FORMATTING_CODE.deserialize(message)); + sender.sendMessage(colorize(message)); + } + + @Override + protected void sendMessage(String message) { + Text msg = colorize(message); + broadcast(msg); } @Override - protected void sendLink(CommandSource sender, String url) { + protected void sendLink(String url) { try { - LiteralText text = Text.builder(url) + Text msg = Text.builder(url) .color(TextColors.GRAY) .onClick(TextActions.openUrl(new URL(url))) .build(); - - sender.sendMessage(text); + broadcast(msg); } catch (MalformedURLException e) { e.printStackTrace(); } @@ -69,6 +89,11 @@ public class SparkSpongePlugin implements CommandCallable { protected ThreadDumper getDefaultThreadDumper() { return new ThreadDumper.Specific(new long[]{Thread.currentThread().getId()}); } + + @Override + protected TickCounter newTickCounter() { + return new SpongeTickCounter(SparkSpongePlugin.this); + } }; @Inject diff --git a/sponge/src/main/java/me/lucko/spark/sponge/SpongeTickCounter.java b/sponge/src/main/java/me/lucko/spark/sponge/SpongeTickCounter.java new file mode 100644 index 0000000..bda2a69 --- /dev/null +++ b/sponge/src/main/java/me/lucko/spark/sponge/SpongeTickCounter.java @@ -0,0 +1,54 @@ +package me.lucko.spark.sponge; + +import me.lucko.spark.profiler.TickCounter; + +import org.spongepowered.api.scheduler.Task; + +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.atomic.LongAdder; + +public class SpongeTickCounter implements TickCounter, Runnable { + private final SparkSpongePlugin plugin; + private Task task; + + private final Set<Runnable> tasks = new HashSet<>(); + private final LongAdder tick = new LongAdder(); + + public SpongeTickCounter(SparkSpongePlugin plugin) { + this.plugin = plugin; + } + + @Override + public void run() { + this.tick.increment(); + for (Runnable r : this.tasks){ + r.run(); + } + } + + @Override + public void start() { + this.task = Task.builder().intervalTicks(1).name("spark-ticker").execute(this).submit(this.plugin); + } + + @Override + public void close() { + this.task.cancel(); + } + + @Override + public long getCurrentTick() { + return this.tick.longValue(); + } + + @Override + public void addTickTask(Runnable runnable) { + this.tasks.add(runnable); + } + + @Override + public void removeTickTask(Runnable runnable) { + this.tasks.remove(runnable); + } +} |