Named entity will no longer stack.

When using a nametag on an entity the entity a new entity will break apart from the stack with the custom name rather than just naming the whole stack.
Two stacks will now stack together.
When epicspawners is installed spawner stacking will automatically disable.
This commit is contained in:
Brianna O'Keefe 2018-11-07 22:26:38 -05:00
parent dea3a461af
commit 9276bf397a
7 changed files with 109 additions and 36 deletions

8
.gitignore vendored
View File

@ -144,3 +144,11 @@ target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles\.ls
\.idea/vcs\.xml
UltimateStacker\.iml
\.idea/libraries/Maven__org_spigotmc_spigot_1_12_2\.xml
target/classes/com/songoda/ultimatestacker/events/InteractListeners\.class
target/classes/com/songoda/ultimatestacker/utils/Reflection\.class
target/classes/com/songoda/ultimatestacker/utils/ServerVersion\.class

View File

@ -170,7 +170,6 @@ public class UltimateStacker extends JavaPlugin {
}
}
for (SpawnerStack stack : spawnerStackManager.getStacks()) {
storage.prepareSaveItem("spawners", new StorageItem("location", Serialize.getInstance().serializeLocation(stack.getLocation())),
new StorageItem("amount", stack.getAmount()));
@ -185,7 +184,7 @@ public class UltimateStacker extends JavaPlugin {
Bukkit.getPluginManager().registerEvents(new BlockListeners(this), this);
Bukkit.getPluginManager().registerEvents(new DeathListeners(this), this);
Bukkit.getPluginManager().registerEvents(new ShearListeners(this), this);
Bukkit.getPluginManager().registerEvents(new DropListeners(this), this);
Bukkit.getPluginManager().registerEvents(new InteractListeners(this), this);
Bukkit.getScheduler().runTaskTimerAsynchronously(this, this::saveToFile, 6000, 6000);
@ -272,6 +271,11 @@ public class UltimateStacker extends JavaPlugin {
return null;
}
public boolean spawnersEnabled() {
if (this.getServer().getPluginManager().isPluginEnabled("EpicSpawners")) return false;
return this.getConfig().getBoolean("Main.Stack Spawners");
}
public ServerVersion getServerVersion() {
return serverVersion;
}

View File

@ -1,5 +1,6 @@
package com.songoda.ultimatestacker.entity;
import com.songoda.ultimatestacker.UltimateStacker;
import com.songoda.ultimatestacker.utils.Methods;
import org.bukkit.entity.Entity;
@ -36,6 +37,12 @@ public class EntityStack {
}
public void setAmount(int amount) {
if (amount == 1) {
UltimateStacker.getInstance().getEntityStackManager().removeStack(entity);
entity.setCustomName(null);
entity.setCustomNameVisible(false);
return;
}
this.amount = amount;
updateStack();
}

View File

@ -37,7 +37,7 @@ public class BlockListeners implements Listener {
if (block == null || item == null || block.getType() != Material.MOB_SPAWNER || item.getType() != Material.MOB_SPAWNER || event.getAction() == Action.LEFT_CLICK_BLOCK) return;
if (!instance.getConfig().getBoolean("Main.Stack Spawners")) return;
if (!instance.spawnersEnabled()) return;
BlockStateMeta bsm = (BlockStateMeta) item.getItemMeta();
CreatureSpawner cs = (CreatureSpawner) bsm.getBlockState();
@ -87,7 +87,7 @@ public class BlockListeners implements Listener {
if (block == null || block.getType() != Material.MOB_SPAWNER) return;
if (!instance.getConfig().getBoolean("Main.Stack Spawners")) return;
if (!instance.spawnersEnabled()) return;
SpawnerStack stack = instance.getSpawnerStackManager().addSpawner(new SpawnerStack(block.getLocation(), getSpawnerAmount(event.getItemInHand())));
instance.getHologramHandler().updateHologram(stack);
@ -100,7 +100,7 @@ public class BlockListeners implements Listener {
Block block = event.getBlock();
if (block.getType() != Material.MOB_SPAWNER) return;
if (!instance.getConfig().getBoolean("Main.Stack Spawners")) return;
if (!instance.spawnersEnabled()) return;
CreatureSpawner cs = (CreatureSpawner) block.getState();

View File

@ -1,21 +0,0 @@
package com.songoda.ultimatestacker.events;
import com.songoda.ultimatestacker.UltimateStacker;
import org.bukkit.entity.Item;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerDropItemEvent;
public class DropListeners implements Listener {
private final UltimateStacker instance;
public DropListeners(UltimateStacker instance) {
this.instance = instance;
}
@EventHandler
public void onDrop(PlayerDropItemEvent event) {
Item dropped = event.getItemDrop();
}
}

View File

@ -0,0 +1,67 @@
package com.songoda.ultimatestacker.events;
import com.songoda.ultimatestacker.UltimateStacker;
import com.songoda.ultimatestacker.entity.EntityStack;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.*;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;
import java.util.concurrent.ThreadLocalRandom;
public class InteractListeners implements Listener {
private final UltimateStacker instance;
public InteractListeners(UltimateStacker instance) {
this.instance = instance;
}
@EventHandler
public void onInteract(PlayerInteractAtEntityEvent event) {
Player player = event.getPlayer();
Entity entity = event.getRightClicked();
ItemStack item = player.getInventory().getItemInMainHand();
if (item.getType() != Material.NAME_TAG
|| !instance.getEntityStackManager().isStacked(entity)) return;
EntityStack stack = instance.getEntityStackManager().getStack(entity);
if (stack.getAmount() == 1) return;
event.setCancelled(true);
Bukkit.getScheduler().scheduleSyncDelayedTask(instance, () -> stack.setAmount(stack.getAmount() - 1), 1L);
Entity newEntity = entity.getWorld().spawnEntity(entity.getLocation(), entity.getType());
newEntity.setVelocity(getRandomVector());
if (entity instanceof Ageable) {
if (((Ageable) entity).isAdult()) {
((Ageable) newEntity).setAdult();
} else {
((Ageable) entity).setBaby();
}
}
if (entity instanceof Sheep) {
Sheep sheep = ((Sheep) newEntity);
sheep.setSheared(sheep.isSheared());
sheep.setColor(sheep.getColor());
}
if (entity instanceof Villager) {
Villager villager = ((Villager) newEntity);
villager.setProfession(villager.getProfession());
}
newEntity.setCustomName(item.getItemMeta().getDisplayName());
}
private Vector getRandomVector() {
return new Vector(ThreadLocalRandom.current().nextDouble(-1, 1.01), 0, ThreadLocalRandom.current().nextDouble(-1, 1.01)).normalize().multiply(0.5);
}
}

View File

@ -8,15 +8,13 @@ import com.songoda.ultimatestacker.utils.Methods;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.*;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class StackingTask extends BukkitRunnable {
@ -57,6 +55,8 @@ public class StackingTask extends BukkitRunnable {
nextEntity:
for (Entity entityO : world.getEntities()) {
if (entityO instanceof Player) continue;
if (entityO instanceof Item && instance.getConfig().getBoolean("Main.Stack Items")) {
ItemStack item = ((Item) entityO).getItemStack();
@ -85,8 +85,11 @@ public class StackingTask extends BukkitRunnable {
if (initalEntity.isDead()
|| !initalEntity.isValid()
|| initalEntity instanceof ArmorStand
|| stackManager.isStacked(initalEntity)) continue;
|| initalEntity instanceof ArmorStand) continue;
EntityStack initialStack = stackManager.getStack(initalEntity);
if (initialStack == null && initalEntity.getCustomName() != null) continue;
int amtToStack = initialStack != null ? initialStack.getAmount() : 1;
ConfigurationSection configurationSection = UltimateStacker.getInstance().getMobFile().getConfig();
@ -98,19 +101,24 @@ public class StackingTask extends BukkitRunnable {
List<Entity> entityList = Methods.getSimilarEntitesAroundEntity(initalEntity);
for (Entity entity : entityList) {
for (Entity entity : new ArrayList<>(entityList)) {
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() < maxEntityStackSize) {
stack.addAmount(1);
if (stack != null && (stack.getAmount() + amtToStack) <= maxEntityStackSize) {
stack.addAmount(amtToStack);
stack.updateStack();
initalEntity.remove();
continue nextEntity;
}
}
if (initialStack != null) continue;
entityList.removeIf(stackManager::isStacked);
if (entityList.size() < instance.getConfig().getInt("Entity.Min Stack Amount") - 1) continue;