Add UtilListener to track player interactions with entities

The UtilListener class has been added to the project. It provides various event handlers which tag entities (such as ExplosiveMinecart, TNTPrimed, EnderCrystal) with the UUID of the player who either placed, nudged, primed or hit the entity. Changes have also been made to the Target class, adding methods for getting and assigning a player source ID for entities.
This commit is contained in:
2024-03-02 00:40:35 +01:00
parent 58a4a9f25d
commit 7b155d0b3e
3 changed files with 105 additions and 1 deletions
@@ -1,9 +1,13 @@
package me.youhavetrouble.preventstabby.data;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.entity.Tameable;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataHolder;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;
import javax.annotation.Nullable;
@@ -11,6 +15,8 @@ import java.util.UUID;
public class Target {
public static final NamespacedKey playerSourceIdKey = new NamespacedKey("preventstabby", "playerSource");
/**
* The unique identifier for a player.
*/
@@ -59,7 +65,27 @@ public class Target {
}
}
return null;
// Try to get player's id from other various entities
PersistentDataContainer pdc = entity.getPersistentDataContainer();
if (!pdc.has(playerSourceIdKey)) return null;
String id = pdc.get(playerSourceIdKey, PersistentDataType.STRING);
if (id == null) return null;
try {
UUID uuid = UUID.fromString(id);
return new Target(uuid, EntityClassifier.OTHER);
} catch (Exception e) {
return null;
}
}
/**
* Assigns the player source ID to the given data holder.
* @param dataHolder The persistent data holder to assign the player source ID to.
* @param id The UUID of the player source ID to assign.
*/
public static void assignPlayerSourceId(@NotNull PersistentDataHolder dataHolder, @NotNull UUID id) {
dataHolder.getPersistentDataContainer().set(playerSourceIdKey, PersistentDataType.STRING, id.toString());
}
public enum EntityClassifier {