-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
149 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/main/java/com/craftaro/epicfarming/utils/CropUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
37
src/main/java/com/craftaro/epicfarming/utils/CropUtilsModern.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |