feat: Introduce CompatibleCrop class

At least EpicFarming and EpicAutomators can make good
use of this class and get rid of code that relies on
magic numbers and needs cross-version support.
This commit is contained in:
Christian Koop 2024-10-02 20:20:24 +02:00
parent b25dc736ce
commit bdcc9bb0b9
No known key found for this signature in database
GPG Key ID: 6A4A09E8ED946113
2 changed files with 108 additions and 0 deletions

View File

@ -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);
}
}
}

View File

@ -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);
}
}