diff --git a/src/main/java/com/songoda/ultimatestacker/UltimateStacker.java b/src/main/java/com/songoda/ultimatestacker/UltimateStacker.java index ebb4c35..1395a0c 100644 --- a/src/main/java/com/songoda/ultimatestacker/UltimateStacker.java +++ b/src/main/java/com/songoda/ultimatestacker/UltimateStacker.java @@ -145,7 +145,6 @@ public class UltimateStacker extends JavaPlugin { this.spawnerStackManager = new SpawnerStackManager(); this.entityStackManager = new EntityStackManager(); this.stackingTask = new StackingTask(this); - this.stackingTask.startTask(); checkStorage(); diff --git a/src/main/java/com/songoda/ultimatestacker/tasks/StackingTask.java b/src/main/java/com/songoda/ultimatestacker/tasks/StackingTask.java index 73d8f99..7f663e0 100644 --- a/src/main/java/com/songoda/ultimatestacker/tasks/StackingTask.java +++ b/src/main/java/com/songoda/ultimatestacker/tasks/StackingTask.java @@ -17,25 +17,23 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.UUID; -import java.util.stream.Collectors; public class StackingTask extends BukkitRunnable { private final UltimateStacker instance; - private Class clazzItemStack, clazzItem, clazzCraftItemStack; - private Method methodGetItem, methodAsNMSCopy; - private Field fieldMaxStackSize; public StackingTask(UltimateStacker instance) { this.instance = instance; + + // Cache reflection. try { String ver = Bukkit.getServer().getClass().getPackage().getName().substring(23); - clazzCraftItemStack = Class.forName("org.bukkit.craftbukkit." + ver + ".inventory.CraftItemStack"); - clazzItemStack = Class.forName("net.minecraft.server." + ver + ".ItemStack"); - clazzItem = Class.forName("net.minecraft.server." + ver + ".Item"); + Class clazzCraftItemStack = Class.forName("org.bukkit.craftbukkit." + ver + ".inventory.CraftItemStack"); + Class clazzItemStack = Class.forName("net.minecraft.server." + ver + ".ItemStack"); + Class clazzItem = Class.forName("net.minecraft.server." + ver + ".Item"); methodAsNMSCopy = clazzCraftItemStack.getMethod("asNMSCopy", ItemStack.class); methodGetItem = clazzItemStack.getDeclaredMethod("getItem"); @@ -45,6 +43,10 @@ public class StackingTask extends BukkitRunnable { } catch (ReflectiveOperationException e) { e.printStackTrace(); } + + // Start stacking task. + new StackingTask(instance).runTaskTimer(instance, 0, + instance.getConfig().getInt("Main.Stack Search Tick Speed")); } @Override @@ -135,7 +137,6 @@ public class StackingTask extends BukkitRunnable { continue nextEntity; } - } if (initialStack != null) continue; @@ -149,11 +150,11 @@ public class StackingTask extends BukkitRunnable { //If stack was never found make a new one. EntityStack stack = stackManager.addStack(new EntityStack(initalEntity, entityList.size() + 1)); - for (Entity entity : entityList) { - if (stackManager.isStacked(entity) || removed.contains(entity.getUniqueId())) continue; + entityList.stream().filter(entity -> !stackManager.isStacked(entity) + && !removed.contains(entity.getUniqueId())).forEach(entity -> { removed.add(entity.getUniqueId()); entity.remove(); - } + }); stack.updateStack(); } @@ -171,8 +172,4 @@ public class StackingTask extends BukkitRunnable { } return item; } - - public void startTask() { - new StackingTask(instance).runTaskTimer(instance, 0, instance.getConfig().getInt("Main.Stack Search Tick Speed")); - } } diff --git a/src/main/java/com/songoda/ultimatestacker/utils/Methods.java b/src/main/java/com/songoda/ultimatestacker/utils/Methods.java index c7b0fad..9b0ac73 100644 --- a/src/main/java/com/songoda/ultimatestacker/utils/Methods.java +++ b/src/main/java/com/songoda/ultimatestacker/utils/Methods.java @@ -17,6 +17,7 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; public class Methods { @@ -40,12 +41,12 @@ public class Methods { private static void handleSingleStackDeath(LivingEntity killed) { UltimateStacker instance = UltimateStacker.getInstance(); EntityStackManager stackManager = instance.getEntityStackManager(); - Entity newEntity = newEntity(killed); + LivingEntity newEntity = newEntity(killed); - ((LivingEntity) newEntity).getEquipment().clear(); + newEntity.getEquipment().clear(); if (killed.getType() == EntityType.PIG_ZOMBIE) - ((LivingEntity) newEntity).getEquipment().setItemInHand(new ItemStack(Material.GOLD_SWORD)); + newEntity.getEquipment().setItemInHand(new ItemStack(Material.GOLD_SWORD)); if (Bukkit.getPluginManager().isPluginEnabled("EpicSpawners")) if (killed.hasMetadata("ES")) @@ -114,12 +115,10 @@ public class Methods { public static List getSimilarEntitesAroundEntity(Entity initalEntity) { - //Create a list of all entities around the initial entity. - List entityList = initalEntity.getNearbyEntities(5, 5, 5); - - //Remove entities of a different type. - entityList.removeIf(entity1 -> entity1.getType() != initalEntity.getType() - || entity1 == initalEntity); + //Create a list of all entities around the initial entity of the same type. + List entityList = initalEntity.getNearbyEntities(5, 5, 5).stream() + .filter(entity -> entity.getType() == initalEntity.getType() && entity != initalEntity) + .collect(Collectors.toList()); if (initalEntity instanceof Ageable) { if (((Ageable) initalEntity).isAdult()) { @@ -137,14 +136,10 @@ public class Methods { entityList.removeIf(entity -> ((Sheep) entity).isSheared()); } entityList.removeIf(entity -> ((Sheep) entity).getColor() != sheep.getColor()); - } - - if (initalEntity instanceof Villager) { + } else if (initalEntity instanceof Villager) { Villager villager = ((Villager) initalEntity); entityList.removeIf(entity -> ((Villager) entity).getProfession() != villager.getProfession()); - } - - if (initalEntity instanceof Slime) { + } else if (initalEntity instanceof Slime) { Slime slime = ((Slime) initalEntity); entityList.removeIf(entity -> ((Slime)entity).getSize() != slime.getSize()); }