-
-
Notifications
You must be signed in to change notification settings - Fork 20
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
70 changed files
with
832 additions
and
1,549 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
85 changes: 85 additions & 0 deletions
85
Compatibility/src/main/java/com/craftaro/core/compatibility/crops/CompatibleCrop.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,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); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
Compatibility/src/main/java/com/craftaro/core/compatibility/crops/CompatibleCropModern.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,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); | ||
} | ||
} |
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
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
Oops, something went wrong.