From 88f43d9a91d3d719fb626cdd081b3a9dd3013c6c Mon Sep 17 00:00:00 2001 From: GJ Date: Tue, 30 Apr 2013 09:48:53 -0400 Subject: [PATCH] Add command to clear or change scoreboards. --- .../nossr50/commands/McscoreboardCommand.java | 141 ++++++++++++++++++ src/main/java/com/gmail/nossr50/mcMMO.java | 1 + .../commands/CommandRegistrationManager.java | 11 ++ src/main/resources/plugin.yml | 5 + 4 files changed, 158 insertions(+) create mode 100644 src/main/java/com/gmail/nossr50/commands/McscoreboardCommand.java diff --git a/src/main/java/com/gmail/nossr50/commands/McscoreboardCommand.java b/src/main/java/com/gmail/nossr50/commands/McscoreboardCommand.java new file mode 100644 index 000000000..67ee40c9b --- /dev/null +++ b/src/main/java/com/gmail/nossr50/commands/McscoreboardCommand.java @@ -0,0 +1,141 @@ +package com.gmail.nossr50.commands; + +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabExecutor; +import org.bukkit.entity.Player; +import org.bukkit.util.StringUtil; + +import com.gmail.nossr50.mcMMO; +import com.gmail.nossr50.config.Config; +import com.gmail.nossr50.datatypes.skills.SkillType; +import com.gmail.nossr50.util.StringUtils; +import com.gmail.nossr50.util.commands.CommandUtils; +import com.gmail.nossr50.util.player.UserManager; +import com.gmail.nossr50.util.scoreboards.ScoreboardManager; +import com.google.common.collect.ImmutableList; + +public class McscoreboardCommand implements TabExecutor { + private static final List SCOREBOARD_TYPES = ImmutableList.of("clear", "rank", "stats", "top"); + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (CommandUtils.noConsoleUsage(sender)) { + return true; + } + + Player player = (Player) sender; + + switch (args.length) { + case 0: + clearScoreboard(player); + return true; + + case 1: + if (args[0].equalsIgnoreCase("clear")) { + clearScoreboard(player); + } + else if (args[0].equalsIgnoreCase("rank")) { + if (!Config.getInstance().getMcrankScoreboardEnabled()) { + sender.sendMessage("This scoreboard is not enabled."); //TODO: Localize + return true; + } + + ScoreboardManager.setupPlayerScoreboard(player.getName()); + ScoreboardManager.enablePlayerRankScoreboard(player); + } + else if (args[0].equalsIgnoreCase("stats")) { + if (!Config.getInstance().getMcstatsScoreboardsEnabled()) { + sender.sendMessage("This scoreboard is not enabled."); //TODO: Localize + return true; + } + + ScoreboardManager.setupPlayerScoreboard(player.getName()); + ScoreboardManager.enablePlayerStatsScoreboard(UserManager.getPlayer(player)); + } + else if (args[0].equalsIgnoreCase("top")) { + if (!Config.getInstance().getMctopScoreboardEnabled()) { + sender.sendMessage("This scoreboard is not enabled."); //TODO: Localize + return true; + } + + ScoreboardManager.enableGlobalStatsScoreboard(player, "all", 1); + } + else { + return false; + } + + return true; + + case 2: + if (!args[0].equalsIgnoreCase("top")) { + return false; + } + + if (!Config.getInstance().getMctopScoreboardEnabled()) { + sender.sendMessage("This scoreboard is not enabled."); //TODO: Localize + return true; + } + + if (StringUtils.isInt(args[1])) { + ScoreboardManager.enableGlobalStatsScoreboard(player, "all", Math.abs(Integer.parseInt(args[1]))); + return true; + } + + if (CommandUtils.isInvalidSkill(sender, args[1])) { + return true; + } + + ScoreboardManager.enableGlobalStatsScoreboard(player, args[1], 1); + return true; + + case 3: + if (!args[0].equalsIgnoreCase("top")) { + return false; + } + + if (!Config.getInstance().getMctopScoreboardEnabled()) { + sender.sendMessage("This scoreboard is not enabled."); //TODO: Localize + return true; + } + + if (CommandUtils.isInvalidSkill(sender, args[1])) { + return true; + } + + if (CommandUtils.isInvalidInteger(sender, args[2])) { + return true; + } + + ScoreboardManager.enableGlobalStatsScoreboard(player, args[1], Math.abs(Integer.parseInt(args[2]))); + return true; + + default: + return false; + } + } + + @Override + public List onTabComplete(CommandSender sender, Command command, String alias, String[] args) { + switch (args.length) { + case 1: + return StringUtil.copyPartialMatches(args[0], SCOREBOARD_TYPES, new ArrayList(SCOREBOARD_TYPES.size())); + case 2: + if (args[0].equalsIgnoreCase("top")) { + return StringUtil.copyPartialMatches(args[1], SkillType.SKILL_NAMES, new ArrayList(SkillType.SKILL_NAMES.size())); + } + // Fallthrough + + default: + return ImmutableList.of(); + } + } + + private void clearScoreboard(Player player) { + player.setScoreboard(mcMMO.p.getServer().getScoreboardManager().getMainScoreboard()); + player.sendMessage("Your scoreboard has been cleared!"); //TODO: Locale + } +} diff --git a/src/main/java/com/gmail/nossr50/mcMMO.java b/src/main/java/com/gmail/nossr50/mcMMO.java index 0251007df..8352c438c 100644 --- a/src/main/java/com/gmail/nossr50/mcMMO.java +++ b/src/main/java/com/gmail/nossr50/mcMMO.java @@ -348,6 +348,7 @@ public class mcMMO extends JavaPlugin { CommandRegistrationManager.registerMctopCommand(); CommandRegistrationManager.registerMcrankCommand(); CommandRegistrationManager.registerMcstatsCommand(); + CommandRegistrationManager.registerMcscoreboardCommand(); // Party commands CommandRegistrationManager.registerAdminChatCommand(); diff --git a/src/main/java/com/gmail/nossr50/util/commands/CommandRegistrationManager.java b/src/main/java/com/gmail/nossr50/util/commands/CommandRegistrationManager.java index 0a4f8ee39..48b3f38c1 100644 --- a/src/main/java/com/gmail/nossr50/util/commands/CommandRegistrationManager.java +++ b/src/main/java/com/gmail/nossr50/util/commands/CommandRegistrationManager.java @@ -11,6 +11,7 @@ import com.gmail.nossr50.commands.McgodCommand; import com.gmail.nossr50.commands.McmmoCommand; import com.gmail.nossr50.commands.McnotifyCommand; import com.gmail.nossr50.commands.McrefreshCommand; +import com.gmail.nossr50.commands.McscoreboardCommand; import com.gmail.nossr50.commands.MobhealthCommand; import com.gmail.nossr50.commands.XprateCommand; import com.gmail.nossr50.commands.chat.AdminChatCommand; @@ -376,4 +377,14 @@ public final class CommandRegistrationManager { command.setUsage(LocaleLoader.getString("Commands.Usage.1", "mobhealth", "")); command.setExecutor(new MobhealthCommand()); } + + public static void registerMcscoreboardCommand() { + PluginCommand command = mcMMO.p.getCommand("mcscoreboard"); + command.setDescription("Change the current mcMMO scoreboard being displayed"); //TODO: Localize + command.setPermission("mcmmo.commands.mcscoreboard"); + command.setPermissionMessage(permissionsMessage); + command.setUsage(LocaleLoader.getString("Commands.Usage.1", "mcscoreboard", "")); + command.setUsage(command.getUsage() + "\n" + LocaleLoader.getString("Commands.Usage.3", "mcscoreboard", "top", "[" + LocaleLoader.getString("Commands.Usage.Skill") + "]", "[" + LocaleLoader.getString("Commands.Usage.Page") + "]")); + command.setExecutor(new McscoreboardCommand()); + } } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index ef1720d54..add4c980b 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -107,6 +107,8 @@ commands: mobhealth: aliases: [mcmobhealth] description: Change the style of the mob healthbar + mcscoreboard: + description: Change the current mcMMO scoreboard being displayed permissions: mcmmo.*: default: false @@ -665,6 +667,7 @@ permissions: mcmmo.commands.mcmmo.all: true mcmmo.commands.mcnotify: true mcmmo.commands.mcrank: true + mcmmo.commands.mcscoreboard: true mcmmo.commands.mcstats: true mcmmo.commands.mctop.all: true mcmmo.commands.mining: true @@ -821,6 +824,8 @@ permissions: mcmmo.commands.mcremove: default: false description: Allows access to the mcremove command + mcmmo.commands.mcscoreboard: + description: Allows access to the mcscoreboard command mcmmo.commands.mcstats: description: Allows access to the mcstats command mcmmo.commands.mctop.*: