This commit is contained in:
GJ 2012-05-23 09:35:16 -04:00
parent d7b8ec1274
commit 9525ce8299
4 changed files with 54 additions and 34 deletions

View File

@ -14,12 +14,10 @@ import com.gmail.nossr50.skills.repair.Repairable;
import com.gmail.nossr50.skills.repair.RepairableFactory; import com.gmail.nossr50.skills.repair.RepairableFactory;
public class RepairConfig extends ConfigLoader { public class RepairConfig extends ConfigLoader {
private final String fileName;
private List<Repairable> repairables; private List<Repairable> repairables;
public RepairConfig(mcMMO plugin, String fileName) { public RepairConfig(mcMMO plugin, String fileName) {
super(plugin, fileName); super(plugin, fileName);
this.fileName = fileName;
this.config = YamlConfiguration.loadConfiguration(this.configFile); this.config = YamlConfiguration.loadConfiguration(this.configFile);
} }
@ -34,19 +32,19 @@ public class RepairConfig extends ConfigLoader {
ConfigurationSection section = config.getConfigurationSection("Repairables"); ConfigurationSection section = config.getConfigurationSection("Repairables");
Set<String> keys = section.getKeys(false); Set<String> keys = section.getKeys(false);
for(String key : keys) { for (String key : keys) {
// Validate all the things! // Validate all the things!
List<String> reason = new ArrayList<String>(); List<String> reason = new ArrayList<String>();
if(!config.contains("Repairables." + key + ".ItemId")) { if (!config.contains("Repairables." + key + ".ItemId")) {
reason.add(key + " is missing ItemId"); reason.add(key + " is missing ItemId");
} }
if(!config.contains("Repairables." + key + ".RepairMaterialId")) { if (!config.contains("Repairables." + key + ".RepairMaterialId")) {
reason.add(key + " is missing RepairMaterialId"); reason.add(key + " is missing RepairMaterialId");
} }
if(!config.contains("Repairables." + key + ".MaximumDurability")) { if (!config.contains("Repairables." + key + ".MaximumDurability")) {
reason.add(key + " is missing MaximumDurability"); reason.add(key + " is missing MaximumDurability");
} }
@ -65,27 +63,29 @@ public class RepairConfig extends ConfigLoader {
String repairItemTypeString = config.getString("Repairables." + key + ".ItemType", "OTHER"); String repairItemTypeString = config.getString("Repairables." + key + ".ItemType", "OTHER");
String repairMaterialTypeString = config.getString("Repairables." + key + ".MaterialType", "OTHER"); String repairMaterialTypeString = config.getString("Repairables." + key + ".MaterialType", "OTHER");
if(minimumLevel < 0) { if (minimumLevel < 0) {
reason.add(key + " has an invalid MinimumLevel of " + minimumLevel); reason.add(key + " has an invalid MinimumLevel of " + minimumLevel);
} }
if(minimumQuantity < 0) { if (minimumQuantity < 0) {
reason.add(key + " has an invalid MinimumQuantity of " + minimumQuantity); reason.add(key + " has an invalid MinimumQuantity of " + minimumQuantity);
} }
try { try {
repairItemType = RepairItemType.valueOf(repairItemTypeString); repairItemType = RepairItemType.valueOf(repairItemTypeString);
} catch (IllegalArgumentException ex) { }
catch (IllegalArgumentException ex) {
reason.add(key + " has an invalid ItemType of " + repairItemTypeString); reason.add(key + " has an invalid ItemType of " + repairItemTypeString);
} }
try { try {
repairMaterialType = RepairMaterialType.valueOf(repairMaterialTypeString); repairMaterialType = RepairMaterialType.valueOf(repairMaterialTypeString);
} catch (IllegalArgumentException ex) { }
catch (IllegalArgumentException ex) {
reason.add(key + " has an invalid MaterialType of " + repairMaterialTypeString); reason.add(key + " has an invalid MaterialType of " + repairMaterialTypeString);
} }
if(noErrorsInRepairable(reason)) { if (noErrorsInRepairable(reason)) {
Repairable repairable = RepairableFactory.getRepairable(itemId, repairMaterialId, (byte) repairMetadata, minimumLevel, minimumQuantity, (short) maximumDurability, repairItemType, repairMaterialType, xpMultiplier); Repairable repairable = RepairableFactory.getRepairable(itemId, repairMaterialId, (byte) repairMetadata, minimumLevel, minimumQuantity, (short) maximumDurability, repairItemType, repairMaterialType, xpMultiplier);
repairables.add(repairable); repairables.add(repairable);
} }
@ -93,7 +93,10 @@ public class RepairConfig extends ConfigLoader {
} }
protected List<Repairable> getLoadedRepairables() { protected List<Repairable> getLoadedRepairables() {
if(repairables == null) return new ArrayList<Repairable>(); if (repairables == null) {
return new ArrayList<Repairable>();
}
return repairables; return repairables;
} }

View File

@ -19,10 +19,13 @@ public enum RepairItemType {
switch (this) { switch (this) {
case ARMOR: case ARMOR:
return Permissions.getInstance().armorRepair(player); return Permissions.getInstance().armorRepair(player);
case TOOL: case TOOL:
return Permissions.getInstance().toolRepair(player); return Permissions.getInstance().toolRepair(player);
case OTHER: case OTHER:
return Permissions.getInstance().otherRepair(player); return Permissions.getInstance().otherRepair(player);
default: default:
return false; return false;
} }

View File

@ -24,20 +24,28 @@ public enum RepairMaterialType {
switch (this) { switch (this) {
case STRING: case STRING:
return Permissions.getInstance().stringRepair(player); return Permissions.getInstance().stringRepair(player);
case LEATHER: case LEATHER:
return Permissions.getInstance().leatherRepair(player); return Permissions.getInstance().leatherRepair(player);
case WOOD: case WOOD:
return Permissions.getInstance().woodRepair(player); return Permissions.getInstance().woodRepair(player);
case STONE: case STONE:
return Permissions.getInstance().stoneRepair(player); return Permissions.getInstance().stoneRepair(player);
case IRON: case IRON:
return Permissions.getInstance().ironRepair(player); return Permissions.getInstance().ironRepair(player);
case GOLD: case GOLD:
return Permissions.getInstance().goldRepair(player); return Permissions.getInstance().goldRepair(player);
case DIAMOND: case DIAMOND:
return Permissions.getInstance().diamondRepair(player); return Permissions.getInstance().diamondRepair(player);
case OTHER: case OTHER:
return Permissions.getInstance().otherMaterialRepair(player); return Permissions.getInstance().otherMaterialRepair(player);
default: default:
return false; return false;
} }

View File

@ -37,7 +37,7 @@ public class SimpleRepairManager implements RepairManager {
@Override @Override
public void registerRepairables(List<Repairable> repairables) { public void registerRepairables(List<Repairable> repairables) {
for(Repairable repairable : repairables) { for (Repairable repairable : repairables) {
registerRepairable(repairable); registerRepairable(repairable);
} }
} }
@ -67,28 +67,28 @@ public class SimpleRepairManager implements RepairManager {
Repairable repairable = repairables.get(item.getTypeId()); Repairable repairable = repairables.get(item.getTypeId());
// Permissions checks on material and item types // Permissions checks on material and item types
if(!repairable.getRepairItemType().getPermissions(player)) { if (!repairable.getRepairItemType().getPermissions(player)) {
player.sendMessage(LocaleLoader.getString("mcMMO.NoPermission")); player.sendMessage(LocaleLoader.getString("mcMMO.NoPermission"));
return; return;
} }
if(!repairable.getRepairMaterialType().getPermissions(player)) { if (!repairable.getRepairMaterialType().getPermissions(player)) {
player.sendMessage(LocaleLoader.getString("mcMMO.NoPermission")); player.sendMessage(LocaleLoader.getString("mcMMO.NoPermission"));
return; return;
} }
// Level check // Level check
if(skillLevel < repairable.getMinimumLevel()) { if (skillLevel < repairable.getMinimumLevel()) {
player.sendMessage(LocaleLoader.getString("Repair.Skills.Adept", new Object[] { String.valueOf(repairable.getMinimumLevel()), Misc.prettyItemString(item.getTypeId()) } )); player.sendMessage(LocaleLoader.getString("Repair.Skills.Adept", new Object[] { String.valueOf(repairable.getMinimumLevel()), Misc.prettyItemString(item.getTypeId()) } ));
return; return;
} }
// Check if they have the proper material to repair with // Check if they have the proper material to repair with
if(!inventory.contains(repairable.getRepairMaterialId())) { if (!inventory.contains(repairable.getRepairMaterialId())) {
String message = LocaleLoader.getString("Skills.NeedMore") + " " + ChatColor.YELLOW + Misc.prettyItemString(repairable.getRepairMaterialId()); String message = LocaleLoader.getString("Skills.NeedMore") + " " + ChatColor.YELLOW + Misc.prettyItemString(repairable.getRepairMaterialId());
if(repairable.getRepairMaterialMetadata() != (byte) -1) { if (repairable.getRepairMaterialMetadata() != (byte) -1) {
// TODO: Do something nicer than append the metadata as a :# ? // TODO: Do something nicer than append the metadata as a :# ?
if(findInInventory(inventory, repairable.getRepairMaterialId(), repairable.getRepairMaterialMetadata()) == -1) { if (findInInventory(inventory, repairable.getRepairMaterialId(), repairable.getRepairMaterialMetadata()) == -1) {
message += ":" + repairable.getRepairMaterialMetadata(); message += ":" + repairable.getRepairMaterialMetadata();
} }
} }
@ -97,33 +97,34 @@ public class SimpleRepairManager implements RepairManager {
} }
// Do not repair if at full durability // Do not repair if at full durability
if(startDurability <= 0) { if (startDurability <= 0) {
player.sendMessage(LocaleLoader.getString("Repair.Skills.FullDurability")); player.sendMessage(LocaleLoader.getString("Repair.Skills.FullDurability"));
return; return;
} }
// Do not repair stacked items // Do not repair stacked items
if(item.getAmount() != 1) { if (item.getAmount() != 1) {
player.sendMessage(LocaleLoader.getString("Repair.Skills.StackedItems")); player.sendMessage(LocaleLoader.getString("Repair.Skills.StackedItems"));
return; return;
} }
// Lets get down to business, // Lets get down to business,
// To defeat, the huns. // To defeat, the huns.
int baseRepairAmount = repairable.getBaseRepairDurability(); int baseRepairAmount = repairable.getBaseRepairDurability(); // Did they send me daughters?
short newDurability = Repair.repairCalculate(player, skillLevel, startDurability, baseRepairAmount); short newDurability = Repair.repairCalculate(player, skillLevel, startDurability, baseRepairAmount); // When I asked for sons?
// We're going to hold onto our repair item location // We're going to hold onto our repair item location
int repairItemLocation; int repairItemLocation;
if(repairable.getRepairMaterialMetadata() == (byte) -1) { if (repairable.getRepairMaterialMetadata() == (byte) -1) {
repairItemLocation = findInInventory(inventory, repairable.getRepairMaterialId()); repairItemLocation = findInInventory(inventory, repairable.getRepairMaterialId());
} else { }
else {
// Special case for when the repairable has metadata that must be addressed // Special case for when the repairable has metadata that must be addressed
repairItemLocation = findInInventory(inventory, repairable.getRepairMaterialId(), repairable.getRepairMaterialMetadata()); repairItemLocation = findInInventory(inventory, repairable.getRepairMaterialId(), repairable.getRepairMaterialMetadata());
} }
// This should never happen, but if it does we need to complain loudly about it. // This should never happen, but if it does we need to complain loudly about it.
if(repairItemLocation == -1) { if (repairItemLocation == -1) {
player.sendMessage("mcMMO encountered an error attempting to repair this item!"); // TODO: Locale ? player.sendMessage("mcMMO encountered an error attempting to repair this item!"); // TODO: Locale ?
return; return;
} }
@ -160,9 +161,10 @@ public class SimpleRepairManager implements RepairManager {
*/ */
private void removeOneFrom(PlayerInventory inventory, int index) { private void removeOneFrom(PlayerInventory inventory, int index) {
ItemStack item = inventory.getItem(index); ItemStack item = inventory.getItem(index);
if(item.getAmount() > 1) { if (item.getAmount() > 1) {
item.setAmount(item.getAmount() - 1); item.setAmount(item.getAmount() - 1);
} else { }
else {
item = new ItemStack(0); item = new ItemStack(0);
} }
@ -179,10 +181,12 @@ public class SimpleRepairManager implements RepairManager {
*/ */
private int findInInventory(PlayerInventory inventory, int itemId) { private int findInInventory(PlayerInventory inventory, int itemId) {
int location = inventory.first(itemId); int location = inventory.first(itemId);
// VALIDATE // VALIDATE
if(inventory.getItem(location).getTypeId() == itemId) { if (inventory.getItem(location).getTypeId() == itemId) {
return location; return location;
} else { }
else {
return -1; return -1;
} }
} }
@ -199,14 +203,16 @@ public class SimpleRepairManager implements RepairManager {
int location = -1; int location = -1;
ItemStack[] contents = inventory.getContents(); ItemStack[] contents = inventory.getContents();
for(int i = 0; i < contents.length; i++) { for (int i = 0; i < contents.length; i++) {
ItemStack item = contents[i]; ItemStack item = contents[i];
if(item.getTypeId() == itemId) { if (item.getTypeId() == itemId) {
if(item.getData().getData() == metadata) { if (item.getData().getData() == metadata) {
location = i; location = i;
} }
} }
if(location != -1) break; if (location != -1) {
break;
}
} }
return location; return location;