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.Map;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.attribute.Attributable;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
@ -59,6 +61,7 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.projectiles.ProjectileSource;
import net.coreprotect.CoreProtect;
import net.coreprotect.bukkit.BukkitAdapter;
import net.coreprotect.config.Config;
import net.coreprotect.consumer.Queue;
@ -66,19 +69,55 @@ import net.coreprotect.utility.serialize.ItemMetaHandler;
public final class EntityDeathListener extends Queue implements Listener {
@EventHandler(priority = EventPriority.MONITOR)
public void onEntityDeath(EntityDeathEvent event) {
LivingEntity entity = event.getEntity();
if (entity == null) {
public static void parseEntityKills(String message) {
message = message.trim().toLowerCase(Locale.ROOT);
if (!message.contains(" ")) {
return;
}
if (Config.getConfig(entity.getWorld()).ENTITY_KILLS) {
EntityDamageEvent damage = entity.getLastDamageCause();
if (damage != null) {
String e = "";
boolean skip = true;
String[] args = message.split(" ");
if (args.length < 2 || !args[0].replaceFirst("/", "").equals("kill") || !args[1].startsWith("@e")) {
return;
}
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))) {
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;
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());
}
}
*/
}