From 7a1cede33bdb0163cfa19301371fd8e2651ed6ac Mon Sep 17 00:00:00 2001 From: Brianna Date: Sat, 14 Dec 2019 18:46:38 -0500 Subject: [PATCH 1/3] Major performance improvements. --- .../ultimatestacker/entity/EntityStackManager.java | 4 ++-- .../songoda/ultimatestacker/utils/CachedChunk.java | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/songoda/ultimatestacker/entity/EntityStackManager.java b/src/main/java/com/songoda/ultimatestacker/entity/EntityStackManager.java index fb05c04..b13f5eb 100644 --- a/src/main/java/com/songoda/ultimatestacker/entity/EntityStackManager.java +++ b/src/main/java/com/songoda/ultimatestacker/entity/EntityStackManager.java @@ -25,7 +25,7 @@ public class EntityStackManager { public EntityStack addStack(UUID uuid, int amount) { EntityStack stack = new EntityStack(uuid, amount); - stacks.put(uuid, new EntityStack(uuid, amount)); + stacks.put(uuid, stack); return stack; } @@ -36,7 +36,7 @@ public class EntityStackManager { if (!name.contains(":")) return null; String split = name.split(":")[0]; int amount = Methods.isInt(split) ? Integer.parseInt(split) : 0; - addStack(entity, amount); + return addStack(entity, amount); } return null; } diff --git a/src/main/java/com/songoda/ultimatestacker/utils/CachedChunk.java b/src/main/java/com/songoda/ultimatestacker/utils/CachedChunk.java index 9194c0d..e950c80 100644 --- a/src/main/java/com/songoda/ultimatestacker/utils/CachedChunk.java +++ b/src/main/java/com/songoda/ultimatestacker/utils/CachedChunk.java @@ -2,7 +2,9 @@ package com.songoda.ultimatestacker.utils; import org.bukkit.Bukkit; import org.bukkit.Chunk; +import org.bukkit.Location; import org.bukkit.World; +import org.bukkit.entity.Entity; import java.util.Objects; @@ -16,6 +18,10 @@ public class CachedChunk { this(chunk.getWorld().getName(), chunk.getX(), chunk.getZ()); } + public CachedChunk(Location location) { + this(location.getWorld().getName(), (int)location.getX() >> 4, (int)location.getZ() >> 4); + } + public CachedChunk(String world, int x, int z) { this.world = world; this.x = x; @@ -41,6 +47,13 @@ public class CachedChunk { return world.getChunkAt(this.x, this.z); } + public Entity[] getEntities() { + if (!Bukkit.getWorld(world).isChunkLoaded(x, z)) { + return new Entity[0]; + } + return getChunk().getEntities(); + } + @Override public boolean equals(Object o) { if (o instanceof Chunk) { From 6ba64cc39106857678b7b34954a3d7df3faa2c26 Mon Sep 17 00:00:00 2001 From: Brianna Date: Sat, 14 Dec 2019 18:46:57 -0500 Subject: [PATCH 2/3] Added support for disabling knockback on stacked mobs. --- .../listeners/EntityListeners.java | 18 +++++++++++++++++- .../ultimatestacker/settings/Settings.java | 3 +++ .../ultimatestacker/utils/EntityUtils.java | 19 ++++++++++++++----- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/songoda/ultimatestacker/listeners/EntityListeners.java b/src/main/java/com/songoda/ultimatestacker/listeners/EntityListeners.java index d23ccb6..8af7652 100644 --- a/src/main/java/com/songoda/ultimatestacker/listeners/EntityListeners.java +++ b/src/main/java/com/songoda/ultimatestacker/listeners/EntityListeners.java @@ -12,20 +12,22 @@ import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.block.CreatureSpawner; +import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.*; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.entity.CreatureSpawnEvent; +import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.entity.ItemSpawnEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.metadata.FixedMetadataValue; +import org.bukkit.util.Vector; import java.util.ArrayList; import java.util.Iterator; import java.util.List; -import java.util.stream.Collectors; public class EntityListeners implements Listener { @@ -77,6 +79,20 @@ public class EntityListeners implements Listener { event.getEntity().setItemStack(item); } + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onHurt(EntityDamageByEntityEvent event) { + if (!Settings.STACK_ENTITIES.getBoolean() || !(event.getDamager() instanceof Player)) return; + + if (plugin.getEntityStackManager().isStacked(event.getEntity()) + && Settings.DISABLE_KNOCKBACK.getBoolean() + && ((Player) event.getDamager()).getItemInHand().getEnchantmentLevel(Enchantment.KNOCKBACK) == 0) { + Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> { + event.getEntity().setVelocity(new Vector()); + }, 0L); + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onBlow(EntityExplodeEvent event) { if (!plugin.spawnersEnabled()) return; diff --git a/src/main/java/com/songoda/ultimatestacker/settings/Settings.java b/src/main/java/com/songoda/ultimatestacker/settings/Settings.java index f2e50c1..c55e69a 100644 --- a/src/main/java/com/songoda/ultimatestacker/settings/Settings.java +++ b/src/main/java/com/songoda/ultimatestacker/settings/Settings.java @@ -163,6 +163,9 @@ public class Settings { public static final ConfigSetting REALISTIC_DAMAGE = new ConfigSetting(config, "Entities.Use Realistic Weapon Damage", true, "Should weapons take damage based on the amount of entites in the stack?"); + public static final ConfigSetting DISABLE_KNOCKBACK = new ConfigSetting(config, "Entities.Disable Knockback", false, + "Should knockback be disabled on unstacked mobs?"); + public static final ConfigSetting STACK_ITEMS = new ConfigSetting(config, "Items.Enabled", true, "Should items be stacked?"); diff --git a/src/main/java/com/songoda/ultimatestacker/utils/EntityUtils.java b/src/main/java/com/songoda/ultimatestacker/utils/EntityUtils.java index a8896e0..5751d04 100644 --- a/src/main/java/com/songoda/ultimatestacker/utils/EntityUtils.java +++ b/src/main/java/com/songoda/ultimatestacker/utils/EntityUtils.java @@ -5,7 +5,10 @@ import com.songoda.ultimatestacker.UltimateStacker; import com.songoda.ultimatestacker.entity.Check; import com.songoda.ultimatestacker.entity.EntityStack; import com.songoda.ultimatestacker.settings.Settings; -import org.bukkit.*; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.World; +import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.*; import java.util.*; @@ -34,8 +37,8 @@ public class EntityUtils { Set chunks = new HashSet<>(); if (world == null) return chunks; - Chunk firstChunk = location.getChunk(); - chunks.add(new CachedChunk(firstChunk)); + CachedChunk firstChunk = new CachedChunk(location); + chunks.add(firstChunk); if (singleChunk) return chunks; @@ -60,7 +63,7 @@ public class EntityUtils { if (cachedChunks.containsKey(chunk)) { entityArray = cachedChunks.get(chunk); } else { - entityArray = chunk.getChunk().getEntities(); + entityArray = chunk.getEntities(); cachedChunks.put(chunk, entityArray); } for (Entity e : entityArray) { @@ -84,7 +87,13 @@ public class EntityUtils { public LivingEntity newEntity(LivingEntity toClone) { LivingEntity newEntity = (LivingEntity) toClone.getWorld().spawnEntity(toClone.getLocation(), toClone.getType()); - newEntity.setVelocity(toClone.getVelocity()); + + Player player = toClone.getKiller(); + if (player == null + || !Settings.DISABLE_KNOCKBACK.getBoolean() + || player.getItemInHand().getEnchantmentLevel(Enchantment.KNOCKBACK) != 0) { + newEntity.setVelocity(toClone.getVelocity()); + } for (String checkStr : checks) { Check check = Check.valueOf(checkStr); From cd6e2753b52b3335a7f00620492704d8ec575662 Mon Sep 17 00:00:00 2001 From: Brianna Date: Sat, 14 Dec 2019 18:47:33 -0500 Subject: [PATCH 3/3] version 1.11.1 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index bddfd88..3e58e07 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ com.songoda UltimateStacker 4.0.0 - 1.11 + 1.11.1 clean install UltimateStacker-${project.version}