move profiler command to separate addon

This commit is contained in:
dfsek
2021-12-31 18:20:44 -07:00
parent 012a01da71
commit edac8953c1
5 changed files with 96 additions and 51 deletions

View File

@@ -0,0 +1,5 @@
version = version("0.1.0")
dependencies {
shadedApi(project(":common:addons:manifest-addon-loader"))
}

View File

@@ -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<CommandSender> 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.");
}));
});
}
}

View File

@@ -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

View File

@@ -165,7 +165,7 @@ public abstract class AbstractPlatform implements Platform {
List<BaseAddon> addonList = new ArrayList<>();
InternalAddon internalAddon = new InternalAddon(this);
InternalAddon internalAddon = new InternalAddon();
addonList.add(internalAddon);

View File

@@ -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<CommandSender> 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