Add EntityChangePreLogEvent

This commit is contained in:
Brokkonaut
2021-08-26 20:54:55 +02:00
parent 597b1831f1
commit 610f3edcab
3 changed files with 84 additions and 8 deletions

View File

@@ -53,6 +53,7 @@ import de.diddiz.LogBlock.blockstate.BlockStateCodecSign;
import de.diddiz.LogBlock.blockstate.BlockStateCodecs;
import de.diddiz.LogBlock.config.Config;
import de.diddiz.LogBlock.events.BlockChangePreLogEvent;
import de.diddiz.LogBlock.events.EntityChangePreLogEvent;
import de.diddiz.util.BukkitUtils;
import de.diddiz.util.Utils;
@@ -731,10 +732,31 @@ public class Consumer extends Thread {
addQueueLast(new BlockRow(loc, actor, replacedMaterialId, replacedStateId, Utils.serializeYamlConfiguration(stateBefore), typeMaterialId, typeStateId, Utils.serializeYamlConfiguration(stateAfter), ca));
}
public void queueEntityModification(Actor actor, UUID entityId, EntityType entityType, Location loc, EntityChangeType changeType, YamlConfiguration data) {
if (actor == null || loc == null || changeType == null || entityId == null || entityType == null || hiddenPlayers.contains(actor.getName().toLowerCase()) || !isLogged(loc.getWorld())) {
public void queueEntityModification(Actor actor, Entity entity, EntityChangeType changeType, YamlConfiguration data) {
if (actor == null || changeType == null || entity == null || hiddenPlayers.contains(actor.getName().toLowerCase()) || !isLogged(entity.getWorld())) {
return;
}
UUID entityId = entity.getUniqueId();
EntityType entityType = entity.getType();
Location loc = entity.getLocation();
if (EntityChangePreLogEvent.getHandlerList().getRegisteredListeners().length > 0) {
// Create and call the event
EntityChangePreLogEvent event = new EntityChangePreLogEvent(actor, loc, entity, changeType, data);
logblock.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
return;
}
// Update variables
actor = event.getOwnerActor();
loc = event.getLocation();
}
// Do this last so LogBlock still has final say in what is being added
if (actor == null || loc == null || hiddenPlayers.contains(actor.getName().toLowerCase()) || !isLogged(loc.getWorld())) {
return;
}
addQueueLast(new EntityRow(loc, actor, entityType, entityId, changeType, Utils.serializeYamlConfiguration(data)));
}

View File

@@ -0,0 +1,54 @@
package de.diddiz.LogBlock.events;
import de.diddiz.LogBlock.Actor;
import de.diddiz.LogBlock.EntityChange.EntityChangeType;
import org.bukkit.Location;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Entity;
import org.bukkit.event.HandlerList;
public class EntityChangePreLogEvent extends PreLogEvent {
private static final HandlerList handlers = new HandlerList();
private Location location;
private Entity entity;
private EntityChangeType changeType;
private YamlConfiguration changeData;
public EntityChangePreLogEvent(Actor owner, Location location, Entity entity, EntityChangeType changeType, YamlConfiguration changeData) {
super(owner);
this.location = location;
this.entity = entity;
this.changeType = changeType;
this.changeData = changeData;
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
public Entity getEntity() {
return entity;
}
public EntityChangeType getChangeType() {
return changeType;
}
public YamlConfiguration getChangeData() {
return changeData;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@@ -131,7 +131,7 @@ public class AdvancedEntityLogging extends LoggingListener {
inHand = inHand.clone();
inHand.setAmount(1);
data.set("item", inHand);
consumer.queueEntityModification(actor, entity.getUniqueId(), entity.getType(), entity.getLocation(), EntityChange.EntityChangeType.ADDEQUIP, data);
consumer.queueEntityModification(actor, entity, EntityChange.EntityChangeType.ADDEQUIP, data);
}
}
}
@@ -246,7 +246,7 @@ public class AdvancedEntityLogging extends LoggingListener {
}
YamlConfiguration data = new YamlConfiguration();
data.set("item", oldItem);
consumer.queueEntityModification(actor, entity.getUniqueId(), entity.getType(), entity.getLocation(), EntityChange.EntityChangeType.REMOVEEQUIP, data);
consumer.queueEntityModification(actor, entity, EntityChange.EntityChangeType.REMOVEEQUIP, data);
}
}
}
@@ -274,7 +274,7 @@ public class AdvancedEntityLogging extends LoggingListener {
if (damager instanceof Bee && !((Bee) damager).hasStung()) {
if (Config.isLogging(damager.getWorld(), EntityLogging.MODIFY, damager)) {
Actor actor = Actor.actorFromEntity(event.getEntity());
consumer.queueEntityModification(actor, damager.getUniqueId(), damager.getType(), damager.getLocation(), EntityChange.EntityChangeType.GET_STUNG, null);
consumer.queueEntityModification(actor, damager, EntityChange.EntityChangeType.GET_STUNG, null);
}
}
}
@@ -295,13 +295,13 @@ public class AdvancedEntityLogging extends LoggingListener {
YamlConfiguration data = new YamlConfiguration();
data.set("item", oldItem);
data.set("slot", event.getSlot().name());
consumer.queueEntityModification(actor, entity.getUniqueId(), entity.getType(), entity.getLocation(), EntityChange.EntityChangeType.REMOVEEQUIP, data);
consumer.queueEntityModification(actor, entity, EntityChange.EntityChangeType.REMOVEEQUIP, data);
}
if (!newEmpty) {
YamlConfiguration data = new YamlConfiguration();
data.set("item", newItem);
data.set("slot", event.getSlot().name());
consumer.queueEntityModification(actor, entity.getUniqueId(), entity.getType(), entity.getLocation(), EntityChange.EntityChangeType.ADDEQUIP, data);
consumer.queueEntityModification(actor, entity, EntityChange.EntityChangeType.ADDEQUIP, data);
}
}
}
@@ -319,6 +319,6 @@ public class AdvancedEntityLogging extends LoggingListener {
} else {
data.set("worldedit", WorldEditHelper.serializeEntity(entity));
}
consumer.queueEntityModification(actor, entity.getUniqueId(), entity.getType(), location, changeType, data);
consumer.queueEntityModification(actor, entity, changeType, data);
}
}