Merge branch 'development'

This commit is contained in:
Brianna 2020-01-17 21:10:10 -05:00
commit a74cf75c73
8 changed files with 70 additions and 51 deletions

View File

@ -2,7 +2,7 @@
<groupId>com.songoda</groupId>
<artifactId>EpicFarming</artifactId>
<modelVersion>4.0.0</modelVersion>
<version>3.0.9</version>
<version>3.0.10</version>
<build>
<defaultGoal>clean install</defaultGoal>
<finalName>EpicFarming-${project.version}</finalName>
@ -90,7 +90,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot</artifactId>
<version>1.14.4</version>
<version>1.15</version>
</dependency>
<dependency>
<groupId>com.songoda</groupId>

View File

@ -4,6 +4,7 @@ import com.songoda.core.configuration.Config;
import com.songoda.epicfarming.EpicFarming;
import com.songoda.epicfarming.farming.Farm;
import com.songoda.epicfarming.utils.Methods;
import org.bukkit.block.Block;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
@ -12,6 +13,7 @@ import org.bukkit.inventory.ItemStack;
import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public abstract class Module {
@ -37,18 +39,18 @@ public abstract class Module {
public abstract int runEveryXTicks();
public void run(Farm farm, Collection<LivingEntity> entitiesAroundFarm) {
public void run(Farm farm, Collection<LivingEntity> entitiesAroundFarm, List<Block> crops) {
if (!currentTicks.containsKey(farm))
currentTicks.put(farm, 1);
int currentTick = currentTicks.get(farm);
if (currentTick >= runEveryXTicks()) {
runFinal(farm, entitiesAroundFarm);
runFinal(farm, entitiesAroundFarm, crops);
currentTick = 0;
}
currentTicks.put(farm, currentTick + 1);
}
public abstract void runFinal(Farm farm, Collection<LivingEntity> entitiesAroundFarm);
public abstract void runFinal(Farm farm, Collection<LivingEntity> entitiesAroundFarm, List<Block> crops);
public abstract ItemStack getGUIButton(Farm farm);

View File

@ -12,6 +12,7 @@ import com.songoda.epicfarming.utils.Methods;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.*;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
@ -40,7 +41,7 @@ public class ModuleAutoBreeding extends Module {
}
@Override
public void runFinal(Farm farm, Collection<LivingEntity> entitiesAroundFarm) {
public void runFinal(Farm farm, Collection<LivingEntity> entitiesAroundFarm, List<Block> crops) {
if (!isEnabled(farm) || farm.getFarmType() == FarmType.CROPS) return;
List<LivingEntity> entities = new ArrayList<>(entitiesAroundFarm);

View File

@ -9,6 +9,7 @@ import com.songoda.epicfarming.farming.Farm;
import com.songoda.epicfarming.farming.FarmType;
import com.songoda.epicfarming.utils.Methods;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.*;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
@ -36,7 +37,7 @@ public class ModuleAutoButcher extends Module {
}
@Override
public void runFinal(Farm farm, Collection<LivingEntity> entitiesAroundFarm) {
public void runFinal(Farm farm, Collection<LivingEntity> entitiesAroundFarm, List<Block> crops) {
if (!isEnabled(farm)|| farm.getFarmType() == FarmType.CROPS) return;
List<LivingEntity> entities = new ArrayList<>(entitiesAroundFarm);

View File

@ -41,21 +41,18 @@ public class ModuleAutoCollect extends Module {
}
@Override
public void runFinal(Farm farm, Collection<LivingEntity> entitiesAroundFarm) {
public void runFinal(Farm farm, Collection<LivingEntity> entitiesAroundFarm, List<Block> crops) {
if (farm.getFarmType() != FarmType.LIVESTOCK)
collectCrops(farm);
collectCrops(farm, crops);
if (farm.getFarmType() != FarmType.CROPS)
collectLivestock(farm, entitiesAroundFarm);
}
private void collectCrops(Farm farm) {
for (Block block : getCrops(farm, true)) {
private void collectCrops(Farm farm, List<Block> crops) {
for (Block block : crops) {
if (!BlockUtils.isCropFullyGrown(block)) {
// Add to GrowthTask
plugin.getGrowthTask().addLiveCrop(block.getLocation(), new Crop(block.getLocation(), farm));
} else if (isEnabled(farm) && doCropDrop(farm, CompatibleMaterial.getMaterial(block).getBlockMaterial())) {
if (BlockUtils.isCropFullyGrown(block) && isEnabled(farm) && doCropDrop(farm, CompatibleMaterial.getMaterial(block).getBlockMaterial())) {
if (farm.getLevel().isAutoReplant()) {
BlockUtils.resetGrowthStage(block);
@ -175,36 +172,6 @@ public class ModuleAutoCollect extends Module {
return false;
}
public static List<Block> getCrops(Farm farm, boolean add) {
if (((System.currentTimeMillis() - farm.getLastCached()) > (30 * 1000)) || !add) {
farm.setLastCached(System.currentTimeMillis());
if (add) farm.clearCache();
Block block = farm.getLocation().getBlock();
int radius = farm.getLevel().getRadius();
int bx = block.getX();
int by = block.getY();
int bz = block.getZ();
for (int fx = -radius; fx <= radius; fx++) {
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);
if (!mat.isCrop() || !CropType.isGrowableCrop(mat.getBlockMaterial())) continue;
if (add) {
farm.addCachedCrop(b2);
continue;
}
farm.removeCachedCrop(b2);
EpicFarming.getInstance().getGrowthTask().removeCropByLocation(b2.getLocation());
}
}
}
}
return farm.getCachedCrops();
}
private int getMin(Entity entity) {
switch (entity.getType()) {
case SHEEP:

View File

@ -8,6 +8,7 @@ import com.songoda.epicfarming.farming.FarmType;
import com.songoda.epicfarming.farming.levels.Level;
import com.songoda.epicfarming.farming.levels.modules.ModuleAutoCollect;
import com.songoda.epicfarming.settings.Settings;
import com.songoda.epicfarming.tasks.FarmTask;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
@ -141,7 +142,7 @@ public class BlockListeners implements Listener {
if (farm == null) return;
ModuleAutoCollect.getCrops(farm, false);
FarmTask.getCrops(farm, false);
event.setCancelled(true);
@ -165,7 +166,7 @@ public class BlockListeners implements Listener {
Farm farm = instance.getFarmManager().removeFarm(event.getBlock().getLocation());
if (farm == null) return;
ModuleAutoCollect.getCrops(farm, false);
FarmTask.getCrops(farm, false);
event.setCancelled(true);

View File

@ -6,6 +6,7 @@ import com.songoda.epicfarming.farming.Farm;
import com.songoda.epicfarming.farming.levels.modules.Module;
import com.songoda.epicfarming.farming.levels.modules.ModuleAutoCollect;
import com.songoda.epicfarming.settings.Settings;
import com.songoda.epicfarming.tasks.FarmTask;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
@ -105,7 +106,7 @@ public class EntityListeners implements Listener {
Farm farm = instance.getFarmManager().removeFarm(block.getLocation());
ModuleAutoCollect.getCrops(farm, false);
FarmTask.getCrops(farm, false);
ItemStack item = instance.makeFarmItem(farm.getLevel());

View File

@ -1,10 +1,15 @@
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.farming.Crop;
import com.songoda.epicfarming.farming.Farm;
import com.songoda.epicfarming.farming.FarmType;
import com.songoda.epicfarming.settings.Settings;
import org.bukkit.Bukkit;
import com.songoda.epicfarming.utils.CropType;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
@ -12,6 +17,8 @@ import org.bukkit.scheduler.BukkitRunnable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
public class FarmTask extends BukkitRunnable {
@ -41,13 +48,52 @@ public class FarmTask extends BukkitRunnable {
.stream().filter(e -> !(e instanceof Player) && e instanceof LivingEntity && !(e instanceof ArmorStand))
.map(entity -> (LivingEntity) entity).collect(Collectors.toCollection(ArrayList::new));
List<Block> crops = getCrops(farm, true);
if (farm.getFarmType() != FarmType.LIVESTOCK)
for (Block block : crops)
if (!BlockUtils.isCropFullyGrown(block)) {
// Add to GrowthTask
plugin.getGrowthTask().addLiveCrop(block.getLocation(), new Crop(block.getLocation(), farm));
}
// Cycle through modules.
farm.getLevel().getRegisteredModules().stream()
.filter(module -> module != null)
.filter(Objects::nonNull)
.forEach(module -> {
// Run Module
module.run(farm, entitiesAroundFarm);
module.run(farm, entitiesAroundFarm, crops);
});
}
}
public static List<Block> getCrops(Farm farm, boolean add) {
if (((System.currentTimeMillis() - farm.getLastCached()) > (30 * 1000)) || !add) {
farm.setLastCached(System.currentTimeMillis());
if (add) farm.clearCache();
Block block = farm.getLocation().getBlock();
int radius = farm.getLevel().getRadius();
int bx = block.getX();
int by = block.getY();
int bz = block.getZ();
for (int fx = -radius; fx <= radius; fx++) {
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);
if (!mat.isCrop() || !CropType.isGrowableCrop(mat.getBlockMaterial())) continue;
if (add) {
farm.addCachedCrop(b2);
continue;
}
farm.removeCachedCrop(b2);
EpicFarming.getInstance().getGrowthTask().removeCropByLocation(b2.getLocation());
}
}
}
}
return farm.getCachedCrops();
}
}