mcMMO/src/main/java/com/gmail/nossr50/skills/Repair.java

491 lines
19 KiB
Java
Raw Normal View History

2012-01-09 20:00:13 +01:00
package com.gmail.nossr50.skills;
import java.util.Map;
2012-03-26 17:04:17 +02:00
import java.util.Random;
import java.util.Map.Entry;
2012-01-09 20:00:13 +01:00
import org.bukkit.ChatColor;
2012-02-25 08:49:53 +01:00
import org.bukkit.Material;
2012-01-09 20:00:13 +01:00
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.getspout.spoutapi.SpoutManager;
import org.getspout.spoutapi.player.SpoutPlayer;
import com.gmail.nossr50.ItemChecks;
2012-01-09 20:00:13 +01:00
import com.gmail.nossr50.Users;
import com.gmail.nossr50.m;
2012-04-21 00:09:50 +02:00
import com.gmail.nossr50.mcMMO;
2012-01-09 20:00:13 +01:00
import com.gmail.nossr50.mcPermissions;
import com.gmail.nossr50.config.LoadProperties;
2012-03-21 03:33:58 +01:00
import com.gmail.nossr50.spout.SpoutSounds;
2012-01-09 20:00:13 +01:00
import com.gmail.nossr50.datatypes.PlayerProfile;
import com.gmail.nossr50.datatypes.SkillType;
2012-03-27 07:09:28 +02:00
import com.gmail.nossr50.events.skills.McMMOPlayerRepairCheckEvent;
2012-01-09 20:00:13 +01:00
import com.gmail.nossr50.locale.mcLocale;
public class Repair {
2012-03-26 17:04:17 +02:00
private static Random random = new Random();
/**
* Handle all the item repair checks.
*
* @param player Player repairing the item
* @param is The item being repaired
*/
public static void repairCheck(Player player, ItemStack is) {
2012-03-13 19:09:32 +01:00
PlayerProfile PP = Users.getProfile(player);
short durabilityBefore = is.getDurability();
PlayerInventory inventory = player.getInventory();
int skillLevel = PP.getSkillLevel(SkillType.REPAIR);
if (durabilityBefore > 0 && is.getAmount() == 1) {
/*
* REPAIR ARMOR
*/
if (ItemChecks.isArmor(is) && LoadProperties.repairArmor && mcPermissions.getInstance().armorRepair(player)) {
2012-04-22 17:34:27 +02:00
if (ItemChecks.isDiamondArmor(is) && inventory.contains(LoadProperties.rDiamond) && skillLevel >= LoadProperties.repairDiamondLevel && mcPermissions.getInstance().diamondRepair(player)) {
repairItem(player, is, new ItemStack(LoadProperties.rDiamond));
xpHandler(player, PP, is, durabilityBefore, 6, true);
2012-03-13 19:09:32 +01:00
}
else if (ItemChecks.isIronArmor(is) && inventory.contains(LoadProperties.rIron) && skillLevel >= LoadProperties.repairIronLevel && mcPermissions.getInstance().ironRepair(player)) {
repairItem(player, is, new ItemStack(LoadProperties.rIron));
xpHandler(player, PP, is, durabilityBefore, 2, true);
}
else if (ItemChecks.isGoldArmor(is) && inventory.contains(LoadProperties.rGold) && skillLevel >= LoadProperties.repairGoldLevel && mcPermissions.getInstance().goldRepair(player)) {
repairItem(player, is, new ItemStack(LoadProperties.rGold));
xpHandler(player, PP, is, durabilityBefore, 4, true);
}
else if (ItemChecks.isLeatherArmor(is) && inventory.contains(LoadProperties.rLeather) && mcPermissions.getInstance().leatherRepair(player)) {
repairItem(player, is, new ItemStack(LoadProperties.rLeather));
xpHandler(player, PP, is, durabilityBefore, 1, true);
}
else {
needMoreVespeneGas(is, player); //UNABLE TO REPAIR
2012-03-13 19:09:32 +01:00
}
}
2012-03-13 19:09:32 +01:00
/*
* REPAIR TOOLS
2012-03-13 19:09:32 +01:00
*/
else if (ItemChecks.isTool(is) && LoadProperties.repairTools && mcPermissions.getInstance().toolRepair(player)) {
if (ItemChecks.isStoneTool(is) && inventory.contains(LoadProperties.rStone) && skillLevel >= LoadProperties.repairStoneLevel && mcPermissions.getInstance().stoneRepair(player)) {
repairItem(player, is, new ItemStack(LoadProperties.rStone));
xpHandler(player, PP, is, durabilityBefore, 2, false);
}
else if (ItemChecks.isWoodTool(is) && inventory.contains(LoadProperties.rWood) && mcPermissions.getInstance().woodRepair(player)) {
repairItem(player, is, new ItemStack(LoadProperties.rWood));
xpHandler(player, PP, is, durabilityBefore, 2, false);
}
else if (ItemChecks.isIronTool(is) && inventory.contains(LoadProperties.rIron) && skillLevel >= LoadProperties.repairIronLevel && mcPermissions.getInstance().ironRepair(player)) {
repairItem(player, is, new ItemStack(LoadProperties.rIron));
xpHandler(player, PP, is, durabilityBefore, 1, true);
}
2012-04-22 17:34:27 +02:00
else if (ItemChecks.isDiamondTool(is) && inventory.contains(LoadProperties.rDiamond) && skillLevel >= LoadProperties.repairDiamondLevel && mcPermissions.getInstance().diamondRepair(player)) {
repairItem(player, is, new ItemStack(LoadProperties.rDiamond));
xpHandler(player, PP, is, durabilityBefore, 1, true);
}
else if (ItemChecks.isGoldTool(is) && inventory.contains(LoadProperties.rGold) && skillLevel >= LoadProperties.repairGoldLevel && mcPermissions.getInstance().goldRepair(player)) {
repairItem(player, is, new ItemStack(LoadProperties.rGold));
xpHandler(player, PP, is, durabilityBefore, 8, true);
}
2012-04-22 17:34:27 +02:00
else if (ItemChecks.isStringTool(is) && inventory.contains(LoadProperties.rString) && skillLevel >= LoadProperties.repairStringLevel && mcPermissions.getInstance().stringRepair(player)){
repairItem(player, is, new ItemStack(LoadProperties.rString));
xpHandler(player, PP, is, durabilityBefore, 2, false);
}
else {
needMoreVespeneGas(is, player); //UNABLE TO REPAIR
}
}
}
else {
2012-04-24 15:21:21 +02:00
player.sendMessage(mcLocale.getString("Repair.Skills.FullDurability"));
2012-03-13 19:09:32 +01:00
}
}
/**
* Handle the XP gain for repair events.
*
* @param player Player repairing the item
* @param PP PlayerProfile of the repairing player
* @param is Item being repaired
* @param durabilityBefore Durability of the item before repair
* @param modify Amount to modify the durability by
* @param boost True if the modifier is a boost, false if the modifier is a reduction
*/
private static void xpHandler(Player player, PlayerProfile PP, ItemStack is, short durabilityBefore, int modify, boolean boost) {
2012-03-13 19:09:32 +01:00
short durabilityAfter = is.getDurability();
short dif = (short) (durabilityBefore - durabilityAfter);
if (boost) {
2012-03-13 19:09:32 +01:00
dif = (short) (dif * modify);
}
else{
2012-03-13 19:09:32 +01:00
dif = (short) (dif / modify);
}
if (ItemChecks.isShovel(is)) {
2012-03-13 19:09:32 +01:00
dif = (short) (dif / 3);
}
else if(ItemChecks.isSword(is)) {
2012-03-13 19:09:32 +01:00
dif = (short) (dif / 2);
}
else if(ItemChecks.isHoe(is)) {
2012-03-13 19:09:32 +01:00
dif = (short) (dif / 2);
}
PP.addXP(SkillType.REPAIR, dif * 10);
Skills.XpCheckSkill(SkillType.REPAIR, player);
2012-03-13 19:09:32 +01:00
//CLANG CLANG
if (LoadProperties.spoutEnabled) {
2012-04-21 00:09:50 +02:00
SpoutSounds.playRepairNoise(player, mcMMO.p);
}
2012-03-13 19:09:32 +01:00
}
2012-03-13 19:09:32 +01:00
/**
* Get current Arcane Forging rank.
*
* @param skillLevel The skill level of the player whose rank is being checked
* @return The player's current Arcane Forging rank
*/
public static int getArcaneForgingRank(int skillLevel) {
if (skillLevel >= LoadProperties.arcaneRank4) {
2012-03-13 19:09:32 +01:00
return 4;
}
else if (skillLevel >= LoadProperties.arcaneRank3) {
2012-03-13 19:09:32 +01:00
return 3;
}
else if (skillLevel >= LoadProperties.arcaneRank2) {
2012-03-13 19:09:32 +01:00
return 2;
}
else if (skillLevel >= LoadProperties.arcaneRank1) {
2012-03-13 19:09:32 +01:00
return 1;
}
else {
return 0;
}
2012-03-13 19:09:32 +01:00
}
/**
* Handles removing & downgrading enchants.
*
* @param player Player repairing the item
* @param is Item being repaired
*/
private static void addEnchants(Player player, ItemStack is) {
2012-03-13 19:09:32 +01:00
Map<Enchantment, Integer> enchants = is.getEnchantments();
if (enchants.size() == 0) {
2012-03-13 19:09:32 +01:00
return;
}
2012-03-13 19:09:32 +01:00
int rank = getArcaneForgingRank(Users.getProfile(player).getSkillLevel(SkillType.REPAIR));
if (rank == 0 || !mcPermissions.getInstance().arcaneForging(player)) {
for (Enchantment x : enchants.keySet()) {
2012-03-13 19:09:32 +01:00
is.removeEnchantment(x);
}
2012-04-24 15:21:21 +02:00
player.sendMessage(mcLocale.getString("Repair.Arcane.Lost"));
2012-03-13 19:09:32 +01:00
return;
}
2012-03-13 19:09:32 +01:00
boolean downgraded = false;
for (Entry<Enchantment, Integer> enchant : enchants.entrySet()) {
Enchantment enchantment = enchant.getKey();
2012-03-26 17:04:17 +02:00
if (random.nextInt(100) <= getEnchantChance(rank)) {
2012-03-13 19:09:32 +01:00
int enchantLevel = enchant.getValue();
if (LoadProperties.mayDowngradeEnchants && enchantLevel > 1) {
2012-03-26 17:04:17 +02:00
if (random.nextInt(100) <= getDowngradeChance(rank)) {
is.addEnchantment(enchantment, enchantLevel--);
2012-03-13 19:09:32 +01:00
downgraded = true;
}
}
}
else {
is.removeEnchantment(enchantment);
}
2012-03-13 19:09:32 +01:00
}
2012-03-13 19:09:32 +01:00
Map<Enchantment, Integer> newEnchants = is.getEnchantments();
if (newEnchants.isEmpty()) {
2012-04-24 15:21:21 +02:00
player.sendMessage(mcLocale.getString("Repair.Arcane.Fail"));
}
else if (downgraded || newEnchants.size() < enchants.size()) {
2012-04-24 15:21:21 +02:00
player.sendMessage(mcLocale.getString("Repair.Arcane.Downgrade"));
}
else {
2012-04-24 15:21:21 +02:00
player.sendMessage(mcLocale.getString("Repair.Arcane.Perfect"));
}
2012-03-13 19:09:32 +01:00
}
2012-03-13 19:09:32 +01:00
/**
* Gets chance of keeping enchantment during repair.
*
2012-03-13 19:09:32 +01:00
* @param rank Arcane Forging rank
* @return The chance of keeping the enchantment
2012-03-13 19:09:32 +01:00
*/
public static int getEnchantChance(int rank) {
switch (rank) {
2012-03-13 19:09:32 +01:00
case 4:
return LoadProperties.keepEnchantsRank4;
2012-03-13 19:09:32 +01:00
case 3:
return LoadProperties.keepEnchantsRank3;
2012-03-13 19:09:32 +01:00
case 2:
return LoadProperties.keepEnchantsRank2;
2012-03-13 19:09:32 +01:00
case 1:
return LoadProperties.keepEnchantsRank1;
2012-03-13 19:09:32 +01:00
default:
return 0;
}
}
2012-03-13 19:09:32 +01:00
/**
* Gets chance of enchantment being downgraded during repair.
*
2012-03-13 19:09:32 +01:00
* @param rank Arcane Forging rank
* @return The chance of the enchantment being downgraded
*/
public static int getDowngradeChance(int rank) {
switch (rank) {
2012-03-13 19:09:32 +01:00
case 4:
return LoadProperties.downgradeRank4;
2012-03-13 19:09:32 +01:00
case 3:
return LoadProperties.downgradeRank3;
2012-03-13 19:09:32 +01:00
case 2:
return LoadProperties.downgradeRank2;
2012-03-13 19:09:32 +01:00
case 1:
return LoadProperties.downgradeRank1;
2012-03-13 19:09:32 +01:00
default:
return 100;
}
}
2012-03-13 19:09:32 +01:00
/**
* Computes repair bonuses.
*
2012-03-13 19:09:32 +01:00
* @param player The player repairing an item
* @param durability The durability of the item being repaired
* @param ramt The base amount of durability repaired to the item
* @return The final amount of durability repaired to the item
*/
private static short repairCalculate(Player player, short durability, int ramt) {
2012-03-13 19:09:32 +01:00
int skillLevel = Users.getProfile(player).getSkillLevel(SkillType.REPAIR);
float bonus = (float) skillLevel / 500;
2012-03-13 19:09:32 +01:00
bonus = (ramt * bonus);
ramt += bonus;
if (checkPlayerProcRepair(player)) {
2012-03-13 19:09:32 +01:00
ramt = (short) (ramt * 2);
}
durability -= ramt;
if (durability < 0) {
2012-03-13 19:09:32 +01:00
durability = 0;
}
2012-03-13 19:09:32 +01:00
return durability;
}
2012-03-13 19:09:32 +01:00
/**
* Gets the base durability amount to repair an item.
*
2012-03-13 19:09:32 +01:00
* @param is The item being repaired
* @param player The player repairing the item
* @return The final amount of durability repaired to the item
*/
private static short getRepairAmount(ItemStack is, Player player){
2012-03-13 19:09:32 +01:00
short maxDurability = is.getType().getMaxDurability();
int ramt = 0;
if (ItemChecks.isShovel(is)) {
2012-03-13 19:09:32 +01:00
ramt = maxDurability;
}
else if (ItemChecks.isHoe(is) || ItemChecks.isSword(is) || is.getType().equals(Material.SHEARS)) {
2012-03-13 19:09:32 +01:00
ramt = maxDurability / 2;
}
2012-04-22 17:34:27 +02:00
else if (ItemChecks.isAxe(is) || ItemChecks.isMiningPick(is) || ItemChecks.isStringTool(is)) {
2012-03-13 19:09:32 +01:00
ramt = maxDurability / 3;
}
else if (ItemChecks.isBoots(is)) {
2012-03-13 19:09:32 +01:00
ramt = maxDurability / 4;
}
else if (ItemChecks.isHelmet(is)) {
2012-03-13 19:09:32 +01:00
ramt = maxDurability / 5;
}
else if (ItemChecks.isPants(is)) {
2012-03-13 19:09:32 +01:00
ramt = maxDurability / 7;
}
else if (ItemChecks.isChestplate(is)) {
2012-03-13 19:09:32 +01:00
ramt = maxDurability / 8;
}
return repairCalculate(player, is.getDurability(), ramt);
2012-03-13 19:09:32 +01:00
}
2012-03-13 19:09:32 +01:00
/**
* Informs a player that the repair has failed.
*
2012-03-13 19:09:32 +01:00
* @param is The item being repaired
* @param player The player repairing the item
*/
private static void needMoreVespeneGas(ItemStack is, Player player) {
2012-03-13 19:09:32 +01:00
int skillLevel = Users.getProfile(player).getSkillLevel(SkillType.REPAIR);
if (is.getAmount() != 1) {
2012-04-24 15:21:21 +02:00
player.sendMessage(mcLocale.getString("Repair.Skills.StackedItems"));
}
else {
if (ItemChecks.isDiamondTool(is) || ItemChecks.isDiamondArmor(is)) {
2012-04-22 17:34:27 +02:00
if (skillLevel < LoadProperties.repairDiamondLevel) {
2012-04-24 15:21:21 +02:00
player.sendMessage(mcLocale.getString("Repair.Skills.AdeptDiamond"));
}
else {
player.sendMessage(mcLocale.getString("Skills.NeedMore") + " " + ChatColor.BLUE + m.prettyItemString(LoadProperties.rDiamond));
}
2012-03-13 19:09:32 +01:00
}
else if (ItemChecks.isIronTool(is) || ItemChecks.isIronArmor(is)) {
if (skillLevel < LoadProperties.repairIronLevel) {
2012-04-24 15:21:21 +02:00
player.sendMessage(mcLocale.getString("Repair.Skills.AdeptIron"));
}
else {
player.sendMessage(mcLocale.getString("Skills.NeedMore")+ " " + ChatColor.GRAY + m.prettyItemString(LoadProperties.rIron));
}
2012-03-13 19:09:32 +01:00
}
else if (ItemChecks.isGoldTool(is) || ItemChecks.isGoldArmor(is)) {
if (skillLevel < LoadProperties.repairGoldLevel) {
2012-04-24 15:21:21 +02:00
player.sendMessage(mcLocale.getString("Repair.Skills.AdeptGold"));
}
else {
player.sendMessage(mcLocale.getString("Skills.NeedMore") + " " + ChatColor.GOLD + m.prettyItemString(LoadProperties.rGold));
}
2012-03-13 19:09:32 +01:00
}
else if (ItemChecks.isStoneTool(is)) {
if (skillLevel < LoadProperties.repairStoneLevel) {
2012-04-24 15:21:21 +02:00
player.sendMessage(mcLocale.getString("Repair.Skills.AdeptStone"));
}
else {
player.sendMessage(mcLocale.getString("Skills.NeedMore") + " " + ChatColor.GRAY + m.prettyItemString(LoadProperties.rStone));
}
}
else if (ItemChecks.isWoodTool(is)) {
player.sendMessage(mcLocale.getString("Skills.NeedMore") + " " + ChatColor.DARK_GREEN + m.prettyItemString(LoadProperties.rWood));
}
else if (ItemChecks.isLeatherArmor(is)) {
player.sendMessage(mcLocale.getString("Skills.NeedMore") + " " + ChatColor.YELLOW + m.prettyItemString(LoadProperties.rLeather));
}
2012-04-22 17:34:27 +02:00
else if (ItemChecks.isStringTool(is)) {
player.sendMessage(mcLocale.getString("Skills.NeedMore") + " " + ChatColor.YELLOW + m.prettyItemString(LoadProperties.rString));
2012-03-13 19:09:32 +01:00
}
}
}
2012-03-13 19:09:32 +01:00
/**
* Checks for Super Repair bonus.
*
* @param player The player repairing an item
2012-03-13 19:09:32 +01:00
* @return true if bonus granted, false otherwise
*/
public static boolean checkPlayerProcRepair(Player player) {
final int MAX_BONUS_LEVEL = 1000;
2012-03-13 19:09:32 +01:00
int skillLevel = Users.getProfile(player).getSkillLevel(SkillType.REPAIR);
2012-03-26 17:04:17 +02:00
if ((skillLevel > MAX_BONUS_LEVEL || random.nextInt(1000) <= skillLevel) && mcPermissions.getInstance().repairBonus(player)) {
2012-04-24 15:21:21 +02:00
player.sendMessage(mcLocale.getString("Repair.Skills.FeltEasy"));
2012-03-13 19:09:32 +01:00
return true;
}
2012-03-13 19:09:32 +01:00
return false;
}
2012-03-13 19:09:32 +01:00
/**
* Repairs an item.
*
2012-03-13 19:09:32 +01:00
* @param player The player repairing an item
* @param item The item being repaired
* @param repairMaterial The repair reagent
2012-03-13 19:09:32 +01:00
*/
public static void repairItem(Player player, ItemStack item, ItemStack repairMaterial) {
short initialDurability = item.getDurability();
short newDurability = getRepairAmount(item, player);
2012-03-28 21:53:55 +02:00
PlayerInventory inventory = player.getInventory();
2012-04-03 22:13:16 +02:00
McMMOPlayerRepairCheckEvent event = new McMMOPlayerRepairCheckEvent(player, (short) (initialDurability - newDurability), repairMaterial, item);
mcMMO.p.getServer().getPluginManager().callEvent(event);
2012-04-03 22:13:16 +02:00
if (event.isCancelled()) {
return;
}
2012-03-28 21:53:55 +02:00
if (repairMaterial.getType().equals(Material.WOOD)) {
removeWood(inventory);
}
else {
inventory.removeItem(repairMaterial);
}
/* Handle the enchants */
if (LoadProperties.mayLoseEnchants && !mcPermissions.getInstance().arcaneBypass(player)) {
addEnchants(player, item);
}
item.setDurability(newDurability);
2012-03-13 19:09:32 +01:00
}
/**
* Handles notifications for placing an anvil.
*
* @param player The player placing the anvil
* @param anvilID The item ID of the anvil block
*/
public static void placedAnvilCheck(Player player, int anvilID) {
PlayerProfile PP = Users.getProfile(player);
if (!PP.getPlacedAnvil()) {
if (LoadProperties.spoutEnabled) {
SpoutPlayer sPlayer = SpoutManager.getPlayer(player);
if (sPlayer.isSpoutCraftEnabled()) {
sPlayer.sendNotification("[mcMMO] Anvil Placed", "Right click to repair!", Material.getMaterial(anvilID)); //TODO: Use Locale
}
}
else {
2012-04-24 15:21:21 +02:00
player.sendMessage(mcLocale.getString("Repair.Listener.Anvil"));
}
PP.togglePlacedAnvil();
}
}
2012-03-28 21:53:55 +02:00
/**
* Removes wood from a player's inventory on repair. Needed due to wood having multiple possible data values.
*
* @param inventory The inventory to remove wood from
*/
private static void removeWood(PlayerInventory inventory) {
//TODO: Make this less hackish once there's a better way to do it...
int slot = inventory.first(Material.WOOD);
ItemStack item = inventory.getItem(slot);
item.setAmount(item.getAmount() - 1);
2012-04-05 20:45:35 +02:00
inventory.setItem(slot, item);
2012-03-28 21:53:55 +02:00
}
}