Refactor all our repairable stuff into a special package.

This commit is contained in:
GJ
2013-10-07 10:23:04 -04:00
parent 4aef4c63fc
commit 3927427b5b
16 changed files with 32 additions and 26 deletions

View File

@@ -0,0 +1,33 @@
package com.gmail.nossr50.skills.repair.repairables;
import org.bukkit.entity.Player;
import com.gmail.nossr50.util.Permissions;
public enum RepairItemType {
ARMOR,
TOOL,
OTHER;
/**
* Get the base permissions associated with this RepairItemType.
*
* @param player The player to check the permissions for
* @return true if the player has permissions, false otherwise
*/
public boolean getPermissions(Player player) {
switch (this) {
case ARMOR:
return Permissions.repairArmor(player);
case TOOL:
return Permissions.repairTools(player);
case OTHER:
return Permissions.repairOtherItems(player);
default:
return false;
}
}
}

View File

@@ -0,0 +1,84 @@
package com.gmail.nossr50.skills.repair.repairables;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import com.gmail.nossr50.util.Permissions;
public enum RepairMaterialType {
STRING,
LEATHER,
WOOD,
STONE,
IRON,
GOLD,
DIAMOND,
OTHER;
/**
* Get the base permissions associated with this RepairMaterialType.
*
* @param player The player to check the permissions for
*
* @return true if the player has permissions, false otherwise
*/
public boolean getPermissions(Player player) {
switch (this) {
case STRING:
return Permissions.repairString(player);
case LEATHER:
return Permissions.repairLeather(player);
case WOOD:
return Permissions.repairWood(player);
case STONE:
return Permissions.repairStone(player);
case IRON:
return Permissions.repairIron(player);
case GOLD:
return Permissions.repairGold(player);
case DIAMOND:
return Permissions.repairDiamond(player);
case OTHER:
return Permissions.repairOtherMaterials(player);
default:
return false;
}
}
public Material getDefaultRepairMaterial() {
switch (this) {
case STRING:
return Material.STRING;
case LEATHER:
return Material.LEATHER;
case WOOD:
return Material.WOOD;
case STONE:
return Material.COBBLESTONE;
case IRON:
return Material.IRON_INGOT;
case GOLD:
return Material.GOLD_INGOT;
case DIAMOND:
return Material.DIAMOND;
case OTHER:
default:
return null;
}
}
}

View File

@@ -0,0 +1,80 @@
package com.gmail.nossr50.skills.repair.repairables;
import org.bukkit.Material;
public interface Repairable {
/**
* Gets the type of this repairable item
*
* @return the type of this repairable
*/
public Material getItemMaterial();
/**
* Gets the id of the material used to repair this item
*
* @return the id of the repair material
*/
public Material getRepairMaterial();
/**
* Gets the metadata byte value of the material used to repair this item
*
* @return the byte metadata of the repair material
*/
public byte getRepairMaterialMetadata();
/**
* Gets the RepairItemType value for this repairable item
*
* @return the RepairItemType for this repairable
*/
public RepairItemType getRepairItemType();
/**
* Gets the RepairMaterialType value for this repairable item
*
* @return the RepairMaterialType for this repairable
*/
public RepairMaterialType getRepairMaterialType();
/**
* Gets the minimum quantity of repair materials ignoring all other repair bonuses
*
* This is typically set to the number of items needed to create that item, for example 5 for helmets or 2 for swords
*
* @return the minimum number of items
*/
public int getMinimumQuantity();
/**
* Gets the maximum durability of this item before it breaks
*
* @return the maximum durability
*/
public short getMaximumDurability();
/**
* Gets the base repair durability on which to calculate bonuses.
*
* This is actually the maximum durability divided by the minimum quantity
*
* @return the base repair durability
*/
public short getBaseRepairDurability();
/**
* Gets the minimum repair level needed to repair this item
*
* @return the minimum level to repair this item, or 0 for no minimum
*/
public int getMinimumLevel();
/**
* Gets the xpMultiplier for this repairable
*
* @return the xpMultiplier of this repairable
*/
public double getXpMultiplier();
}

View File

@@ -0,0 +1,15 @@
package com.gmail.nossr50.skills.repair.repairables;
import org.bukkit.Material;
public class RepairableFactory {
public static Repairable getRepairable(Material itemMaterial, Material repairMaterial, byte repairMetadata, int minimumQuantity, short maximumDurability) {
return getRepairable(itemMaterial, repairMaterial, repairMetadata, 0, minimumQuantity, maximumDurability, RepairItemType.OTHER, RepairMaterialType.OTHER, 1);
}
public static Repairable getRepairable(Material itemMaterial, Material repairMaterial, byte repairMetadata, int minimumLevel, int minimumQuantity, short maximumDurability, RepairItemType repairItemType, RepairMaterialType repairMaterialType, double xpMultiplier) {
// TODO: Add in loading from config what type of repairable we want.
return new SimpleRepairable(itemMaterial, repairMaterial, repairMetadata, minimumLevel, minimumQuantity, maximumDurability, repairItemType, repairMaterialType, xpMultiplier);
}
}

View File

@@ -0,0 +1,49 @@
package com.gmail.nossr50.skills.repair.repairables;
import java.util.List;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
public interface RepairableManager {
/**
* Register a repairable with the RepairManager
*
* @param repairable Repairable to register
*/
public void registerRepairable(Repairable repairable);
/**
* Register a list of repairables with the RepairManager
*
* @param repairables List<Repairable> to register
*/
public void registerRepairables(List<Repairable> repairables);
/**
* Checks if an item is repairable
*
* @param type Material to check if repairable
*
* @return true if repairable, false if not
*/
public boolean isRepairable(Material type);
/**
* Checks if an item is repairable
*
* @param itemStack Item to check if repairable
*
* @return true if repairable, false if not
*/
public boolean isRepairable(ItemStack itemStack);
/**
* Gets the repairable with this type
*
* @param type Material of the repairable to look for
*
* @return the repairable, can be null
*/
public Repairable getRepairable(Material type);
}

View File

@@ -0,0 +1,14 @@
package com.gmail.nossr50.skills.repair.repairables;
public class RepairableManagerFactory {
public static RepairableManager getRepairManager() {
// TODO: Add in loading from config what type of manager we want.
return new SimpleRepairableManager();
}
public static RepairableManager getRepairManager(int repairablesSize) {
// TODO: Add in loading from config what type of manager we want.
return new SimpleRepairableManager(repairablesSize);
}
}

View File

@@ -0,0 +1,77 @@
package com.gmail.nossr50.skills.repair.repairables;
import org.bukkit.Material;
public class SimpleRepairable implements Repairable {
private final Material itemMaterial, repairMaterial;
private final int minimumQuantity, minimumLevel;
private final short maximumDurability, baseRepairDurability;
private final byte repairMetadata;
private final RepairItemType repairItemType;
private final RepairMaterialType repairMaterialType;
private final double xpMultiplier;
protected SimpleRepairable(Material type, Material repairMaterial, byte repairMetadata, int minimumLevel, int minimumQuantity, short maximumDurability, RepairItemType repairItemType, RepairMaterialType repairMaterialType, double xpMultiplier) {
this.itemMaterial = type;
this.repairMaterial = repairMaterial;
this.repairMetadata = repairMetadata;
this.repairItemType = repairItemType;
this.repairMaterialType = repairMaterialType;
this.minimumLevel = minimumLevel;
this.minimumQuantity = minimumQuantity;
this.maximumDurability = maximumDurability;
this.baseRepairDurability = (short) (maximumDurability / minimumQuantity);
this.xpMultiplier = xpMultiplier;
}
@Override
public Material getItemMaterial() {
return itemMaterial;
}
@Override
public Material getRepairMaterial() {
return repairMaterial;
}
@Override
public byte getRepairMaterialMetadata() {
return repairMetadata;
}
@Override
public RepairItemType getRepairItemType() {
return repairItemType;
}
@Override
public RepairMaterialType getRepairMaterialType() {
return repairMaterialType;
}
@Override
public int getMinimumQuantity() {
return minimumQuantity;
}
@Override
public short getMaximumDurability() {
return maximumDurability;
}
@Override
public short getBaseRepairDurability() {
return baseRepairDurability;
}
@Override
public int getMinimumLevel() {
return minimumLevel;
}
@Override
public double getXpMultiplier() {
return xpMultiplier;
}
}

View File

@@ -0,0 +1,48 @@
package com.gmail.nossr50.skills.repair.repairables;
import java.util.HashMap;
import java.util.List;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
public class SimpleRepairableManager implements RepairableManager {
private HashMap<Material, Repairable> repairables;
protected SimpleRepairableManager() {
this(55);
}
protected SimpleRepairableManager(int repairablesSize) {
this.repairables = new HashMap<Material, Repairable>(repairablesSize);
}
@Override
public void registerRepairable(Repairable repairable) {
Material item = repairable.getItemMaterial();
repairables.put(item, repairable);
}
@Override
public void registerRepairables(List<Repairable> repairables) {
for (Repairable repairable : repairables) {
registerRepairable(repairable);
}
}
@Override
public boolean isRepairable(Material type) {
return repairables.containsKey(type);
}
@Override
public boolean isRepairable(ItemStack itemStack) {
return isRepairable(itemStack.getType());
}
@Override
public Repairable getRepairable(Material type) {
return repairables.get(type);
}
}