mirror of
https://github.com/songoda/EpicFarming.git
synced 2024-09-30 23:57:29 +02:00
Added support for stacking plugins.
This commit is contained in:
parent
066f3eb5e4
commit
1f7991aa30
@ -7,6 +7,7 @@ import com.songoda.core.compatibility.CompatibleMaterial;
|
|||||||
import com.songoda.core.configuration.Config;
|
import com.songoda.core.configuration.Config;
|
||||||
import com.songoda.core.gui.GuiManager;
|
import com.songoda.core.gui.GuiManager;
|
||||||
import com.songoda.core.hooks.EconomyManager;
|
import com.songoda.core.hooks.EconomyManager;
|
||||||
|
import com.songoda.core.hooks.EntityStackerManager;
|
||||||
import com.songoda.epicfarming.boost.BoostData;
|
import com.songoda.epicfarming.boost.BoostData;
|
||||||
import com.songoda.epicfarming.boost.BoostManager;
|
import com.songoda.epicfarming.boost.BoostManager;
|
||||||
import com.songoda.epicfarming.commands.*;
|
import com.songoda.epicfarming.commands.*;
|
||||||
@ -89,6 +90,9 @@ public class EpicFarming extends SongodaPlugin {
|
|||||||
// Set economy preference
|
// Set economy preference
|
||||||
EconomyManager.getManager().setPreferredHook(Setting.ECONOMY_PLUGIN.getString());
|
EconomyManager.getManager().setPreferredHook(Setting.ECONOMY_PLUGIN.getString());
|
||||||
|
|
||||||
|
// Load entity stack manager.
|
||||||
|
EntityStackerManager.load();
|
||||||
|
|
||||||
// Register commands
|
// Register commands
|
||||||
this.commandManager = new CommandManager(this);
|
this.commandManager = new CommandManager(this);
|
||||||
this.commandManager.addCommand(new CommandEpicFarming(this))
|
this.commandManager.addCommand(new CommandEpicFarming(this))
|
||||||
|
@ -1,17 +1,19 @@
|
|||||||
package com.songoda.epicfarming.tasks;
|
package com.songoda.epicfarming.tasks;
|
||||||
|
|
||||||
|
import com.songoda.core.hooks.EntityStackerManager;
|
||||||
import com.songoda.epicfarming.EpicFarming;
|
import com.songoda.epicfarming.EpicFarming;
|
||||||
import com.songoda.epicfarming.boost.BoostData;
|
import com.songoda.epicfarming.boost.BoostData;
|
||||||
import com.songoda.epicfarming.farming.Farm;
|
import com.songoda.epicfarming.farming.Farm;
|
||||||
import com.songoda.epicfarming.utils.EntityInfo;
|
import com.songoda.epicfarming.utils.EntityInfo;
|
||||||
import com.songoda.epicfarming.utils.Methods;
|
import com.songoda.epicfarming.utils.Methods;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.Sound;
|
import org.bukkit.Sound;
|
||||||
import org.bukkit.entity.*;
|
import org.bukkit.entity.*;
|
||||||
import org.bukkit.inventory.Inventory;
|
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.material.Wool;
|
import org.bukkit.material.Wool;
|
||||||
|
import org.bukkit.metadata.FixedMetadataValue;
|
||||||
import org.bukkit.scheduler.BukkitRunnable;
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@ -43,8 +45,9 @@ public class EntityTask extends BukkitRunnable {
|
|||||||
location.add(.5, .5, .5);
|
location.add(.5, .5, .5);
|
||||||
|
|
||||||
double radius = farm.getLevel().getRadius() + .5;
|
double radius = farm.getLevel().getRadius() + .5;
|
||||||
Collection<Entity> amt = location.getWorld().getNearbyEntities(location, radius, radius, radius);
|
Collection<LivingEntity> amt = location.getWorld().getNearbyEntities(location, radius, radius, radius)
|
||||||
amt.removeIf(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));
|
||||||
|
|
||||||
if (farm.getLevel().isAutoBreeding()) doAutoBreeding(farm, amt);
|
if (farm.getLevel().isAutoBreeding()) doAutoBreeding(farm, amt);
|
||||||
|
|
||||||
@ -126,29 +129,30 @@ public class EntityTask extends BukkitRunnable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final Map<Entity, Integer> lastBreed = new HashMap<>();
|
private void doAutoBreeding(Farm farm, Collection<LivingEntity> entities1) {
|
||||||
|
List<LivingEntity> entities = new ArrayList<>(entities1);
|
||||||
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);
|
Collections.shuffle(entities);
|
||||||
entities.removeIf(e -> lastBreed.containsKey(e) || !(e instanceof Ageable) || !((Ageable) e).isAdult());
|
entities.removeIf(e -> !(e instanceof Ageable) || !((Ageable) e).isAdult());
|
||||||
|
|
||||||
Map<EntityType, Long> counts =
|
Map<EntityType, Long> counts =
|
||||||
entities.stream().collect(Collectors.groupingBy(Entity::getType, Collectors.counting()));
|
entities.stream().collect(Collectors.groupingBy(Entity::getType, Collectors.counting()));
|
||||||
|
|
||||||
|
for (LivingEntity entity : entities) {
|
||||||
|
counts.put(entity.getType(),
|
||||||
|
counts.get(entity.getType()) - 1 + EntityStackerManager.getSize(entity));
|
||||||
|
}
|
||||||
|
|
||||||
boolean mate1 = false;
|
boolean mate1 = false;
|
||||||
|
|
||||||
for (Map.Entry<EntityType, Long> entry : counts.entrySet()) {
|
for (Map.Entry<EntityType, Long> entry : counts.entrySet()) {
|
||||||
for (Entity entity : entities) {
|
for (LivingEntity entity : entities) {
|
||||||
if (entry.getKey() != entity.getType()) continue;
|
if (entry.getKey() != entity.getType()) continue;
|
||||||
if (mate1) {
|
if (mate1) {
|
||||||
lastBreed.put(entity, 0);
|
int count = EntityStackerManager.getSize(entity);
|
||||||
|
if (count > 1)
|
||||||
|
handleStackedBreed(entity);
|
||||||
|
else
|
||||||
|
handleBreed(entity);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -170,7 +174,18 @@ public class EntityTask extends BukkitRunnable {
|
|||||||
Location location = entity.getLocation();
|
Location location = entity.getLocation();
|
||||||
Entity newSpawn = location.getWorld().spawnEntity(location, entityType);
|
Entity newSpawn = location.getWorld().spawnEntity(location, entityType);
|
||||||
((Ageable) newSpawn).setBaby();
|
((Ageable) newSpawn).setBaby();
|
||||||
lastBreed.put(entity, 0);
|
|
||||||
|
int count = EntityStackerManager.getSize(entity);
|
||||||
|
if (count > 1) {
|
||||||
|
handleStackedBreed(entity);
|
||||||
|
if (count - 1 > 1) {
|
||||||
|
handleStackedBreed(entity);
|
||||||
|
} else {
|
||||||
|
handleBreed(entity);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
handleBreed(entity);
|
||||||
mate1 = true;
|
mate1 = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -178,6 +193,18 @@ public class EntityTask extends BukkitRunnable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void handleStackedBreed(LivingEntity entity) {
|
||||||
|
EntityStackerManager.removeOne(entity);
|
||||||
|
LivingEntity spawned = (LivingEntity) entity.getWorld().spawnEntity(entity.getLocation(), entity.getType());
|
||||||
|
handleBreed(spawned);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleBreed(Entity entity) {
|
||||||
|
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () ->
|
||||||
|
entity.removeMetadata("breedCooldown", plugin), 5 * 20 * 60);
|
||||||
|
entity.setMetadata("breedCooldown", new FixedMetadataValue(plugin, true));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private boolean doDrop(Farm farm, ItemStack stack) {
|
private boolean doDrop(Farm farm, ItemStack stack) {
|
||||||
BoostData boostData = plugin.getBoostManager().getBoost(farm.getPlacedBy());
|
BoostData boostData = plugin.getBoostManager().getBoost(farm.getPlacedBy());
|
||||||
|
Loading…
Reference in New Issue
Block a user