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> <groupId>com.songoda</groupId>
<artifactId>EpicFarming</artifactId> <artifactId>EpicFarming</artifactId>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<version>3.0.9</version> <version>3.0.10</version>
<build> <build>
<defaultGoal>clean install</defaultGoal> <defaultGoal>clean install</defaultGoal>
<finalName>EpicFarming-${project.version}</finalName> <finalName>EpicFarming-${project.version}</finalName>
@ -90,7 +90,7 @@
<dependency> <dependency>
<groupId>org.spigotmc</groupId> <groupId>org.spigotmc</groupId>
<artifactId>spigot</artifactId> <artifactId>spigot</artifactId>
<version>1.14.4</version> <version>1.15</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.songoda</groupId> <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.EpicFarming;
import com.songoda.epicfarming.farming.Farm; import com.songoda.epicfarming.farming.Farm;
import com.songoda.epicfarming.utils.Methods; import com.songoda.epicfarming.utils.Methods;
import org.bukkit.block.Block;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.ClickType;
@ -12,6 +13,7 @@ import org.bukkit.inventory.ItemStack;
import java.io.File; import java.io.File;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
public abstract class Module { public abstract class Module {
@ -37,18 +39,18 @@ public abstract class Module {
public abstract int runEveryXTicks(); 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)) if (!currentTicks.containsKey(farm))
currentTicks.put(farm, 1); currentTicks.put(farm, 1);
int currentTick = currentTicks.get(farm); int currentTick = currentTicks.get(farm);
if (currentTick >= runEveryXTicks()) { if (currentTick >= runEveryXTicks()) {
runFinal(farm, entitiesAroundFarm); runFinal(farm, entitiesAroundFarm, crops);
currentTick = 0; currentTick = 0;
} }
currentTicks.put(farm, currentTick + 1); 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); 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.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.*; import org.bukkit.entity.*;
import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -40,7 +41,7 @@ public class ModuleAutoBreeding extends Module {
} }
@Override @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; if (!isEnabled(farm) || farm.getFarmType() == FarmType.CROPS) return;
List<LivingEntity> entities = new ArrayList<>(entitiesAroundFarm); 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.farming.FarmType;
import com.songoda.epicfarming.utils.Methods; import com.songoda.epicfarming.utils.Methods;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.*; import org.bukkit.entity.*;
import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -36,7 +37,7 @@ public class ModuleAutoButcher extends Module {
} }
@Override @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; if (!isEnabled(farm)|| farm.getFarmType() == FarmType.CROPS) return;
List<LivingEntity> entities = new ArrayList<>(entitiesAroundFarm); List<LivingEntity> entities = new ArrayList<>(entitiesAroundFarm);

View File

@ -41,21 +41,18 @@ public class ModuleAutoCollect extends Module {
} }
@Override @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) if (farm.getFarmType() != FarmType.LIVESTOCK)
collectCrops(farm); collectCrops(farm, crops);
if (farm.getFarmType() != FarmType.CROPS) if (farm.getFarmType() != FarmType.CROPS)
collectLivestock(farm, entitiesAroundFarm); collectLivestock(farm, entitiesAroundFarm);
} }
private void collectCrops(Farm farm) { private void collectCrops(Farm farm, List<Block> crops) {
for (Block block : getCrops(farm, true)) { for (Block block : crops) {
if (!BlockUtils.isCropFullyGrown(block)) { if (BlockUtils.isCropFullyGrown(block) && isEnabled(farm) && doCropDrop(farm, CompatibleMaterial.getMaterial(block).getBlockMaterial())) {
// Add to GrowthTask
plugin.getGrowthTask().addLiveCrop(block.getLocation(), new Crop(block.getLocation(), farm));
} else if (isEnabled(farm) && doCropDrop(farm, CompatibleMaterial.getMaterial(block).getBlockMaterial())) {
if (farm.getLevel().isAutoReplant()) { if (farm.getLevel().isAutoReplant()) {
BlockUtils.resetGrowthStage(block); BlockUtils.resetGrowthStage(block);
@ -175,36 +172,6 @@ public class ModuleAutoCollect extends Module {
return false; 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) { private int getMin(Entity entity) {
switch (entity.getType()) { switch (entity.getType()) {
case SHEEP: 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.Level;
import com.songoda.epicfarming.farming.levels.modules.ModuleAutoCollect; import com.songoda.epicfarming.farming.levels.modules.ModuleAutoCollect;
import com.songoda.epicfarming.settings.Settings; import com.songoda.epicfarming.settings.Settings;
import com.songoda.epicfarming.tasks.FarmTask;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
@ -141,7 +142,7 @@ public class BlockListeners implements Listener {
if (farm == null) return; if (farm == null) return;
ModuleAutoCollect.getCrops(farm, false); FarmTask.getCrops(farm, false);
event.setCancelled(true); event.setCancelled(true);
@ -165,7 +166,7 @@ public class BlockListeners implements Listener {
Farm farm = instance.getFarmManager().removeFarm(event.getBlock().getLocation()); Farm farm = instance.getFarmManager().removeFarm(event.getBlock().getLocation());
if (farm == null) return; if (farm == null) return;
ModuleAutoCollect.getCrops(farm, false); FarmTask.getCrops(farm, false);
event.setCancelled(true); 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.Module;
import com.songoda.epicfarming.farming.levels.modules.ModuleAutoCollect; import com.songoda.epicfarming.farming.levels.modules.ModuleAutoCollect;
import com.songoda.epicfarming.settings.Settings; import com.songoda.epicfarming.settings.Settings;
import com.songoda.epicfarming.tasks.FarmTask;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
@ -105,7 +106,7 @@ public class EntityListeners implements Listener {
Farm farm = instance.getFarmManager().removeFarm(block.getLocation()); Farm farm = instance.getFarmManager().removeFarm(block.getLocation());
ModuleAutoCollect.getCrops(farm, false); FarmTask.getCrops(farm, false);
ItemStack item = instance.makeFarmItem(farm.getLevel()); ItemStack item = instance.makeFarmItem(farm.getLevel());

View File

@ -1,10 +1,15 @@
package com.songoda.epicfarming.tasks; 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.EpicFarming;
import com.songoda.epicfarming.farming.Crop;
import com.songoda.epicfarming.farming.Farm; import com.songoda.epicfarming.farming.Farm;
import com.songoda.epicfarming.farming.FarmType;
import com.songoda.epicfarming.settings.Settings; import com.songoda.epicfarming.settings.Settings;
import org.bukkit.Bukkit; import com.songoda.epicfarming.utils.CropType;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.ArmorStand; import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -12,6 +17,8 @@ import org.bukkit.scheduler.BukkitRunnable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class FarmTask extends BukkitRunnable { 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)) .stream().filter(e -> !(e instanceof Player) && e instanceof LivingEntity && !(e instanceof ArmorStand))
.map(entity -> (LivingEntity) entity).collect(Collectors.toCollection(ArrayList::new)); .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. // Cycle through modules.
farm.getLevel().getRegisteredModules().stream() farm.getLevel().getRegisteredModules().stream()
.filter(module -> module != null) .filter(Objects::nonNull)
.forEach(module -> { .forEach(module -> {
// Run 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();
}
} }