diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/TimingsCommand.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/TimingsCommand.java index ca4b11228..30df7a7b7 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/TimingsCommand.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/TimingsCommand.java @@ -1,6 +1,12 @@ package io.github.thebusybiscuit.slimefun4.core.commands.subcommands; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Locale; +import java.util.Set; + import javax.annotation.Nonnull; +import javax.annotation.ParametersAreNonnullByDefault; import org.bukkit.command.CommandSender; import org.bukkit.command.ConsoleCommandSender; @@ -15,6 +21,9 @@ import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; class TimingsCommand extends SubCommand { + private static final String FLAG_PREFIX = "--"; + private final Set flags = new HashSet<>(Arrays.asList("verbose")); + TimingsCommand(SlimefunPlugin plugin, SlimefunCommand cmd) { super(plugin, cmd, "timings", false); } @@ -22,20 +31,61 @@ class TimingsCommand extends SubCommand { @Override public void onExecute(CommandSender sender, String[] args) { if (sender.hasPermission("slimefun.command.timings") || sender instanceof ConsoleCommandSender) { - sender.sendMessage("Please wait a second... The results are coming in!"); - PerformanceInspector inspector = inspectorOf(sender); + if (hasInvalidFlags(sender, args)) { + return; + } + + boolean verbose = hasFlag(args, "verbose"); + + if (verbose && sender instanceof Player) { + SlimefunPlugin.getLocalization().sendMessage(sender, "commands.timings.verbose-player", true); + return; + } + + SlimefunPlugin.getLocalization().sendMessage(sender, "commands.timings.please-wait", true); + + PerformanceInspector inspector = inspectorOf(sender, verbose); SlimefunPlugin.getProfiler().requestSummary(inspector); } else { SlimefunPlugin.getLocalization().sendMessage(sender, "messages.no-permission", true); } } + @ParametersAreNonnullByDefault + private boolean hasInvalidFlags(CommandSender sender, String[] args) { + boolean hasInvalidFlags = false; + + // We start at 1 because args[0] will be "timings". + for (int i = 1; i < args.length; i++) { + String argument = args[i].toLowerCase(Locale.ROOT); + + if (!argument.startsWith(FLAG_PREFIX) || !flags.contains(argument.substring(2))) { + hasInvalidFlags = true; + SlimefunPlugin.getLocalization().sendMessage(sender, "commands.timings.unknown-flag", true, msg -> msg.replace("%flag%", argument)); + } + } + + return hasInvalidFlags; + } + + @ParametersAreNonnullByDefault + private boolean hasFlag(String[] args, String flag) { + // We start at 1 because args[0] will be "timings". + for (int i = 1; i < args.length; i++) { + if (args[i].equalsIgnoreCase(FLAG_PREFIX + flag)) { + return true; + } + } + + return false; + } + @Nonnull - private PerformanceInspector inspectorOf(@Nonnull CommandSender sender) { + private PerformanceInspector inspectorOf(@Nonnull CommandSender sender, boolean verbose) { if (sender instanceof Player) { return new PlayerPerformanceInspector((Player) sender); } else { - return new ConsolePerformanceInspector(sender); + return new ConsolePerformanceInspector(sender, verbose); } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/PerformanceInspector.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/PerformanceInspector.java index 3106654ef..6a8fd5900 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/PerformanceInspector.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/PerformanceInspector.java @@ -36,6 +36,6 @@ public interface PerformanceInspector { * * @return Whether to send the full {@link PerformanceSummary} or a trimmed version */ - boolean hasFullView(); + boolean isVerbose(); } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/PerformanceSummary.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/PerformanceSummary.java index 56667b656..01057a966 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/PerformanceSummary.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/PerformanceSummary.java @@ -152,7 +152,7 @@ class PerformanceSummary { builder.append(ChatColor.YELLOW); for (Map.Entry entry : results) { - if (!inspector.hasFullView() || (shownEntries < MAX_ITEMS && (shownEntries < MIN_ITEMS || entry.getValue() > VISIBILITY_THRESHOLD))) { + if (!inspector.isVerbose() || (shownEntries < MAX_ITEMS && (shownEntries < MIN_ITEMS || entry.getValue() > VISIBILITY_THRESHOLD))) { builder.append("\n "); builder.append(ChatColor.stripColor(formatter.apply(entry))); shownEntries++; diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/inspectors/ConsolePerformanceInspector.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/inspectors/ConsolePerformanceInspector.java index 960dea230..6acb35a44 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/inspectors/ConsolePerformanceInspector.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/inspectors/ConsolePerformanceInspector.java @@ -23,16 +23,22 @@ public class ConsolePerformanceInspector implements PerformanceInspector { */ private final CommandSender console; + /** + * Whether a summary will be verbose or trimmed of. + */ + private final boolean verbose; + /** * This creates a new {@link ConsolePerformanceInspector} for the given {@link CommandSender}. * * @param console * The {@link CommandSender}, preferabbly a {@link ConsoleCommandSender} */ - public ConsolePerformanceInspector(@Nonnull CommandSender console) { + public ConsolePerformanceInspector(@Nonnull CommandSender console, boolean verbose) { Validate.notNull(console, "CommandSender cannot be null"); this.console = console; + this.verbose = verbose; } @Override @@ -42,8 +48,8 @@ public class ConsolePerformanceInspector implements PerformanceInspector { } @Override - public boolean hasFullView() { - return false; + public boolean isVerbose() { + return verbose; } @Override diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/inspectors/PlayerPerformanceInspector.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/inspectors/PlayerPerformanceInspector.java index 15ace9385..2e0697ff4 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/inspectors/PlayerPerformanceInspector.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/inspectors/PlayerPerformanceInspector.java @@ -50,7 +50,7 @@ public class PlayerPerformanceInspector implements PerformanceInspector { } @Override - public boolean hasFullView() { + public boolean isVerbose() { return false; } diff --git a/src/main/resources/languages/messages_en.yml b/src/main/resources/languages/messages_en.yml index 14727df62..ddf1c2139 100644 --- a/src/main/resources/languages/messages_en.yml +++ b/src/main/resources/languages/messages_en.yml @@ -26,6 +26,11 @@ commands: description: Charges the item you are holding charge-success: Item has been charged! not-rechargeable: This item can not be charged! + + timings: + please-wait: '&ePlease wait a second... The results are coming in!' + verbose-player: '&4The verbose flag cannot by used by a Player!' + unknown-flag: '&4Unknown flag: &c%flag%' guide: locked: 'LOCKED'