diff --git a/common/addons/command-profiler/build.gradle.kts b/common/addons/command-profiler/build.gradle.kts new file mode 100644 index 000000000..5aa7c37fb --- /dev/null +++ b/common/addons/command-profiler/build.gradle.kts @@ -0,0 +1,5 @@ +version = version("0.1.0") + +dependencies { + shadedApi(project(":common:addons:manifest-addon-loader")) +} diff --git a/common/addons/command-profiler/src/main/java/com/dfsek/terra/addons/commands/profiler/ProfilerCommandAddon.java b/common/addons/command-profiler/src/main/java/com/dfsek/terra/addons/commands/profiler/ProfilerCommandAddon.java new file mode 100644 index 000000000..821a96020 --- /dev/null +++ b/common/addons/command-profiler/src/main/java/com/dfsek/terra/addons/commands/profiler/ProfilerCommandAddon.java @@ -0,0 +1,76 @@ +package com.dfsek.terra.addons.commands.profiler; + +import cloud.commandframework.ArgumentDescription; +import cloud.commandframework.CommandManager; + +import com.dfsek.terra.addons.manifest.api.AddonInitializer; +import com.dfsek.terra.api.Platform; +import com.dfsek.terra.api.addon.BaseAddon; +import com.dfsek.terra.api.command.arguments.RegistryArgument; +import com.dfsek.terra.api.command.CommandSender; +import com.dfsek.terra.api.event.events.platform.CommandRegistrationEvent; +import com.dfsek.terra.api.event.functional.FunctionalEventHandler; +import com.dfsek.terra.api.inject.annotations.Inject; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class ProfilerCommandAddon implements AddonInitializer { + private static final Logger logger = LoggerFactory.getLogger(ProfilerCommandAddon.class); + + @Inject + private Platform platform; + + @Inject + private BaseAddon addon; + + + @Override + public void initialize() { + platform.getEventManager() + .getHandler(FunctionalEventHandler.class) + .register(addon, CommandRegistrationEvent.class) + .then(event -> { + CommandManager manager = event.getCommandManager(); + manager + .command( + manager.commandBuilder("profiler", ArgumentDescription.of("Access the profiler")) + .literal("start", ArgumentDescription.of("Start profiling"), "st") + .permission("terra.profiler.start") + .handler(context -> { + platform.getProfiler().start(); + context.getSender().sendMessage("Profiling started."); + })) + .command( + manager.commandBuilder("profiler", ArgumentDescription.of("Access the profiler")) + .literal("stop", ArgumentDescription.of("Stop profiling"), "s") + .permission("terra.profiler.stop") + .handler(context -> { + platform.getProfiler().stop(); + context.getSender().sendMessage("Profiling stopped."); + })) + .command( + manager.commandBuilder("profiler", ArgumentDescription.of("Access the profiler")) + .literal("query", ArgumentDescription.of("Query profiler results"), "q") + .permission("terra.profiler.query") + .handler(context -> { + StringBuilder data = new StringBuilder("Terra Profiler data: \n"); + platform.getProfiler().getTimings().forEach((id, timings) -> data.append(id) + .append(": ") + .append(timings.toString()) + .append('\n')); + logger.info(data.toString()); + context.getSender().sendMessage("Profiling data dumped to console."); + })) + .command( + manager.commandBuilder("profiler", ArgumentDescription.of("Access the profiler")) + .literal("reset", ArgumentDescription.of("Reset the profiler"), "r") + .permission("terra.profiler.reset") + .handler(context -> { + platform.getProfiler().reset(); + context.getSender().sendMessage("Profiler reset."); + })); + }); + } +} diff --git a/common/addons/command-profiler/src/main/resources/terra.addon.yml b/common/addons/command-profiler/src/main/resources/terra.addon.yml new file mode 100644 index 000000000..fea79d736 --- /dev/null +++ b/common/addons/command-profiler/src/main/resources/terra.addon.yml @@ -0,0 +1,12 @@ +schema-version: 1 +contributors: + - Terra contributors +id: command-profiler +version: @VERSION@ +entrypoints: + - "com.dfsek.terra.addons.commands.profiler.ProfilerCommandAddon" +website: + issues: https://github.com/PolyhedralDev/Terra/issues + source: https://github.com/PolyhedralDev/Terra + docs: https://github.com/PolyhedralDev/Terra/wiki +license: MIT License \ No newline at end of file diff --git a/common/implementation/base/src/main/java/com/dfsek/terra/AbstractPlatform.java b/common/implementation/base/src/main/java/com/dfsek/terra/AbstractPlatform.java index 05e9a1260..914c9f39d 100644 --- a/common/implementation/base/src/main/java/com/dfsek/terra/AbstractPlatform.java +++ b/common/implementation/base/src/main/java/com/dfsek/terra/AbstractPlatform.java @@ -165,7 +165,7 @@ public abstract class AbstractPlatform implements Platform { List addonList = new ArrayList<>(); - InternalAddon internalAddon = new InternalAddon(this); + InternalAddon internalAddon = new InternalAddon(); addonList.add(internalAddon); diff --git a/common/implementation/base/src/main/java/com/dfsek/terra/addon/InternalAddon.java b/common/implementation/base/src/main/java/com/dfsek/terra/addon/InternalAddon.java index 99033b117..bba04251d 100644 --- a/common/implementation/base/src/main/java/com/dfsek/terra/addon/InternalAddon.java +++ b/common/implementation/base/src/main/java/com/dfsek/terra/addon/InternalAddon.java @@ -34,58 +34,10 @@ import com.dfsek.terra.api.event.functional.FunctionalEventHandler; public class InternalAddon implements BaseAddon { - private static final Logger logger = LoggerFactory.getLogger(InternalAddon.class); private static final Version VERSION = Versions.getVersion(1, 0, 0); - public InternalAddon(Platform platform) { - platform.getEventManager() - .getHandler(FunctionalEventHandler.class) - .register(this, CommandRegistrationEvent.class) - .then(event -> { - CommandManager manager = event.getCommandManager(); - manager - .command( - manager.commandBuilder("profiler", ArgumentDescription.of("Access the profiler")) - .literal("start", ArgumentDescription.of("Start profiling"), "st") - .permission("terra.profiler.start") - .handler(context -> { - platform.getProfiler().start(); - context.getSender().sendMessage("Profiling started."); - })) - .command( - manager.commandBuilder("profiler", ArgumentDescription.of("Access the profiler")) - .literal("stop", ArgumentDescription.of("Stop profiling"), "s") - .permission("terra.profiler.stop") - .handler(context -> { - platform.getProfiler().stop(); - context.getSender().sendMessage("Profiling stopped."); - })) - .command( - manager.commandBuilder("profiler", ArgumentDescription.of("Access the profiler")) - .literal("query", ArgumentDescription.of("Query profiler results"), "q") - .permission("terra.profiler.query") - .handler(context -> { - StringBuilder data = new StringBuilder("Terra Profiler data: \n"); - platform.getProfiler().getTimings().forEach((id, timings) -> data.append(id) - .append(": ") - .append(timings.toString()) - .append('\n')); - logger.info(data.toString()); - context.getSender().sendMessage("Profiling data dumped to console."); - })) - .command( - manager.commandBuilder("profiler", ArgumentDescription.of("Access the profiler")) - .literal("reset", ArgumentDescription.of("Reset the profiler"), "r") - .permission("terra.profiler.reset") - .handler(context -> { - platform.getProfiler().reset(); - context.getSender().sendMessage("Profiler reset."); - })) - ; - - - }); - + public InternalAddon() { + } @Override