Minor refactoring in EntityDeathListener

This commit is contained in:
Intelli 2022-03-01 20:17:12 -07:00
parent 7c7fd200d2
commit a6869176be
2 changed files with 458 additions and 395 deletions

View File

@ -5,9 +5,11 @@ import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.Color; import org.bukkit.Color;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.attribute.Attributable; import org.bukkit.attribute.Attributable;
import org.bukkit.attribute.Attribute; import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance; import org.bukkit.attribute.AttributeInstance;
@ -59,6 +61,7 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta; import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.projectiles.ProjectileSource; import org.bukkit.projectiles.ProjectileSource;
import net.coreprotect.CoreProtect;
import net.coreprotect.bukkit.BukkitAdapter; import net.coreprotect.bukkit.BukkitAdapter;
import net.coreprotect.config.Config; import net.coreprotect.config.Config;
import net.coreprotect.consumer.Queue; import net.coreprotect.consumer.Queue;
@ -66,19 +69,55 @@ import net.coreprotect.utility.serialize.ItemMetaHandler;
public final class EntityDeathListener extends Queue implements Listener { public final class EntityDeathListener extends Queue implements Listener {
@EventHandler(priority = EventPriority.MONITOR) public static void parseEntityKills(String message) {
public void onEntityDeath(EntityDeathEvent event) { message = message.trim().toLowerCase(Locale.ROOT);
LivingEntity entity = event.getEntity(); if (!message.contains(" ")) {
if (entity == null) {
return; return;
} }
if (Config.getConfig(entity.getWorld()).ENTITY_KILLS) { String[] args = message.split(" ");
EntityDamageEvent damage = entity.getLastDamageCause(); if (args.length < 2 || !args[0].replaceFirst("/", "").equals("kill") || !args[1].startsWith("@e")) {
if (damage != null) { return;
String e = ""; }
boolean skip = true;
List<LivingEntity> entityList = new ArrayList<>();
for (World world : Bukkit.getWorlds()) {
List<LivingEntity> livingEntities = world.getLivingEntities();
for (LivingEntity entity : livingEntities) {
if (entity instanceof Player) {
continue;
}
if (entity.isValid()) {
entityList.add(entity);
}
}
}
Bukkit.getScheduler().runTask(CoreProtect.getInstance(), () -> {
for (LivingEntity entity : entityList) {
if (entity != null && entity.isDead()) {
logEntityDeath(entity, "#command");
}
}
});
}
protected static void logEntityDeath(LivingEntity entity, String e) {
if (!Config.getConfig(entity.getWorld()).ENTITY_KILLS) {
return;
}
EntityDamageEvent damage = entity.getLastDamageCause();
if (damage == null) {
return;
}
if (e == null) {
e = "";
}
boolean skip = true;
if (!Config.getConfig(entity.getWorld()).SKIP_GENERIC_DATA || (!(entity instanceof Zombie) && !(entity instanceof Skeleton))) { if (!Config.getConfig(entity.getWorld()).SKIP_GENERIC_DATA || (!(entity instanceof Zombie) && !(entity instanceof Skeleton))) {
skip = false; skip = false;
} }
@ -467,6 +506,14 @@ public final class EntityDeathListener extends Queue implements Listener {
} }
} }
} }
@EventHandler(priority = EventPriority.MONITOR)
public void onEntityDeath(EntityDeathEvent event) {
LivingEntity entity = event.getEntity();
if (entity == null) {
return;
} }
logEntityDeath(entity, null);
} }
} }

View File

@ -18,5 +18,21 @@ public final class PlayerCommandListener extends Queue implements Listener {
long timestamp = System.currentTimeMillis() / 1000L; long timestamp = System.currentTimeMillis() / 1000L;
Queue.queuePlayerCommand(player, event.getMessage(), timestamp); Queue.queuePlayerCommand(player, event.getMessage(), timestamp);
} }
/*
if (Config.getGlobal().ENTITY_KILLS && player.hasPermission("bukkit.command.kill")) {
EntityDeathListener.parseEntityKills(event.getMessage());
} }
*/
}
/*
@EventHandler(priority = EventPriority.MONITOR)
public void onServerCommand(ServerCommandEvent event) {
if (Config.getGlobal().ENTITY_KILLS && event.getCommand().toLowerCase(Locale.ROOT).startsWith("kill")) {
EntityDeathListener.parseEntityKills(event.getCommand());
}
}
*/
} }