Make this a bit more sane.

This commit is contained in:
GJ 2013-04-18 09:09:12 -04:00
parent c3845abd2a
commit 8af425d067
5 changed files with 119 additions and 100 deletions

View File

@ -6,9 +6,9 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.datatypes.party.ItemShareType;
import com.gmail.nossr50.datatypes.party.Party; import com.gmail.nossr50.datatypes.party.Party;
import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.party.ShareHandler;
import com.gmail.nossr50.party.ShareHandler.ShareMode; import com.gmail.nossr50.party.ShareHandler.ShareMode;
import com.gmail.nossr50.util.StringUtils; import com.gmail.nossr50.util.StringUtils;
import com.gmail.nossr50.util.commands.CommandUtils; import com.gmail.nossr50.util.commands.CommandUtils;
@ -27,20 +27,14 @@ public class PartyItemShareCommand implements CommandExecutor {
switch (args.length) { switch (args.length) {
case 2: case 2:
playerParty = UserManager.getPlayer(sender.getName()).getParty(); playerParty = UserManager.getPlayer(sender.getName()).getParty();
ShareMode mode = ShareMode.getShareMode(args[1].toUpperCase());
if (args[1].equalsIgnoreCase("none") || CommandUtils.shouldDisableToggle(args[1])) { if (mode == null) {
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 {
sender.sendMessage(LocaleLoader.getString("Commands.Usage.2", "party", "itemshare", "<NONE | EQUAL | RANDOM>")); sender.sendMessage(LocaleLoader.getString("Commands.Usage.2", "party", "itemshare", "<NONE | EQUAL | RANDOM>"));
return true;
} }
handleChangingShareMode(mode);
return true; return true;
case 3: case 3:
@ -58,26 +52,13 @@ public class PartyItemShareCommand implements CommandExecutor {
return true; return true;
} }
if (args[1].equalsIgnoreCase("loot")) { try {
playerParty.setSharingLootDrops(toggle); handleToggleItemShareCategory(ItemShareType.valueOf(args[1].toUpperCase()), toggle);
} }
else if (args[1].equalsIgnoreCase("mining")) { catch (IllegalArgumentException ex) {
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 {
sender.sendMessage(LocaleLoader.getString("Commands.Usage.2", "party", "itemshare", "<loot | mining | herbalism | woodcutting | misc> <true | false>")); sender.sendMessage(LocaleLoader.getString("Commands.Usage.2", "party", "itemshare", "<loot | mining | herbalism | woodcutting | misc> <true | false>"));
} }
notifyToggleItemShareCategory(args[1], toggle);
return true; return true;
default: default:
@ -87,7 +68,7 @@ public class PartyItemShareCommand implements CommandExecutor {
} }
} }
private void handleChangingShareMode(ShareHandler.ShareMode mode) { private void handleChangingShareMode(ShareMode mode) {
playerParty.setItemShareMode(mode); playerParty.setItemShareMode(mode);
String changeModeMessage = LocaleLoader.getString("Commands.Party.SetSharing", LocaleLoader.getString("Party.ShareType.Item"), LocaleLoader.getString("Party.ShareMode." + StringUtils.getCapitalized(mode.toString()))); 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) { private void handleToggleItemShareCategory(ItemShareType type, boolean toggle) {
String state = toggle ? "enabled" : "disabled"; 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()) { for (Player member : playerParty.getOnlineMembers()) {
member.sendMessage(toggleMessage); member.sendMessage(toggleMessage);

View File

@ -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;
}
}

View File

@ -8,7 +8,7 @@ import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.party.ShareHandler; import com.gmail.nossr50.party.ShareHandler.ShareMode;
public class Party { public class Party {
private LinkedHashSet<String> members = new LinkedHashSet<String>(); private LinkedHashSet<String> members = new LinkedHashSet<String>();
@ -17,8 +17,8 @@ public class Party {
private String password; private String password;
private boolean locked; private boolean locked;
private ShareHandler.ShareMode xpShareMode = ShareHandler.ShareMode.NONE; private ShareMode xpShareMode = ShareMode.NONE;
private ShareHandler.ShareMode itemShareMode = ShareHandler.ShareMode.NONE; private ShareMode itemShareMode = ShareMode.NONE;
private boolean shareLootDrops = true; private boolean shareLootDrops = true;
private boolean shareMiningDrops = true; private boolean shareMiningDrops = true;
@ -59,47 +59,27 @@ public class Party {
return locked; 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<String> getItemShareCategories() { public List<String> getItemShareCategories() {
List<String> shareCategories = new ArrayList<String>(); List<String> shareCategories = new ArrayList<String>();
// TODO Locale the category names! // TODO Locale the category names!
if (sharingLootDrops()) { if (shareLootDrops) {
shareCategories.add("Loot"); shareCategories.add("Loot");
} }
if (sharingMiningDrops()) { if (shareMiningDrops) {
shareCategories.add("Mining"); shareCategories.add("Mining");
} }
if (sharingHerbalismDrops()) { if (shareHerbalismDrops) {
shareCategories.add("Herbalism"); shareCategories.add("Herbalism");
} }
if (sharingWoodcuttingDrops()) { if (shareWoodcuttingDrops) {
shareCategories.add("Woodcutting"); shareCategories.add("Woodcutting");
} }
if (sharingMiscDrops()) { if (shareMiscDrops) {
shareCategories.add("Misc"); shareCategories.add("Misc");
} }
@ -122,39 +102,68 @@ public class Party {
this.locked = locked; this.locked = locked;
} }
public void setXpShareMode(ShareHandler.ShareMode xpShareMode) { public void setXpShareMode(ShareMode xpShareMode) {
this.xpShareMode = xpShareMode; this.xpShareMode = xpShareMode;
} }
public ShareHandler.ShareMode getXpShareMode() { public ShareMode getXpShareMode() {
return xpShareMode; return xpShareMode;
} }
public void setItemShareMode(ShareHandler.ShareMode itemShareMode) { public void setItemShareMode(ShareMode itemShareMode) {
this.itemShareMode = itemShareMode; this.itemShareMode = itemShareMode;
} }
public ShareHandler.ShareMode getItemShareMode() { public ShareMode getItemShareMode() {
return itemShareMode; return itemShareMode;
} }
public void setSharingLootDrops(boolean enabled) { public boolean sharingDrops(ItemShareType shareType) {
shareLootDrops = enabled; 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) { public void setSharingDrops(ItemShareType shareType, boolean enabled) {
shareMiningDrops = enabled; switch (shareType) {
} case HERBALISM:
public void setSharingHerbalismDrops(boolean enabled) {
shareHerbalismDrops = enabled; shareHerbalismDrops = enabled;
} break;
public void setSharingWoodcuttingDrops(boolean enabled) { case LOOT:
shareWoodcuttingDrops = enabled; shareLootDrops = enabled;
} break;
public void setSharingMiscDrops(boolean enabled) { case MINING:
shareMiningDrops = enabled;
break;
case MISC:
shareMiscDrops = enabled; shareMiscDrops = enabled;
break;
case WOODCUTTING:
shareWoodcuttingDrops = enabled;
break;
default:
return;
}
} }
} }

View File

@ -461,8 +461,8 @@ public final class PartyManager {
party.setLeader(partiesFile.getString(partyName + ".Leader")); party.setLeader(partiesFile.getString(partyName + ".Leader"));
party.setPassword(partiesFile.getString(partyName + ".Password")); party.setPassword(partiesFile.getString(partyName + ".Password"));
party.setLocked(partiesFile.getBoolean(partyName + ".Locked")); party.setLocked(partiesFile.getBoolean(partyName + ".Locked"));
party.setXpShareMode(ShareHandler.ShareMode.getFromString(partiesFile.getString(partyName + ".ExpShareMode"))); party.setXpShareMode(ShareHandler.ShareMode.getShareMode(partiesFile.getString(partyName + ".ExpShareMode", "NONE")));
party.setItemShareMode(ShareHandler.ShareMode.getFromString(partiesFile.getString(partyName + ".ItemShareMode"))); party.setItemShareMode(ShareHandler.ShareMode.getShareMode(partiesFile.getString(partyName + ".ItemShareMode", "NONE")));
List<String> memberNames = partiesFile.getStringList(partyName + ".Members"); List<String> memberNames = partiesFile.getStringList(partyName + ".Members");
LinkedHashSet<String> members = party.getMembers(); LinkedHashSet<String> members = party.getMembers();

View File

@ -8,11 +8,12 @@ import org.bukkit.inventory.ItemStack;
import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.config.party.ItemWeightConfig; 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.party.Party;
import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.datatypes.skills.SkillType;
import com.gmail.nossr50.util.ItemUtils;
import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.commands.CommandUtils;
import com.gmail.nossr50.util.player.UserManager; import com.gmail.nossr50.util.player.UserManager;
public final class ShareHandler { public final class ShareHandler {
@ -21,15 +22,19 @@ public final class ShareHandler {
EQUAL, EQUAL,
RANDOM; RANDOM;
public static ShareMode getFromString(String string) { public static ShareMode getShareMode(String string) {
try { try {
return valueOf(string); return valueOf(string);
} }
catch (IllegalArgumentException exception) { catch (IllegalArgumentException ex) {
if (string.equalsIgnoreCase("even")) {
return EQUAL;
}
else if (CommandUtils.shouldDisableToggle(string)) {
return NONE; return NONE;
} }
catch (NullPointerException exception) {
return NONE; return null;
} }
} }
}; };
@ -76,8 +81,8 @@ public final class ShareHandler {
mcMMOPlayer.beginUnsharedXpGain(skillType, roundedXp); mcMMOPlayer.beginUnsharedXpGain(skillType, roundedXp);
return true; return true;
case NONE: case NONE:
// Fallthrough
default: default:
return false; return false;
} }
@ -99,19 +104,9 @@ public final class ShareHandler {
ItemStack newStack = itemStack.clone(); ItemStack newStack = itemStack.clone();
newStack.setAmount(1); newStack.setAmount(1);
if (ItemUtils.isMobDrop(itemStack) && !party.sharingLootDrops()) { ItemShareType dropType = ItemShareType.getShareType(itemStack);
return false;
} if (dropType == null || !party.sharingDrops(dropType)) {
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()) {
return false; return false;
} }
@ -163,6 +158,7 @@ public final class ShareHandler {
winningPlayer.updateInventory(); winningPlayer.updateInventory();
} }
return true; return true;
case RANDOM: case RANDOM:
nearMembers = PartyManager.getNearMembers(player, party, Config.getInstance().getPartyShareRange()); nearMembers = PartyManager.getNearMembers(player, party, Config.getInstance().getPartyShareRange());
@ -191,8 +187,8 @@ public final class ShareHandler {
winningPlayer.updateInventory(); winningPlayer.updateInventory();
} }
return true; return true;
case NONE: case NONE:
// Fallthrough
default: default:
return false; return false;
} }