From e92c6b8f268c6a317edd6c73342011ecaef0e8d3 Mon Sep 17 00:00:00 2001 From: GJ Date: Tue, 12 Feb 2013 09:22:57 -0500 Subject: [PATCH] Convert /party rename to a subcommand --- .../nossr50/party/commands/PartyCommand.java | 61 +---------------- .../party/commands/PartyRenameCommand.java | 65 +++++++++++++++++++ .../resources/locale/locale_en_US.properties | 1 + 3 files changed, 68 insertions(+), 59 deletions(-) create mode 100644 src/main/java/com/gmail/nossr50/party/commands/PartyRenameCommand.java diff --git a/src/main/java/com/gmail/nossr50/party/commands/PartyCommand.java b/src/main/java/com/gmail/nossr50/party/commands/PartyCommand.java index 064ccf349..29df41f96 100644 --- a/src/main/java/com/gmail/nossr50/party/commands/PartyCommand.java +++ b/src/main/java/com/gmail/nossr50/party/commands/PartyCommand.java @@ -7,12 +7,9 @@ import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.commands.CommandHelper; import com.gmail.nossr50.config.Config; import com.gmail.nossr50.datatypes.McMMOPlayer; -import com.gmail.nossr50.events.party.McMMOPartyChangeEvent; -import com.gmail.nossr50.events.party.McMMOPartyChangeEvent.EventReason; import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.party.Party; import com.gmail.nossr50.party.PartyManager; @@ -35,6 +32,7 @@ public class PartyCommand implements CommandExecutor { private CommandExecutor partyChangeOwnerCommand = new PartyChangeOwnerCommand(); private CommandExecutor partyLockCommand = new PartyLockCommand(); private CommandExecutor partyChangePasswordCommand = new PartyChangePasswordCommand(); + private CommandExecutor partyRenameCommand = new PartyRenameCommand(); @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { @@ -106,7 +104,7 @@ public class PartyCommand implements CommandExecutor { return partyChangePasswordCommand.onCommand(sender, command, label, args); } else if (args[0].equalsIgnoreCase("rename")) { - return rename(args); + return partyRenameCommand.onCommand(sender, command, label, args); } else { return printUsage(); @@ -195,59 +193,4 @@ public class PartyCommand implements CommandExecutor { player.sendMessage(LocaleLoader.getString("Party.Help.8")); return true; } - - /** - * Rename the current party - */ - private boolean rename(String[] args) { - if (CommandHelper.noCommandPermissions(player, "mcmmo.commands.party.rename")) { - return true; - } - - String playerName = player.getName(); - Party party = mcMMOPlayer.getParty(); - String leader = party.getLeader(); - - if (party.getLeader().equals(playerName)) { - if (args.length < 2) { - player.sendMessage(LocaleLoader.getString("Commands.Usage.2", "party", "rename", "<" + LocaleLoader.getString("Commands.Usage.PartyName") + ">")); - return true; - } - - String newPartyName = args[1]; - - // This is to prevent party leaders from spamming other players with the rename message - if (!party.getName().equals(newPartyName)) { - Party newParty = PartyManager.getParty(newPartyName); - - // Check to see if the party exists, and if it does cancel renaming the party - if (newParty != null) { - player.sendMessage(LocaleLoader.getString("Commands.Party.AlreadyExists", newPartyName)); - return true; - } - - for (Player onlineMembers : party.getOnlineMembers()) { - McMMOPartyChangeEvent event = new McMMOPartyChangeEvent(onlineMembers, party.getName(), newPartyName, EventReason.CHANGED_PARTIES); - mcMMO.p.getServer().getPluginManager().callEvent(event); - - if (event.isCancelled()) { - return true; - } - - if (!onlineMembers.getName().equals(leader)) { - onlineMembers.sendMessage(LocaleLoader.getString("Party.InformedOnNameChange", leader, newPartyName)); - } - } - - party.setName(newPartyName); - } - - player.sendMessage(LocaleLoader.getString("Commands.Party.Rename", newPartyName)); - } - else { - player.sendMessage(LocaleLoader.getString("Party.NotOwner")); - } - - return true; - } } diff --git a/src/main/java/com/gmail/nossr50/party/commands/PartyRenameCommand.java b/src/main/java/com/gmail/nossr50/party/commands/PartyRenameCommand.java new file mode 100644 index 000000000..9ae2dea45 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/party/commands/PartyRenameCommand.java @@ -0,0 +1,65 @@ +package com.gmail.nossr50.party.commands; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import com.gmail.nossr50.events.party.McMMOPartyChangeEvent.EventReason; +import com.gmail.nossr50.locale.LocaleLoader; +import com.gmail.nossr50.party.Party; +import com.gmail.nossr50.party.PartyManager; +import com.gmail.nossr50.util.Permissions; +import com.gmail.nossr50.util.Users; + +public class PartyRenameCommand implements CommandExecutor { + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (!Permissions.hasPermission(sender, "mcmmo.commands.party.rename")) { + sender.sendMessage(command.getPermissionMessage()); + return true; + } + + Party playerParty = Users.getPlayer((Player) sender).getParty(); + String leaderName = playerParty.getLeader(); + + switch (args.length) { + case 2: + String newPartyName = args[1]; + + // This is to prevent party leaders from spamming other players with the rename message + if (playerParty.getName().equalsIgnoreCase(newPartyName)) { + sender.sendMessage(LocaleLoader.getString("Party.Rename.Same")); + return true; + } + + Party newParty = PartyManager.getParty(newPartyName); + + // Check to see if the party exists, and if it does cancel renaming the party + if (newParty != null) { + sender.sendMessage(LocaleLoader.getString("Commands.Party.AlreadyExists", newPartyName)); + return true; + } + + for (Player member : playerParty.getOnlineMembers()) { + if (!PartyManager.handlePartyChangeEvent(member, playerParty.getName(), newPartyName, EventReason.CHANGED_PARTIES)) { + return true; + } + + if (!member.getName().equals(leaderName)) { + member.sendMessage(LocaleLoader.getString("Party.InformedOnNameChange", leaderName, newPartyName)); + } + } + + playerParty.setName(newPartyName); + + sender.sendMessage(LocaleLoader.getString("Commands.Party.Rename", newPartyName)); + return true; + + default: + sender.sendMessage(LocaleLoader.getString("Commands.Usage.2", "party", "rename", "<" + LocaleLoader.getString("Commands.Usage.PartyName") + ">")); + return true; + } + } +} diff --git a/src/main/resources/locale/locale_en_US.properties b/src/main/resources/locale/locale_en_US.properties index 51f719705..c8cf6e036 100644 --- a/src/main/resources/locale/locale_en_US.properties +++ b/src/main/resources/locale/locale_en_US.properties @@ -543,6 +543,7 @@ Party.Teleport.Player=[[GREEN]]You have teleported to {0}. Party.Teleport.Self=[[RED]]You can't teleport to yourself! Party.Teleport.Target=[[GREEN]]{0} has teleported to you. Party.Teleport.Disabled=[[RED]]{0} doesn't allow party teleportation. +Party.Rename.Same=[[RED]]That is already the name of your party! Party.Join.Self=[[RED]]You can't join to yourself! Party.Unlocked=[[GRAY]]Party is unlocked Party.Disband=[[GRAY]]The party has been disbanded