From 9f5994596a7b0eedba921dab57c7b01705aab114 Mon Sep 17 00:00:00 2001 From: nossr50 Date: Sat, 8 Nov 2025 13:27:53 -0800 Subject: [PATCH] Fixed ageable cast class exception Fixes #5224 Fixed Herbalism not replanting certain crops --- Changelog.txt | 7 +++ .../runnables/skills/DelayedCropReplant.java | 56 ++++++++----------- .../skills/herbalism/HerbalismManager.java | 5 +- 3 files changed, 33 insertions(+), 35 deletions(-) diff --git a/Changelog.txt b/Changelog.txt index 0d5eabdca..6011cac09 100644 --- a/Changelog.txt +++ b/Changelog.txt @@ -1,3 +1,10 @@ +Version 2.2.045 + Fixed an error that could happen when replanting crops with Green Thumb + Green Thumb now replants harvested plants faster + + NOTES: + The delay from a plant being replanted from Green Thumb is intentional, and while looking into bugs it seemd maybe a tad slow, so I sped it up a bit. + Version 2.2.044 Fixed copper armor and tools not working with Repair or Salvage 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 b04ad0578..248435cbd 100644 --- a/src/main/java/com/gmail/nossr50/runnables/skills/DelayedCropReplant.java +++ b/src/main/java/com/gmail/nossr50/runnables/skills/DelayedCropReplant.java @@ -51,8 +51,7 @@ public class DelayedCropReplant extends CancellableRunnable { @Override public void run() { - Block cropBlock = cropLocation.getBlock(); - BlockState currentState = cropBlock.getState(); + final BlockState blockState = cropLocation.getBlock().getState(); PlantAnchorType plantAnchorType = PlantAnchorType.NORMAL; //Remove the metadata marking the block as recently replanted @@ -64,51 +63,44 @@ public class DelayedCropReplant extends CancellableRunnable { wasImmaturePlant = true; } - //Two kinds of air in Minecraft - 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); + if (blockIsAirOrExpectedCrop(blockState)) { + // Modify the new state of the block, not any old snapshot of it + blockState.setType(cropMaterial); + final BlockData newData = blockState.getBlockData(); - //Get new state (necessary?) - BlockState newState = cropBlock.getState(); - BlockData newData = newState.getBlockData(); - - int age = 0; - - //Crop age should always be 0 if the plant was immature - if (!wasImmaturePlant) { - age = desiredCropAge; - //Otherwise make the plant the desired age - } + // Immature plants should be age 0, others get the desired age + int age = wasImmaturePlant ? 0 : desiredCropAge; if (newData instanceof Directional) { - //Cocoa Version - Directional directional = (Directional) newState.getBlockData(); + // Cocoa Version + Directional directional = (Directional) blockState.getBlockData(); directional.setFacing(cropFace); - newState.setBlockData(directional); + blockState.setBlockData(directional); if (newData instanceof Cocoa) { plantAnchorType = PlantAnchorType.COCOA; } } - //Age the crop - Ageable ageable = (Ageable) newState.getBlockData(); - ageable.setAge(age); - newState.setBlockData(ageable); + if (blockState.getBlockData() instanceof Ageable ageable) { + ageable.setAge(age); + blockState.setBlockData(ageable); + blockState.update(true, true); - newState.update(true, true); - - //Play an effect - ParticleEffectUtils.playGreenThumbEffect(cropLocation); - mcMMO.p.getFoliaLib().getScheduler().runAtLocationLater(newState.getLocation(), - new PhysicsBlockUpdate(newState.getBlock(), cropFace, plantAnchorType), 1); + //Play an effect + ParticleEffectUtils.playGreenThumbEffect(cropLocation); + mcMMO.p.getFoliaLib().getScheduler().runAtLocationLater(blockState.getLocation(), + new PhysicsBlockUpdate(blockState.getBlock(), cropFace, plantAnchorType), 1); + } } } + private boolean blockIsAirOrExpectedCrop(BlockState blockState) { + return blockState.getType().equals(cropMaterial) || blockState.getType() + .equals(Material.AIR) || blockState.getType().equals(Material.CAVE_AIR); + } + private enum PlantAnchorType { NORMAL, COCOA 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 30314b5a7..4557eeabd 100644 --- a/src/main/java/com/gmail/nossr50/skills/herbalism/HerbalismManager.java +++ b/src/main/java/com/gmail/nossr50/skills/herbalism/HerbalismManager.java @@ -2,6 +2,7 @@ package com.gmail.nossr50.skills.herbalism; import static com.gmail.nossr50.util.ItemUtils.hasItemIncludingOffHand; import static com.gmail.nossr50.util.ItemUtils.removeItemIncludingOffHand; +import static com.gmail.nossr50.util.Misc.TICK_CONVERSION_FACTOR; import static com.gmail.nossr50.util.Misc.getBlockCenter; import static com.gmail.nossr50.util.text.ConfigStringUtils.getMaterialConfigString; import static java.util.Objects.requireNonNull; @@ -822,7 +823,7 @@ public class HerbalismManager extends SkillManager { mcMMO.p.getFoliaLib().getScheduler() .runAtLocationLater(blockBreakEvent.getBlock().getLocation(), new DelayedCropReplant(blockBreakEvent, cropState, desiredCropAge, - isImmature), 2 * Misc.TICK_CONVERSION_FACTOR); + isImmature), TICK_CONVERSION_FACTOR); blockBreakEvent.getBlock().setMetadata(MetadataConstants.METADATA_KEY_REPLANT, new RecentlyReplantedCropMeta(mcMMO.p, true)); } @@ -914,9 +915,7 @@ public class HerbalismManager extends SkillManager { //Immature plants will start over at 0 if (!isAgeableMature(ageable)) { -// blockBreakEvent.setCancelled(true); startReplantTask(0, blockBreakEvent, blockState, true); -// blockState.setType(Material.AIR); blockBreakEvent.setDropItems(false); return true; }