Advertisement
Guest User

HerbalismManager.java

a guest
Jul 18th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.31 KB | None | 0 0
  1. package com.gmail.nossr50.skills.herbalism;
  2.  
  3. import com.gmail.nossr50.config.Config;
  4. import com.gmail.nossr50.config.experience.ExperienceConfig;
  5. import com.gmail.nossr50.config.treasure.TreasureConfig;
  6. import com.gmail.nossr50.datatypes.mods.CustomBlock;
  7. import com.gmail.nossr50.datatypes.player.McMMOPlayer;
  8. import com.gmail.nossr50.datatypes.skills.*;
  9. import com.gmail.nossr50.datatypes.treasure.HylianTreasure;
  10. import com.gmail.nossr50.locale.LocaleLoader;
  11. import com.gmail.nossr50.mcMMO;
  12. import com.gmail.nossr50.runnables.skills.HerbalismBlockUpdaterTask;
  13. import com.gmail.nossr50.skills.SkillManager;
  14. import com.gmail.nossr50.util.*;
  15. import com.gmail.nossr50.util.skills.SkillUtils;
  16. import org.bukkit.CropState;
  17. import org.bukkit.Location;
  18. import org.bukkit.Material;
  19. import org.bukkit.NetherWartsState;
  20. import org.bukkit.block.BlockState;
  21. import org.bukkit.entity.Player;
  22. import org.bukkit.inventory.ItemStack;
  23. import org.bukkit.inventory.PlayerInventory;
  24. import org.bukkit.material.CocoaPlant;
  25. import org.bukkit.material.CocoaPlant.CocoaPlantSize;
  26. import org.bukkit.material.Crops;
  27. import org.bukkit.material.NetherWarts;
  28. import org.bukkit.metadata.FixedMetadataValue;
  29.  
  30. import java.util.Collection;
  31. import java.util.List;
  32.  
  33. public class HerbalismManager extends SkillManager {
  34.  
  35.     public HerbalismManager(McMMOPlayer mcMMOPlayer) {
  36.         super(mcMMOPlayer, SkillType.HERBALISM);
  37.     }
  38.  
  39.     public boolean canBlockCheck() {
  40.         return !(Config.getInstance().getHerbalismPreventAFK() && getPlayer().isInsideVehicle());
  41.     }
  42.  
  43.     public boolean canGreenThumbBlock(BlockState blockState) {
  44.         Player player = getPlayer();
  45.         ItemStack item = player.getInventory().getItemInMainHand();
  46.  
  47.         return item.getAmount() > 0 && item.getType() == Material.SEEDS && BlockUtils.canMakeMossy(blockState) && Permissions.greenThumbBlock(player, blockState.getType());
  48.     }
  49.  
  50.     public boolean canUseShroomThumb(BlockState blockState) {
  51.         Player player = getPlayer();
  52.         PlayerInventory inventory = player.getInventory();
  53.         Material itemType = inventory.getItemInMainHand().getType();
  54.  
  55.         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.secondaryAbilityEnabled(player, SecondaryAbility.SHROOM_THUMB);
  56.     }
  57.  
  58.     public boolean canUseHylianLuck() {
  59.         return Permissions.secondaryAbilityEnabled(getPlayer(), SecondaryAbility.HYLIAN_LUCK);
  60.     }
  61.  
  62.     public boolean canGreenTerraBlock(BlockState blockState) {
  63.         return mcMMOPlayer.getAbilityMode(AbilityType.GREEN_TERRA) && BlockUtils.canMakeMossy(blockState);
  64.     }
  65.  
  66.     public boolean canActivateAbility() {
  67.         return mcMMOPlayer.getToolPreparationMode(ToolType.HOE) && Permissions.greenTerra(getPlayer());
  68.     }
  69.  
  70.     public boolean canGreenTerraPlant() {
  71.         return mcMMOPlayer.getAbilityMode(AbilityType.GREEN_TERRA);
  72.     }
  73.  
  74.     /**
  75.      * Handle the Farmer's Diet ability
  76.      *
  77.      * @param rankChange     The # of levels to change rank for the food
  78.      * @param eventFoodLevel The initial change in hunger from the event
  79.      * @return the modified change in hunger for the event
  80.      */
  81.     public int farmersDiet(int rankChange, int eventFoodLevel) {
  82.         return SkillUtils.handleFoodSkills(getPlayer(), skill, eventFoodLevel, Herbalism.farmersDietRankLevel1, Herbalism.farmersDietMaxLevel, rankChange);
  83.     }
  84.  
  85.     /**
  86.      * Process the Green Terra ability.
  87.      *
  88.      * @param blockState The {@link BlockState} to check ability activation for
  89.      * @return true if the ability was successful, false otherwise
  90.      */
  91.     public boolean processGreenTerra(BlockState blockState) {
  92.         Player player = getPlayer();
  93.  
  94.         if (!Permissions.greenThumbBlock(player, blockState.getType())) {
  95.             return false;
  96.         }
  97.  
  98.         PlayerInventory playerInventory = player.getInventory();
  99.         ItemStack seed = new ItemStack(Material.SEEDS);
  100.  
  101.         if (!playerInventory.containsAtLeast(seed, 1)) {
  102.             player.sendMessage(LocaleLoader.getString("Herbalism.Ability.GTe.NeedMore"));
  103.             return false;
  104.         }
  105.  
  106.         playerInventory.removeItem(seed);
  107.         player.updateInventory(); // Needed until replacement available
  108.  
  109.         return Herbalism.convertGreenTerraBlocks(blockState);
  110.     }
  111.  
  112.     /**
  113.      * @param blockState The {@link BlockState} to check ability activation for
  114.      */
  115.     public void herbalismBlockCheck(BlockState blockState) {
  116.         Player player = getPlayer();
  117.         Material material = blockState.getType();
  118.         boolean oneBlockPlant = !(material == Material.CACTUS || material == Material.CHORUS_PLANT || material == Material.SUGAR_CANE_BLOCK);
  119.  
  120.         if (!oneBlockPlant && mcMMO.getPlaceStore().isTrue(blockState)) {
  121.             return;
  122.         }
  123.  
  124.         if (!canBlockCheck()) {
  125.             return;
  126.         }
  127.  
  128.         Collection<ItemStack> drops = null;
  129.         int amount = 1;
  130.         int xp;
  131.         boolean greenTerra = mcMMOPlayer.getAbilityMode(skill.getAbility());
  132.  
  133.         if (mcMMO.getModManager().isCustomHerbalismBlock(blockState)) {
  134.             CustomBlock customBlock = mcMMO.getModManager().getBlock(blockState);
  135.             xp = customBlock.getXpGain();
  136.  
  137.             if (Permissions.secondaryAbilityEnabled(player, SecondaryAbility.HERBALISM_DOUBLE_DROPS) && customBlock.isDoubleDropEnabled()) {
  138.                 drops = blockState.getBlock().getDrops();
  139.             }
  140.         } else {
  141.             if (Permissions.greenThumbPlant(player, material)) {
  142.                 processGreenThumbPlants(blockState, greenTerra);
  143.             }
  144.  
  145.             if (material == Material.CHORUS_FLOWER && blockState.getRawData() != 5) {
  146.                 return;
  147.             }
  148.             xp = ExperienceConfig.getInstance().getXp(skill, blockState.getData());
  149.  
  150.             if (Config.getInstance().getDoubleDropsEnabled(skill, material) && Permissions.secondaryAbilityEnabled(player, SecondaryAbility.HERBALISM_DOUBLE_DROPS)) {
  151.                 drops = blockState.getBlock().getDrops();
  152.             }
  153.  
  154.             if (!oneBlockPlant) {
  155.                 amount = Herbalism.calculateMultiBlockPlantDrops(blockState);
  156.                 xp *= amount;
  157.             }
  158.         }
  159.  
  160.         applyXpGain(xp, XPGainReason.PVE);
  161.  
  162.         if (drops == null) {
  163.             return;
  164.         }
  165.  
  166.         for (int i = greenTerra ? 2 : 1; i != 0; i--) {
  167.             if (SkillUtils.activationSuccessful(SecondaryAbility.HERBALISM_DOUBLE_DROPS, getPlayer(), getSkillLevel(), activationChance)) {
  168.                 for (ItemStack item : drops) {
  169.                     Misc.dropItems(Misc.getBlockCenter(blockState), item, amount);
  170.                 }
  171.             }
  172.         }
  173.     }
  174.  
  175.     /**
  176.      * Process the Green Thumb ability for blocks.
  177.      *
  178.      * @param blockState The {@link BlockState} to check ability activation for
  179.      * @return true if the ability was successful, false otherwise
  180.      */
  181.     public boolean processGreenThumbBlocks(BlockState blockState) {
  182.         if (!SkillUtils.activationSuccessful(SecondaryAbility.GREEN_THUMB_BLOCK, getPlayer(), getSkillLevel(), activationChance)) {
  183.             getPlayer().sendMessage(LocaleLoader.getString("Herbalism.Ability.GTh.Fail"));
  184.             return false;
  185.         }
  186.  
  187.         return Herbalism.convertGreenTerraBlocks(blockState);
  188.     }
  189.  
  190.     /**
  191.      * Process the Hylian Luck ability.
  192.      *
  193.      * @param blockState The {@link BlockState} to check ability activation for
  194.      * @return true if the ability was successful, false otherwise
  195.      */
  196.     public boolean processHylianLuck(BlockState blockState) {
  197.         if (!SkillUtils.activationSuccessful(SecondaryAbility.HYLIAN_LUCK, getPlayer(), getSkillLevel(), activationChance)) {
  198.             return false;
  199.         }
  200.  
  201.         String friendly = StringUtils.getFriendlyConfigMaterialDataString(blockState.getData());
  202.         if (!TreasureConfig.getInstance().hylianMap.containsKey(friendly))
  203.             return false;
  204.         List<HylianTreasure> treasures = TreasureConfig.getInstance().hylianMap.get(friendly);
  205.  
  206.         Player player = getPlayer();
  207.  
  208.         if (treasures.isEmpty()) {
  209.             return false;
  210.         }
  211.         int skillLevel = getSkillLevel();
  212.         Location location = Misc.getBlockCenter(blockState);
  213.  
  214.         for (HylianTreasure treasure : treasures) {
  215.             if (skillLevel >= treasure.getDropLevel() && SkillUtils.treasureDropSuccessful(getPlayer(), treasure.getDropChance(), activationChance)) {
  216.                 if (!EventUtils.simulateBlockBreak(blockState.getBlock(), player, false)) {
  217.                     return false;
  218.                 }
  219.                 blockState.setType(Material.AIR);
  220.                 Misc.dropItem(location, treasure.getDrop());
  221.                 player.sendMessage(LocaleLoader.getString("Herbalism.HylianLuck"));
  222.                 return true;
  223.             }
  224.         }
  225.         return false;
  226.     }
  227.  
  228.     /**
  229.      * Process the Shroom Thumb ability.
  230.      *
  231.      * @param blockState The {@link BlockState} to check ability activation for
  232.      * @return true if the ability was successful, false otherwise
  233.      */
  234.     public boolean processShroomThumb(BlockState blockState) {
  235.         Player player = getPlayer();
  236.         PlayerInventory playerInventory = player.getInventory();
  237.  
  238.         if (!playerInventory.contains(Material.BROWN_MUSHROOM, 1)) {
  239.             player.sendMessage(LocaleLoader.getString("Skills.NeedMore", StringUtils.getPrettyItemString(Material.BROWN_MUSHROOM)));
  240.             return false;
  241.         }
  242.  
  243.         if (!playerInventory.contains(Material.RED_MUSHROOM, 1)) {
  244.             player.sendMessage(LocaleLoader.getString("Skills.NeedMore", StringUtils.getPrettyItemString(Material.RED_MUSHROOM)));
  245.             return false;
  246.         }
  247.  
  248.         playerInventory.removeItem(new ItemStack(Material.BROWN_MUSHROOM));
  249.         playerInventory.removeItem(new ItemStack(Material.RED_MUSHROOM));
  250.         player.updateInventory();
  251.  
  252.         if (!SkillUtils.activationSuccessful(SecondaryAbility.SHROOM_THUMB, getPlayer(), getSkillLevel(), activationChance)) {
  253.             player.sendMessage(LocaleLoader.getString("Herbalism.Ability.ShroomThumb.Fail"));
  254.             return false;
  255.         }
  256.  
  257.         return Herbalism.convertShroomThumb(blockState);
  258.     }
  259.  
  260.     /**
  261.      * Process the Green Thumb ability for plants.
  262.      *
  263.      * @param blockState The {@link BlockState} to check ability activation for
  264.      * @param greenTerra boolean to determine if greenTerra is active or not
  265.      */
  266.     private void processGreenThumbPlants(BlockState blockState, boolean greenTerra) {
  267.         Player player = getPlayer();
  268.         PlayerInventory playerInventory = player.getInventory();
  269.         Material seed = null;
  270.  
  271.         switch (blockState.getType()) {
  272.             case CARROT:
  273.                 seed = Material.CARROT_ITEM;
  274.                 break;
  275.  
  276.             case CROPS:
  277.                 seed = Material.SEEDS;
  278.                 break;
  279.  
  280.             case NETHER_WARTS:
  281.                 seed = Material.NETHER_STALK;
  282.                 break;
  283.  
  284.             case POTATO:
  285.                 seed = Material.POTATO_ITEM;
  286.                 break;
  287.  
  288.             case BEETROOT_BLOCK:
  289.                 seed = Material.BEETROOT_SEEDS;
  290.                 break;
  291.  
  292.             default:
  293.                 return;
  294.         }
  295.  
  296.         if (!playerInventory.contains(seed, 1)) {
  297.             return;
  298.         }
  299.  
  300.         if (!greenTerra && !SkillUtils.activationSuccessful(SecondaryAbility.GREEN_THUMB_PLANT, getPlayer(), getSkillLevel(), activationChance)) {
  301.             return;
  302.         }
  303.  
  304.         if (!handleBlockState(blockState, greenTerra)) {
  305.             return;
  306.         }
  307.  
  308.         playerInventory.removeItem(new ItemStack(seed));
  309.         player.updateInventory(); // Needed until replacement available
  310.         new HerbalismBlockUpdaterTask(blockState).runTaskLater(mcMMO.p, 0);
  311.     }
  312.  
  313.     private boolean handleBlockState(BlockState blockState, boolean greenTerra) {
  314.         byte greenThumbStage = getGreenThumbStage();
  315.  
  316.         blockState.setMetadata(mcMMO.greenThumbDataKey, new FixedMetadataValue(mcMMO.p, (int) (System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR)));
  317.  
  318.         switch (blockState.getType()) {
  319.  
  320.             case POTATO:
  321.             case CARROT:
  322.             case BEETROOT_BLOCK:
  323.             case CROPS:
  324.                 Crops crops = (Crops) blockState.getData();
  325.  
  326.                 if (greenTerra) {
  327.                     crops.setState(CropState.MEDIUM);
  328.                 } else {
  329.                     switch (greenThumbStage) {
  330.                         case 4:
  331.                             crops.setState(CropState.SMALL);
  332.                             break;
  333.                         case 3:
  334.                             crops.setState(CropState.VERY_SMALL);
  335.                             break;
  336.                         case 2:
  337.                             crops.setState(CropState.GERMINATED);
  338.                             break;
  339.                         default:
  340.                             crops.setState(CropState.SEEDED);
  341.                             break;
  342.                     }
  343.                 }
  344.  
  345.                 return true;
  346.  
  347.             case NETHER_WARTS:
  348.                 NetherWarts warts = (NetherWarts) blockState.getData();
  349.  
  350.                 if (greenTerra || greenThumbStage > 2) {
  351.                     warts.setState(NetherWartsState.STAGE_TWO);
  352.                 } else if (greenThumbStage == 2) {
  353.                     warts.setState(NetherWartsState.STAGE_ONE);
  354.                 } else {
  355.                     warts.setState(NetherWartsState.SEEDED);
  356.                 }
  357.  
  358.                 return true;
  359.  
  360.             case COCOA:
  361.                 CocoaPlant plant = (CocoaPlant) blockState.getData();
  362.  
  363.                 if (greenTerra || getGreenThumbStage() > 1) {
  364.                     plant.setSize(CocoaPlantSize.MEDIUM);
  365.                 } else {
  366.                     plant.setSize(CocoaPlantSize.SMALL);
  367.                 }
  368.  
  369.                 return true;
  370.  
  371.             default:
  372.                 return false;
  373.         }
  374.     }
  375.  
  376.     private byte getGreenThumbStage() {
  377.         return (byte) Math.min(Math.min(getSkillLevel(), Herbalism.greenThumbStageMaxLevel) / Herbalism.greenThumbStageChangeLevel, 4);
  378.     }
  379. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement