SmeltingManager.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package com.gmail.nossr50.skills.smelting;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.bukkit.ChatColor;
  5. import org.bukkit.Material;
  6. import org.bukkit.Sound;
  7. import org.bukkit.block.BlockState;
  8. import org.bukkit.entity.Player;
  9. import org.bukkit.event.inventory.FurnaceBurnEvent;
  10. import org.bukkit.inventory.FurnaceRecipe;
  11. import org.bukkit.inventory.ItemStack;
  12. import org.bukkit.inventory.meta.ItemMeta;
  13. import com.gmail.nossr50.mcMMO;
  14. import com.gmail.nossr50.config.Config;
  15. import com.gmail.nossr50.datatypes.player.McMMOPlayer;
  16. import com.gmail.nossr50.datatypes.skills.SecondaryAbility;
  17. import com.gmail.nossr50.datatypes.skills.SkillType;
  18. import com.gmail.nossr50.datatypes.skills.XPGainReason;
  19. import com.gmail.nossr50.events.skills.secondaryabilities.SecondaryAbilityWeightedActivationCheckEvent;
  20. import com.gmail.nossr50.locale.LocaleLoader;
  21. import com.gmail.nossr50.skills.SkillManager;
  22. import com.gmail.nossr50.skills.mining.Mining;
  23. import com.gmail.nossr50.skills.smelting.Smelting.Tier;
  24. import com.gmail.nossr50.util.BlockUtils;
  25. import com.gmail.nossr50.util.EventUtils;
  26. import com.gmail.nossr50.util.Misc;
  27. import com.gmail.nossr50.util.Permissions;
  28. import com.gmail.nossr50.util.adapter.SoundAdapter;
  29. import com.gmail.nossr50.util.skills.ParticleEffectUtils;
  30. import com.gmail.nossr50.util.skills.SkillUtils;
  31. public class SmeltingManager extends SkillManager {
  32. public SmeltingManager(McMMOPlayer mcMMOPlayer) {
  33. super(mcMMOPlayer, SkillType.SMELTING);
  34. }
  35. public boolean canUseFluxMining(BlockState blockState) {
  36. return getSkillLevel() >= Smelting.fluxMiningUnlockLevel && BlockUtils.affectedByFluxMining(blockState) && Permissions.secondaryAbilityEnabled(getPlayer(), SecondaryAbility.FLUX_MINING) && !mcMMO.getPlaceStore().isTrue(blockState);
  37. }
  38. public boolean isSecondSmeltSuccessful() {
  39. return Permissions.secondaryAbilityEnabled(getPlayer(), SecondaryAbility.SECOND_SMELT) && SkillUtils.activationSuccessful(SecondaryAbility.SECOND_SMELT, getPlayer(), getSkillLevel(), activationChance);
  40. }
  41. /**
  42. * Process the Flux Mining ability.
  43. *
  44. * @param blockState The {@link BlockState} to check ability activation for
  45. * @return true if the ability was successful, false otherwise
  46. */
  47. public boolean processFluxMining(BlockState blockState) {
  48. Player player = getPlayer();
  49. SecondaryAbilityWeightedActivationCheckEvent event = new SecondaryAbilityWeightedActivationCheckEvent(getPlayer(), SecondaryAbility.FLUX_MINING, Smelting.fluxMiningChance / activationChance);
  50. mcMMO.p.getServer().getPluginManager().callEvent(event);
  51. if ((event.getChance() * activationChance) > Misc.getRandom().nextInt(activationChance)) {
  52. ItemStack item = null;
  53. switch (blockState.getType()) {
  54. case IRON_ORE:
  55. item = new ItemStack(Material.IRON_INGOT);
  56. break;
  57. case GOLD_ORE:
  58. item = new ItemStack(Material.GOLD_INGOT);
  59. break;
  60. default:
  61. break;
  62. }
  63. if (item == null) {
  64. return false;
  65. }
  66. if (!EventUtils.simulateBlockBreak(blockState.getBlock(), player, true)) {
  67. return false;
  68. }
  69. // We need to distribute Mining XP here, because the block break event gets cancelled
  70. applyXpGain(Mining.getBlockXp(blockState), XPGainReason.PVE);
  71. SkillUtils.handleDurabilityChange(getPlayer().getInventory().getItemInMainHand(), Config.getInstance().getAbilityToolDamage());
  72. Misc.dropItems(blockState.getLocation(), item, isSecondSmeltSuccessful() ? 2 : 1);
  73. blockState.setType(Material.AIR);
  74. if (Config.getInstance().getFluxPickaxeSoundEnabled()) {
  75. player.playSound(blockState.getLocation(), SoundAdapter.FIZZ, Misc.FIZZ_VOLUME, Misc.getFizzPitch());
  76. }
  77. ParticleEffectUtils.playFluxEffect(blockState.getLocation());
  78. return true;
  79. }
  80. return false;
  81. }
  82. public static ItemStack getFluxPickaxe(Material material, int amount) {
  83. ItemStack itemStack = new ItemStack(material, amount);
  84. ItemMeta itemMeta = itemStack.getItemMeta();
  85. itemMeta.setDisplayName(ChatColor.GOLD + LocaleLoader.getString("Item.FluxPickaxe.Name"));
  86. List<String> itemLore = itemMeta.hasLore() ? itemMeta.getLore() : new ArrayList<String>();
  87. itemLore.add("mcMMO Item");
  88. itemLore.add(LocaleLoader.getString("Item.FluxPickaxe.Lore.1"));
  89. itemLore.add(LocaleLoader.getString("Item.FluxPickaxe.Lore.2", Smelting.fluxMiningUnlockLevel));
  90. itemMeta.setLore(itemLore);
  91. itemStack.setItemMeta(itemMeta);
  92. return itemStack;
  93. }
  94. public static FurnaceRecipe getFluxPickaxeRecipe(Material material) {
  95. return new FurnaceRecipe(getFluxPickaxe(material, 1), material);
  96. }
  97. /**
  98. * Increases burn time for furnace fuel.
  99. *
  100. * @param burnTime The initial burn time from the {@link FurnaceBurnEvent}
  101. */
  102. public int fuelEfficiency(int burnTime) {
  103. double burnModifier = 1 + (((double) getSkillLevel() / Smelting.burnModifierMaxLevel) * Smelting.burnTimeMultiplier);
  104. return (int) (burnTime * burnModifier);
  105. }
  106. public ItemStack smeltProcessing(ItemStack smelting, ItemStack result) {
  107. applyXpGain(Smelting.getResourceXp(smelting), XPGainReason.PVE);
  108. if (isSecondSmeltSuccessful()) {
  109. ItemStack newResult = result.clone();
  110. newResult.setAmount(result.getAmount() + 1);
  111. return newResult;
  112. }
  113. return result;
  114. }
  115. public int vanillaXPBoost(int experience) {
  116. return experience * getVanillaXpMultiplier();
  117. }
  118. /**
  119. * Gets the vanilla XP multiplier
  120. *
  121. * @return the vanilla XP multiplier
  122. */
  123. public int getVanillaXpMultiplier() {
  124. int skillLevel = getSkillLevel();
  125. for (Tier tier : Tier.values()) {
  126. if (skillLevel >= tier.getLevel()) {
  127. return tier.getVanillaXPBoostModifier();
  128. }
  129. }
  130. return 1;
  131. }
  132. }