123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- package com.gmail.nossr50.skills.herbalism;
- import com.gmail.nossr50.config.treasure.HerbalismTreasureConfig;
- import com.gmail.nossr50.core.MetadataConstants;
- import com.gmail.nossr50.datatypes.experience.XPGainReason;
- import com.gmail.nossr50.datatypes.interactions.NotificationType;
- import com.gmail.nossr50.datatypes.player.McMMOPlayer;
- import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
- import com.gmail.nossr50.datatypes.skills.SubSkillType;
- import com.gmail.nossr50.datatypes.skills.SuperAbilityType;
- import com.gmail.nossr50.datatypes.skills.ToolType;
- import com.gmail.nossr50.datatypes.treasure.HylianTreasure;
- import com.gmail.nossr50.runnables.skills.HerbalismBlockUpdaterTask;
- import com.gmail.nossr50.skills.SkillManager;
- import com.gmail.nossr50.util.*;
- 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.SkillActivationType;
- import com.gmail.nossr50.util.skills.SkillUtils;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.block.BlockState;
- import org.bukkit.block.data.Ageable;
- import org.bukkit.entity.Player;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.inventory.PlayerInventory;
- import org.bukkit.metadata.FixedMetadataValue;
- import java.util.List;
- public class HerbalismManager extends SkillManager {
- public HerbalismManager(McMMOPlayer mcMMOPlayer) {
- super(mcMMOPlayer, PrimarySkillType.HERBALISM);
- }
- public boolean canBlockCheck() {
- return !(pluginRef.getConfigManager().getConfigExploitPrevention().isPreventVehicleAutoFarming() && getPlayer().isInsideVehicle());
- }
- public boolean canGreenThumbBlock(BlockState blockState) {
- if (!RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.HERBALISM_GREEN_THUMB))
- return false;
- Player player = getPlayer();
- ItemStack item = player.getInventory().getItemInMainHand();
- return item.getAmount() > 0 && item.getType() == Material.WHEAT_SEEDS && BlockUtils.canMakeMossy(blockState) && Permissions.greenThumbBlock(player, blockState.getType());
- }
- public boolean canUseShroomThumb(BlockState blockState) {
- if (!RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.HERBALISM_SHROOM_THUMB))
- return false;
- Player player = getPlayer();
- PlayerInventory inventory = player.getInventory();
- Material itemType = inventory.getItemInMainHand().getType();
- return (itemType == Material.BROWN_MUSHROOM || itemType == Material.RED_MUSHROOM) && inventory.contains(Material.BROWN_MUSHROOM, 1) && inventory.contains(Material.RED_MUSHROOM, 1) && BlockUtils.canMakeShroomy(blockState) && Permissions.isSubSkillEnabled(player, SubSkillType.HERBALISM_SHROOM_THUMB);
- }
- public boolean canUseHylianLuck() {
- if (!RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.HERBALISM_HYLIAN_LUCK))
- return false;
- return Permissions.isSubSkillEnabled(getPlayer(), SubSkillType.HERBALISM_HYLIAN_LUCK);
- }
- public boolean canGreenTerraBlock(BlockState blockState) {
- return mcMMOPlayer.getAbilityMode(SuperAbilityType.GREEN_TERRA) && BlockUtils.canMakeMossy(blockState);
- }
- public boolean canActivateAbility() {
- return mcMMOPlayer.getToolPreparationMode(ToolType.HOE) && Permissions.greenTerra(getPlayer());
- }
- public boolean canGreenTerraPlant() {
- return mcMMOPlayer.getAbilityMode(SuperAbilityType.GREEN_TERRA);
- }
- /**
- * Handle the Farmer's Diet ability
- *
- * @param eventFoodLevel The initial change in hunger from the event
- * @return the modified change in hunger for the event
- */
- public int farmersDiet(int eventFoodLevel) {
- return SkillUtils.handleFoodSkills(getPlayer(), eventFoodLevel, SubSkillType.HERBALISM_FARMERS_DIET);
- }
- /**
- * Process the Green Terra ability.
- *
- * @param blockState The {@link BlockState} to check ability activation for
- * @return true if the ability was successful, false otherwise
- */
- public boolean processGreenTerra(BlockState blockState) {
- Player player = getPlayer();
- if (!Permissions.greenThumbBlock(player, blockState.getType())) {
- return false;
- }
- PlayerInventory playerInventory = player.getInventory();
- ItemStack seed = new ItemStack(Material.WHEAT_SEEDS);
- if (!playerInventory.containsAtLeast(seed, 1)) {
- pluginRef.getNotificationManager().sendPlayerInformation(player, NotificationType.REQUIREMENTS_NOT_MET, "Herbalism.Ability.GTe.NeedMore");
- return false;
- }
- playerInventory.removeItem(seed);
- player.updateInventory(); // Needed until replacement available
- return Herbalism.convertGreenTerraBlocks(blockState);
- }
- /**
- * @param blockState The {@link BlockState} to check ability activation for
- */
- public void herbalismBlockCheck(BlockState blockState) {
- Player player = getPlayer();
- Material material = blockState.getType();
- boolean oneBlockPlant = isOneBlockPlant(material);
- // Prevents placing and immediately breaking blocks for exp
- if (oneBlockPlant && pluginRef.getPlaceStore().isTrue(blockState)) {
- return;
- }
- if (!canBlockCheck()) {
- return;
- }
- int amount;
- int xp;
- boolean greenTerra = mcMMOPlayer.getAbilityMode(skill.getSuperAbility());
- // if (mcMMO.getModManager().isCustomHerbalismBlock(blockState)) {
- // CustomBlock customBlock = mcMMO.getModManager().getBlock(blockState);
- // xp = customBlock.getXpGain();
- //
- // if (Permissions.isSubSkillEnabled(player, SubSkillType.HERBALISM_DOUBLE_DROPS) && customBlock.isDoubleDropEnabled()) {
- // drops = blockState.getBlock().getDrops();
- // }
- // }
- // else {
- xp = pluginRef.getDynamicSettingsManager().getExperienceManager().getHerbalismXp(blockState.getType());
- if (!oneBlockPlant) {
- //Kelp is actually two blocks mixed together
- if (material == Material.KELP_PLANT || material == Material.KELP) {
- amount = Herbalism.countAndMarkDoubleDropsKelp(blockState, greenTerra, this);
- } else {
- amount = Herbalism.countAndMarkDoubleDropsMultiBlockPlant(blockState, greenTerra, this);
- }
- xp *= amount;
- } else {
- /* MARK SINGLE BLOCK CROP FOR DOUBLE DROP */
- if (checkDoubleDrop(blockState))
- BlockUtils.markDropsAsBonus(blockState, greenTerra);
- }
- if (Permissions.greenThumbPlant(player, material)) {
- processGreenThumbPlants(blockState, greenTerra);
- }
- //} mod config close
- applyXpGain(xp, XPGainReason.PVE);
- }
- public boolean isOneBlockPlant(Material material) {
- return !pluginRef.getMaterialMapStore().isMultiBlock(material);
- }
- /**
- * Check for success on herbalism double drops
- *
- * @param blockState target block state
- * @return true if double drop succeeds
- */
- public boolean checkDoubleDrop(BlockState blockState) {
- return BlockUtils.checkDoubleDrops(getPlayer(), blockState, SubSkillType.HERBALISM_DOUBLE_DROPS);
- }
- /**
- * Process the Green Thumb ability for blocks.
- *
- * @param blockState The {@link BlockState} to check ability activation for
- * @return true if the ability was successful, false otherwise
- */
- public boolean processGreenThumbBlocks(BlockState blockState) {
- if (!RandomChanceUtil.isActivationSuccessful(SkillActivationType.RANDOM_LINEAR_100_SCALE_WITH_CAP, SubSkillType.HERBALISM_GREEN_THUMB, getPlayer())) {
- pluginRef.getNotificationManager().sendPlayerInformation(getPlayer(), NotificationType.SUBSKILL_MESSAGE_FAILED, "Herbalism.Ability.GTh.Fail");
- return false;
- }
- return Herbalism.convertGreenTerraBlocks(blockState);
- }
- /**
- * Process the Hylian Luck ability.
- *
- * @param blockState The {@link BlockState} to check ability activation for
- * @return true if the ability was successful, false otherwise
- */
- public boolean processHylianLuck(BlockState blockState) {
- if (!RandomChanceUtil.isActivationSuccessful(SkillActivationType.RANDOM_LINEAR_100_SCALE_WITH_CAP, SubSkillType.HERBALISM_HYLIAN_LUCK, getPlayer())) {
- return false;
- }
- String friendly = StringUtils.getFriendlyConfigBlockDataString(blockState.getBlockData());
- if (!HerbalismTreasureConfig.getInstance().hylianMap.containsKey(friendly))
- return false;
- List<HylianTreasure> treasures = HerbalismTreasureConfig.getInstance().hylianMap.get(friendly);
- Player player = getPlayer();
- if (treasures.isEmpty()) {
- return false;
- }
- int skillLevel = getSkillLevel();
- Location location = Misc.getBlockCenter(blockState);
- for (HylianTreasure treasure : treasures) {
- if (skillLevel >= treasure.getDropLevel()
- && RandomChanceUtil.checkRandomChanceExecutionSuccess(new RandomChanceSkillStatic(treasure.getDropChance(), getPlayer(), SubSkillType.HERBALISM_HYLIAN_LUCK))) {
- if (!pluginRef.getEventManager().simulateBlockBreak(blockState.getBlock(), player, false)) {
- return false;
- }
- blockState.setType(Material.AIR);
- Misc.dropItem(location, treasure.getDrop());
- pluginRef.getNotificationManager().sendPlayerInformation(player, NotificationType.SUBSKILL_MESSAGE, "Herbalism.HylianLuck");
- return true;
- }
- }
- return false;
- }
- /**
- * Process the Shroom Thumb ability.
- *
- * @param blockState The {@link BlockState} to check ability activation for
- * @return true if the ability was successful, false otherwise
- */
- public boolean processShroomThumb(BlockState blockState) {
- Player player = getPlayer();
- PlayerInventory playerInventory = player.getInventory();
- if (!playerInventory.contains(Material.BROWN_MUSHROOM, 1)) {
- pluginRef.getNotificationManager().sendPlayerInformation(player, NotificationType.REQUIREMENTS_NOT_MET, "Skills.NeedMore", StringUtils.getPrettyItemString(Material.BROWN_MUSHROOM));
- return false;
- }
- if (!playerInventory.contains(Material.RED_MUSHROOM, 1)) {
- pluginRef.getNotificationManager().sendPlayerInformation(player, NotificationType.REQUIREMENTS_NOT_MET, "Skills.NeedMore", StringUtils.getPrettyItemString(Material.RED_MUSHROOM));
- return false;
- }
- playerInventory.removeItem(new ItemStack(Material.BROWN_MUSHROOM));
- playerInventory.removeItem(new ItemStack(Material.RED_MUSHROOM));
- player.updateInventory();
- if (!RandomChanceUtil.isActivationSuccessful(SkillActivationType.RANDOM_LINEAR_100_SCALE_WITH_CAP, SubSkillType.HERBALISM_SHROOM_THUMB, player)) {
- pluginRef.getNotificationManager().sendPlayerInformation(player, NotificationType.SUBSKILL_MESSAGE_FAILED, "Herbalism.Ability.ShroomThumb.Fail");
- return false;
- }
- return Herbalism.convertShroomThumb(blockState);
- }
- /**
- * Process the Green Thumb ability for plants.
- *
- * @param blockState The {@link BlockState} to check ability activation for
- * @param greenTerra boolean to determine if greenTerra is active or not
- */
- private void processGreenThumbPlants(BlockState blockState, boolean greenTerra) {
- if (!BlockUtils.isFullyGrown(blockState))
- return;
- Player player = getPlayer();
- PlayerInventory playerInventory = player.getInventory();
- Material seed;
- switch (blockState.getType()) {
- case CARROTS:
- seed = Material.CARROT;
- break;
- case WHEAT:
- seed = Material.WHEAT_SEEDS;
- break;
- case NETHER_WART:
- seed = Material.NETHER_WART;
- break;
- case POTATOES:
- seed = Material.POTATO;
- break;
- case BEETROOTS:
- seed = Material.BEETROOT_SEEDS;
- break;
- case COCOA:
- seed = Material.COCOA_BEANS;
- break;
- default:
- return;
- }
- ItemStack seedStack = new ItemStack(seed);
- if (!greenTerra && !RandomChanceUtil.checkRandomChanceExecutionSuccess(player, SubSkillType.HERBALISM_GREEN_THUMB)) {
- return;
- }
- if (!handleBlockState(blockState, greenTerra)) {
- return;
- }
- if (!ItemUtils.isHoe(getPlayer().getInventory().getItemInMainHand())) {
- if (!playerInventory.containsAtLeast(seedStack, 1)) {
- return;
- }
- playerInventory.removeItem(seedStack);
- player.updateInventory(); // Needed until replacement available
- }
- new HerbalismBlockUpdaterTask(blockState).runTaskLater(pluginRef, 0);
- }
- private boolean handleBlockState(BlockState blockState, boolean greenTerra) {
- int greenThumbStage = getGreenThumbStage();
- blockState.setMetadata(MetadataConstants.GREEN_THUMB_METAKEY, new FixedMetadataValue(pluginRef, (int) (System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR)));
- Ageable crops = (Ageable) blockState.getBlockData();
- switch (blockState.getType()) {
- case POTATOES:
- case CARROTS:
- case WHEAT:
- if (greenTerra) {
- crops.setAge(3);
- } else {
- crops.setAge(greenThumbStage);
- }
- break;
- case BEETROOTS:
- case NETHER_WART:
- if (greenTerra || greenThumbStage > 2) {
- crops.setAge(2);
- } else if (greenThumbStage == 2) {
- crops.setAge(1);
- } else {
- crops.setAge(0);
- }
- break;
- case COCOA:
- if (greenTerra || getGreenThumbStage() > 1) {
- crops.setAge(1);
- } else {
- crops.setAge(0);
- }
- break;
- default:
- return false;
- }
- blockState.setBlockData(crops);
- return true;
- }
- private int getGreenThumbStage() {
- return RankUtils.getRank(getPlayer(), SubSkillType.HERBALISM_GREEN_THUMB);
- }
- }
|