From 35f243a6e6a979944c28238e28cff882b021af29 Mon Sep 17 00:00:00 2001 From: GJ Date: Thu, 7 Feb 2013 23:59:02 -0500 Subject: [PATCH] No reason to use the SAME CODE in three different clases. --- .../com/gmail/nossr50/party/PartyManager.java | 15 +++++++++++++++ .../party/commands/PartyAcceptCommand.java | 15 ++------------- .../party/commands/PartyCreateCommand.java | 12 +----------- .../nossr50/party/commands/PartyJoinCommand.java | 12 ++---------- 4 files changed, 20 insertions(+), 34 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/party/PartyManager.java b/src/main/java/com/gmail/nossr50/party/PartyManager.java index 8a8a6cf51..cfc4deb90 100644 --- a/src/main/java/com/gmail/nossr50/party/PartyManager.java +++ b/src/main/java/com/gmail/nossr50/party/PartyManager.java @@ -21,6 +21,21 @@ public final class PartyManager { private PartyManager() {} + public static boolean changeOrJoinParty(McMMOPlayer mcMMOPlayer, Player player, Party oldParty, Party newParty) { + if (mcMMOPlayer.inParty()) { + if (!handlePartyChangeEvent(player, oldParty.getName(), newParty.getName(), EventReason.CHANGED_PARTIES)) { + return false; + } + + removeFromParty(player.getName(), oldParty); + } + else if (!handlePartyChangeEvent(player, null, newParty.getName(), EventReason.JOINED_PARTY)) { + return false; + } + + return true; + } + /** * Check if two players are in the same party. * diff --git a/src/main/java/com/gmail/nossr50/party/commands/PartyAcceptCommand.java b/src/main/java/com/gmail/nossr50/party/commands/PartyAcceptCommand.java index 01c3514d1..9d0b5a864 100644 --- a/src/main/java/com/gmail/nossr50/party/commands/PartyAcceptCommand.java +++ b/src/main/java/com/gmail/nossr50/party/commands/PartyAcceptCommand.java @@ -6,9 +6,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import com.gmail.nossr50.datatypes.McMMOPlayer; -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; @@ -16,7 +14,6 @@ import com.gmail.nossr50.util.Users; public class PartyAcceptCommand implements CommandExecutor { private McMMOPlayer mcMMOPlayer; private Player player; - private Party playerParty; @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { @@ -35,17 +32,9 @@ public class PartyAcceptCommand implements CommandExecutor { return true; } + // Changing parties - if (mcMMOPlayer.inParty()) { - playerParty = mcMMOPlayer.getParty(); - - if (!PartyManager.handlePartyChangeEvent(player, playerParty.getName(), mcMMOPlayer.getPartyInvite().getName(), EventReason.CHANGED_PARTIES)) { - return true; - } - - PartyManager.removeFromParty(player.getName(), playerParty); - } - else if (!PartyManager.handlePartyChangeEvent(player, null, mcMMOPlayer.getPartyInvite().getName(), EventReason.JOINED_PARTY)) { + if (!PartyManager.changeOrJoinParty(mcMMOPlayer, player, mcMMOPlayer.getParty(), mcMMOPlayer.getPartyInvite())) { return true; } diff --git a/src/main/java/com/gmail/nossr50/party/commands/PartyCreateCommand.java b/src/main/java/com/gmail/nossr50/party/commands/PartyCreateCommand.java index 3ae192344..a8b7cdab1 100644 --- a/src/main/java/com/gmail/nossr50/party/commands/PartyCreateCommand.java +++ b/src/main/java/com/gmail/nossr50/party/commands/PartyCreateCommand.java @@ -6,7 +6,6 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import com.gmail.nossr50.datatypes.McMMOPlayer; -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; @@ -16,7 +15,6 @@ import com.gmail.nossr50.util.Users; public class PartyCreateCommand implements CommandExecutor { private McMMOPlayer mcMMOPlayer; private Player player; - private Party playerParty; @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { @@ -40,15 +38,7 @@ public class PartyCreateCommand implements CommandExecutor { mcMMOPlayer = Users.getPlayer(player); // Changing parties - if (mcMMOPlayer.inParty()) { - playerParty = mcMMOPlayer.getParty(); - if (!PartyManager.handlePartyChangeEvent(player, playerParty.getName(), args[1], EventReason.CHANGED_PARTIES)) { - return true; - } - - PartyManager.removeFromParty(player.getName(), playerParty); - } - else if (!PartyManager.handlePartyChangeEvent(player, null, args[1], EventReason.JOINED_PARTY)) { + if (!PartyManager.changeOrJoinParty(mcMMOPlayer, player, mcMMOPlayer.getParty(), newParty)) { return true; } diff --git a/src/main/java/com/gmail/nossr50/party/commands/PartyJoinCommand.java b/src/main/java/com/gmail/nossr50/party/commands/PartyJoinCommand.java index 8d3e23caf..092bac578 100644 --- a/src/main/java/com/gmail/nossr50/party/commands/PartyJoinCommand.java +++ b/src/main/java/com/gmail/nossr50/party/commands/PartyJoinCommand.java @@ -7,7 +7,6 @@ import org.bukkit.entity.Player; import com.gmail.nossr50.datatypes.McMMOPlayer; import com.gmail.nossr50.datatypes.PlayerProfile; -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; @@ -46,18 +45,11 @@ public class PartyJoinCommand implements CommandExecutor { } // Changing parties - if (mcMMOPlayer.inParty()) { - if (!PartyManager.handlePartyChangeEvent(player, playerParty.getName(), targetParty.getName(), EventReason.CHANGED_PARTIES)) { - return true; - } - - PartyManager.removeFromParty(player.getName(), playerParty); - } - else if (!PartyManager.handlePartyChangeEvent(player, null, targetParty.getName(), EventReason.JOINED_PARTY)) { + if (!PartyManager.changeOrJoinParty(mcMMOPlayer, player, playerParty, targetParty)) { return true; } - PartyManager.joinParty(player, mcMMOPlayer, targetParty.getName(), password); + PartyManager.joinParty(player, mcMMOPlayer, playerParty.getName(), password); return true; default: