Refactor damage source checking and restructure player management

The source of damage detection for PvP interactions has been updated to improve accuracy and flexibility. A 'Target' sub-class has been implemented in the PlayerManager class, removing the need for a separate DamageCheck class. The PlayerManager class was also moved to the 'data' package. Distribution of feedback messages has been simplified using the new DamageCheckResult class.
This commit is contained in:
2024-02-24 19:23:54 +01:00
parent b9e2aefc40
commit 1aeb245666
16 changed files with 266 additions and 167 deletions
@@ -0,0 +1,71 @@
package me.youhavetrouble.preventstabby.data;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.entity.Tameable;
import org.jetbrains.annotations.NotNull;
import javax.annotation.Nullable;
import java.util.UUID;
public class Target {
/**
* The unique identifier for a player.
*/
public final UUID playerUuid;
/**
* Represents the entity classifier of a target.
* This indicates what type of entity player's id was assumed from
*/
public final EntityClassifier classifier;
private Target(UUID uuid, EntityClassifier classifier) {
this.playerUuid = uuid;
this.classifier = classifier;
}
/**
* Get the UUID of the actual player, being owner of a pet, shooter of a projectile, etc.
*
* @param entity Base entity to get the player UUID from
* @return UUID of the actual player, null if not found
*/
@Nullable
public static Target getTarget(@NotNull Entity entity) {
if (entity instanceof Player) return new Target(entity.getUniqueId(), EntityClassifier.PLAYER);
// Get shooter of projectile
if (entity instanceof Projectile projectile) {
if (projectile.getShooter() instanceof Player shooter) {
return new Target(shooter.getUniqueId(), EntityClassifier.PLAYER);
}
}
// Get player riding mount
if (!entity.getPassengers().isEmpty()) {
Entity passenger = entity.getPassengers().get(0);
if (passenger instanceof Player) {
return new Target(passenger.getUniqueId(), EntityClassifier.MOUNT);
}
}
// Get owner of tamed entity
if (entity instanceof Tameable tameable) {
if (tameable.getOwner() != null) {
return new Target(tameable.getOwner().getUniqueId(), EntityClassifier.PET);
}
}
return null;
}
public enum EntityClassifier {
PLAYER,
PET,
MOUNT,
OTHER
}
}