From 6ba64cc39106857678b7b34954a3d7df3faa2c26 Mon Sep 17 00:00:00 2001 From: Brianna Date: Sat, 14 Dec 2019 18:46:57 -0500 Subject: [PATCH] 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);