From 8af425d067a813fb57d0b1c090f8d4d7f6f921e1 Mon Sep 17 00:00:00 2001 From: GJ Date: Thu, 18 Apr 2013 09:09:12 -0400 Subject: [PATCH] Make this a bit more sane. --- .../commands/party/PartyItemShareCommand.java | 43 +++----- .../datatypes/party/ItemShareType.java | 33 +++++++ .../gmail/nossr50/datatypes/party/Party.java | 99 ++++++++++--------- .../com/gmail/nossr50/party/PartyManager.java | 4 +- .../com/gmail/nossr50/party/ShareHandler.java | 40 ++++---- 5 files changed, 119 insertions(+), 100 deletions(-) create mode 100644 src/main/java/com/gmail/nossr50/datatypes/party/ItemShareType.java diff --git a/src/main/java/com/gmail/nossr50/commands/party/PartyItemShareCommand.java b/src/main/java/com/gmail/nossr50/commands/party/PartyItemShareCommand.java index a3f922354..99b41eca0 100644 --- a/src/main/java/com/gmail/nossr50/commands/party/PartyItemShareCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/party/PartyItemShareCommand.java @@ -6,9 +6,9 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import com.gmail.nossr50.config.Config; +import com.gmail.nossr50.datatypes.party.ItemShareType; import com.gmail.nossr50.datatypes.party.Party; import com.gmail.nossr50.locale.LocaleLoader; -import com.gmail.nossr50.party.ShareHandler; import com.gmail.nossr50.party.ShareHandler.ShareMode; import com.gmail.nossr50.util.StringUtils; import com.gmail.nossr50.util.commands.CommandUtils; @@ -27,20 +27,14 @@ public class PartyItemShareCommand implements CommandExecutor { switch (args.length) { case 2: playerParty = UserManager.getPlayer(sender.getName()).getParty(); + ShareMode mode = ShareMode.getShareMode(args[1].toUpperCase()); - if (args[1].equalsIgnoreCase("none") || CommandUtils.shouldDisableToggle(args[1])) { - handleChangingShareMode(ShareMode.NONE); - } - else if (args[1].equalsIgnoreCase("equal") || args[1].equalsIgnoreCase("even")) { - handleChangingShareMode(ShareMode.EQUAL); - } - else if (args[1].equalsIgnoreCase("random")) { - handleChangingShareMode(ShareMode.RANDOM); - } - else { + if (mode == null) { sender.sendMessage(LocaleLoader.getString("Commands.Usage.2", "party", "itemshare", "")); + return true; } + handleChangingShareMode(mode); return true; case 3: @@ -58,26 +52,13 @@ public class PartyItemShareCommand implements CommandExecutor { return true; } - if (args[1].equalsIgnoreCase("loot")) { - playerParty.setSharingLootDrops(toggle); + try { + handleToggleItemShareCategory(ItemShareType.valueOf(args[1].toUpperCase()), toggle); } - else if (args[1].equalsIgnoreCase("mining")) { - playerParty.setSharingMiningDrops(toggle); - } - else if (args[1].equalsIgnoreCase("herbalism")) { - playerParty.setSharingHerbalismDrops(toggle); - } - else if (args[1].equalsIgnoreCase("woodcutting")) { - playerParty.setSharingWoodcuttingDrops(toggle); - } - else if (args[1].equalsIgnoreCase("misc")) { - playerParty.setSharingMiscDrops(toggle); - } - else { + catch (IllegalArgumentException ex) { sender.sendMessage(LocaleLoader.getString("Commands.Usage.2", "party", "itemshare", " ")); } - notifyToggleItemShareCategory(args[1], toggle); return true; default: @@ -87,7 +68,7 @@ public class PartyItemShareCommand implements CommandExecutor { } } - private void handleChangingShareMode(ShareHandler.ShareMode mode) { + private void handleChangingShareMode(ShareMode mode) { playerParty.setItemShareMode(mode); String changeModeMessage = LocaleLoader.getString("Commands.Party.SetSharing", LocaleLoader.getString("Party.ShareType.Item"), LocaleLoader.getString("Party.ShareMode." + StringUtils.getCapitalized(mode.toString()))); @@ -97,10 +78,10 @@ public class PartyItemShareCommand implements CommandExecutor { } } - private void notifyToggleItemShareCategory(String category, boolean toggle) { - String state = toggle ? "enabled" : "disabled"; + private void handleToggleItemShareCategory(ItemShareType type, boolean toggle) { + playerParty.setSharingDrops(type, toggle); - String toggleMessage = LocaleLoader.getString("Commands.Party.ToggleShareCategory", StringUtils.getCapitalized(category), state); + String toggleMessage = LocaleLoader.getString("Commands.Party.ToggleShareCategory", StringUtils.getCapitalized(type.toString()), toggle ? "enabled" : "disabled"); for (Player member : playerParty.getOnlineMembers()) { member.sendMessage(toggleMessage); diff --git a/src/main/java/com/gmail/nossr50/datatypes/party/ItemShareType.java b/src/main/java/com/gmail/nossr50/datatypes/party/ItemShareType.java new file mode 100644 index 000000000..1f04e41e1 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/datatypes/party/ItemShareType.java @@ -0,0 +1,33 @@ +package com.gmail.nossr50.datatypes.party; + +import org.bukkit.inventory.ItemStack; + +import com.gmail.nossr50.util.ItemUtils; + +public enum ItemShareType { + LOOT, + MINING, + HERBALISM, + WOODCUTTING, + MISC; + + public static ItemShareType getShareType(ItemStack itemStack) { + if (ItemUtils.isMobDrop(itemStack)) { + return LOOT; + } + else if (ItemUtils.isMiningDrop(itemStack)) { + return MINING; + } + else if (ItemUtils.isHerbalismDrop(itemStack)) { + return HERBALISM; + } + else if (ItemUtils.isWoodcuttingDrop(itemStack)) { + return WOODCUTTING; + } + else if (ItemUtils.isMiscDrop(itemStack)) { + return MISC; + } + + return null; + } +} diff --git a/src/main/java/com/gmail/nossr50/datatypes/party/Party.java b/src/main/java/com/gmail/nossr50/datatypes/party/Party.java index 5802def36..8bb455254 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/party/Party.java +++ b/src/main/java/com/gmail/nossr50/datatypes/party/Party.java @@ -8,7 +8,7 @@ import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; import com.gmail.nossr50.mcMMO; -import com.gmail.nossr50.party.ShareHandler; +import com.gmail.nossr50.party.ShareHandler.ShareMode; public class Party { private LinkedHashSet members = new LinkedHashSet(); @@ -17,8 +17,8 @@ public class Party { private String password; private boolean locked; - private ShareHandler.ShareMode xpShareMode = ShareHandler.ShareMode.NONE; - private ShareHandler.ShareMode itemShareMode = ShareHandler.ShareMode.NONE; + private ShareMode xpShareMode = ShareMode.NONE; + private ShareMode itemShareMode = ShareMode.NONE; private boolean shareLootDrops = true; private boolean shareMiningDrops = true; @@ -59,47 +59,27 @@ public class Party { return locked; } - public boolean sharingLootDrops() { - return shareLootDrops; - } - - public boolean sharingMiningDrops() { - return shareMiningDrops; - } - - public boolean sharingHerbalismDrops() { - return shareHerbalismDrops; - } - - public boolean sharingWoodcuttingDrops() { - return shareWoodcuttingDrops; - } - - public boolean sharingMiscDrops() { - return shareMiscDrops; - } - public List getItemShareCategories() { List shareCategories = new ArrayList(); // TODO Locale the category names! - if (sharingLootDrops()) { + if (shareLootDrops) { shareCategories.add("Loot"); } - if (sharingMiningDrops()) { + if (shareMiningDrops) { shareCategories.add("Mining"); } - if (sharingHerbalismDrops()) { + if (shareHerbalismDrops) { shareCategories.add("Herbalism"); } - if (sharingWoodcuttingDrops()) { + if (shareWoodcuttingDrops) { shareCategories.add("Woodcutting"); } - if (sharingMiscDrops()) { + if (shareMiscDrops) { shareCategories.add("Misc"); } @@ -122,39 +102,68 @@ public class Party { this.locked = locked; } - public void setXpShareMode(ShareHandler.ShareMode xpShareMode) { + public void setXpShareMode(ShareMode xpShareMode) { this.xpShareMode = xpShareMode; } - public ShareHandler.ShareMode getXpShareMode() { + public ShareMode getXpShareMode() { return xpShareMode; } - public void setItemShareMode(ShareHandler.ShareMode itemShareMode) { + public void setItemShareMode(ShareMode itemShareMode) { this.itemShareMode = itemShareMode; } - public ShareHandler.ShareMode getItemShareMode() { + public ShareMode getItemShareMode() { return itemShareMode; } - public void setSharingLootDrops(boolean enabled) { - shareLootDrops = enabled; + public boolean sharingDrops(ItemShareType shareType) { + switch (shareType) { + case HERBALISM: + return shareHerbalismDrops; + + case LOOT: + return shareLootDrops; + + case MINING: + return shareMiningDrops; + + case MISC: + return shareMiscDrops; + + case WOODCUTTING: + return shareWoodcuttingDrops; + + default: + return false; + } } - public void setSharingMiningDrops(boolean enabled) { - shareMiningDrops = enabled; - } + public void setSharingDrops(ItemShareType shareType, boolean enabled) { + switch (shareType) { + case HERBALISM: + shareHerbalismDrops = enabled; + break; - public void setSharingHerbalismDrops(boolean enabled) { - shareHerbalismDrops = enabled; - } + case LOOT: + shareLootDrops = enabled; + break; - public void setSharingWoodcuttingDrops(boolean enabled) { - shareWoodcuttingDrops = enabled; - } + case MINING: + shareMiningDrops = enabled; + break; - public void setSharingMiscDrops(boolean enabled) { - shareMiscDrops = enabled; + case MISC: + shareMiscDrops = enabled; + break; + + case WOODCUTTING: + shareWoodcuttingDrops = enabled; + break; + + default: + return; + } } } diff --git a/src/main/java/com/gmail/nossr50/party/PartyManager.java b/src/main/java/com/gmail/nossr50/party/PartyManager.java index 63868e3e5..4bd0f7683 100644 --- a/src/main/java/com/gmail/nossr50/party/PartyManager.java +++ b/src/main/java/com/gmail/nossr50/party/PartyManager.java @@ -461,8 +461,8 @@ public final class PartyManager { party.setLeader(partiesFile.getString(partyName + ".Leader")); party.setPassword(partiesFile.getString(partyName + ".Password")); party.setLocked(partiesFile.getBoolean(partyName + ".Locked")); - party.setXpShareMode(ShareHandler.ShareMode.getFromString(partiesFile.getString(partyName + ".ExpShareMode"))); - party.setItemShareMode(ShareHandler.ShareMode.getFromString(partiesFile.getString(partyName + ".ItemShareMode"))); + party.setXpShareMode(ShareHandler.ShareMode.getShareMode(partiesFile.getString(partyName + ".ExpShareMode", "NONE"))); + party.setItemShareMode(ShareHandler.ShareMode.getShareMode(partiesFile.getString(partyName + ".ItemShareMode", "NONE"))); List memberNames = partiesFile.getStringList(partyName + ".Members"); LinkedHashSet members = party.getMembers(); diff --git a/src/main/java/com/gmail/nossr50/party/ShareHandler.java b/src/main/java/com/gmail/nossr50/party/ShareHandler.java index 2a1bf8a31..aa391ae97 100644 --- a/src/main/java/com/gmail/nossr50/party/ShareHandler.java +++ b/src/main/java/com/gmail/nossr50/party/ShareHandler.java @@ -8,11 +8,12 @@ import org.bukkit.inventory.ItemStack; import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.party.ItemWeightConfig; +import com.gmail.nossr50.datatypes.party.ItemShareType; import com.gmail.nossr50.datatypes.party.Party; import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.skills.SkillType; -import com.gmail.nossr50.util.ItemUtils; import com.gmail.nossr50.util.Misc; +import com.gmail.nossr50.util.commands.CommandUtils; import com.gmail.nossr50.util.player.UserManager; public final class ShareHandler { @@ -21,15 +22,19 @@ public final class ShareHandler { EQUAL, RANDOM; - public static ShareMode getFromString(String string) { + public static ShareMode getShareMode(String string) { try { return valueOf(string); } - catch (IllegalArgumentException exception) { - return NONE; - } - catch (NullPointerException exception) { - return NONE; + catch (IllegalArgumentException ex) { + if (string.equalsIgnoreCase("even")) { + return EQUAL; + } + else if (CommandUtils.shouldDisableToggle(string)) { + return NONE; + } + + return null; } } }; @@ -76,8 +81,8 @@ public final class ShareHandler { mcMMOPlayer.beginUnsharedXpGain(skillType, roundedXp); return true; + case NONE: - // Fallthrough default: return false; } @@ -99,19 +104,9 @@ public final class ShareHandler { ItemStack newStack = itemStack.clone(); newStack.setAmount(1); - if (ItemUtils.isMobDrop(itemStack) && !party.sharingLootDrops()) { - return false; - } - else if (ItemUtils.isMiningDrop(itemStack) && !party.sharingMiningDrops()) { - return false; - } - else if (ItemUtils.isHerbalismDrop(itemStack) && !party.sharingHerbalismDrops()) { - return false; - } - else if (ItemUtils.isWoodcuttingDrop(itemStack) && !party.sharingWoodcuttingDrops()) { - return false; - } - else if (ItemUtils.isMiscDrop(itemStack) && !party.sharingMiscDrops()) { + ItemShareType dropType = ItemShareType.getShareType(itemStack); + + if (dropType == null || !party.sharingDrops(dropType)) { return false; } @@ -163,6 +158,7 @@ public final class ShareHandler { winningPlayer.updateInventory(); } return true; + case RANDOM: nearMembers = PartyManager.getNearMembers(player, party, Config.getInstance().getPartyShareRange()); @@ -191,8 +187,8 @@ public final class ShareHandler { winningPlayer.updateInventory(); } return true; + case NONE: - // Fallthrough default: return false; }