mcMMO/src/main/java/com/gmail/nossr50/skills/salvage/SalvageManager.java

318 lines
12 KiB
Java
Raw Normal View History

2013-03-06 18:31:48 +01:00
package com.gmail.nossr50.skills.salvage;
import com.gmail.nossr50.config.AdvancedConfig;
import com.gmail.nossr50.config.Config;
2019-06-15 05:39:57 +02:00
import com.gmail.nossr50.config.experience.ExperienceConfig;
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;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
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;
import com.gmail.nossr50.skills.salvage.salvageables.Salvageable;
2019-06-21 17:05:57 +02:00
import com.gmail.nossr50.util.*;
2019-01-14 07:56:48 +01:00
import com.gmail.nossr50.util.player.NotificationManager;
import com.gmail.nossr50.util.random.RandomChanceSkillStatic;
import com.gmail.nossr50.util.random.RandomChanceUtil;
import com.gmail.nossr50.util.skills.RankUtils;
import com.gmail.nossr50.util.skills.SkillUtils;
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) {
super(mcMMOPlayer, PrimarySkillType.SALVAGE);
2013-03-06 18:31:48 +01:00
}
/**
* Handles notifications for placing an anvil.
*/
public void placedAnvilCheck() {
2013-03-06 18:31:48 +01:00
Player player = getPlayer();
if (getPlacedAnvil()) {
2013-03-06 18:31:48 +01:00
return;
}
if (Config.getInstance().getSalvageAnvilMessagesEnabled()) {
2019-01-14 07:56:48 +01:00
NotificationManager.sendPlayerInformation(player, NotificationType.SUBSKILL_MESSAGE, "Salvage.Listener.Anvil");
}
if (Config.getInstance().getSalvageAnvilPlaceSoundsEnabled()) {
SoundManager.sendSound(player, player.getLocation(), SoundType.ANVIL);
}
2013-03-06 18:31:48 +01:00
togglePlacedAnvil();
2013-03-06 18:31:48 +01:00
}
public void handleSalvage(Location location, ItemStack item) {
Player player = getPlayer();
Salvageable salvageable = mcMMO.getSalvageableManager().getSalvageable(item.getType());
2018-05-10 05:54:35 +02:00
if (item.getItemMeta().isUnbreakable()) {
NotificationManager.sendPlayerInformation(player, NotificationType.SUBSKILL_MESSAGE_FAILED, "Anvil.Unbreakable");
2018-05-10 05:54:35 +02:00
return;
}
// 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");
return;
}
if (!Permissions.salvageMaterialType(player, salvageable.getSalvageMaterialType())) {
2019-01-14 07:56:48 +01:00
NotificationManager.sendPlayerInformation(player, NotificationType.NO_PERMISSION, "mcMMO.NoPermission");
return;
}
/*int skillLevel = getSkillLevel();
int minimumSalvageableLevel = salvageable.getMinimumLevel();*/
// Level check
if (!RankUtils.hasUnlockedSubskill(player, SubSkillType.SALVAGE_ARCANE_SALVAGE)) {
NotificationManager.sendPlayerInformation(player, NotificationType.REQUIREMENTS_NOT_MET, "Salvage.Skills.Adept.Level", String.valueOf(RankUtils.getUnlockLevel(SubSkillType.SALVAGE_ARCANE_SALVAGE)), StringUtils.getPrettyItemString(item.getType()));
return;
}
2019-06-21 17:05:57 +02:00
int potentialSalvageYield = Salvage.calculateSalvageableAmount(item.getDurability(), salvageable.getMaximumDurability(), salvageable.getMaximumQuantity());
2019-06-21 17:05:57 +02:00
if (potentialSalvageYield <= 0) {
NotificationManager.sendPlayerInformation(player, NotificationType.SUBSKILL_MESSAGE_FAILED, "Salvage.Skills.TooDamaged");
2013-03-06 18:31:48 +01:00
return;
}
2019-06-21 17:05:57 +02:00
potentialSalvageYield = Math.min(potentialSalvageYield, getSalvageLimit()); // Always get at least something back, if you're capable of salvaging it.
player.getInventory().setItemInMainHand(new ItemStack(Material.AIR));
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
}
//Lottery on Salvageable Amount
int lotteryResults = 1;
2019-06-20 09:02:48 +02:00
int chanceOfSuccess = 99;
2019-06-21 17:05:57 +02:00
for(int x = 0; x < potentialSalvageYield-1; x++) {
if(RandomChanceUtil.rollDice(chanceOfSuccess, 100)) {
2019-06-20 09:02:48 +02:00
chanceOfSuccess-=2;
Math.max(chanceOfSuccess, 95);
lotteryResults+=1;
}
}
2019-06-21 17:05:57 +02:00
if(lotteryResults == potentialSalvageYield && potentialSalvageYield != 1 && RankUtils.isPlayerMaxRankInSubSkill(player, SubSkillType.SALVAGE_ARCANE_SALVAGE)) {
NotificationManager.sendPlayerInformationChatOnly(player, "Salvage.Skills.Lottery.Perfect", String.valueOf(lotteryResults), StringUtils.getPrettyItemString(item.getType()));
2019-06-21 17:05:57 +02:00
} else if(salvageable.getMaximumQuantity() == 1 || getSalvageLimit() >= salvageable.getMaximumQuantity()) {
NotificationManager.sendPlayerInformationChatOnly(player, "Salvage.Skills.Lottery.Normal", String.valueOf(lotteryResults), StringUtils.getPrettyItemString(item.getType()));
} else {
NotificationManager.sendPlayerInformationChatOnly(player, "Salvage.Skills.Lottery.Untrained", String.valueOf(lotteryResults), StringUtils.getPrettyItemString(item.getType()));
}
ItemStack salvageResults = new ItemStack(salvageable.getSalvageMaterial(), lotteryResults);
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);
}
2019-06-21 17:05:57 +02:00
Misc.spawnItemTowardsLocation(location, player.getLocation().add(0, 0.25, 0), salvageResults);
// BWONG BWONG BWONG - CLUNK!
if (Config.getInstance().getSalvageAnvilUseSoundsEnabled()) {
// 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);
}
2019-01-14 07:56:48 +01:00
NotificationManager.sendPlayerInformation(player, NotificationType.SUBSKILL_MESSAGE, "Salvage.Skills.Success");
}
2013-03-06 18:31:48 +01:00
/*public double getMaxSalvagePercentage() {
return Math.min((((Salvage.salvageMaxPercentage / Salvage.salvageMaxPercentageLevel) * getSkillLevel()) / 100.0D), Salvage.salvageMaxPercentage / 100.0D);
}*/
2019-06-21 17:05:57 +02:00
public int getSalvageLimit() {
return (RankUtils.getRank(getPlayer(), SubSkillType.SALVAGE_SCRAP_COLLECTOR));
2013-03-06 18:31:48 +01:00
}
/**
* Gets the Arcane Salvage rank
*
* @return the current Arcane Salvage rank
*/
public int getArcaneSalvageRank() {
return RankUtils.getRank(getPlayer(), SubSkillType.SALVAGE_ARCANE_SALVAGE);
2013-03-06 18:31:48 +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;
}*/
public double getExtractFullEnchantChance() {
if(Permissions.hasSalvageEnchantBypassPerk(getPlayer()))
return 100.0D;
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 (!RankUtils.hasUnlockedSubskill(player, SubSkillType.SALVAGE_ARCANE_SALVAGE) || !Permissions.arcaneSalvage(player)) {
2019-06-15 05:39:57 +02:00
NotificationManager.sendPlayerInformationChatOnly(player, "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;
int arcaneFailureCount = 0;
2013-03-06 18:31:48 +01:00
for (Entry<Enchantment, Integer> enchant : enchants.entrySet()) {
2019-06-15 05:39:57 +02:00
int enchantLevel = enchant.getValue();
if(!ExperienceConfig.getInstance().allowUnsafeEnchantments()) {
if(enchantLevel > enchant.getKey().getMaxLevel()) {
enchantLevel = enchant.getKey().getMaxLevel();
}
}
if (!Salvage.arcaneSalvageEnchantLoss
|| Permissions.hasSalvageEnchantBypassPerk(player)
|| RandomChanceUtil.checkRandomChanceExecutionSuccess(new RandomChanceSkillStatic(getExtractFullEnchantChance(), getPlayer(), SubSkillType.SALVAGE_ARCANE_SALVAGE))) {
2019-06-15 05:39:57 +02:00
enchantMeta.addStoredEnchant(enchant.getKey(), enchantLevel, true);
2013-03-06 18:31:48 +01:00
}
2019-06-15 05:39:57 +02:00
else if (enchantLevel > 1
&& Salvage.arcaneSalvageDowngrades
&& RandomChanceUtil.checkRandomChanceExecutionSuccess(new RandomChanceSkillStatic(getExtractPartialEnchantChance(), getPlayer(), SubSkillType.SALVAGE_ARCANE_SALVAGE))) {
2019-06-15 05:39:57 +02:00
enchantMeta.addStoredEnchant(enchant.getKey(), enchantLevel - 1, true);
2013-03-06 18:31:48 +01:00
downgraded = true;
} else {
arcaneFailureCount++;
2013-03-06 18:31:48 +01:00
}
}
if(failedAllEnchants(arcaneFailureCount, enchants.entrySet().size()))
{
2019-06-15 05:39:57 +02:00
NotificationManager.sendPlayerInformationChatOnly(player, "Salvage.Skills.ArcaneFailed");
return null;
} else if(downgraded)
{
2019-06-15 05:39:57 +02:00
NotificationManager.sendPlayerInformationChatOnly(player, "Salvage.Skills.ArcanePartial");
2013-03-06 18:31:48 +01:00
}
book.setItemMeta(enchantMeta);
2013-03-06 18:31:48 +01:00
return book;
}
private boolean failedAllEnchants(int arcaneFailureCount, int size) {
return arcaneFailureCount == size;
}
/**
* 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
*/
public boolean checkConfirmation(boolean actualize) {
Player player = getPlayer();
long lastUse = getLastAnvilUse();
2013-03-06 18:31:48 +01:00
if (!SkillUtils.cooldownExpired(lastUse, 3) || !Config.getInstance().getSalvageConfirmRequired()) {
return true;
2013-03-06 18:31:48 +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"));
return false;
2013-03-06 18:31:48 +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
*/
public int getLastAnvilUse() {
return lastClick;
2013-03-06 18:31:48 +01:00
}
public void setLastAnvilUse(int value) {
lastClick = value;
2013-03-06 18:31:48 +01:00
}
public void actualizeLastAnvilUse() {
lastClick = (int) (System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR);
2013-03-06 18:31:48 +01:00
}
}