Small optimizations in Entity Listener

This commit is contained in:
Fabrizio La Rosa 2020-07-12 11:21:45 +02:00
parent dbae4df308
commit 251a193195

View File

@ -37,6 +37,7 @@ import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.io.File; import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.*; import java.util.*;
@ -67,11 +68,10 @@ public class Entity implements Listener {
FileConfiguration configLoad = config.getFileConfiguration(); FileConfiguration configLoad = config.getFileConfiguration();
if (configLoad.getBoolean("Island.Nether.BlazeImmuneToWaterInNether", false) && if (configLoad.getBoolean("Island.Nether.BlazeImmuneToWaterInNether", false) &&
worldManager.getIslandWorld(event.getEntity().getWorld()).equals(IslandWorld.Nether)){ worldManager.getIslandWorld(event.getEntity().getWorld()).equals(IslandWorld.Nether) &&
if(event.getCause().equals(DamageCause.DROWNING)){ event.getCause().equals(DamageCause.DROWNING)) {
event.setCancelled(true); event.setCancelled(true);
} }
}
} else if (event.getEntity() instanceof Player) { } else if (event.getEntity() instanceof Player) {
Player player = (Player) event.getEntity(); Player player = (Player) event.getEntity();
@ -121,17 +121,14 @@ public class Entity implements Listener {
// Fix a bug in minecraft where arrows with flame still apply fire ticks even if // Fix a bug in minecraft where arrows with flame still apply fire ticks even if
// the shot entity isn't damaged // the shot entity isn't damaged
if (event.isCancelled() && event.getDamager() instanceof Arrow) { if (event.isCancelled() && event.getDamager() instanceof Arrow && event.getDamager().getFireTicks() != 0) {
Arrow arrow = (Arrow) event.getDamager();
if (arrow.getFireTicks() != 0) {
preventFireTicks.add(event.getEntity().getUniqueId()); preventFireTicks.add(event.getEntity().getUniqueId());
Bukkit.getScheduler().runTaskLater(plugin, Bukkit.getScheduler().runTaskLater(plugin,
() -> preventFireTicks.remove(event.getEntity().getUniqueId()), () -> preventFireTicks.remove(victim.getUniqueId()),
5L); 5L);
} }
} }
} }
}
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void onPlayerShearEntity(PlayerShearEntityEvent event) { public void onPlayerShearEntity(PlayerShearEntityEvent event) {
@ -298,18 +295,19 @@ public class Entity implements Listener {
&& configLoad.getBoolean("Island.Spawn.Protection")) { && configLoad.getBoolean("Island.Spawn.Protection")) {
FallingBlock fallingBlock = (FallingBlock) event.getEntity(); FallingBlock fallingBlock = (FallingBlock) event.getEntity();
if (fallingBlock.getDropItem()) { if (fallingBlock.getDropItem()) {
if (NMSUtil.getVersionNumber() > 12) { if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13)) {
fallingBlock.getWorld().dropItemNaturally(fallingBlock.getLocation(), fallingBlock.getWorld().dropItemNaturally(fallingBlock.getLocation(),
new ItemStack(fallingBlock.getBlockData().getMaterial(), 1)); new ItemStack(fallingBlock.getBlockData().getMaterial(), 1));
} else { } else {
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")) { // TODO Reflection if (fallingBlock.getMaterial().name().endsWith("ANVIL")) {
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));
} catch (Exception ignored) { } catch(NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
} }
} }
} }