Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
SpraxDev committed Oct 2, 2024
2 parents 90941db + afafb96 commit 51c89b0
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.craftaro</groupId>
<artifactId>EpicFarming</artifactId>
<version>4.4.0</version>
<version>4.4.1</version>

<name>EpicFarming</name>
<description>Allow your players to grow crops faster, automatically replant, harvest and store crops and animal produce in the farm's inventory, as well as much more</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import com.craftaro.core.compatibility.CompatibleMaterial;
import com.craftaro.core.gui.GuiUtils;
import com.craftaro.third_party.com.cryptomorin.xseries.XBlock;
import com.craftaro.third_party.com.cryptomorin.xseries.XMaterial;
import com.craftaro.core.utils.BlockUtils;
import com.craftaro.epicfarming.EpicFarming;
import com.craftaro.epicfarming.boost.BoostData;
import com.craftaro.epicfarming.farming.Farm;
import com.craftaro.epicfarming.farming.FarmType;
import com.craftaro.epicfarming.utils.CropUtils;
import com.craftaro.epicfarming.utils.Methods;
import com.craftaro.third_party.com.cryptomorin.xseries.XBlock;
import com.craftaro.third_party.com.cryptomorin.xseries.XMaterial;
import org.bukkit.Bukkit;
import org.bukkit.DyeColor;
import org.bukkit.Material;
Expand All @@ -23,7 +23,6 @@
import org.bukkit.entity.Sheep;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Wool;

import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -66,10 +65,9 @@ public void runFinal(Farm farm, Collection<LivingEntity> entitiesAroundFarm, Lis

private void collectCrops(Farm farm, List<Block> crops) {
for (Block block : crops) {
if (BlockUtils.isCropFullyGrown(block) && isEnabled(farm) && doCropDrop(farm, CompatibleMaterial.getMaterial(block.getType()).get())) {
if (CropUtils.isFullyGrown(block) && isEnabled(farm) && doCropDrop(farm, CompatibleMaterial.getMaterial(block.getType()).get())) {
if (farm.getLevel().isAutoReplant()) {
Bukkit.getScheduler().runTask(this.plugin, () ->
BlockUtils.resetGrowthStage(block));
Bukkit.getScheduler().runTask(this.plugin, () -> CropUtils.resetGrowthStage(block));
continue;
}
Bukkit.getScheduler().runTask(this.plugin, () -> block.setType(Material.AIR));
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/craftaro/epicfarming/tasks/FarmTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import com.craftaro.core.compatibility.CompatibleMaterial;
import com.craftaro.core.compatibility.ServerVersion;
import com.craftaro.third_party.com.cryptomorin.xseries.XBlock;
import com.craftaro.third_party.com.cryptomorin.xseries.XMaterial;
import com.craftaro.core.utils.BlockUtils;
import com.craftaro.epicfarming.EpicFarming;
import com.craftaro.epicfarming.farming.Crop;
import com.craftaro.epicfarming.farming.Farm;
import com.craftaro.epicfarming.farming.FarmType;
import com.craftaro.epicfarming.settings.Settings;
import com.craftaro.epicfarming.utils.CropUtils;
import com.craftaro.third_party.com.cryptomorin.xseries.XBlock;
import com.craftaro.third_party.com.cryptomorin.xseries.XMaterial;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Block;
Expand Down Expand Up @@ -115,7 +115,7 @@ public void run() {

if (farm.getFarmType() != FarmType.LIVESTOCK) {
for (Block block : crops) {
if (!BlockUtils.isCropFullyGrown(block)) {
if (!CropUtils.isFullyGrown(block)) {
// Add to GrowthTask
growthTask.addLiveCrop(block.getLocation(), new Crop(block.getLocation(), farm));
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/craftaro/epicfarming/tasks/GrowthTask.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.craftaro.epicfarming.tasks;

import com.craftaro.core.compatibility.CompatibleMaterial;
import com.craftaro.third_party.com.cryptomorin.xseries.XBlock;
import com.craftaro.third_party.com.cryptomorin.xseries.XMaterial;
import com.craftaro.core.utils.BlockUtils;
import com.craftaro.epicfarming.EpicFarming;
import com.craftaro.epicfarming.farming.Crop;
import com.craftaro.epicfarming.farming.FarmType;
import com.craftaro.epicfarming.settings.Settings;
import com.craftaro.epicfarming.utils.CropUtils;
import com.craftaro.third_party.com.cryptomorin.xseries.XBlock;
import com.craftaro.third_party.com.cryptomorin.xseries.XMaterial;
import org.bukkit.Location;
import org.bukkit.scheduler.BukkitRunnable;

Expand Down Expand Up @@ -52,7 +52,7 @@ public synchronized void run() {
}
}

BlockUtils.incrementGrowthStage(crop.getLocation().getBlock());
CropUtils.incrementGrowthStage(crop.getLocation().getBlock());
crop.setTicksLived(1);
}

Expand Down
98 changes: 98 additions & 0 deletions src/main/java/com/craftaro/epicfarming/utils/CropUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.craftaro.epicfarming.utils;

import com.craftaro.core.compatibility.CompatibleMaterial;
import com.craftaro.epicfarming.EpicFarming;
import com.craftaro.third_party.com.cryptomorin.xseries.XBlock;
import com.craftaro.third_party.com.cryptomorin.xseries.XMaterial;
import org.bukkit.block.Block;

import java.util.Optional;
import java.util.logging.Level;

public class CropUtils {
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 isFullyGrown(Block crop) {
if (crop == null) {
return false;
}

if (!USE_LEGACY_IMPLEMENTATION) {
return CropUtilsModern.isFullyGrown(crop);
}

Optional<XMaterial> mat = CompatibleMaterial.getMaterial(crop.getType());
if (!mat.isPresent() || !XBlock.isCrop(mat.get())) {
return false;
}

int maxAge = (mat.get() == XMaterial.BEETROOTS || mat.get() == XMaterial.NETHER_WART) ? 3 : 7;
return crop.getData() >= maxAge;
}

public static void resetGrowthStage(Block crop) {
if (crop == null) {
return;
}
setGrowthStage(crop, 0);
}

public static void incrementGrowthStage(Block crop) {
if (crop == null) {
return;
}

if (!USE_LEGACY_IMPLEMENTATION) {
setGrowthStage(crop, getGrowthStage(crop) + 1);
return;
}

Optional<XMaterial> mat = CompatibleMaterial.getMaterial(crop.getType());
if (mat.isPresent() && XBlock.isCrop(mat.get()) && crop.getData() < (mat.get() == XMaterial.BEETROOTS || mat.get() == XMaterial.NETHER_WART ? 3 : 7)) {
try {
setGrowthStage(crop, getGrowthStage(crop) + 1);
} catch (Exception ex) {
EpicFarming.getInstance().getLogger().log(Level.SEVERE, "Unexpected method error", ex);
}
}
}

private static int getGrowthStage(Block block) {
if (!USE_LEGACY_IMPLEMENTATION) {
return CropUtilsModern.getGrowthStage(block);
}
return block.getData();
}

private static void setGrowthStage(Block block, int stage) {
if (stage < 0) {
return;
}

if (!USE_LEGACY_IMPLEMENTATION) {
CropUtilsModern.setGrowthStage(block, stage);
return;
}

Optional<XMaterial> mat = CompatibleMaterial.getMaterial(block.getType());
if (!mat.isPresent() || !XBlock.isCrop(mat.get())) {
return;
}

try {
Block.class.getDeclaredMethod("setData", byte.class).invoke(block, (byte) stage);
} catch (Exception ex) {
EpicFarming.getInstance().getLogger().log(Level.SEVERE, "Unexpected method error", ex);
}
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/craftaro/epicfarming/utils/CropUtilsModern.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.craftaro.epicfarming.utils;

import org.bukkit.block.Block;
import org.bukkit.block.data.Ageable;
import org.bukkit.block.data.BlockData;

public class CropUtilsModern {
static boolean isFullyGrown(Block crop) {
BlockData data = crop.getBlockData();
if (data instanceof Ageable) {
return ((Ageable) data).getAge() == ((Ageable) data).getMaximumAge();
}
return false;
}

static int getGrowthStage(Block block) {
if (!(block.getBlockData() instanceof Ageable)) {
return 0;
}
return ((Ageable) block.getBlockData()).getAge();
}

static void setGrowthStage(Block block, int stage) {
if (!(block.getBlockData() instanceof Ageable)) {
return;
}

Ageable blockData = (Ageable) block.getBlockData();
int max = blockData.getMaximumAge();
if (stage > max) {
return;
}

blockData.setAge(stage);
block.setBlockData(blockData);
}
}

0 comments on commit 51c89b0

Please sign in to comment.