Added a system to despawn broken farms.

Fixed issues with some crops not auto replanting.
Reimplemented the CropType enum.
This commit is contained in:
Brianna O'Keefe 2018-08-18 07:18:28 -04:00
parent 4844d25b5f
commit 418fa34f55
2 changed files with 88 additions and 114 deletions

View File

@ -65,35 +65,41 @@ public class FarmingHandler {
private void hopRunner() { private void hopRunner() {
for (Farm farm : instance.getFarmManager().getFarms().values()) { for (Farm farm : instance.getFarmManager().getFarms().values()) {
if (farm.getLocation().getBlock() == null) {
instance.getFarmManager().removeFarm(farm.getLocation());
continue;
}
Block block = farm.getLocation().getBlock(); Block block = farm.getLocation().getBlock();
if (block.getRelative(BlockFace.DOWN).getType() == Material.HOPPER) { if (block.getRelative(BlockFace.DOWN).getType() != Material.HOPPER)
Inventory inventory = farm.getInventory(); return;
Inventory hopperInventory = ((Hopper) block.getRelative(BlockFace.DOWN).getState()).getInventory();
for (int i = 27; i < inventory.getSize(); i++) { Inventory inventory = farm.getInventory();
if (inventory.getItem(i) == null || inventory.getItem(i).getType() == Material.AIR) continue; Inventory hopperInventory = ((Hopper) block.getRelative(BlockFace.DOWN).getState()).getInventory();
int amtToMove = 1; for (int i = 27; i < inventory.getSize(); i++) {
if (inventory.getItem(i) == null || inventory.getItem(i).getType() == Material.AIR) continue;
ItemStack item = inventory.getItem(i); int amtToMove = 1;
ItemStack toMove = item.clone(); ItemStack item = inventory.getItem(i);
toMove.setAmount(amtToMove);
int newAmt = item.getAmount() - amtToMove; ItemStack toMove = item.clone();
toMove.setAmount(amtToMove);
if (canHop(hopperInventory, toMove)) { int newAmt = item.getAmount() - amtToMove;
if (newAmt <= 0)
inventory.setItem(i, null); if (canHop(hopperInventory, toMove)) {
else if (newAmt <= 0)
item.setAmount(newAmt); inventory.setItem(i, null);
hopperInventory.addItem(toMove); else
} item.setAmount(newAmt);
break; hopperInventory.addItem(toMove);
} }
break;
} }
} }
} }
private boolean canHop(Inventory i, ItemStack item) { private boolean canHop(Inventory i, ItemStack item) {
@ -109,10 +115,11 @@ public class FarmingHandler {
private boolean doDrop(Farm farm, Material material) { private boolean doDrop(Farm farm, Material material) {
Random random = new Random(); Random random = new Random();
CropType.CropTypeData cropTypeData = CropType.getCropType(material); CropType cropTypeData = CropType.getCropType(material);
if (material == null || farm == null || cropTypeData == null) return false; if (material == null || farm == null || cropTypeData == null) return false;
ItemStack stack = new ItemStack(cropTypeData.getYieldMaterial()); ItemStack stack = new ItemStack(cropTypeData.getYieldMaterial());
ItemStack seedStack = new ItemStack(cropTypeData.getSeedMaterial(), random.nextInt(3) + 1); ItemStack seedStack = new ItemStack(cropTypeData.getSeedMaterial(), random.nextInt(3) + 1);

View File

@ -6,81 +6,21 @@ import org.bukkit.Material;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
/** public enum CropType {
* A list of all crop types available in Minecraft that can be
* modified by CropsReborn
*/
public class CropType {
private static List<CropTypeData> crops = new ArrayList<>();
private CropType() { WHEAT("Wheat", Material.WHEAT, Material.WHEAT, Material.WHEAT_SEEDS),
crops.add(new CropTypeData("Wheat", Material.WHEAT, Material.WHEAT, Material.WHEAT_SEEDS));
crops.add(new CropTypeData("Carrot", Material.CARROT, Material.CARROT, Material.CARROT));
crops.add(new CropTypeData("Potato", Material.POTATO, Material.POTATO, Material.POTATO));
crops.add(new CropTypeData("Watermelon", Material.MELON_STEM, Material.MELON, Material.MELON_SEEDS));
crops.add(new CropTypeData("Pumpkin", Material.PUMPKIN_STEM, Material.PUMPKIN, Material.PUMPKIN_SEEDS));
crops.add(new CropTypeData("Melon", Material.MELON_STEM, Material.MELON, Material.MELON_SEEDS));
crops.add(new CropTypeData("Nether Wart", Material.NETHER_WART_BLOCK, Material.NETHER_WART, Material.NETHER_WART)); CARROT("Carrot", Material.CARROTS, Material.CARROT, Material.CARROT),
crops.add(new CropTypeData("Beetroot", Material.BEETROOT, Material.BEETROOT, Material.BEETROOT_SEEDS));
}
private void handleAdd() { POTATO("Potato", Material.POTATOES, Material.POTATO, Material.POTATO),
if (crops.size() < 1) {
crops.add(new CropTypeData("Wheat", Material.WHEAT, Material.WHEAT, Material.WHEAT_SEEDS));
crops.add(new CropTypeData("Carrot", Material.CARROT, Material.CARROT, Material.CARROT));
crops.add(new CropTypeData("Potato", Material.POTATO, Material.POTATO, Material.POTATO));
crops.add(new CropTypeData("Watermelon", Material.MELON_STEM, Material.MELON, Material.MELON_SEEDS));
crops.add(new CropTypeData("Pumpkin", Material.PUMPKIN_STEM, Material.PUMPKIN, Material.PUMPKIN_SEEDS));
crops.add(new CropTypeData("Melon", Material.MELON_STEM, Material.MELON, Material.MELON_SEEDS));
crops.add(new CropTypeData("Nether Wart", Material.NETHER_WART_BLOCK, Material.NETHER_WART, Material.NETHER_WART)); BEETROOT("Beetroot", Material.BEETROOTS, Material.BEETROOT, Material.BEETROOT_SEEDS),
crops.add(new CropTypeData("Beetroot", Material.BEETROOT, Material.BEETROOT, Material.BEETROOT_SEEDS)); }
}
public static boolean isCrop(Material material) {
for (CropTypeData type : values())
if (type.getBlockMaterial() == material) return true;
return false;
}
public static boolean isCropSeed(Material material) {
for (CropTypeData type : values())
if (type.getSeedMaterial() == material) return true;
return false;
}
public static CropTypeData getCropType(Material material) {
for (CropTypeData type : values())
if (type.getBlockMaterial() == material) return type;
return null;
}
public static List<CropTypeData> values() {
if (crops.size() < 1) {
new CropType().handleAdd();
}
return crops;
}
/*
WHEAT("Wheat", Material.CROPS, Material.WHEAT, Material.SEEDS),
CARROT("Carrot", Material.CARROT, Material.CARROT_ITEM, Material.CARROT_ITEM),
POTATO("Potato", Material.POTATO, Material.CARROT_ITEM, Material.POTATO_ITEM),
BEETROOT("Beetroot", Material.BEETROOT_BLOCK, Material.BEETROOT, Material.BEETROOT_SEEDS),
WATER_MELON_STEM("Watermelon", Material.MELON_STEM, Material.MELON, Material.MELON_SEEDS), WATER_MELON_STEM("Watermelon", Material.MELON_STEM, Material.MELON, Material.MELON_SEEDS),
PUMPKIN_STEM("Pumpkin", Material.PUMPKIN_STEM, Material.PUMPKIN, Material.PUMPKIN_SEEDS), PUMPKIN_STEM("Pumpkin", Material.PUMPKIN_STEM, Material.PUMPKIN, Material.PUMPKIN_SEEDS),
NETHER_WARTS("Nether Wart", Material.NETHER_WART_BLOCK, Material.NETHER_WARTS, Material.NETHER_WARTS); NETHER_WARTS("Nether Wart", Material.NETHER_WART_BLOCK, Material.NETHER_WART, Material.NETHER_WART);
private final String name; private final String name;
private final Material yieldMaterial, blockMaterial, seedMaterial; private final Material yieldMaterial, blockMaterial, seedMaterial;
@ -92,50 +32,77 @@ public class CropType {
this.yieldMaterial = yieldMaterial; this.yieldMaterial = yieldMaterial;
} }
/**
* Get the friendly name of the crop
*
* @return the name of the crop
*/
public String getName() { public String getName() {
return name; return name;
} }
/**
* Get the blockMaterial that represents this crop type
*
* @return the represented blockMaterial
*/
public Material getBlockMaterial() { public Material getBlockMaterial() {
return blockMaterial; return blockMaterial;
} }
/**
* Get the yield Material that represents this crop type
*
* @return the represented yieldMaterial
*/
public Material getYieldMaterial() { public Material getYieldMaterial() {
return yieldMaterial; return yieldMaterial;
} }
/**
* Get the blockMaterial that represents the seed item for this crop type
*
* @return the represented seed blockMaterial
*/
public Material getSeedMaterial() { public Material getSeedMaterial() {
return seedMaterial; return seedMaterial;
} }
*/
public class CropTypeData { /**
private final String name; * Check whether a specific blockMaterial is an enumerated crop type or not
private final Material blockMaterial; *
private final Material seedMaterial; * @param material the blockMaterial to check
private final Material yieldMaterial; * @return true if it is a crop, false otherwise
*/
public CropTypeData(String _name, Material _blockMaterial, Material _seedMaterial, Material _yieldMaterial) { public static boolean isCrop(Material material) {
name = _name; for (CropType type : values())
blockMaterial = _blockMaterial; if (type.getBlockMaterial() == material) return true;
seedMaterial = _seedMaterial; return false;
yieldMaterial = _yieldMaterial;
}
public String getName() {
return name;
}
public Material getBlockMaterial() {
return blockMaterial;
}
public Material getSeedMaterial() {
return seedMaterial;
}
public Material getYieldMaterial() {
return yieldMaterial;
}
} }
/**
* Check whether a specific blockMaterial is an enumerated crop type seed or not
*
* @param material the blockMaterial to check
* @return true if it is a seed, false otherwise
*/
public static boolean isCropSeed(Material material) {
for (CropType type : values())
if (type.getSeedMaterial() == material) return true;
return false;
}
/**
* Get the crop type based on the specified blockMaterial
*
* @param material the crop blockMaterial
* @return the respective CropType. null if none found
*/
public static CropType getCropType(Material material) {
for (CropType type : values())
if (type.getBlockMaterial() == material) return type;
return null;
}
} }