diff --git a/src/main/java/com/songoda/epicfarming/handlers/FarmingHandler.java b/src/main/java/com/songoda/epicfarming/handlers/FarmingHandler.java index d1c4def..4bbb8e1 100644 --- a/src/main/java/com/songoda/epicfarming/handlers/FarmingHandler.java +++ b/src/main/java/com/songoda/epicfarming/handlers/FarmingHandler.java @@ -65,35 +65,41 @@ public class FarmingHandler { private void hopRunner() { for (Farm farm : instance.getFarmManager().getFarms().values()) { + if (farm.getLocation().getBlock() == null) { + instance.getFarmManager().removeFarm(farm.getLocation()); + continue; + } Block block = farm.getLocation().getBlock(); - if (block.getRelative(BlockFace.DOWN).getType() == Material.HOPPER) { - Inventory inventory = farm.getInventory(); - Inventory hopperInventory = ((Hopper) block.getRelative(BlockFace.DOWN).getState()).getInventory(); + if (block.getRelative(BlockFace.DOWN).getType() != Material.HOPPER) + return; - for (int i = 27; i < inventory.getSize(); i++) { - if (inventory.getItem(i) == null || inventory.getItem(i).getType() == Material.AIR) continue; + Inventory inventory = farm.getInventory(); + 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(); - toMove.setAmount(amtToMove); + ItemStack item = inventory.getItem(i); - int newAmt = item.getAmount() - amtToMove; + ItemStack toMove = item.clone(); + toMove.setAmount(amtToMove); - if (canHop(hopperInventory, toMove)) { - if (newAmt <= 0) - inventory.setItem(i, null); - else - item.setAmount(newAmt); - hopperInventory.addItem(toMove); - } - break; + int newAmt = item.getAmount() - amtToMove; + + if (canHop(hopperInventory, toMove)) { + if (newAmt <= 0) + inventory.setItem(i, null); + else + item.setAmount(newAmt); + hopperInventory.addItem(toMove); } + break; } } + } private boolean canHop(Inventory i, ItemStack item) { @@ -109,10 +115,11 @@ public class FarmingHandler { private boolean doDrop(Farm farm, Material material) { Random random = new Random(); - CropType.CropTypeData cropTypeData = CropType.getCropType(material); + CropType cropTypeData = CropType.getCropType(material); if (material == null || farm == null || cropTypeData == null) return false; + ItemStack stack = new ItemStack(cropTypeData.getYieldMaterial()); ItemStack seedStack = new ItemStack(cropTypeData.getSeedMaterial(), random.nextInt(3) + 1); diff --git a/src/main/java/com/songoda/epicfarming/utils/CropType.java b/src/main/java/com/songoda/epicfarming/utils/CropType.java index 3887384..a4a22b6 100644 --- a/src/main/java/com/songoda/epicfarming/utils/CropType.java +++ b/src/main/java/com/songoda/epicfarming/utils/CropType.java @@ -6,81 +6,21 @@ import org.bukkit.Material; import java.util.ArrayList; import java.util.List; -/** - * A list of all crop types available in Minecraft that can be - * modified by CropsReborn - */ -public class CropType { - private static List crops = new ArrayList<>(); +public enum CropType { - private CropType() { - 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)); + WHEAT("Wheat", Material.WHEAT, Material.WHEAT, Material.WHEAT_SEEDS), - crops.add(new CropTypeData("Nether Wart", Material.NETHER_WART_BLOCK, Material.NETHER_WART, Material.NETHER_WART)); - crops.add(new CropTypeData("Beetroot", Material.BEETROOT, Material.BEETROOT, Material.BEETROOT_SEEDS)); - } + CARROT("Carrot", Material.CARROTS, Material.CARROT, Material.CARROT), - private void handleAdd() { - 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)); + POTATO("Potato", Material.POTATOES, Material.POTATO, Material.POTATO), - crops.add(new CropTypeData("Nether Wart", Material.NETHER_WART_BLOCK, Material.NETHER_WART, Material.NETHER_WART)); - 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 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), + BEETROOT("Beetroot", Material.BEETROOTS, Material.BEETROOT, Material.BEETROOT_SEEDS), WATER_MELON_STEM("Watermelon", Material.MELON_STEM, Material.MELON, Material.MELON_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 Material yieldMaterial, blockMaterial, seedMaterial; @@ -92,50 +32,77 @@ public class CropType { this.yieldMaterial = yieldMaterial; } + + /** + * Get the friendly name of the crop + * + * @return the name of the crop + */ public String getName() { return name; } + /** + * Get the blockMaterial that represents this crop type + * + * @return the represented blockMaterial + */ public Material getBlockMaterial() { return blockMaterial; } + /** + * Get the yield Material that represents this crop type + * + * @return the represented yieldMaterial + */ public Material getYieldMaterial() { return yieldMaterial; } + /** + * Get the blockMaterial that represents the seed item for this crop type + * + * @return the represented seed blockMaterial + */ public Material getSeedMaterial() { return seedMaterial; } - */ - public class CropTypeData { - private final String name; - private final Material blockMaterial; - private final Material seedMaterial; - private final Material yieldMaterial; - - public CropTypeData(String _name, Material _blockMaterial, Material _seedMaterial, Material _yieldMaterial) { - name = _name; - blockMaterial = _blockMaterial; - seedMaterial = _seedMaterial; - 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 or not + * + * @param material the blockMaterial to check + * @return true if it is a crop, false otherwise + */ + public static boolean isCrop(Material material) { + for (CropType type : values()) + if (type.getBlockMaterial() == material) return true; + return false; } + + /** + * 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; + } + } \ No newline at end of file