mirror of
https://github.com/songoda/EpicFarming.git
synced 2024-11-27 21:15:28 +01:00
use core crop block functions
This commit is contained in:
parent
f59cf66635
commit
a993d8f85a
@ -1,19 +1,18 @@
|
||||
package com.songoda.epicfarming.tasks;
|
||||
|
||||
import com.songoda.core.compatibility.CompatibleMaterial;
|
||||
import com.songoda.core.utils.BlockUtils;
|
||||
import com.songoda.epicfarming.EpicFarming;
|
||||
import com.songoda.epicfarming.boost.BoostData;
|
||||
import com.songoda.epicfarming.farming.Crop;
|
||||
import com.songoda.epicfarming.farming.Farm;
|
||||
import com.songoda.epicfarming.utils.CropType;
|
||||
import com.songoda.epicfarming.utils.Methods;
|
||||
import org.bukkit.CropState;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockState;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.material.Crops;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import java.util.List;
|
||||
@ -50,18 +49,16 @@ public class FarmTask extends BukkitRunnable {
|
||||
|
||||
for (Block block : getCrops(farm, true)) {
|
||||
if (!CropType.isCrop(block.getType())) continue;
|
||||
Crops crop = (Crops) block.getState().getData();
|
||||
|
||||
// Add to GrowthTask
|
||||
plugin.getGrowthTask().addLiveCrop(block.getLocation(), new Crop(block.getLocation(), farm));
|
||||
|
||||
if (!farm.getLevel().isAutoHarvest()
|
||||
|| !crop.getState().equals(CropState.RIPE)) continue;
|
||||
if (!farm.getLevel().isAutoHarvest() || BlockUtils.isCropFullyGrown(block)) continue;
|
||||
|
||||
if (!doDrop(farm, block.getType())) continue main;
|
||||
|
||||
if (farm.getLevel().isAutoReplant()) {
|
||||
CropType.replant(block);
|
||||
BlockUtils.resetGrowthStage(block);
|
||||
continue;
|
||||
}
|
||||
block.setType(Material.AIR);
|
||||
@ -124,8 +121,9 @@ public class FarmTask extends BukkitRunnable {
|
||||
for (int fy = -2; fy <= 1; fy++) {
|
||||
for (int fz = -radius; fz <= radius; fz++) {
|
||||
Block b2 = block.getWorld().getBlockAt(bx + fx, by + fy, bz + fz);
|
||||
CompatibleMaterial mat = CompatibleMaterial.getMaterial(b2.getType());
|
||||
|
||||
if (!(b2.getState().getData() instanceof Crops)) continue;
|
||||
if (!mat.isCrop()) continue;
|
||||
|
||||
if (add) {
|
||||
farm.addCachedCrop(b2);
|
||||
|
@ -1,14 +1,10 @@
|
||||
package com.songoda.epicfarming.tasks;
|
||||
|
||||
import com.songoda.core.utils.BlockUtils;
|
||||
import com.songoda.epicfarming.EpicFarming;
|
||||
import com.songoda.epicfarming.farming.Crop;
|
||||
import com.songoda.epicfarming.utils.CropHandler;
|
||||
import com.songoda.epicfarming.utils.CropType;
|
||||
import org.bukkit.CropState;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.BlockState;
|
||||
import org.bukkit.material.Crops;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import java.util.*;
|
||||
@ -58,8 +54,7 @@ public class GrowthTask extends BukkitRunnable {
|
||||
if (rand != cap - 1 && crop.getTicksLived() != cap / 2) continue;
|
||||
}
|
||||
|
||||
CropType.grow(crop);
|
||||
|
||||
BlockUtils.incrementGrowthStage(crop.getLocation().getBlock());
|
||||
crop.setTicksLived(1);
|
||||
}
|
||||
|
||||
|
@ -1,13 +1,7 @@
|
||||
package com.songoda.epicfarming.utils;
|
||||
|
||||
import com.songoda.core.compatibility.CompatibleMaterial;
|
||||
import com.songoda.core.compatibility.ServerVersion;
|
||||
import com.songoda.epicfarming.farming.Crop;
|
||||
import org.bukkit.CropState;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockState;
|
||||
import org.bukkit.material.Crops;
|
||||
|
||||
public enum CropType {
|
||||
|
||||
@ -23,19 +17,18 @@ public enum CropType {
|
||||
|
||||
PUMPKIN_STEM("Pumpkin", CompatibleMaterial.PUMPKIN_STEM, CompatibleMaterial.PUMPKIN, CompatibleMaterial.PUMPKIN_SEEDS),
|
||||
|
||||
NETHER_WARTS("Nether Wart", CompatibleMaterial.NETHER_WART_BLOCK, CompatibleMaterial.NETHER_WART, CompatibleMaterial.NETHER_WART);
|
||||
NETHER_WARTS("Nether Wart", CompatibleMaterial.NETHER_WART, CompatibleMaterial.NETHER_WART, CompatibleMaterial.NETHER_WART);
|
||||
|
||||
private final String name;
|
||||
private final CompatibleMaterial yieldMaterial, blockMaterial, seedMaterial;
|
||||
private final Material yieldMaterial, blockMaterial, seedMaterial;
|
||||
|
||||
CropType(String name, CompatibleMaterial blockMaterial, CompatibleMaterial yieldMaterial, CompatibleMaterial seedMaterial) {
|
||||
this.name = name;
|
||||
this.blockMaterial = blockMaterial;
|
||||
this.seedMaterial = seedMaterial;
|
||||
this.yieldMaterial = yieldMaterial;
|
||||
this.blockMaterial = blockMaterial.getBlockMaterial();
|
||||
this.seedMaterial = seedMaterial.getMaterial();
|
||||
this.yieldMaterial = yieldMaterial.getMaterial();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the friendly name of the crop
|
||||
*
|
||||
@ -51,7 +44,7 @@ public enum CropType {
|
||||
* @return the represented blockMaterial
|
||||
*/
|
||||
public Material getBlockMaterial() {
|
||||
return blockMaterial.getMaterial();
|
||||
return blockMaterial;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -60,7 +53,7 @@ public enum CropType {
|
||||
* @return the represented yieldMaterial
|
||||
*/
|
||||
public Material getYieldMaterial() {
|
||||
return yieldMaterial.getMaterial();
|
||||
return yieldMaterial;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -69,7 +62,7 @@ public enum CropType {
|
||||
* @return the represented seed blockMaterial
|
||||
*/
|
||||
public Material getSeedMaterial() {
|
||||
return seedMaterial.getMaterial();
|
||||
return seedMaterial;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -118,111 +111,4 @@ public enum CropType {
|
||||
return CropType.isCrop(material);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a crop is at its max growth stage
|
||||
*
|
||||
* @param block The crop block to check
|
||||
* @return True if the crop is at its max growth stage, otherwise false
|
||||
*/
|
||||
public static boolean isMaxGrowthStage(Block block) {
|
||||
if (!isGrowableCrop(block.getType()))
|
||||
throw new IllegalArgumentException("Block given was not a valid crop");
|
||||
|
||||
return block.getData() >= getMaxGrowthStage(block.getType());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the max growth stage for the given material
|
||||
*
|
||||
* @param material The material of the crop
|
||||
* @return The max growth stage of the given crop type
|
||||
*/
|
||||
public static int getMaxGrowthStage(Material material) {
|
||||
if (!isGrowableCrop(material))
|
||||
throw new IllegalArgumentException("Block given was not a valid crop");
|
||||
|
||||
if (material.equals(CompatibleMaterial.BEETROOTS.getMaterial()))
|
||||
return 3;
|
||||
|
||||
return 7;
|
||||
}
|
||||
|
||||
/**
|
||||
* Grows a crop by 1 growth stage
|
||||
*
|
||||
* @param crop The crop to grow
|
||||
*/
|
||||
public static void grow(Crop crop) {
|
||||
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13)) {
|
||||
BlockState cropState = crop.getLocation().getBlock().getState();
|
||||
Crops cropData = (Crops) cropState.getData();
|
||||
|
||||
Material material = crop.getLocation().getBlock().getType();
|
||||
|
||||
switch (cropData.getState()) {
|
||||
case SEEDED:
|
||||
if (material == Material.BEETROOTS)
|
||||
cropData.setState(CropState.VERY_SMALL);
|
||||
else
|
||||
cropData.setState(CropState.GERMINATED);
|
||||
break;
|
||||
case GERMINATED:
|
||||
cropData.setState(CropState.VERY_SMALL);
|
||||
break;
|
||||
case VERY_SMALL:
|
||||
cropData.setState(CropState.SMALL);
|
||||
break;
|
||||
case SMALL:
|
||||
cropData.setState(CropState.MEDIUM);
|
||||
break;
|
||||
case MEDIUM:
|
||||
cropData.setState(CropState.TALL);
|
||||
break;
|
||||
case TALL:
|
||||
cropData.setState(CropState.VERY_TALL);
|
||||
break;
|
||||
case VERY_TALL:
|
||||
cropData.setState(CropState.RIPE);
|
||||
break;
|
||||
case RIPE:
|
||||
break;
|
||||
}
|
||||
cropState.setData(cropData);
|
||||
cropState.update();
|
||||
crop.setTicksLived(1);
|
||||
return;
|
||||
}
|
||||
|
||||
Block block = crop.getLocation().getBlock();
|
||||
|
||||
if (!isGrowableCrop(block.getType()))
|
||||
throw new IllegalArgumentException("Block given was not a valid crop");
|
||||
|
||||
byte data = block.getData();
|
||||
|
||||
if (isMaxGrowthStage(block))
|
||||
return;
|
||||
|
||||
block.setData((byte) (data + 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a crop's growth back to stage 0
|
||||
*
|
||||
* @param block The crop block to set
|
||||
*/
|
||||
public static void replant(Block block) {
|
||||
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13)) {
|
||||
BlockState cropState = block.getState();
|
||||
Crops cropData = (Crops) cropState.getData();
|
||||
cropData.setState(CropState.SEEDED);
|
||||
cropState.setData(cropData);
|
||||
cropState.update();
|
||||
return;
|
||||
}
|
||||
if (!isGrowableCrop(block.getType()))
|
||||
throw new IllegalArgumentException("Block given was not a valid crop");
|
||||
|
||||
block.setData((byte) 0);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user