From a598796c997708c44fef7f5fec7c2e42467af90f Mon Sep 17 00:00:00 2001 From: nossr50 Date: Wed, 19 Feb 2020 17:20:05 -0800 Subject: [PATCH] More tweaks to GT --- Changelog.txt | 2 + .../meta/RecentlyReplantedCropMeta.java | 4 +- .../nossr50/listeners/BlockListener.java | 5 -- .../runnables/skills/DelayedCropReplant.java | 24 ++++--- .../nossr50/skills/herbalism/Herbalism.java | 10 --- .../skills/herbalism/HerbalismManager.java | 65 ++++++++++--------- 6 files changed, 53 insertions(+), 57 deletions(-) diff --git a/Changelog.txt b/Changelog.txt index 15d7084df..b3440eba1 100644 --- a/Changelog.txt +++ b/Changelog.txt @@ -1,4 +1,6 @@ Version 2.1.115 + Cocoa plants now require GT of at least 2 to start at the second stage of growth + Green Terra now boosts growth on Green Thumb by 1 stage (doesn't go above the maximum value though) Green Thumb now requires a hoe to activate You can sneak to break plants with a hoe in your hand (or just put the hoe away) Hoes no longer give free replants diff --git a/src/main/java/com/gmail/nossr50/datatypes/meta/RecentlyReplantedCropMeta.java b/src/main/java/com/gmail/nossr50/datatypes/meta/RecentlyReplantedCropMeta.java index 5c630f7bc..20d8e00df 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/meta/RecentlyReplantedCropMeta.java +++ b/src/main/java/com/gmail/nossr50/datatypes/meta/RecentlyReplantedCropMeta.java @@ -10,8 +10,8 @@ public class RecentlyReplantedCropMeta extends FixedMetadataValue { * * @param owningPlugin the {@link Plugin} that created this metadata value */ - public RecentlyReplantedCropMeta(Plugin owningPlugin) { - super(owningPlugin, true); + public RecentlyReplantedCropMeta(Plugin owningPlugin, Boolean recentlyPlanted) { + super(owningPlugin, recentlyPlanted); } } diff --git a/src/main/java/com/gmail/nossr50/listeners/BlockListener.java b/src/main/java/com/gmail/nossr50/listeners/BlockListener.java index 9023caa3d..56503f3b0 100644 --- a/src/main/java/com/gmail/nossr50/listeners/BlockListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/BlockListener.java @@ -424,11 +424,6 @@ public class BlockListener implements Listener { BlockState blockState = event.getBlock().getState(); ItemStack heldItem = player.getInventory().getItemInMainHand(); - if (Herbalism.isRecentlyRegrown(blockState)) { - event.setCancelled(true); - return; - } - if (ItemUtils.isSword(heldItem)) { HerbalismManager herbalismManager = UserManager.getPlayer(player).getHerbalismManager(); diff --git a/src/main/java/com/gmail/nossr50/runnables/skills/DelayedCropReplant.java b/src/main/java/com/gmail/nossr50/runnables/skills/DelayedCropReplant.java index d19411b5e..f00f9579e 100644 --- a/src/main/java/com/gmail/nossr50/runnables/skills/DelayedCropReplant.java +++ b/src/main/java/com/gmail/nossr50/runnables/skills/DelayedCropReplant.java @@ -1,10 +1,12 @@ package com.gmail.nossr50.runnables.skills; +import com.gmail.nossr50.datatypes.meta.RecentlyReplantedCropMeta; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.util.skills.ParticleEffectUtils; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; import org.bukkit.block.BlockState; import org.bukkit.block.data.Ageable; import org.bukkit.event.block.BlockBreakEvent; @@ -37,19 +39,23 @@ public class DelayedCropReplant extends BukkitRunnable { Block cropBlock = cropLocation.getBlock(); BlockState currentState = cropBlock.getState(); + //Remove the metadata marking the block as recently replanted + new markPlantAsOld(blockBreakEvent.getBlock().getLocation()).runTaskLater(mcMMO.p, 20*5); + if(blockBreakEvent.isCancelled()) { wasImmaturePlant = true; } //Two kinds of air in Minecraft - if(currentState.getType().equals(Material.AIR) || currentState.getType().equals(Material.CAVE_AIR)) { + if(currentState.getType().equals(cropMaterial) || currentState.getType().equals(Material.AIR) || currentState.getType().equals(Material.CAVE_AIR)) { +// if(currentState.getBlock().getRelative(BlockFace.DOWN)) //The space is not currently occupied by a block so we can fill it cropBlock.setType(cropMaterial); //Get new state (necessary?) BlockState newState = cropBlock.getState(); - newState.setType(cropMaterial); - newState.update(); +// newState.update(); + Ageable ageable = (Ageable) newState.getBlockData(); //Crop age should always be 0 if the plant was immature @@ -62,28 +68,30 @@ public class DelayedCropReplant extends BukkitRunnable { //Age the crop newState.setBlockData(ageable); + newState.update(true); //Play an effect ParticleEffectUtils.playGreenThumbEffect(cropLocation); - //Remove the metadata marking the block as recently replanted - new removePlantMeta(blockBreakEvent.getBlock().getLocation()).runTaskLater(mcMMO.p, 20); } } - private class removePlantMeta extends BukkitRunnable { + private class markPlantAsOld extends BukkitRunnable { private final Location cropLoc; - public removePlantMeta(Location cropLoc) { + public markPlantAsOld(Location cropLoc) { this.cropLoc = cropLoc; } @Override public void run() { Block cropBlock = cropLoc.getBlock(); - cropBlock.removeMetadata(mcMMO.REPLANT_META_KEY, mcMMO.p); + if(cropBlock.getMetadata(mcMMO.REPLANT_META_KEY).size() > 0) + cropBlock.setMetadata(mcMMO.REPLANT_META_KEY, new RecentlyReplantedCropMeta(mcMMO.p, false)); + + ParticleEffectUtils.playFluxEffect(cropLocation); } } diff --git a/src/main/java/com/gmail/nossr50/skills/herbalism/Herbalism.java b/src/main/java/com/gmail/nossr50/skills/herbalism/Herbalism.java index b486db4f2..e461d2f37 100644 --- a/src/main/java/com/gmail/nossr50/skills/herbalism/Herbalism.java +++ b/src/main/java/com/gmail/nossr50/skills/herbalism/Herbalism.java @@ -58,14 +58,4 @@ public class Herbalism { } } - /** - * Check if the block has a recently grown crop from Green Thumb - * - * @param blockState - * The {@link BlockState} to check green thumb regrown for - * @return true if the block is recently regrown, false otherwise - */ - public static boolean isRecentlyRegrown(BlockState blockState) { - return blockState.hasMetadata(mcMMO.greenThumbDataKey) && !SkillUtils.cooldownExpired(blockState.getMetadata(mcMMO.greenThumbDataKey).get(0).asInt(), 1); - } } diff --git a/src/main/java/com/gmail/nossr50/skills/herbalism/HerbalismManager.java b/src/main/java/com/gmail/nossr50/skills/herbalism/HerbalismManager.java index 0a50a60bf..423778ce0 100644 --- a/src/main/java/com/gmail/nossr50/skills/herbalism/HerbalismManager.java +++ b/src/main/java/com/gmail/nossr50/skills/herbalism/HerbalismManager.java @@ -139,6 +139,16 @@ public class HerbalismManager extends SkillManager { return; } + //Check if the plant was recently replanted + if(blockBreakEvent.getBlock().getMetadata(mcMMO.REPLANT_META_KEY).size() >= 1) { + if(blockBreakEvent.getBlock().getMetadata(mcMMO.REPLANT_META_KEY).get(0).asBoolean()) { + //Crop is recently replanted to back out of destroying it + blockBreakEvent.setCancelled(true); + + return; + } + } + /* * There are single-block plants and multi-block plants in Minecraft * In order to give out proper rewards, we need to collect all blocks that would be broken from this event @@ -161,22 +171,17 @@ public class HerbalismManager extends SkillManager { */ private void processHerbalismOnBlocksBroken(BlockBreakEvent blockBreakEvent, HashSet brokenPlants) { BlockState originalBreak = blockBreakEvent.getBlock().getState(); - - //Check if the plant was recently replanted - if(blockBreakEvent.getBlock().getMetadata(mcMMO.REPLANT_META_KEY).size() >= 1) { - //Crop is recently replanted to back out of destroying it - blockBreakEvent.setCancelled(true); - return; - } + boolean greenThumbActivated = false; //TODO: The design of Green Terra needs to change, this is a mess if(Permissions.greenThumbPlant(getPlayer(), originalBreak.getType())) { - if(!getPlayer().isSneaking()) - processGreenThumbPlants(originalBreak, blockBreakEvent, isGreenTerraActive()); + if(!getPlayer().isSneaking()) { + greenThumbActivated = processGreenThumbPlants(originalBreak, blockBreakEvent, isGreenTerraActive()); + } } //When replanting a immature crop we cancel the block break event and back out - if(blockBreakEvent.isCancelled()) { + if(greenThumbActivated) { return; } @@ -659,8 +664,8 @@ public class HerbalismManager extends SkillManager { */ private void startReplantTask(int desiredCropAge, BlockBreakEvent blockBreakEvent, BlockState cropState, boolean isImmature) { //Mark the plant as recently replanted to avoid accidental breakage - blockBreakEvent.getBlock().setMetadata(mcMMO.REPLANT_META_KEY, new RecentlyReplantedCropMeta(mcMMO.p)); new DelayedCropReplant(blockBreakEvent, cropState, desiredCropAge, isImmature).runTaskLater(mcMMO.p, 20 * 2); + blockBreakEvent.getBlock().setMetadata(mcMMO.REPLANT_META_KEY, new RecentlyReplantedCropMeta(mcMMO.p, true)); } /** @@ -669,15 +674,15 @@ public class HerbalismManager extends SkillManager { * @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, BlockBreakEvent blockBreakEvent, boolean greenTerra) { + private boolean processGreenThumbPlants(BlockState blockState, BlockBreakEvent blockBreakEvent, boolean greenTerra) { if(!ItemUtils.isHoe(blockBreakEvent.getPlayer().getInventory().getItemInMainHand())) { - return; + return false; } BlockData blockData = blockState.getBlockData(); if (!(blockData instanceof Ageable)) { - return; + return false; } Ageable ageable = (Ageable) blockData; @@ -714,30 +719,30 @@ public class HerbalismManager extends SkillManager { break; default: - return; + return false; } ItemStack seedStack = new ItemStack(seed); if (!greenTerra && !RandomChanceUtil.checkRandomChanceExecutionSuccess(player, SubSkillType.HERBALISM_GREEN_THUMB, true)) { - return; + return false; } if (!playerInventory.containsAtLeast(seedStack, 1)) { - return; + return false; } if (!processGrowingPlants(blockState, ageable, blockBreakEvent, greenTerra)) { - return; + return false; } playerInventory.removeItem(seedStack); player.updateInventory(); // Needed until replacement available //Play sound SoundManager.sendSound(player, player.getLocation(), SoundType.ITEM_CONSUMED); - - new HerbalismBlockUpdaterTask(blockState).runTaskLater(mcMMO.p, 0); + return true; +// new HerbalismBlockUpdaterTask(blockState).runTaskLater(mcMMO.p, 0); } private boolean processGrowingPlants(BlockState blockState, Ageable ageable, BlockBreakEvent blockBreakEvent, boolean greenTerra) { @@ -747,31 +752,23 @@ public class HerbalismManager extends SkillManager { } int finalAge = 0; - int greenThumbStage = getGreenThumbStage(); + int greenThumbStage = getGreenThumbStage(greenTerra); //Immature plants will start over at 0 if(!isAgeableMature(ageable)) { blockBreakEvent.setCancelled(true); startReplantTask(0, blockBreakEvent, blockState, true); blockState.setType(Material.AIR); - blockState.update(); return true; } - blockState.setMetadata(mcMMO.greenThumbDataKey, new FixedMetadataValue(mcMMO.p, (int) (System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR))); - switch (blockState.getType()) { case POTATOES: case CARROTS: case WHEAT: - if (greenTerra) { - finalAge = 3; - } - else { - finalAge = getGreenThumbStage(); - } + finalAge = getGreenThumbStage(greenTerra); break; case BEETROOTS: @@ -790,7 +787,7 @@ public class HerbalismManager extends SkillManager { case COCOA: - if (greenTerra || getGreenThumbStage() > 1) { + if (getGreenThumbStage(greenTerra) >= 2) { finalAge = 1; } else { @@ -807,7 +804,11 @@ public class HerbalismManager extends SkillManager { return true; } - private int getGreenThumbStage() { + private int getGreenThumbStage(boolean greenTerraActive) { + if(greenTerraActive) + return Math.min(RankUtils.getHighestRank(SubSkillType.HERBALISM_GREEN_THUMB), + RankUtils.getRank(getPlayer(), SubSkillType.HERBALISM_GREEN_THUMB) + 1); + return RankUtils.getRank(getPlayer(), SubSkillType.HERBALISM_GREEN_THUMB); } }