Переглянути джерело

Fixed ageable cast class exception Fixes #5224
Fixed Herbalism not replanting certain crops

nossr50 1 місяць тому
батько
коміт
9f5994596a

+ 7 - 0
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
 

+ 24 - 32
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);
-
-            newState.update(true, true);
+            if (blockState.getBlockData() instanceof Ageable ageable) {
+                ageable.setAge(age);
+                blockState.setBlockData(ageable);
+                blockState.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

+ 2 - 3
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;
         }