Added auto breeding.

Added Sheep and Chicken support.
Added Animation.
This commit is contained in:
Brianna O'Keefe 2018-09-27 22:54:43 -04:00
parent cdbb63fe0c
commit a9197b1b58
13 changed files with 362 additions and 31 deletions

View File

@ -13,6 +13,8 @@ public interface Level {
boolean isAutoReplant();
boolean isAutoBreeding();
double getSpeedMultiplier();
int getCostExperiance();

View File

@ -4,7 +4,7 @@ import java.util.Map;
public interface LevelManager {
void addLevel(int level, int costExperiance, int costEconomy, double speedMultiplier, int radius, boolean autoHarvest, boolean autoReplant);
void addLevel(int level, int costExperiance, int costEconomy, double speedMultiplier, int radius, boolean autoHarvest, boolean autoReplant, boolean autoBreeding);
Level getLevel(int level);

View File

@ -21,6 +21,7 @@ import com.songoda.epicfarming.listeners.InteractListeners;
import com.songoda.epicfarming.listeners.InventoryListeners;
import com.songoda.epicfarming.player.PlayerActionManager;
import com.songoda.epicfarming.player.PlayerData;
import com.songoda.epicfarming.tasks.EntityTask;
import com.songoda.epicfarming.tasks.FarmTask;
import com.songoda.epicfarming.tasks.GrowthTask;
import com.songoda.epicfarming.tasks.HopperTask;
@ -69,6 +70,7 @@ public class EpicFarmingPlugin extends JavaPlugin implements EpicFarming {
private GrowthTask growthTask;
private HopperTask hopperTask;
private FarmTask farmTask;
private EntityTask entityTask;
public static EpicFarmingPlugin getInstance() {
return INSTANCE;
@ -182,6 +184,7 @@ public class EpicFarmingPlugin extends JavaPlugin implements EpicFarming {
this.growthTask = GrowthTask.startTask(this);
this.hopperTask = HopperTask.startTask(this);
this.farmTask = FarmTask.startTask(this);
this.entityTask = entityTask.startTask(this);
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, this::saveToFile, 6000, 6000);
@ -220,7 +223,8 @@ public class EpicFarmingPlugin extends JavaPlugin implements EpicFarming {
double speedMultiplier = getConfig().getDouble("settings.levels." + levelName + ".Speed-Multiplier");
boolean autoHarvest = getConfig().getBoolean("settings.levels." + levelName + ".Auto-Harvest");
boolean autoReplant = getConfig().getBoolean("settings.levels." + levelName + ".Auto-Replant");
levelManager.addLevel(level, costExperiance, costEconomy, speedMultiplier, radius, autoHarvest, autoReplant);
boolean autoBreeding = getConfig().getBoolean("settings.levels." + levelName + ".Auto-Breeding");
levelManager.addLevel(level, costExperiance, costEconomy, speedMultiplier, radius, autoHarvest, autoReplant, autoBreeding);
}
}
@ -286,6 +290,7 @@ public class EpicFarmingPlugin extends JavaPlugin implements EpicFarming {
levels.set("Level-3.Speed-Multiplier", 1.5);
levels.set("Level-3.Auto-Harvest", true);
levels.set("Level-3.Auto-Replant", true);
levels.set("Level-3.Auto-Breeding", true);
levels.set("Level-3.Cost-xp", 25);
levels.set("Level-3.Cost-eco", 7500);
@ -293,6 +298,7 @@ public class EpicFarmingPlugin extends JavaPlugin implements EpicFarming {
levels.set("Level-4.Speed-Multiplier", 2);
levels.set("Level-4.Auto-Harvest", true);
levels.set("Level-4.Auto-Replant", true);
levels.set("Level-4.Auto-Breeding", true);
levels.set("Level-4.Cost-xp", 30);
levels.set("Level-4.Cost-eco", 10000);
@ -300,6 +306,7 @@ public class EpicFarmingPlugin extends JavaPlugin implements EpicFarming {
levels.set("Level-5.Speed-Multiplier", 2.5);
levels.set("Level-5.Auto-Harvest", true);
levels.set("Level-5.Auto-Replant", true);
levels.set("Level-5.Auto-Breeding", true);
levels.set("Level-5.Cost-xp", 35);
levels.set("Level-5.Cost-eco", 12000);
@ -307,6 +314,7 @@ public class EpicFarmingPlugin extends JavaPlugin implements EpicFarming {
levels.set("Level-6.Speed-Multiplier", 3);
levels.set("Level-6.Auto-Harvest", true);
levels.set("Level-6.Auto-Replant", true);
levels.set("Level-6.Auto-Breeding", true);
levels.set("Level-6.Cost-xp", 40);
levels.set("Level-6.Cost-eco", 25000);
}
@ -418,4 +426,8 @@ public class EpicFarmingPlugin extends JavaPlugin implements EpicFarming {
public FarmTask getFarmTask() {
return farmTask;
}
public EntityTask getEntityTask() {
return entityTask;
}
}

View File

@ -1,8 +1,5 @@
package com.songoda.epicfarming.boost;
import com.songoda.epicfarming.EpicFarmingPlugin;
import org.bukkit.Bukkit;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
@ -29,7 +26,7 @@ public class BoostManager {
for (BoostData boostData : registeredBoosts) {
if (boostData.getPlayer().toString().equals(player.toString())) {
if (System.currentTimeMillis() >= boostData.getEndTime()) {
Bukkit.getScheduler().scheduleSyncDelayedTask(EpicFarmingPlugin.getInstance(), () -> removeBoostFromPlayer(boostData), 1);
removeBoostFromPlayer(boostData);
}
return boostData;
}

View File

@ -12,11 +12,11 @@ public class ELevel implements Level {
private double speedMultiplier;
private boolean autoHarvest, autoReplant;
private boolean autoHarvest, autoReplant, autoBreeding;
private List<String> description = new ArrayList<>();
public ELevel(int level, int costExperiance, int costEconomy, double speedMultiplier, int radius, boolean autoHarvest, boolean autoReplant) {
public ELevel(int level, int costExperiance, int costEconomy, double speedMultiplier, int radius, boolean autoHarvest, boolean autoReplant, boolean autoBreeding) {
this.level = level;
this.costExperiance = costExperiance;
this.costEconomy = costEconomy;
@ -24,6 +24,7 @@ public class ELevel implements Level {
this.radius = radius;
this.autoHarvest = autoHarvest;
this.autoReplant = autoReplant;
this.autoBreeding = autoBreeding;
EpicFarmingPlugin instance = EpicFarmingPlugin.getInstance();
@ -36,6 +37,9 @@ public class ELevel implements Level {
if (autoReplant)
description.add(instance.getLocale().getMessage("interface.button.autoreplant", autoReplant));
if (autoBreeding)
description.add(instance.getLocale().getMessage("interface.button.autobreeding", autoBreeding));
}
@Override
@ -63,6 +67,9 @@ public class ELevel implements Level {
return autoReplant;
}
@Override
public boolean isAutoBreeding() { return autoBreeding; }
@Override
public double getSpeedMultiplier() {
return speedMultiplier;

View File

@ -13,8 +13,8 @@ public class ELevelManager implements LevelManager {
private final NavigableMap<Integer, ELevel> registeredLevels = new TreeMap<>();
@Override
public void addLevel(int level, int costExperiance, int costEconomy, double speedMultiplier, int radius, boolean autoHarvest, boolean autoReplant) {
registeredLevels.put(level, new ELevel(level, costExperiance, costEconomy, speedMultiplier, radius, autoHarvest, autoReplant));
public void addLevel(int level, int costExperiance, int costEconomy, double speedMultiplier, int radius, boolean autoHarvest, boolean autoReplant, boolean autobreeding) {
registeredLevels.put(level, new ELevel(level, costExperiance, costEconomy, speedMultiplier, radius, autoHarvest, autoReplant, autobreeding));
}
@Override

View File

@ -10,6 +10,11 @@ import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Chicken;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
@ -17,8 +22,13 @@ import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockFadeEvent;
import org.bukkit.event.block.BlockGrowEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.ItemSpawnEvent;
import org.bukkit.event.entity.SheepRegrowWoolEvent;
import org.bukkit.inventory.ItemStack;
import java.util.Collection;
import java.util.List;
/**
* Created by songoda on 3/14/2017.
*/
@ -93,7 +103,7 @@ public class BlockListeners implements Listener {
EFarmManager farmManager = instance.getFarmManager();
Block block = location.getBlock();
for(Level level : instance.getLevelManager().getLevels().values()) {
for (Level level : instance.getLevelManager().getLevels().values()) {
int radius = level.getRadius();
int bx = block.getX();
int by = block.getY();
@ -105,10 +115,10 @@ public class BlockListeners implements Listener {
Block b2 = block.getWorld().getBlockAt(bx + fx, by + fy, bz + fz);
if (b2.getType() == Material.valueOf(instance.getConfig().getString("Main.Farm Block Material"))) {
if (!farmManager.getFarms().containsKey(b2.getLocation())) continue;
if (level.getRadius() != farmManager.getFarm(b2.getLocation()).getLevel().getRadius()) continue;
if (level.getRadius() != farmManager.getFarm(b2.getLocation()).getLevel().getRadius())
continue;
return true;
}
}
}
}
@ -135,14 +145,45 @@ public class BlockListeners implements Listener {
Block block = event.getBlock();
block.setType(Material.AIR);
block.getLocation().getWorld().dropItemNaturally(block.getLocation().add(.5,.5,.5), item);
block.getLocation().getWorld().dropItemNaturally(block.getLocation().add(.5, .5, .5), item);
for (ItemStack itemStack : ((EFarm)farm).dumpInventory()) {
for (ItemStack itemStack : ((EFarm) farm).dumpInventory()) {
if (itemStack == null) continue;
farm.getLocation().getWorld().dropItemNaturally(farm.getLocation().add(.5,.5,.5), itemStack);
farm.getLocation().getWorld().dropItemNaturally(farm.getLocation().add(.5, .5, .5), itemStack);
}
} catch (Exception ex) {
Debugger.runReport(ex);
}
}
@EventHandler(priority = EventPriority.HIGHEST)
public void onSpawn(ItemSpawnEvent event) {
Item item = event.getEntity();
if (item.getItemStack().getType() != Material.EGG) return;
Location location = event.getEntity().getLocation();
Collection<Entity> nearby = location.getWorld().getNearbyEntities(location, 0.01, 0.3, 0.01);
Entity entity = null;
for (Entity e : nearby) {
if (e instanceof Player) return;
if (e instanceof Chicken) entity = e;
}
if (instance.getEntityTask().getTicksLived().containsKey(entity)) {
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.HIGHEST)
public void onSpawn(SheepRegrowWoolEvent event) {
if (instance.getEntityTask().getTicksLived().containsKey(event.getEntity())) {
event.setCancelled(true);
Block block = event.getEntity().getLocation().getBlock().getRelative(BlockFace.DOWN);
if (block.getType() == Material.DIRT) {
block.setType(Material.GRASS_BLOCK);
}
}
}
}

View File

@ -0,0 +1,217 @@
package com.songoda.epicfarming.tasks;
import com.songoda.epicfarming.EpicFarmingPlugin;
import com.songoda.epicfarming.api.farming.Farm;
import com.songoda.epicfarming.boost.BoostData;
import com.songoda.epicfarming.utils.Debugger;
import com.songoda.epicfarming.utils.EntityInfo;
import com.songoda.epicfarming.utils.Methods;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.entity.*;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Wool;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.*;
import java.util.stream.Collectors;
public class EntityTask extends BukkitRunnable {
private static final Random random = new Random();
private Map<Entity, Integer> lastTicksLived = new HashMap<>();
private static final Map<Entity, Integer> ticksLived = new HashMap<>();
private static EntityTask instance;
private static EpicFarmingPlugin plugin;
public static EntityTask startTask(EpicFarmingPlugin pl) {
if (instance == null) {
instance = new EntityTask();
plugin = pl;
instance.runTaskTimer(plugin, 0, plugin.getConfig().getInt("Main.Entity Tick Speed"));
}
return instance;
}
@Override
public void run() {
for (Farm farm : plugin.getFarmManager().getFarms().values()) {
if (farm.getLocation() == null) continue;
Location location = farm.getLocation();
location.add(.5, .5, .5);
double radius = farm.getLevel().getRadius() + .5;
Collection<Entity> amt = location.getWorld().getNearbyEntities(location, radius, radius, radius);
amt.removeIf(e -> e instanceof Player || !(e instanceof LivingEntity) || e instanceof ArmorStand);
if (farm.getLevel().isAutoBreeding()) doAutoBreeding(farm, amt);
for (Entity entity : amt) {
if (!ticksLived.containsKey(entity)) ticksLived.put(entity, 0);
int lived = ticksLived.get(entity);
ticksLived.put(entity, lived + 100);
int min = (int) Math.floor(getMin(entity) / farm.getLevel().getSpeedMultiplier());
int max = (int) Math.floor(getMax(entity) / farm.getLevel().getSpeedMultiplier());
int rand = random.nextInt((int) Math.floor(100 / farm.getLevel().getSpeedMultiplier()));
if (lived < min) continue;
if (rand != 5 && lived < max) continue;
if (entity instanceof Chicken) {
if (!((Ageable) entity).isAdult()) continue;
entity.getLocation().getWorld().playSound(entity.getLocation(), Sound.ENTITY_CHICKEN_EGG, 1, 2);
if (!farm.getLevel().isAutoHarvest()) {
ticksLived.remove(entity);
entity.getLocation().getWorld().dropItemNaturally(entity.getLocation(), new ItemStack(Material.EGG));
} else {
doDrop(farm, new ItemStack(Material.EGG, 1));
}
Methods.animate(farm.getLocation(), Material.EGG);
} else if (entity instanceof Sheep) {
if (!((Ageable) entity).isAdult()) continue;
((Sheep) entity).setSheared(true);
Wool woolColor = new Wool(((Sheep) entity).getColor());
ItemStack wool = woolColor.toItemStack((int) Math.round(1 + (Math.random() * 3)));
if (!farm.getLevel().isAutoHarvest()) {
entity.getLocation().getWorld().dropItemNaturally(entity.getLocation(), wool);
} else {
doDrop(farm, wool);
}
Methods.animate(farm.getLocation(), wool.getType());
}
ticksLived.put(entity, 0);
}
}
for (Map.Entry<Entity, Integer> entry : lastTicksLived.entrySet()) {
int last = entry.getValue();
if (!ticksLived.containsKey(entry.getKey())) continue;
int current = ticksLived.get(entry.getKey());
if (last == current) {
ticksLived.remove(entry.getKey());
}
}
lastTicksLived = new HashMap<>(ticksLived);
}
private int getMin(Entity entity) {
switch (entity.getType()) {
case SHEEP:
return 0;
case CHICKEN:
return 6000;
default:
return 0;
}
}
private int getMax(Entity entity) {
switch (entity.getType()) {
case SHEEP:
return 6000;
case CHICKEN:
return 12000;
default:
return 0;
}
}
private static final Map<Entity, Integer> lastBreed = new HashMap<>();
private void doAutoBreeding(Farm farm, Collection<Entity> entities1) {
for (Map.Entry<Entity, Integer> entry : new HashMap<>(lastBreed).entrySet()) {
if (entry.getValue() >= 6000) lastBreed.remove(entry.getKey());
lastBreed.put(entry.getKey(), entry.getValue() + 100);
}
List<Entity> entities = new ArrayList<>(entities1);
Collections.shuffle(entities);
entities.removeIf(e -> lastBreed.containsKey(e) || !(e instanceof Ageable) || !((Ageable)e).isAdult());
Map<EntityType, Long> counts =
entities.stream().collect(Collectors.groupingBy(Entity::getType, Collectors.counting()));
boolean mate1 = false;
for (Map.Entry<EntityType, Long> entry : counts.entrySet()) {
for (Entity entity : entities) {
if (entry.getKey() != entity.getType()) continue;
if (mate1) {
lastBreed.put(entity, 0);
return;
}
if (entry.getValue() >= 2 && entry.getValue() < plugin.getConfig().getInt("Main.Auto Breeding Cap")) {
EntityType entityType = entry.getKey();
Inventory inventory = farm.getInventory();
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);
if (item.getType() != EntityInfo.valueOf(entityType.name()).getMaterial() || item.getAmount() < 2)
continue;
int newAmt = item.getAmount() - 2;
if (newAmt <= 0)
inventory.setItem(i, null);
else
item.setAmount(newAmt);
Location location = entity.getLocation();
Entity newSpawn = location.getWorld().spawnEntity(location, entityType);
((Ageable) newSpawn).setBaby();
lastBreed.put(entity, 0);
mate1 = true;
}
}
}
}
}
private boolean doDrop(Farm farm, ItemStack stack) {
BoostData boostData = plugin.getBoostManager().getBoost(farm.getPlacedBy());
stack.setAmount(stack.getAmount() * (boostData == null ? 1 : boostData.getMultiplier()));
if (!canMove(farm.getInventory(), stack)) return false;
farm.getInventory().addItem(stack);
return true;
}
private boolean canMove(Inventory inventory, ItemStack item) {
try {
if (inventory.firstEmpty() != -1) return true;
for (ItemStack stack : inventory.getContents()) {
if (stack.isSimilar(item) && stack.getAmount() < stack.getMaxStackSize()) {
return true;
}
}
} catch (Exception e) {
Debugger.runReport(e);
}
return false;
}
public Map<Entity, Integer> getTicksLived() {
return Collections.unmodifiableMap(ticksLived);
}
}

View File

@ -7,11 +7,11 @@ import com.songoda.epicfarming.boost.BoostData;
import com.songoda.epicfarming.farming.Crop;
import com.songoda.epicfarming.utils.CropType;
import com.songoda.epicfarming.utils.Debugger;
import org.bukkit.CropState;
import org.bukkit.Location;
import org.bukkit.Material;
import com.songoda.epicfarming.utils.Methods;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.entity.*;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Crops;
@ -40,6 +40,7 @@ public class FarmTask extends BukkitRunnable {
public void run() {
for (Farm farm : plugin.getFarmManager().getFarms().values()) {
if (farm.getLocation() == null) continue;
for (Block block : getCrops(farm, true)) {
Crops crop = (Crops) block.getState().getData();
@ -70,15 +71,13 @@ public class FarmTask extends BukkitRunnable {
if (material == null || farm == null || cropTypeData == null) return false;
BoostData boostData = plugin.getBoostManager().getBoost(farm.getPlacedBy());
ItemStack stack = new ItemStack(cropTypeData.getYieldMaterial(), (useBoneMeal(farm) ? random.nextInt(2) + 2 : 1) * (boostData == null ? 1 : boostData.getMultiplier()));
ItemStack seedStack = new ItemStack(cropTypeData.getSeedMaterial(), random.nextInt(3) + 1 + (useBoneMeal(farm) ? 1 : 0));
if (!canMove(farm.getInventory(), stack)) return false;
Methods.animate(farm.getLocation(), cropTypeData.getYieldMaterial());
farm.getInventory().addItem(stack);
farm.getInventory().addItem(seedStack);
return true;

View File

@ -0,0 +1,21 @@
package com.songoda.epicfarming.utils;
import org.bukkit.Material;
public enum EntityInfo {
CHICKEN(Material.WHEAT_SEEDS),
COW(Material.WHEAT),
PIG(Material.CARROT),
SHEEP(Material.WHEAT);
Material material;
EntityInfo(Material material) {
this.material = material;
}
public Material getMaterial() {
return material;
}
}

View File

@ -2,7 +2,15 @@ package com.songoda.epicfarming.utils;
import com.songoda.arconix.plugin.Arconix;
import com.songoda.epicfarming.EpicFarmingPlugin;
import com.songoda.epicfarming.api.EpicFarming;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Item;
import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.util.Vector;
public class Methods {
@ -44,4 +52,27 @@ public class Methods {
}
return null;
}
public static void animate(Location location, Material mat) {
try {
if (!EpicFarmingPlugin.getInstance().getConfig().getBoolean("Main.Animate")) return;
Block block = location.getBlock();
if (block.getRelative(0, 1, 0).getType() != Material.AIR && EpicFarmingPlugin.getInstance().getConfig().getBoolean("Main.Do Dispenser Animation"))
return;
Item i = block.getWorld().dropItem(block.getLocation().add(0.5, 1, 0.5), new ItemStack(mat));
// Support for EpicHoppers suction.
i.setMetadata("grabbed", new FixedMetadataValue(EpicFarmingPlugin.getInstance(), "true"));
i.setMetadata("betterdrops_ignore", new FixedMetadataValue(EpicFarmingPlugin.getInstance(), true));
i.setPickupDelay(3600);
i.setVelocity(new Vector(0, .3, 0));
Bukkit.getScheduler().scheduleSyncDelayedTask(EpicFarmingPlugin.getInstance(), i::remove, 10);
} catch (Exception ex) {
Debugger.runReport(ex);
}
}
}

View File

@ -194,17 +194,20 @@ public class SettingsManager implements Listener {
o3("Main.Upgrade Particle Type", "SPELL_WITCH"),
o4("Main.Sounds Enabled", true),
o5("Main.Farm Tick Speed", 70),
o6("Main.Growth Tick Speed", 20),
o7("Main.Farm Block Material", "END_ROD"),
o8("Main.Allow Non Command Issued Farm Items", false),
o6("Main.Entity Tick Speed", 100),
o7("Main.Growth Tick Speed", 20),
o8("Main.Farm Block Material", "END_ROD"),
o9("Main.Allow Non Command Issued Farm Items", false),
o10("Main.Auto Breeding Cap", 15),
o11("Main.Animate", true),
o9("Interfaces.Economy Icon", "SUNFLOWER"),
o10("Interfaces.XP Icon", "EXPERIENCE_BOTTLE"),
o11("Interfaces.Glass Type 1", 7),
o12("Interfaces.Glass Type 2", 11),
o13("Interfaces.Glass Type 3", 3),
o12("Interfaces.Economy Icon", "SUNFLOWER"),
o13("Interfaces.XP Icon", "EXPERIENCE_BOTTLE"),
o14("Interfaces.Glass Type 1", 7),
o15("Interfaces.Glass Type 2", 11),
o16("Interfaces.Glass Type 3", 3),
o14("System.Debugger Enabled", false);
o17("System.Debugger Enabled", false);
private String setting;
private Object option;

View File

@ -14,6 +14,7 @@ interface.button.radius = "&7Radius: &6%speed%"
interface.button.speed = "&7Speed: &6%speed%x"
interface.button.autoharvest = "&7Auto Harvest: &6%status%"
interface.button.autoreplant = "&7Auto Replant: &6%status%"
interface.button.autobreeding = "&7Auto Breeding: &6%status%"
interface.button.boostedstats = "&a&lCurrently boosted!|&7Yeild rate multiplied by &6%amount%x&7.|&7Expires in &6%time%&7."