diff --git a/pom.xml b/pom.xml index 5a0c53c..3843961 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.craftaro EpicFarming - 4.1.1 + 4.2.0 EpicFarming Allow your players to grow crops faster, automatically replant, harvest and store crops and animal produce in the farm's inventory, as well as much more @@ -35,7 +35,7 @@ org.apache.maven.plugins maven-shade-plugin - 3.4.1 + 3.5.2 @@ -123,14 +123,14 @@ com.craftaro CraftaroCore - 3.0.0-SNAPSHOT + 3.0.4-SNAPSHOT compile com.craftaro FabledSkyBlock - 3.0.0-b6-SNAPSHOT + 3.0.4 provided diff --git a/src/main/java/com/craftaro/epicfarming/compatibility/EpicFarmingPermission.java b/src/main/java/com/craftaro/epicfarming/compatibility/EpicFarmingPermission.java index 58f29c6..0019900 100644 --- a/src/main/java/com/craftaro/epicfarming/compatibility/EpicFarmingPermission.java +++ b/src/main/java/com/craftaro/epicfarming/compatibility/EpicFarmingPermission.java @@ -1,6 +1,6 @@ package com.craftaro.epicfarming.compatibility; -import com.craftaro.skyblock.core.third_party.com.cryptomorin.xseries.XMaterial; +import com.craftaro.third_party.com.cryptomorin.xseries.XMaterial; import com.craftaro.skyblock.permission.BasicPermission; import com.craftaro.skyblock.permission.PermissionType; diff --git a/src/main/java/com/craftaro/epicfarming/farming/Farm.java b/src/main/java/com/craftaro/epicfarming/farming/Farm.java index c2756d9..8a47e5b 100644 --- a/src/main/java/com/craftaro/epicfarming/farming/Farm.java +++ b/src/main/java/com/craftaro/epicfarming/farming/Farm.java @@ -22,13 +22,7 @@ import org.bukkit.block.BlockFace; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Random; -import java.util.UUID; +import java.util.*; public class Farm implements Data { // This is the unique identifier for this farm. @@ -198,20 +192,27 @@ public class Farm implements Data { for (int fz = -radius; fz <= radius; fz++) { Block b2 = block.getWorld().getBlockAt(bx + fx, by + fy, bz + fz); - // ToDo: enum for all flowers. - if (Settings.BREAKABLE_BLOCKS.getStringList().contains(CompatibleMaterial.getMaterial(b2.getType()).get().name())) { - Bukkit.getScheduler().runTaskLater(EpicFarming.getInstance(), () -> { - XBlock.setType(b2.getRelative(BlockFace.DOWN), XMaterial.FARMLAND); - b2.breakNaturally(); - XSound.BLOCK_GRASS_BREAK.play(b2.getLocation(), 10, 15); - }, random.nextInt(30) + 1); - } - if ((b2.getType() == XMaterial.GRASS_BLOCK.parseMaterial() - || b2.getType() == Material.DIRT) && b2.getRelative(BlockFace.UP).getType() == Material.AIR) { - Bukkit.getScheduler().runTaskLater(EpicFarming.getInstance(), () -> { - XBlock.setType(b2, XMaterial.FARMLAND); - XSound.BLOCK_GRASS_BREAK.play(b2.getLocation(), 10, 15); - }, random.nextInt(30) + 1); + // Check if the block is a grass block or dirt + if (b2.getType() == XMaterial.GRASS_BLOCK.parseMaterial() || b2.getType() == Material.DIRT) { + // Check if the block above is junk (grass or a flower) + Block blockAbove = b2.getRelative(BlockFace.UP); + if (isJunk(blockAbove.getType())) { + Bukkit.getScheduler().runTaskLater(EpicFarming.getInstance(), () -> { + XBlock.setType(b2, XMaterial.FARMLAND); + Collection drops = blockAbove.getDrops(); + if (!drops.isEmpty()) { + blockAbove.breakNaturally(drops.iterator().next()); + } else { + blockAbove.breakNaturally(); + } + XSound.BLOCK_GRASS_BREAK.play(b2.getLocation(), 10, 15); + }, random.nextInt(30) + 1); + } else if (blockAbove.getType() == Material.AIR) { + Bukkit.getScheduler().runTaskLater(EpicFarming.getInstance(), () -> { + XBlock.setType(b2, XMaterial.FARMLAND); + XSound.BLOCK_GRASS_BREAK.play(b2.getLocation(), 10, 15); + }, random.nextInt(30) + 1); + } } } } @@ -219,6 +220,40 @@ public class Farm implements Data { return false; } + private boolean isJunk(Material material) { + XMaterial xMaterial = XMaterial.matchXMaterial(material); + if (xMaterial == null) { + return false; + } + + switch (xMaterial) { + case SHORT_GRASS: + case TALL_GRASS: + case FERN: + case LARGE_FERN: + case DANDELION: + case POPPY: + case BLUE_ORCHID: + case ALLIUM: + case AZURE_BLUET: + case RED_TULIP: + case ORANGE_TULIP: + case WHITE_TULIP: + case PINK_TULIP: + case OXEYE_DAISY: + case CORNFLOWER: + case LILY_OF_THE_VALLEY: + case WITHER_ROSE: + case SUNFLOWER: + case LILAC: + case ROSE_BUSH: + case PEONY: + return true; + default: + return false; + } + } + //Don't use getUniqueId here as it conflicts with the Data interface. public UUID getFarmUUID() { return this.uniqueId; diff --git a/src/main/java/com/craftaro/epicfarming/farming/levels/modules/ModuleAutoBreeding.java b/src/main/java/com/craftaro/epicfarming/farming/levels/modules/ModuleAutoBreeding.java index dc9a848..b434eb9 100644 --- a/src/main/java/com/craftaro/epicfarming/farming/levels/modules/ModuleAutoBreeding.java +++ b/src/main/java/com/craftaro/epicfarming/farming/levels/modules/ModuleAutoBreeding.java @@ -29,6 +29,9 @@ import java.util.Map; import java.util.stream.Collectors; public class ModuleAutoBreeding extends Module { + private static final String BREED_COOLDOWN_METADATA = "breedCooldown"; + private static final int BREED_COOLDOWN_TICKS = 5 * 20 * 60; + private final int autoBreedCap; public ModuleAutoBreeding(EpicFarming plugin, int autoBreedCap) { @@ -58,73 +61,59 @@ public class ModuleAutoBreeding extends Module { return; } - entities.removeIf(e -> !(e instanceof Ageable) || !((Ageable) e).isAdult() || e.hasMetadata("breedCooldown") || e.isDead()); + entities.removeIf(e -> !(e instanceof Ageable) || !((Ageable) e).isAdult() || e.hasMetadata(BREED_COOLDOWN_METADATA) || e.isDead()); - Map counts = - entities.stream().collect(Collectors.groupingBy(Entity::getType, Collectors.counting())); - - for (LivingEntity entity : entities) { - int stackSize = EntityStackerManager.getSize(entity); - if (stackSize == 0) { - stackSize = 1; - } - counts.put(entity.getType(), counts.get(entity.getType()) - 1 + stackSize); - } + Map counts = entities.stream() + .collect(Collectors.groupingBy(Entity::getType, Collectors.summingLong(entity -> { + int stackSize = EntityStackerManager.getSize(entity); + return stackSize > 0 ? stackSize : 1; + }))); boolean mate1 = false; for (Map.Entry entry : counts.entrySet()) { + if (entry.getValue() < 2) { + continue; + } + + EntityType entityType = entry.getKey(); + ItemStack breedingItem = getBreedingItem(farm, entityType); + if (breedingItem == null || breedingItem.getAmount() < 2) { + continue; + } + for (LivingEntity entity : entities) { - if (entry.getKey() != entity.getType()) { + if (entity.getType() != entityType) { continue; } - int count = EntityStackerManager.getSize(entity) == 0 ? 1 : EntityStackerManager.getSize(entity); + + int stackSize = EntityStackerManager.getSize(entity); + if (stackSize == 0) { + stackSize = 1; + } + if (mate1) { - if (count > 1) { + farm.removeMaterial(breedingItem.getType(), 2); + + if (stackSize > 1) { handleStackedBreed(entity); + if (stackSize > 2) { + handleStackedBreed(entity); + } } else { handleBreed(entity); } - Bukkit.getScheduler().runTask(this.plugin, () -> Methods.animate(farm.getLocation(), XMaterial.EGG)); + + spawnParticlesAndAnimation(entity.getLocation(), farm.getLocation()); return; } - if (entry.getValue() >= 2) { - EntityType entityType = entry.getKey(); - - for (ItemStack item : farm.getItems().toArray(new ItemStack[0])) { - EntityInfo info = EntityInfo.of(entityType); - try { - if (info == null || item.getType() != info.getMaterial() || item.getAmount() < 2) { - continue; - } - } catch (IllegalArgumentException e) { - continue; - } - - farm.removeMaterial(item.getType(), 2); - - Location location = entity.getLocation(); - CompatibleParticleHandler.spawnParticles(CompatibleParticleHandler.ParticleType.HEART, - location, 5, .5, .5, .5); - Bukkit.getScheduler().runTask(this.plugin, () -> { - Entity newSpawn = location.getWorld().spawnEntity(location, entityType); - ((Ageable) newSpawn).setBaby(); - }); - - if (count > 1) { - handleStackedBreed(entity); - if (count - 1 > 1) { - handleStackedBreed(entity); - } else { - handleBreed(entity); - } - return; - } - handleBreed(entity); - mate1 = true; - break; - } + if (stackSize > 1) { + handleStackedBreed(entity); + } else { + handleBreed(entity); } + mate1 = true; + break; } } } @@ -161,9 +150,9 @@ public class ModuleAutoBreeding extends Module { } private void handleBreed(Entity entity) { + entity.setMetadata(BREED_COOLDOWN_METADATA, new FixedMetadataValue(this.plugin, true)); Bukkit.getScheduler().scheduleSyncDelayedTask(this.plugin, () -> - entity.removeMetadata("breedCooldown", this.plugin), 5 * 20 * 60); - entity.setMetadata("breedCooldown", new FixedMetadataValue(this.plugin, true)); + entity.removeMetadata(BREED_COOLDOWN_METADATA, this.plugin), BREED_COOLDOWN_TICKS); } private boolean isEnabled(Farm farm) { @@ -174,4 +163,23 @@ public class ModuleAutoBreeding extends Module { private void toggleEnabled(Farm farm) { saveData(farm, "enabled", !isEnabled(farm)); } -} + + private ItemStack getBreedingItem(Farm farm, EntityType entityType) { + EntityInfo info = EntityInfo.of(entityType); + if (info == null) { + return null; + } + + for (ItemStack item : farm.getItems().toArray(new ItemStack[0])) { + if (item.getType() == info.getMaterial()) { + return item; + } + } + return null; + } + + private void spawnParticlesAndAnimation(Location entityLocation, Location farmLocation) { + CompatibleParticleHandler.spawnParticles(CompatibleParticleHandler.ParticleType.HEART, entityLocation, 5, .5, .5, .5); + Bukkit.getScheduler().runTask(this.plugin, () -> Methods.animate(farmLocation, XMaterial.EGG)); + } +} \ No newline at end of file