mirror of
https://github.com/songoda/UltimateStacker.git
synced 2025-03-02 10:11:02 +01:00
Fixed spawner deserialization.
Added spawning directly into stack.
This commit is contained in:
parent
481a6a255e
commit
79e091c6ba
@ -168,6 +168,7 @@ public class UltimateStacker extends JavaPlugin {
|
||||
pluginManager.registerEvents(new ItemListeners(this), this);
|
||||
pluginManager.registerEvents(new TameListeners(this), this);
|
||||
pluginManager.registerEvents(new SheepDyeListeners(this), this);
|
||||
pluginManager.registerEvents(new SpawnerListeners(this), this);
|
||||
|
||||
if (Setting.CLEAR_LAG.getBoolean() && pluginManager.isPluginEnabled("ClearLag"))
|
||||
pluginManager.registerEvents(new ClearLagListeners(this), this);
|
||||
|
@ -187,11 +187,10 @@ public class BlockListeners implements Listener {
|
||||
block.getWorld().dropItemNaturally(block.getLocation(), Methods.getSpawnerItem(blockType, amt));
|
||||
}
|
||||
|
||||
|
||||
private int getSpawnerAmount(ItemStack item) {
|
||||
if (!item.hasItemMeta() || !item.getItemMeta().hasDisplayName()) return 1;
|
||||
if (item.getItemMeta().getDisplayName().contains(":")) {
|
||||
int amt = NumberUtils.toInt(item.getItemMeta().getDisplayName().replace("\u00A7", "").split(":")[0], 1);
|
||||
int amt = NumberUtils.toInt(item.getItemMeta().getDisplayName().replace("\u00A7", "").replace(";", "").split(":")[0], 1);
|
||||
return amt == 0 ? 1 : amt;
|
||||
}
|
||||
return 1;
|
||||
|
@ -0,0 +1,51 @@
|
||||
package com.songoda.ultimatestacker.listeners;
|
||||
|
||||
import com.songoda.ultimatestacker.UltimateStacker;
|
||||
import com.songoda.ultimatestacker.events.SpawnerBreakEvent;
|
||||
import com.songoda.ultimatestacker.events.SpawnerPlaceEvent;
|
||||
import com.songoda.ultimatestacker.spawner.SpawnerStack;
|
||||
import com.songoda.ultimatestacker.utils.Methods;
|
||||
import com.songoda.ultimatestacker.utils.ServerVersion;
|
||||
import com.songoda.ultimatestacker.utils.settings.Setting;
|
||||
import org.apache.commons.lang.math.NumberUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.CreatureSpawner;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.LivingEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.Action;
|
||||
import org.bukkit.event.block.BlockBreakEvent;
|
||||
import org.bukkit.event.block.BlockPlaceEvent;
|
||||
import org.bukkit.event.entity.SpawnerSpawnEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.BlockStateMeta;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class SpawnerListeners implements Listener {
|
||||
|
||||
private final UltimateStacker instance;
|
||||
|
||||
public SpawnerListeners(UltimateStacker instance) {
|
||||
this.instance = instance;
|
||||
}
|
||||
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
|
||||
public void onSpawnerSpawn(SpawnerSpawnEvent event) {
|
||||
if (!instance.spawnersEnabled() || !(event.getEntity() instanceof LivingEntity)) return;
|
||||
Bukkit.broadcastMessage("doing a spawn");
|
||||
|
||||
if (instance.getStackingTask().attemptAddToStack((LivingEntity) event.getEntity(), null)) {
|
||||
Bukkit.broadcastMessage("Doing a stack");
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
}
|
@ -23,8 +23,14 @@ public class StackingTask extends BukkitRunnable {
|
||||
|
||||
private final UltimateStacker instance;
|
||||
|
||||
private EntityStackManager stackManager;
|
||||
|
||||
|
||||
private List<UUID> removed = new ArrayList<>();
|
||||
|
||||
public StackingTask(UltimateStacker instance) {
|
||||
this.instance = instance;
|
||||
this.stackManager = instance.getEntityStackManager();
|
||||
|
||||
// Start stacking task.
|
||||
runTaskTimer(instance, 0, Setting.STACK_SEARCH_TICK_SPEED.getInt());
|
||||
@ -32,28 +38,22 @@ public class StackingTask extends BukkitRunnable {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
int maxEntityStackSizeGlobal = Setting.MAX_STACK_ENTITIES.getInt();
|
||||
int minEntityStackAmount = Setting.MIN_STACK_ENTITIES.getInt();
|
||||
|
||||
List<String> disabledWorlds = Setting.DISABLED_WORLDS.getStringList();
|
||||
|
||||
EntityStackManager stackManager = instance.getEntityStackManager();
|
||||
nextEntity:
|
||||
for (World world : Bukkit.getWorlds()) {
|
||||
if (disabledWorlds.stream().anyMatch(worldStr -> world.getName().equalsIgnoreCase(worldStr))) continue;
|
||||
|
||||
List<Entity> entities = world.getEntities();
|
||||
Collections.reverse(entities);
|
||||
|
||||
List<UUID> removed = new ArrayList<>();
|
||||
|
||||
nextEntity:
|
||||
for (Entity entityO : entities) {
|
||||
if (entityO == null
|
||||
|| !(entityO instanceof LivingEntity)
|
||||
|| entityO instanceof Player
|
||||
|| !entityO.isValid()
|
||||
|| !(entityO instanceof LivingEntity)
|
||||
|| !Setting.STACK_ENTITIES.getBoolean())
|
||||
continue;
|
||||
continue nextEntity;
|
||||
|
||||
LivingEntity initialEntity = (LivingEntity) entityO;
|
||||
|
||||
@ -69,82 +69,92 @@ public class StackingTask extends BukkitRunnable {
|
||||
|| Setting.ONLY_STACK_ON_SURFACE.getBoolean()
|
||||
&& !Methods.canFly(initialEntity)
|
||||
&& (!initialEntity.isOnGround() && !initialEntity.getLocation().getBlock().isLiquid()))
|
||||
continue;
|
||||
continue nextEntity;
|
||||
|
||||
EntityStack initialStack = stackManager.getStack(initialEntity);
|
||||
if (initialStack == null && initialEntity.getCustomName() != null) continue;
|
||||
int amtToStack = initialStack != null ? initialStack.getAmount() : 1;
|
||||
if (initialStack == null && initialEntity.getCustomName() != null) continue nextEntity;
|
||||
|
||||
ConfigurationSection configurationSection = UltimateStacker.getInstance().getMobFile().getConfig();
|
||||
|
||||
if (!configurationSection.getBoolean("Mobs." + initialEntity.getType().name() + ".Enabled"))
|
||||
continue;
|
||||
continue nextEntity;
|
||||
|
||||
int maxEntityStackSize = maxEntityStackSizeGlobal;
|
||||
if (configurationSection.getInt("Mobs." + initialEntity.getType().name() + ".Max Stack Size") != -1)
|
||||
maxEntityStackSize = configurationSection.getInt("Mobs." + initialEntity.getType().name() + ".Max Stack Size");
|
||||
|
||||
List<LivingEntity> entityList = Methods.getSimilarEntitiesAroundEntity(initialEntity);
|
||||
entityList.removeIf(entity -> entity.hasMetadata("inLove")
|
||||
|| entity.hasMetadata("breedCooldown")
|
||||
|
||||
|| Setting.ONLY_STACK_FROM_SPAWNERS.getBoolean()
|
||||
&& !(initialEntity.hasMetadata("US_REASON")
|
||||
&& initialEntity.getMetadata("US_REASON").get(0).asString().equals("SPAWNER")));
|
||||
|
||||
for (LivingEntity entity : new ArrayList<>(entityList)) {
|
||||
if (removed.contains(entity.getUniqueId())) continue;
|
||||
EntityStack stack = stackManager.getStack(entity);
|
||||
if (stack == null && entity.getCustomName() != null) {
|
||||
entityList.remove(entity);
|
||||
continue;
|
||||
}
|
||||
|
||||
//If a stack was found add 1 to this stack.
|
||||
if (stack != null && (stack.getAmount() + amtToStack) <= maxEntityStackSize) {
|
||||
stack.addAmount(amtToStack);
|
||||
stack.updateStack();
|
||||
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();
|
||||
|
||||
continue nextEntity;
|
||||
}
|
||||
}
|
||||
|
||||
if (initialStack != null) continue;
|
||||
|
||||
entityList.removeIf(stackManager::isStacked);
|
||||
|
||||
if (entityList.size() < minEntityStackAmount - 1
|
||||
|| minEntityStackAmount > maxEntityStackSize
|
||||
|| minEntityStackAmount == 1 && entityList.size() == 0) continue;
|
||||
|
||||
//If stack was never found make a new one.
|
||||
EntityStack stack = stackManager.addStack(new EntityStack(initialEntity, (entityList.size() + 1) >
|
||||
maxEntityStackSize ? maxEntityStackSize : entityList.size() + 1));
|
||||
|
||||
entityList.stream().filter(entity -> !stackManager.isStacked(entity)
|
||||
&& !removed.contains(entity.getUniqueId())).limit(maxEntityStackSize).forEach(entity -> {
|
||||
removed.add(entity.getUniqueId());
|
||||
entity.remove();
|
||||
});
|
||||
|
||||
stack.updateStack();
|
||||
attemptAddToStack(initialEntity, initialStack);
|
||||
}
|
||||
entities.clear();
|
||||
removed.clear();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean attemptAddToStack(LivingEntity initialEntity, EntityStack initialStack) {
|
||||
ConfigurationSection configurationSection = UltimateStacker.getInstance().getMobFile().getConfig();
|
||||
int minEntityStackAmount = Setting.MIN_STACK_ENTITIES.getInt();
|
||||
int amtToStack = initialStack != null ? initialStack.getAmount() : 1;
|
||||
|
||||
List<LivingEntity> entityList = Methods.getSimilarEntitiesAroundEntity(initialEntity);
|
||||
entityList.removeIf(entity -> entity.hasMetadata("inLove")
|
||||
|| entity.hasMetadata("breedCooldown")
|
||||
|
||||
|| Setting.ONLY_STACK_FROM_SPAWNERS.getBoolean()
|
||||
&& !(initialEntity.hasMetadata("US_REASON")
|
||||
&& initialEntity.getMetadata("US_REASON").get(0).asString().equals("SPAWNER")));
|
||||
|
||||
|
||||
int maxEntityStackSize = Setting.MAX_STACK_ENTITIES.getInt();
|
||||
if (configurationSection.getInt("Mobs." + initialEntity.getType().name() + ".Max Stack Size") != -1)
|
||||
maxEntityStackSize = configurationSection.getInt("Mobs." + initialEntity.getType().name() + ".Max Stack Size");
|
||||
|
||||
|
||||
for (LivingEntity entity : new ArrayList<>(entityList)) {
|
||||
if (removed.contains(entity.getUniqueId())) continue;
|
||||
EntityStack stack = stackManager.getStack(entity);
|
||||
if (stack == null && entity.getCustomName() != null) {
|
||||
entityList.remove(entity);
|
||||
continue;
|
||||
}
|
||||
|
||||
//If a stack was found add 1 to this stack.
|
||||
if (stack != null && (stack.getAmount() + amtToStack) <= maxEntityStackSize) {
|
||||
stack.addAmount(amtToStack);
|
||||
stack.updateStack();
|
||||
removed.add(initialEntity.getUniqueId());
|
||||
initialEntity.remove();
|
||||
|
||||
return true;
|
||||
} 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();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (initialStack != null) return false;
|
||||
|
||||
entityList.removeIf(stackManager::isStacked);
|
||||
|
||||
if (entityList.size() < minEntityStackAmount - 1
|
||||
|| minEntityStackAmount > maxEntityStackSize
|
||||
|| minEntityStackAmount == 1 && entityList.size() == 0) return false;
|
||||
|
||||
//If stack was never found make a new one.
|
||||
EntityStack stack = stackManager.addStack(new EntityStack(initialEntity, (entityList.size() + 1) >
|
||||
maxEntityStackSize ? maxEntityStackSize : entityList.size() + 1));
|
||||
|
||||
entityList.stream().filter(entity -> !stackManager.isStacked(entity)
|
||||
&& !removed.contains(entity.getUniqueId())).limit(maxEntityStackSize).forEach(entity -> {
|
||||
removed.add(entity.getUniqueId());
|
||||
entity.remove();
|
||||
});
|
||||
|
||||
stack.updateStack();
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user