diff --git a/Compatibility/src/main/java/com/craftaro/core/compatibility/crops/CompatibleCrop.java b/Compatibility/src/main/java/com/craftaro/core/compatibility/crops/CompatibleCrop.java new file mode 100644 index 00000000..12efa1b2 --- /dev/null +++ b/Compatibility/src/main/java/com/craftaro/core/compatibility/crops/CompatibleCrop.java @@ -0,0 +1,85 @@ +package com.craftaro.core.compatibility.crops; + +import com.craftaro.core.compatibility.CompatibleMaterial; +import com.cryptomorin.xseries.XBlock; +import com.cryptomorin.xseries.XMaterial; +import org.bukkit.block.Block; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class CompatibleCrop { + private static final boolean USE_LEGACY_IMPLEMENTATION; + + static { + boolean useLegacy = false; + try { + Class.forName("org.bukkit.block.data.Ageable"); + } catch (ClassNotFoundException ignore) { + useLegacy = true; + } + USE_LEGACY_IMPLEMENTATION = useLegacy; + } + + public static boolean isCrop(@Nullable Block block) { + if (block == null) { + return false; + } + + XMaterial material = CompatibleMaterial.getMaterial(block.getType()).get(); + return isCrop(material); + } + + public static boolean isCrop(@Nullable XMaterial material) { + return material != null && XBlock.isCrop(material); + } + + public static boolean isCropFullyGrown(@NotNull Block crop) { + return getCropAge(crop) >= getCropMaxAge(crop); + } + + public static int getCropAge(@NotNull Block crop) { + if (!USE_LEGACY_IMPLEMENTATION) { + return CompatibleCropModern.getCropAge(crop); + } + return crop.getData(); + } + + public static int getCropMaxAge(@NotNull Block crop) { + if (!USE_LEGACY_IMPLEMENTATION) { + return CompatibleCropModern.getCropMaxAge(crop); + } + + switch (CompatibleMaterial.getMaterial(crop.getType()).get()) { + case BEETROOTS: + case NETHER_WART: + return 3; + default: + return 7; + } + } + + public static void resetCropAge(@NotNull Block crop) { + setCropAge(crop, 0); + } + + public static void incrementCropAge(@NotNull Block crop) { + setCropAge(crop, getCropAge(crop) + 1); + } + + private static void setCropAge(Block block, int stage) { + if (stage > getCropMaxAge(block)) { + return; + } + + if (!USE_LEGACY_IMPLEMENTATION) { + CompatibleCropModern.setGrowthStage(block, stage); + return; + } + + try { + Block.class.getDeclaredMethod("setData", byte.class).invoke(block, (byte) stage); + } catch (ReflectiveOperationException ex) { + throw new RuntimeException(ex); + } + } +} diff --git a/Compatibility/src/main/java/com/craftaro/core/compatibility/crops/CompatibleCropModern.java b/Compatibility/src/main/java/com/craftaro/core/compatibility/crops/CompatibleCropModern.java new file mode 100644 index 00000000..59676cb4 --- /dev/null +++ b/Compatibility/src/main/java/com/craftaro/core/compatibility/crops/CompatibleCropModern.java @@ -0,0 +1,23 @@ +package com.craftaro.core.compatibility.crops; + +import org.bukkit.block.Block; +import org.bukkit.block.data.Ageable; +import org.bukkit.block.data.BlockData; + +class CompatibleCropModern { + static int getCropAge(Block block) { + BlockData blockData = block.getBlockData(); + return ((Ageable) blockData).getAge(); + } + + static int getCropMaxAge(Block block) { + BlockData blockData = block.getBlockData(); + return ((Ageable) blockData).getMaximumAge(); + } + + static void setGrowthStage(Block block, int stage) { + Ageable blockData = (Ageable) block.getBlockData(); + blockData.setAge(stage); + block.setBlockData(blockData); + } +}