2013-03-06 18:31:48 +01:00
package com.gmail.nossr50.skills.salvage ;
2019-01-17 17:46:10 +01:00
import com.gmail.nossr50.config.AdvancedConfig ;
2013-12-14 14:27:50 +01:00
import com.gmail.nossr50.config.Config ;
2019-01-14 07:56:48 +01:00
import com.gmail.nossr50.datatypes.interactions.NotificationType ;
2013-03-06 18:31:48 +01:00
import com.gmail.nossr50.datatypes.player.McMMOPlayer ;
2019-01-13 08:54:53 +01:00
import com.gmail.nossr50.datatypes.skills.PrimarySkillType ;
2019-01-17 17:46:10 +01:00
import com.gmail.nossr50.datatypes.skills.SubSkillType ;
2013-03-06 18:31:48 +01:00
import com.gmail.nossr50.locale.LocaleLoader ;
2018-07-26 02:29:40 +02:00
import com.gmail.nossr50.mcMMO ;
2013-03-06 18:31:48 +01:00
import com.gmail.nossr50.skills.SkillManager ;
2013-12-14 14:27:50 +01:00
import com.gmail.nossr50.skills.salvage.salvageables.Salvageable ;
2018-04-02 18:59:00 +02:00
import com.gmail.nossr50.util.EventUtils ;
2013-03-06 18:31:48 +01:00
import com.gmail.nossr50.util.Misc ;
import com.gmail.nossr50.util.Permissions ;
2013-12-14 14:27:50 +01:00
import com.gmail.nossr50.util.StringUtils ;
2019-01-14 07:56:48 +01:00
import com.gmail.nossr50.util.player.NotificationManager ;
2019-01-28 04:36:16 +01:00
import com.gmail.nossr50.util.random.RandomChanceSkillStatic ;
import com.gmail.nossr50.util.random.RandomChanceUtil ;
2019-01-17 17:46:10 +01:00
import com.gmail.nossr50.util.skills.RankUtils ;
2013-12-14 14:27:50 +01:00
import com.gmail.nossr50.util.skills.SkillUtils ;
2019-01-13 04:08:54 +01:00
import com.gmail.nossr50.util.sounds.SoundManager ;
import com.gmail.nossr50.util.sounds.SoundType ;
2018-07-26 02:29:40 +02:00
import org.bukkit.Location ;
import org.bukkit.Material ;
import org.bukkit.enchantments.Enchantment ;
import org.bukkit.entity.Player ;
import org.bukkit.inventory.ItemStack ;
import org.bukkit.inventory.meta.EnchantmentStorageMeta ;
import java.util.Map ;
import java.util.Map.Entry ;
2013-03-06 18:31:48 +01:00
public class SalvageManager extends SkillManager {
private boolean placedAnvil ;
private int lastClick ;
public SalvageManager ( McMMOPlayer mcMMOPlayer ) {
2019-01-13 08:54:53 +01:00
super ( mcMMOPlayer , PrimarySkillType . SALVAGE ) ;
2013-03-06 18:31:48 +01:00
}
/ * *
* Handles notifications for placing an anvil .
* /
2013-12-14 14:27:50 +01:00
public void placedAnvilCheck ( ) {
2013-03-06 18:31:48 +01:00
Player player = getPlayer ( ) ;
2013-12-14 14:27:50 +01:00
if ( getPlacedAnvil ( ) ) {
2013-03-06 18:31:48 +01:00
return ;
}
2013-12-14 14:27:50 +01:00
if ( Config . getInstance ( ) . getSalvageAnvilMessagesEnabled ( ) ) {
2019-01-14 07:56:48 +01:00
NotificationManager . sendPlayerInformation ( player , NotificationType . SUBSKILL_MESSAGE , " Salvage.Listener.Anvil " ) ;
2013-12-14 14:27:50 +01:00
}
if ( Config . getInstance ( ) . getSalvageAnvilPlaceSoundsEnabled ( ) ) {
2019-01-13 04:08:54 +01:00
SoundManager . sendSound ( player , player . getLocation ( ) , SoundType . ANVIL ) ;
2013-12-14 14:27:50 +01:00
}
2013-03-06 18:31:48 +01:00
2013-12-14 14:27:50 +01:00
togglePlacedAnvil ( ) ;
2013-03-06 18:31:48 +01:00
}
public void handleSalvage ( Location location , ItemStack item ) {
Player player = getPlayer ( ) ;
2013-12-14 14:27:50 +01:00
Salvageable salvageable = mcMMO . getSalvageableManager ( ) . getSalvageable ( item . getType ( ) ) ;
2018-05-10 05:54:35 +02:00
if ( item . getItemMeta ( ) . isUnbreakable ( ) ) {
2019-01-14 07:56:48 +01:00
NotificationManager . sendPlayerInformation ( player , NotificationType . SUBSKILL_MESSAGE_FAILURE , " Anvil.Unbreakable " ) ;
2018-05-10 05:54:35 +02:00
return ;
}
2019-01-17 17:46:10 +01:00
2013-12-14 14:27:50 +01:00
// Permissions checks on material and item types
if ( ! Permissions . salvageItemType ( player , salvageable . getSalvageItemType ( ) ) ) {
2019-01-14 07:56:48 +01:00
NotificationManager . sendPlayerInformation ( player , NotificationType . NO_PERMISSION , " mcMMO.NoPermission " ) ;
2013-12-14 14:27:50 +01:00
return ;
}
if ( ! Permissions . salvageMaterialType ( player , salvageable . getSalvageMaterialType ( ) ) ) {
2019-01-14 07:56:48 +01:00
NotificationManager . sendPlayerInformation ( player , NotificationType . NO_PERMISSION , " mcMMO.NoPermission " ) ;
2013-12-14 14:27:50 +01:00
return ;
}
int skillLevel = getSkillLevel ( ) ;
int minimumSalvageableLevel = salvageable . getMinimumLevel ( ) ;
// Level check
if ( skillLevel < minimumSalvageableLevel ) {
2019-01-14 07:56:48 +01:00
NotificationManager . sendPlayerInformation ( player , NotificationType . REQUIREMENTS_NOT_MET , " Salvage.Skills.Adept.Level " , String . valueOf ( minimumSalvageableLevel ) , StringUtils . getPrettyItemString ( item . getType ( ) ) ) ;
2013-12-14 14:27:50 +01:00
return ;
}
2013-03-06 18:31:48 +01:00
if ( item . getDurability ( ) ! = 0 & & ( getSkillLevel ( ) < Salvage . advancedSalvageUnlockLevel | | ! Permissions . advancedSalvage ( player ) ) ) {
2019-01-14 07:56:48 +01:00
NotificationManager . sendPlayerInformation ( player , NotificationType . SUBSKILL_MESSAGE_FAILURE , " Salvage.Skills.Adept.Damaged " ) ;
2013-03-06 18:31:48 +01:00
return ;
}
2013-12-14 14:27:50 +01:00
int salvageableAmount = Salvage . calculateSalvageableAmount ( item . getDurability ( ) , salvageable . getMaximumDurability ( ) , salvageable . getMaximumQuantity ( ) ) ;
2013-03-06 18:31:48 +01:00
if ( salvageableAmount = = 0 ) {
2019-01-14 07:56:48 +01:00
NotificationManager . sendPlayerInformation ( player , NotificationType . SUBSKILL_MESSAGE_FAILURE , " Salvage.Skills.TooDamaged " ) ;
2013-03-06 18:31:48 +01:00
player . sendMessage ( LocaleLoader . getString ( " Salvage.Skills.TooDamaged " ) ) ;
return ;
}
2015-11-04 22:31:30 +01:00
salvageableAmount = Math . max ( ( int ) ( salvageableAmount * getMaxSalvagePercentage ( ) ) , 1 ) ; // Always get at least something back, if you're capable of salvaging it.
2013-03-06 18:31:48 +01:00
2016-03-11 15:20:23 +01:00
player . getInventory ( ) . setItemInMainHand ( new ItemStack ( Material . AIR ) ) ;
2016-03-17 08:06:19 +01:00
location . add ( 0 . 5 , 1 , 0 . 5 ) ;
2013-03-06 18:31:48 +01:00
Map < Enchantment , Integer > enchants = item . getEnchantments ( ) ;
2018-04-02 18:59:00 +02:00
ItemStack enchantBook = null ;
2013-03-06 18:31:48 +01:00
if ( ! enchants . isEmpty ( ) ) {
2018-04-02 18:59:00 +02:00
enchantBook = arcaneSalvageCheck ( enchants ) ;
2013-03-06 18:31:48 +01:00
}
2018-07-27 01:53:29 +02:00
ItemStack salvageResults = new ItemStack ( salvageable . getSalvageMaterial ( ) , salvageableAmount ) ;
2018-04-02 18:59:00 +02:00
//Call event
if ( EventUtils . callSalvageCheckEvent ( player , item , salvageResults , enchantBook ) . isCancelled ( ) ) {
return ;
}
if ( enchantBook ! = null ) {
Misc . dropItem ( location , enchantBook ) ;
}
Misc . dropItems ( location , salvageResults , 1 ) ;
2013-12-14 14:27:50 +01:00
// BWONG BWONG BWONG - CLUNK!
if ( Config . getInstance ( ) . getSalvageAnvilUseSoundsEnabled ( ) ) {
2019-01-13 04:08:54 +01:00
SoundManager . sendSound ( player , player . getLocation ( ) , SoundType . ANVIL ) ;
SoundManager . sendSound ( player , player . getLocation ( ) , SoundType . ITEM_BREAK ) ;
//player.playSound(player.getLocation(), Sound.ENTITY_ITEM_BREAK, 1.0F, 1.0F);
2013-12-14 14:27:50 +01:00
}
2019-01-14 07:56:48 +01:00
NotificationManager . sendPlayerInformation ( player , NotificationType . SUBSKILL_MESSAGE , " Salvage.Skills.Success " ) ;
2013-12-14 14:27:50 +01:00
}
2013-03-06 18:31:48 +01:00
2013-12-14 14:27:50 +01:00
public double getMaxSalvagePercentage ( ) {
return Math . min ( ( ( ( Salvage . salvageMaxPercentage / Salvage . salvageMaxPercentageLevel ) * getSkillLevel ( ) ) / 100 . 0D ) , Salvage . salvageMaxPercentage / 100 . 0D ) ;
2013-03-06 18:31:48 +01:00
}
/ * *
* Gets the Arcane Salvage rank
*
* @return the current Arcane Salvage rank
* /
public int getArcaneSalvageRank ( ) {
2019-01-17 17:46:10 +01:00
return RankUtils . getRank ( getPlayer ( ) , SubSkillType . SALVAGE_ARCANE_SALVAGE ) ;
2013-03-06 18:31:48 +01:00
}
2019-01-17 17:46:10 +01:00
/ * public double getExtractFullEnchantChance ( ) {
2013-03-06 18:31:48 +01:00
int skillLevel = getSkillLevel ( ) ;
for ( Tier tier : Tier . values ( ) ) {
if ( skillLevel > = tier . getLevel ( ) ) {
return tier . getExtractFullEnchantChance ( ) ;
}
}
return 0 ;
}
public double getExtractPartialEnchantChance ( ) {
int skillLevel = getSkillLevel ( ) ;
for ( Tier tier : Tier . values ( ) ) {
if ( skillLevel > = tier . getLevel ( ) ) {
return tier . getExtractPartialEnchantChance ( ) ;
}
}
return 0 ;
2019-01-17 17:46:10 +01:00
} * /
public double getExtractFullEnchantChance ( ) {
return AdvancedConfig . getInstance ( ) . getArcaneSalvageExtractFullEnchantsChance ( getArcaneSalvageRank ( ) ) ;
}
public double getExtractPartialEnchantChance ( ) {
return AdvancedConfig . getInstance ( ) . getArcaneSalvageExtractPartialEnchantsChance ( getArcaneSalvageRank ( ) ) ;
2013-03-06 18:31:48 +01:00
}
private ItemStack arcaneSalvageCheck ( Map < Enchantment , Integer > enchants ) {
Player player = getPlayer ( ) ;
if ( getArcaneSalvageRank ( ) = = 0 | | ! Permissions . arcaneSalvage ( player ) ) {
2019-01-14 07:56:48 +01:00
NotificationManager . sendPlayerInformation ( player , NotificationType . SUBSKILL_MESSAGE_FAILURE , " Salvage.Skills.ArcaneFailed " ) ;
2013-03-06 18:31:48 +01:00
return null ;
}
ItemStack book = new ItemStack ( Material . ENCHANTED_BOOK ) ;
EnchantmentStorageMeta enchantMeta = ( EnchantmentStorageMeta ) book . getItemMeta ( ) ;
boolean downgraded = false ;
for ( Entry < Enchantment , Integer > enchant : enchants . entrySet ( ) ) {
2019-01-28 04:36:16 +01:00
if ( ! Salvage . arcaneSalvageEnchantLoss
| | RandomChanceUtil . checkRandomChanceExecutionSuccess ( new RandomChanceSkillStatic ( getExtractFullEnchantChance ( ) , getPlayer ( ) , SubSkillType . SALVAGE_ARCANE_SALVAGE ) ) ) {
2013-03-06 18:31:48 +01:00
enchantMeta . addStoredEnchant ( enchant . getKey ( ) , enchant . getValue ( ) , true ) ;
}
2019-01-28 04:36:16 +01:00
else if ( enchant . getValue ( ) > 1
& & Salvage . arcaneSalvageDowngrades
& & ! RandomChanceUtil . checkRandomChanceExecutionSuccess ( new RandomChanceSkillStatic ( getExtractPartialEnchantChance ( ) , getPlayer ( ) , SubSkillType . SALVAGE_ARCANE_SALVAGE ) ) ) {
2013-03-06 18:31:48 +01:00
enchantMeta . addStoredEnchant ( enchant . getKey ( ) , enchant . getValue ( ) - 1 , true ) ;
downgraded = true ;
}
else {
downgraded = true ;
}
}
Map < Enchantment , Integer > newEnchants = enchantMeta . getStoredEnchants ( ) ;
if ( newEnchants . isEmpty ( ) ) {
2019-01-14 07:56:48 +01:00
NotificationManager . sendPlayerInformation ( player , NotificationType . SUBSKILL_MESSAGE_FAILURE , " Salvage.Skills.ArcaneFailed " ) ;
2013-03-06 18:31:48 +01:00
return null ;
}
if ( downgraded | | newEnchants . size ( ) < enchants . size ( ) ) {
2019-01-14 07:56:48 +01:00
NotificationManager . sendPlayerInformation ( player , NotificationType . SUBSKILL_MESSAGE_FAILURE , " Salvage.Skills.ArcanePartial " ) ;
2013-03-06 18:31:48 +01:00
}
else {
2019-01-14 07:56:48 +01:00
NotificationManager . sendPlayerInformation ( player , NotificationType . SUBSKILL_MESSAGE , " Salvage.Skills.ArcanePartial " ) ;
2013-03-06 18:31:48 +01:00
}
book . setItemMeta ( enchantMeta ) ;
return book ;
}
2013-12-14 14:27:50 +01:00
/ * *
* Check if the player has tried to use an Anvil before .
* @param actualize
*
* @return true if the player has confirmed using an Anvil
2013-03-06 18:31:48 +01:00
* /
2013-12-14 14:27:50 +01:00
public boolean checkConfirmation ( boolean actualize ) {
Player player = getPlayer ( ) ;
long lastUse = getLastAnvilUse ( ) ;
2013-03-06 18:31:48 +01:00
2013-12-14 14:27:50 +01:00
if ( ! SkillUtils . cooldownExpired ( lastUse , 3 ) | | ! Config . getInstance ( ) . getSalvageConfirmRequired ( ) ) {
return true ;
2013-03-06 18:31:48 +01:00
}
2013-12-14 14:27:50 +01:00
if ( ! actualize ) {
return false ;
}
actualizeLastAnvilUse ( ) ;
2019-01-14 07:56:48 +01:00
NotificationManager . sendPlayerInformation ( player , NotificationType . SUBSKILL_MESSAGE , " Skills.ConfirmOrCancel " , LocaleLoader . getString ( " Salvage.Pretty.Name " ) ) ;
2013-12-14 14:27:50 +01:00
return false ;
2013-03-06 18:31:48 +01:00
}
2013-12-14 14:27:50 +01:00
/ *
* Salvage Anvil Placement
* /
public boolean getPlacedAnvil ( ) {
return placedAnvil ;
}
public void togglePlacedAnvil ( ) {
placedAnvil = ! placedAnvil ;
2013-03-06 18:31:48 +01:00
}
/ *
* Salvage Anvil Usage
* /
2013-12-14 14:27:50 +01:00
public int getLastAnvilUse ( ) {
return lastClick ;
2013-03-06 18:31:48 +01:00
}
2013-12-14 14:27:50 +01:00
public void setLastAnvilUse ( int value ) {
lastClick = value ;
2013-03-06 18:31:48 +01:00
}
2013-12-14 14:27:50 +01:00
public void actualizeLastAnvilUse ( ) {
lastClick = ( int ) ( System . currentTimeMillis ( ) / Misc . TIME_CONVERSION_FACTOR ) ;
2013-03-06 18:31:48 +01:00
}
}