Reworked Entity listeners

This commit is contained in:
Fabrizio La Rosa 2020-07-11 20:27:44 +02:00
parent 493cb70987
commit dbae4df308

View File

@ -1,6 +1,7 @@
package com.songoda.skyblock.listeners; package com.songoda.skyblock.listeners;
import com.songoda.core.compatibility.CompatibleMaterial; import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.compatibility.ServerVersion;
import com.songoda.skyblock.SkyBlock; import com.songoda.skyblock.SkyBlock;
import com.songoda.skyblock.config.FileManager; import com.songoda.skyblock.config.FileManager;
import com.songoda.skyblock.config.FileManager.Config; import com.songoda.skyblock.config.FileManager.Config;
@ -34,7 +35,6 @@ import org.bukkit.event.vehicle.VehicleDamageEvent;
import org.bukkit.event.vehicle.VehicleDestroyEvent; import org.bukkit.event.vehicle.VehicleDestroyEvent;
import org.bukkit.inventory.EntityEquipment; import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import java.io.File; import java.io.File;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@ -44,20 +44,20 @@ public class Entity implements Listener {
private final SkyBlock plugin; private final SkyBlock plugin;
private Set<UUID> preventFireTicks = new HashSet<>(); private final Set<UUID> preventFireTicks = new HashSet<>();
public Entity(SkyBlock plugin) { public Entity(SkyBlock plugin) {
this.plugin = plugin; this.plugin = plugin;
} }
@EventHandler @EventHandler(ignoreCancelled = true)
public void onFireWorkBoom(EntityDamageByEntityEvent event) { public void onFireWorkBoom(EntityDamageByEntityEvent event) {
if (event.getDamager().getType() == EntityType.FIREWORK if (event.getDamager().getType() == EntityType.FIREWORK
&& plugin.getWorldManager().isIslandWorld(event.getEntity().getWorld())) && plugin.getWorldManager().isIslandWorld(event.getEntity().getWorld()))
event.setCancelled(true); event.setCancelled(true);
} }
@EventHandler @EventHandler(ignoreCancelled = true)
public void onEntityDamage(EntityDamageEvent event) { public void onEntityDamage(EntityDamageEvent event) {
IslandManager islandManager = plugin.getIslandManager(); IslandManager islandManager = plugin.getIslandManager();
if(event.getEntity() instanceof Blaze){ if(event.getEntity() instanceof Blaze){
@ -89,61 +89,51 @@ public class Entity implements Listener {
} }
} }
@EventHandler @EventHandler(ignoreCancelled = true)
public void onEntityDamageByEntity(EntityDamageByEntityEvent event) { public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
IslandManager islandManager = plugin.getIslandManager(); IslandManager islandManager = plugin.getIslandManager();
if (event.getDamager() instanceof Player) { org.bukkit.entity.Entity victim = event.getEntity();
Player player = (Player) event.getDamager(); Island island = islandManager.getIslandAtLocation(victim.getLocation());
org.bukkit.entity.Entity entity = event.getEntity();
if (plugin.getWorldManager().isIslandWorld(entity.getWorld())) { if(island != null) {
org.bukkit.entity.Entity attacker = event.getDamager();
// Check permissions. if(attacker instanceof Projectile && ((Projectile) attacker).getShooter() instanceof org.bukkit.entity.Entity) {
plugin.getPermissionManager() attacker = (org.bukkit.entity.Entity) ((Projectile) attacker).getShooter();
.processPermission(event, player, islandManager.getIslandAtLocation(entity.getLocation()));
} }
return; if(victim instanceof Player && attacker instanceof Player) { // PVP
} if(plugin.getPermissionManager()
.processPermission(event, (Player) attacker, island)) {
plugin.getPermissionManager()
.processPermission(event, (Player) victim, island);
}
} else if(victim instanceof Player) { // EVP
plugin.getPermissionManager()
.processPermission(event, (Player) victim, island, true);
} else if(attacker instanceof Player) { // PVE
plugin.getPermissionManager()
.processPermission(event, (Player) attacker, island);
} else { // EVE
plugin.getPermissionManager()
.processPermission(event, island);
}
if (event.getEntity() instanceof Player) { // Fix a bug in minecraft where arrows with flame still apply fire ticks even if
Player player = (Player) event.getEntity(); // the shot entity isn't damaged
if (event.isCancelled() && event.getDamager() instanceof Arrow) {
// Check permissions. Arrow arrow = (Arrow) event.getDamager();
plugin.getPermissionManager() if (arrow.getFireTicks() != 0) {
.processPermission(event, player, islandManager.getIslandAtLocation(player.getLocation()), true); preventFireTicks.add(event.getEntity().getUniqueId());
Bukkit.getScheduler().runTaskLater(plugin,
} else if((event.getDamager() instanceof org.bukkit.entity.Projectile () -> preventFireTicks.remove(event.getEntity().getUniqueId()),
&& ((Projectile) event.getDamager()).getShooter() instanceof Player)){ 5L);
Player player = (Player) ((Projectile) event.getDamager()).getShooter(); }
plugin.getPermissionManager()
.processPermission(event, player, islandManager.getIslandAtLocation(player.getLocation()));
} else { // Make it work with all the entities, not just TNT
org.bukkit.entity.Entity entity = event.getEntity();
// Check permissions.
plugin.getPermissionManager()
.processPermission(event, islandManager.getIslandAtLocation(entity.getLocation()));
}
// Fix a bug in minecraft where arrows with flame still apply fire ticks even if
// the shot entity isn't damaged
if (event.isCancelled() && event.getDamager() instanceof Arrow
&& ((Arrow) event.getDamager()).getShooter() instanceof Player) {
Arrow arrow = (Arrow) event.getDamager();
if (arrow.getFireTicks() != 0) {
preventFireTicks.add(event.getEntity().getUniqueId());
new BukkitRunnable() {
public void run() {
preventFireTicks.remove(event.getEntity().getUniqueId());
}
}.runTaskLater(SkyBlock.getInstance(), 5L);
} }
} }
} }
@EventHandler @EventHandler(ignoreCancelled = true)
public void onPlayerShearEntity(PlayerShearEntityEvent event) { public void onPlayerShearEntity(PlayerShearEntityEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
@ -161,7 +151,7 @@ public class Entity implements Listener {
* *
* @author LimeGlass * @author LimeGlass
*/ */
@EventHandler @EventHandler(ignoreCancelled = true)
public void onEntityTarget(EntityTargetEvent event) { public void onEntityTarget(EntityTargetEvent event) {
org.bukkit.entity.Entity entity = event.getEntity(); org.bukkit.entity.Entity entity = event.getEntity();
WorldManager worldManager = plugin.getWorldManager(); WorldManager worldManager = plugin.getWorldManager();
@ -184,11 +174,10 @@ public class Entity implements Listener {
// Both entities are on different islands. // Both entities are on different islands.
if (!entityIsland.getIslandUUID().equals(targetIsland.getIslandUUID())) { if (!entityIsland.getIslandUUID().equals(targetIsland.getIslandUUID())) {
event.setCancelled(true); event.setCancelled(true);
return;
} }
} }
@EventHandler @EventHandler(ignoreCancelled = true)
public void onStackableInteract(PlayerArmorStandManipulateEvent event) { public void onStackableInteract(PlayerArmorStandManipulateEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
if (!plugin.getWorldManager().isIslandWorld(player.getWorld())) return; if (!plugin.getWorldManager().isIslandWorld(player.getWorld())) return;
@ -213,18 +202,20 @@ public class Entity implements Listener {
} }
} }
@EventHandler @EventHandler(ignoreCancelled = true)
public void onHangingPlace(HangingPlaceEvent event) { public void onHangingPlace(HangingPlaceEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
if (!plugin.getWorldManager().isIslandWorld(player.getWorld())) return; if (player != null) {
IslandManager islandManager = plugin.getIslandManager(); if (!plugin.getWorldManager().isIslandWorld(player.getWorld())) return;
IslandManager islandManager = plugin.getIslandManager();
// Check permissions. // Check permissions.
plugin.getPermissionManager().processPermission(event, player, plugin.getPermissionManager().processPermission(event, player,
islandManager.getIslandAtLocation(event.getEntity().getLocation())); islandManager.getIslandAtLocation(event.getEntity().getLocation()));
}
} }
@EventHandler @EventHandler(ignoreCancelled = true)
public void onHangingBreak(HangingBreakEvent event) { public void onHangingBreak(HangingBreakEvent event) {
Hanging hanging = event.getEntity(); Hanging hanging = event.getEntity();
@ -236,7 +227,7 @@ public class Entity implements Listener {
islandManager.getIslandAtLocation(hanging.getLocation())); islandManager.getIslandAtLocation(hanging.getLocation()));
} }
@EventHandler @EventHandler(ignoreCancelled = true)
public void onHangingBreak(HangingBreakByEntityEvent event) { public void onHangingBreak(HangingBreakByEntityEvent event) {
Hanging hanging = event.getEntity(); Hanging hanging = event.getEntity();
@ -252,7 +243,7 @@ public class Entity implements Listener {
islandManager.getIslandAtLocation(hanging.getLocation())); islandManager.getIslandAtLocation(hanging.getLocation()));
} }
@EventHandler @EventHandler(ignoreCancelled = true)
public void onEntityTaming(EntityTameEvent event) { public void onEntityTaming(EntityTameEvent event) {
if (!(event.getOwner() instanceof Player)) if (!(event.getOwner() instanceof Player))
return; return;
@ -267,8 +258,7 @@ public class Entity implements Listener {
islandManager.getIslandAtLocation(entity.getLocation())); islandManager.getIslandAtLocation(entity.getLocation()));
} }
@EventHandler @EventHandler(ignoreCancelled = true)
@SuppressWarnings("deprecation")
public void onEntityChangeBlock(EntityChangeBlockEvent event) { public void onEntityChangeBlock(EntityChangeBlockEvent event) {
org.bukkit.entity.Entity entity = event.getEntity(); org.bukkit.entity.Entity entity = event.getEntity();
@ -315,7 +305,7 @@ public class Entity implements Listener {
try { try {
Method getBlockDataMethod = FallingBlock.class.getMethod("getBlockData"); Method getBlockDataMethod = FallingBlock.class.getMethod("getBlockData");
byte data = (byte) getBlockDataMethod.invoke(fallingBlock); byte data = (byte) getBlockDataMethod.invoke(fallingBlock);
if (fallingBlock.getMaterial().name().endsWith("ANVIL")) { if (fallingBlock.getMaterial().name().endsWith("ANVIL")) { // TODO Reflection
data = (byte) Math.ceil(data / 4.0); data = (byte) Math.ceil(data / 4.0);
} }
fallingBlock.getWorld().dropItemNaturally(fallingBlock.getLocation(), new ItemStack(fallingBlock.getMaterial(), 1, data)); fallingBlock.getWorld().dropItemNaturally(fallingBlock.getLocation(), new ItemStack(fallingBlock.getMaterial(), 1, data));
@ -346,7 +336,7 @@ public class Entity implements Listener {
removeBlockFromLevel(island, block); removeBlockFromLevel(island, block);
CompatibleMaterial materials; CompatibleMaterial materials;
if (event.getTo() != null && event.getTo() != Material.AIR) { if (event.getTo() != Material.AIR) {
materials = CompatibleMaterial.getBlockMaterial(event.getTo()); materials = CompatibleMaterial.getBlockMaterial(event.getTo());
; ;
@ -364,7 +354,7 @@ public class Entity implements Listener {
} }
@EventHandler(priority = EventPriority.LOW) @EventHandler(ignoreCancelled = true, priority = EventPriority.LOW)
public void onEntityExplode(EntityExplodeEvent event) { public void onEntityExplode(EntityExplodeEvent event) {
org.bukkit.entity.Entity entity = event.getEntity(); org.bukkit.entity.Entity entity = event.getEntity();
@ -463,7 +453,7 @@ public class Entity implements Listener {
removeBlockFromLevel(island, CompatibleMaterial.getBlockMaterial(block.getType())); removeBlockFromLevel(island, CompatibleMaterial.getBlockMaterial(block.getType()));
} }
@EventHandler(priority = EventPriority.MONITOR) @EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
public void onEntityDeath(EntityDeathEvent event) { public void onEntityDeath(EntityDeathEvent event) {
LivingEntity livingEntity = event.getEntity(); LivingEntity livingEntity = event.getEntity();
@ -472,20 +462,20 @@ public class Entity implements Listener {
return; return;
} }
if (NMSUtil.getVersionNumber() > 9) { if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_10)) {
if (livingEntity instanceof Donkey || livingEntity instanceof Mule || livingEntity instanceof ElderGuardian) if (livingEntity instanceof Donkey || livingEntity instanceof Mule || livingEntity instanceof ElderGuardian)
return; return;
} }
if (NMSUtil.getVersionNumber() > 10) { if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_11)) {
if (livingEntity instanceof Evoker) return; if (livingEntity instanceof Evoker) return;
} }
if (NMSUtil.getVersionNumber() > 11) { if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_12)) {
if (livingEntity instanceof Llama) return; if (livingEntity instanceof Llama) return;
} }
if (NMSUtil.getVersionNumber() > 13) { if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_14)) {
if (livingEntity instanceof Ravager || livingEntity instanceof Illager) return; if (livingEntity instanceof Ravager || livingEntity instanceof Illager) return;
} }
@ -528,7 +518,7 @@ public class Entity implements Listener {
} }
} }
@EventHandler @EventHandler(ignoreCancelled = true)
public void onEntityTargetLivingEntity(EntityTargetLivingEntityEvent event) { public void onEntityTargetLivingEntity(EntityTargetLivingEntityEvent event) {
if (!(event.getTarget() instanceof Player)) if (!(event.getTarget() instanceof Player))
return; return;
@ -555,8 +545,7 @@ public class Entity implements Listener {
} }
@SuppressWarnings("deprecation") @EventHandler(ignoreCancelled = true)
@EventHandler
public void onCreatureSpawn(CreatureSpawnEvent event) { public void onCreatureSpawn(CreatureSpawnEvent event) {
LivingEntity entity = event.getEntity(); LivingEntity entity = event.getEntity();
if (entity instanceof ArmorStand) return; if (entity instanceof ArmorStand) return;
@ -601,10 +590,10 @@ public class Entity implements Listener {
return; return;
} }
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> { Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> {
if (NMSUtil.getVersionNumber() > 10) { // getPassengers() was added in 1.11 if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_11)) { // getPassengers() was added in 1.11
for (org.bukkit.entity.Entity passenger : entity.getPassengers()) for (org.bukkit.entity.Entity passenger : entity.getPassengers())
passenger.remove(); passenger.remove();
} else { } else { // TODO Reflection
if (entity.getPassenger() != null) entity.getPassenger().remove(); if (entity.getPassenger() != null) entity.getPassenger().remove();
} }
entity.remove(); entity.remove();
@ -612,7 +601,7 @@ public class Entity implements Listener {
event.setCancelled(true); // For other plugin API reasons. event.setCancelled(true); // For other plugin API reasons.
} }
@EventHandler @EventHandler(ignoreCancelled = true)
public void onDamageVehicle(VehicleDamageEvent event) { public void onDamageVehicle(VehicleDamageEvent event) {
if (!(event.getAttacker() instanceof Player)) { if (!(event.getAttacker() instanceof Player)) {
IslandManager islandManager = plugin.getIslandManager(); IslandManager islandManager = plugin.getIslandManager();
@ -620,7 +609,7 @@ public class Entity implements Listener {
} }
} }
@EventHandler @EventHandler(ignoreCancelled = true)
public void onDestroyVehicle(VehicleDestroyEvent event) { public void onDestroyVehicle(VehicleDestroyEvent event) {
if (!(event.getAttacker() instanceof Player)) { if (!(event.getAttacker() instanceof Player)) {
IslandManager islandManager = plugin.getIslandManager(); IslandManager islandManager = plugin.getIslandManager();