2018-11-06 04:33:10 +01:00
|
|
|
package com.songoda.ultimatestacker.tasks;
|
|
|
|
|
|
|
|
import com.songoda.ultimatestacker.UltimateStacker;
|
|
|
|
import com.songoda.ultimatestacker.entity.EntityStack;
|
|
|
|
import com.songoda.ultimatestacker.entity.EntityStackManager;
|
|
|
|
import com.songoda.ultimatestacker.utils.Methods;
|
2019-05-23 22:15:04 +02:00
|
|
|
import com.songoda.ultimatestacker.utils.settings.Setting;
|
2018-11-06 04:33:10 +01:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.World;
|
|
|
|
import org.bukkit.configuration.ConfigurationSection;
|
2019-06-15 11:05:53 +02:00
|
|
|
import org.bukkit.entity.ArmorStand;
|
|
|
|
import org.bukkit.entity.Entity;
|
|
|
|
import org.bukkit.entity.LivingEntity;
|
|
|
|
import org.bukkit.entity.Player;
|
2018-11-06 04:33:10 +01:00
|
|
|
import org.bukkit.scheduler.BukkitRunnable;
|
|
|
|
|
2018-11-08 04:26:38 +01:00
|
|
|
import java.util.ArrayList;
|
2019-01-15 05:45:25 +01:00
|
|
|
import java.util.Collections;
|
2018-11-06 04:33:10 +01:00
|
|
|
import java.util.List;
|
2019-01-15 06:08:51 +01:00
|
|
|
import java.util.UUID;
|
2018-11-06 04:33:10 +01:00
|
|
|
|
|
|
|
public class StackingTask extends BukkitRunnable {
|
|
|
|
|
|
|
|
private final UltimateStacker instance;
|
|
|
|
|
|
|
|
public StackingTask(UltimateStacker instance) {
|
|
|
|
this.instance = instance;
|
2019-03-21 04:36:58 +01:00
|
|
|
|
|
|
|
// Start stacking task.
|
2019-05-23 22:15:04 +02:00
|
|
|
runTaskTimer(instance, 0, Setting.STACK_SEARCH_TICK_SPEED.getInt());
|
2018-11-06 04:33:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2019-05-23 22:15:04 +02:00
|
|
|
int maxEntityStackSizeGlobal = Setting.MAX_STACK_ENTITIES.getInt();
|
|
|
|
int minEntityStackAmount = Setting.MIN_STACK_ENTITIES.getInt();
|
2019-04-11 08:54:59 +02:00
|
|
|
|
2019-05-23 22:15:04 +02:00
|
|
|
List<String> disabledWorlds = Setting.DISABLED_WORLDS.getStringList();
|
2018-11-06 04:33:10 +01:00
|
|
|
|
|
|
|
EntityStackManager stackManager = instance.getEntityStackManager();
|
|
|
|
for (World world : Bukkit.getWorlds()) {
|
2019-04-11 08:54:59 +02:00
|
|
|
if (disabledWorlds.stream().anyMatch(worldStr -> world.getName().equalsIgnoreCase(worldStr))) continue;
|
2018-11-06 04:33:10 +01:00
|
|
|
|
2019-01-15 05:45:25 +01:00
|
|
|
List<Entity> entities = world.getEntities();
|
|
|
|
Collections.reverse(entities);
|
|
|
|
|
2019-01-15 06:08:51 +01:00
|
|
|
List<UUID> removed = new ArrayList<>();
|
|
|
|
|
2018-11-06 04:33:10 +01:00
|
|
|
nextEntity:
|
2019-01-15 05:45:25 +01:00
|
|
|
for (Entity entityO : entities) {
|
2019-06-04 09:17:25 +02:00
|
|
|
if (entityO == null
|
|
|
|
|| entityO instanceof Player
|
|
|
|
|| !entityO.isValid()
|
|
|
|
|| !(entityO instanceof LivingEntity)
|
|
|
|
|| !Setting.STACK_ENTITIES.getBoolean())
|
2018-11-06 04:33:10 +01:00
|
|
|
continue;
|
|
|
|
|
2019-06-23 03:10:11 +02:00
|
|
|
LivingEntity initialEntity = (LivingEntity) entityO;
|
2018-11-06 04:33:10 +01:00
|
|
|
|
2019-06-23 03:10:11 +02:00
|
|
|
if (initialEntity.isDead()
|
|
|
|
|| !initialEntity.isValid()
|
|
|
|
|| initialEntity instanceof ArmorStand
|
|
|
|
|| initialEntity.hasMetadata("inLove")
|
2019-06-15 11:05:53 +02:00
|
|
|
|
|
|
|
|| Setting.ONLY_STACK_FROM_SPAWNERS.getBoolean()
|
2019-06-23 03:10:11 +02:00
|
|
|
&& !(initialEntity.hasMetadata("US_REASON")
|
|
|
|
&& initialEntity.getMetadata("US_REASON").get(0).asString().equals("SPAWNER"))
|
2019-06-23 00:28:47 +02:00
|
|
|
|
|
|
|
|| Setting.ONLY_STACK_ON_SURFACE.getBoolean()
|
2019-06-23 03:10:11 +02:00
|
|
|
&& !Methods.canFly(initialEntity)
|
|
|
|
&& (!initialEntity.isOnGround() && !initialEntity.getLocation().getBlock().isLiquid()))
|
2019-06-15 11:05:53 +02:00
|
|
|
continue;
|
2018-11-08 04:26:38 +01:00
|
|
|
|
2019-06-23 03:10:11 +02:00
|
|
|
EntityStack initialStack = stackManager.getStack(initialEntity);
|
|
|
|
if (initialStack == null && initialEntity.getCustomName() != null) continue;
|
2018-11-08 04:26:38 +01:00
|
|
|
int amtToStack = initialStack != null ? initialStack.getAmount() : 1;
|
2018-11-06 04:33:10 +01:00
|
|
|
|
|
|
|
ConfigurationSection configurationSection = UltimateStacker.getInstance().getMobFile().getConfig();
|
|
|
|
|
2019-06-23 03:10:11 +02:00
|
|
|
if (!configurationSection.getBoolean("Mobs." + initialEntity.getType().name() + ".Enabled"))
|
2018-11-06 04:33:10 +01:00
|
|
|
continue;
|
|
|
|
|
2019-04-27 21:11:29 +02:00
|
|
|
int maxEntityStackSize = maxEntityStackSizeGlobal;
|
2019-06-23 03:10:11 +02:00
|
|
|
if (configurationSection.getInt("Mobs." + initialEntity.getType().name() + ".Max Stack Size") != -1)
|
|
|
|
maxEntityStackSize = configurationSection.getInt("Mobs." + initialEntity.getType().name() + ".Max Stack Size");
|
2018-11-06 04:33:10 +01:00
|
|
|
|
2019-06-23 03:10:11 +02:00
|
|
|
List<LivingEntity> entityList = Methods.getSimilarEntitiesAroundEntity(initialEntity);
|
2019-06-15 11:05:53 +02:00
|
|
|
entityList.removeIf(entity -> entity.hasMetadata("inLove")
|
|
|
|
|| entity.hasMetadata("breedCooldown")
|
|
|
|
|
|
|
|
|| Setting.ONLY_STACK_FROM_SPAWNERS.getBoolean()
|
2019-06-23 03:10:11 +02:00
|
|
|
&& !(initialEntity.hasMetadata("US_REASON")
|
|
|
|
&& initialEntity.getMetadata("US_REASON").get(0).asString().equals("SPAWNER")));
|
2018-11-06 04:33:10 +01:00
|
|
|
|
2019-06-23 03:10:11 +02:00
|
|
|
for (LivingEntity entity : new ArrayList<>(entityList)) {
|
2019-03-07 22:00:57 +01:00
|
|
|
if (removed.contains(entity.getUniqueId())) continue;
|
2018-11-06 04:33:10 +01:00
|
|
|
EntityStack stack = stackManager.getStack(entity);
|
2019-03-07 22:00:57 +01:00
|
|
|
if (stack == null && entity.getCustomName() != null) {
|
|
|
|
entityList.remove(entity);
|
|
|
|
continue;
|
|
|
|
}
|
2018-11-06 04:33:10 +01:00
|
|
|
|
|
|
|
//If a stack was found add 1 to this stack.
|
2019-03-07 22:00:57 +01:00
|
|
|
if (stack != null && (stack.getAmount() + amtToStack) <= maxEntityStackSize) {
|
2018-11-08 04:26:38 +01:00
|
|
|
stack.addAmount(amtToStack);
|
2018-11-06 04:33:10 +01:00
|
|
|
stack.updateStack();
|
2019-06-23 03:10:11 +02:00
|
|
|
removed.add(initialEntity.getUniqueId());
|
|
|
|
initialEntity.remove();
|
|
|
|
|
|
|
|
continue nextEntity;
|
|
|
|
} else if (stack == null
|
|
|
|
&& initialStack != null
|
|
|
|
&& (initialStack.getAmount() + 1) <= maxEntityStackSize
|
|
|
|
&& Methods.canFly(entity)
|
|
|
|
&& Setting.ONLY_STACK_FLYING_DOWN.getBoolean()
|
|
|
|
&& initialEntity.getLocation().getY() > entity.getLocation().getY()) {
|
|
|
|
stackManager.addStack(entity, initialStack.getAmount() + 1);
|
|
|
|
removed.add(initialEntity.getUniqueId());
|
|
|
|
initialEntity.remove();
|
2019-01-15 06:08:51 +01:00
|
|
|
|
2018-11-06 04:33:10 +01:00
|
|
|
continue nextEntity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-08 04:26:38 +01:00
|
|
|
if (initialStack != null) continue;
|
|
|
|
|
2018-11-06 04:33:10 +01:00
|
|
|
entityList.removeIf(stackManager::isStacked);
|
|
|
|
|
2019-01-24 18:14:28 +01:00
|
|
|
if (entityList.size() < minEntityStackAmount - 1
|
|
|
|
|| minEntityStackAmount > maxEntityStackSize
|
|
|
|
|| minEntityStackAmount == 1 && entityList.size() == 0) continue;
|
2018-11-06 04:33:10 +01:00
|
|
|
|
|
|
|
//If stack was never found make a new one.
|
2019-06-23 03:10:11 +02:00
|
|
|
EntityStack stack = stackManager.addStack(new EntityStack(initialEntity, (entityList.size() + 1) >
|
2019-04-27 21:18:10 +02:00
|
|
|
maxEntityStackSize ? maxEntityStackSize : entityList.size() + 1));
|
2018-11-06 04:33:10 +01:00
|
|
|
|
2019-03-21 04:36:58 +01:00
|
|
|
entityList.stream().filter(entity -> !stackManager.isStacked(entity)
|
2019-04-27 21:18:10 +02:00
|
|
|
&& !removed.contains(entity.getUniqueId())).limit(maxEntityStackSize).forEach(entity -> {
|
2019-01-15 06:08:51 +01:00
|
|
|
removed.add(entity.getUniqueId());
|
2018-11-06 04:33:10 +01:00
|
|
|
entity.remove();
|
2019-03-21 04:36:58 +01:00
|
|
|
});
|
2018-11-06 04:33:10 +01:00
|
|
|
|
|
|
|
stack.updateStack();
|
|
|
|
}
|
2019-01-15 05:45:25 +01:00
|
|
|
entities.clear();
|
2019-01-15 06:08:51 +01:00
|
|
|
removed.clear();
|
2018-11-06 04:33:10 +01:00
|
|
|
}
|
|
|
|
}
|
2019-06-23 02:10:22 +02:00
|
|
|
|
2018-11-06 04:33:10 +01:00
|
|
|
}
|