From 1450849c342cf956e223e3768b16457ef74ec0d8 Mon Sep 17 00:00:00 2001 From: Potato Date: Wed, 21 Nov 2018 08:12:46 +0800 Subject: [PATCH] Fix matchstats displaying wrongly (#84) * Implement matchstats command * Hotfix for displaying matchstats * Improvements to the command * this should take precedence --- .../oc/pgm/playerstats/MatchStatsCommand.java | 43 ++++++++++++++----- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/PGM/src/main/java/tc/oc/pgm/playerstats/MatchStatsCommand.java b/PGM/src/main/java/tc/oc/pgm/playerstats/MatchStatsCommand.java index fdd0cf6..651f615 100644 --- a/PGM/src/main/java/tc/oc/pgm/playerstats/MatchStatsCommand.java +++ b/PGM/src/main/java/tc/oc/pgm/playerstats/MatchStatsCommand.java @@ -6,17 +6,23 @@ import com.sk89q.minecraft.util.commands.CommandContext; import com.sk89q.minecraft.util.commands.CommandException; import com.sk89q.minecraft.util.commands.CommandPermissions; import java.text.DecimalFormat; +import java.util.List; import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.TranslatableComponent; import org.bukkit.command.CommandSender; +import tc.oc.api.bukkit.users.OnlinePlayers; +import tc.oc.commons.bukkit.chat.Audiences; import tc.oc.commons.bukkit.chat.HeaderComponent; -import tc.oc.commons.bukkit.chat.NameStyle; +import tc.oc.commons.bukkit.chat.PlayerComponent; import tc.oc.commons.bukkit.commands.UserFinder; +import tc.oc.commons.bukkit.nick.IdentityProvider; +import tc.oc.commons.core.chat.Audience; import tc.oc.commons.core.chat.Component; import tc.oc.commons.core.chat.Components; import tc.oc.commons.core.commands.CommandFutureCallback; import tc.oc.commons.core.concurrent.Flexecutor; +import tc.oc.commons.core.formatting.StringUtils; import tc.oc.minecraft.scheduler.Sync; import tc.oc.pgm.match.MatchPlayer; import tc.oc.pgm.match.inject.MatchScoped; @@ -29,12 +35,18 @@ import javax.inject.Inject; public class MatchStatsCommand { private final UserFinder userFinder; private final Flexecutor flexecutor; + private final OnlinePlayers onlinePlayers; + private final IdentityProvider identityProvider; + private final Audiences audiences; private static final DecimalFormat FORMAT = new DecimalFormat("0.00"); - @Inject MatchStatsCommand(UserFinder userFinder, @Sync Flexecutor flexecutor) { + @Inject MatchStatsCommand(UserFinder userFinder, @Sync Flexecutor flexecutor, OnlinePlayers onlinePlayers, IdentityProvider identityProvider, Audiences audiences) { this.userFinder = userFinder; this.flexecutor = flexecutor; + this.onlinePlayers = onlinePlayers; + this.identityProvider = identityProvider; + this.audiences = audiences; } @Command(aliases = {"matchstats", "mstats"}, @@ -43,15 +55,23 @@ public class MatchStatsCommand { max = 1 ) @CommandPermissions("pgm.playerstats.matchstats") - public void matchStats(CommandContext args, CommandSender sender) throws CommandException { + public List matchStats(CommandContext args, CommandSender sender) throws CommandException { + if(args.getSuggestionContext() != null) { + if(args.getSuggestionContext().getIndex() == 0) { + return StringUtils.complete(args.getString(0), + onlinePlayers.stream().map(player -> player.getFakeDisplayName(sender))); + } + } + if(args.argsLength() == 0) { - displayStats(senderToMatchPlayer(sender)); + displayStats(sender, senderToMatchPlayer(sender)); } else { flexecutor.callback( - userFinder.findLocalPlayerOrSender(sender, args, 0), - CommandFutureCallback.onSuccess(sender, user -> displayStats(senderToMatchPlayer(user.player()))) + userFinder.findLocalPlayer(sender, args, 0), + CommandFutureCallback.onSuccess(sender, user -> displayStats(sender, senderToMatchPlayer(user.player()))) ); } + return null; } private BaseComponent parseStats(MatchPlayer player) { @@ -61,9 +81,12 @@ public class MatchStatsCommand { BaseComponent matchRatio = new Component(ChatColor.AQUA).translate("command.matchstats.kdr", FORMAT.format((double) facet.matchKills() / Math.max(facet.deaths(), 1))); return Components.join(Components.newline(), Lists.newArrayList(matchKills, matchDeaths, matchRatio)); } - - private void displayStats(MatchPlayer player) { - player.sendMessage(new HeaderComponent(new TranslatableComponent("command.matchstats.header", player.getStyledName(NameStyle.VERBOSE)))); - player.sendMessage(parseStats(player)); + + private void displayStats(CommandSender sender, MatchPlayer player) { + Audience audience = audiences.get(sender); + audience.sendMessage(new HeaderComponent( + new TranslatableComponent("command.matchstats.header", + new PlayerComponent(identityProvider.createIdentity(player.getBukkit()))))); + audience.sendMessage(parseStats(player)); } }