mirror of
https://github.com/YouHaveTrouble/PreventStabby.git
synced 2026-05-12 13:26:56 +00:00
potential fix for #15
This commit is contained in:
+21
-23
@@ -2,15 +2,14 @@ package me.youhavetrouble.preventstabby.listeners.pets;
|
||||
|
||||
import me.youhavetrouble.preventstabby.PreventStabby;
|
||||
import me.youhavetrouble.preventstabby.config.ConfigCache;
|
||||
import me.youhavetrouble.preventstabby.players.SmartCache;
|
||||
import me.youhavetrouble.preventstabby.util.CombatTimer;
|
||||
import me.youhavetrouble.preventstabby.util.PluginMessages;
|
||||
import me.youhavetrouble.preventstabby.util.PreventStabbyListener;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.entity.Tameable;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@@ -18,32 +17,31 @@ import java.util.UUID;
|
||||
public class PlayerAttackPetListener implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
public void onPlayerAttackPet(org.bukkit.event.entity.EntityDamageByEntityEvent event) {
|
||||
public void onPlayerAttackPet(EntityDamageByEntityEvent event) {
|
||||
|
||||
if (event.getDamager() instanceof Player && event.getEntity() instanceof Tameable) {
|
||||
SmartCache smartCache = PreventStabby.getPlugin().getSmartCache();
|
||||
Tameable tameable = (Tameable) event.getEntity();
|
||||
if (tameable.getOwner() == null) return;
|
||||
if (!(event.getDamager() instanceof Player) || !(event.getEntity() instanceof Tameable)) return;
|
||||
|
||||
UUID damager = event.getDamager().getUniqueId();
|
||||
UUID victim = tameable.getOwner().getUniqueId();
|
||||
Tameable tameable = (Tameable) event.getEntity();
|
||||
if (tameable.getOwner() == null) return;
|
||||
|
||||
if (damager.equals(victim)) return;
|
||||
UUID damager = event.getDamager().getUniqueId();
|
||||
UUID victim = tameable.getOwner().getUniqueId();
|
||||
|
||||
ConfigCache config = PreventStabby.getPlugin().getConfigCache();
|
||||
boolean damagerPvpState = PreventStabby.getPlugin().getPlayerManager().getPlayerPvPState(damager);
|
||||
if (!damagerPvpState) {
|
||||
PluginMessages.sendActionBar(damager, config.getCannot_attack_pets_attacker());
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
if (!smartCache.getPlayerData(victim).isPvpEnabled()) {
|
||||
PluginMessages.sendActionBar(damager, config.getCannot_attack_pets_victim());
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
if (damager.equals(victim)) return;
|
||||
|
||||
ConfigCache config = PreventStabby.getPlugin().getConfigCache();
|
||||
|
||||
if (PreventStabby.getPlugin().getPlayerManager()
|
||||
.canDamage(
|
||||
damager,
|
||||
victim,
|
||||
config.getCannot_attack_pets_attacker(),
|
||||
config.getCannot_attack_pets_victim(),
|
||||
false
|
||||
))
|
||||
CombatTimer.refreshPlayersCombatTime(damager);
|
||||
else
|
||||
event.setCancelled(true);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.time.Instant;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@@ -142,6 +143,22 @@ public class PlayerManager {
|
||||
* @return Whenever attacker can harm the victim.
|
||||
*/
|
||||
public boolean canDamage(UUID attacker, UUID victim, boolean sendDenyMessage, boolean checkVictimSpawnProtection) {
|
||||
ConfigCache config = PreventStabby.getPlugin().getConfigCache();
|
||||
String attackerMessage = sendDenyMessage ? config.getCannot_attack_attacker() : null;
|
||||
String victimMessage = sendDenyMessage ? config.getCannot_attack_victim() : null;
|
||||
return canDamage(attacker, victim, attackerMessage, victimMessage, checkVictimSpawnProtection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if attacker can harm the victim.
|
||||
* @param attacker Atacker's UUID.
|
||||
* @param victim Victim's UUID.
|
||||
* @param attackerDenyMessage Message that action was denied to the attacker.
|
||||
* @param victimDenyMessage Message that action was denied to the victim.
|
||||
* @param checkVictimSpawnProtection Should teleport and spawn protections be taken into account?
|
||||
* @return Whenever attacker can harm the victim.
|
||||
*/
|
||||
public boolean canDamage(UUID attacker, UUID victim, @Nullable String attackerDenyMessage, @Nullable String victimDenyMessage, boolean checkVictimSpawnProtection) {
|
||||
|
||||
if (hasLoginProtection(attacker) || hasTeleportProtection(attacker)) return false;
|
||||
if (checkVictimSpawnProtection && hasLoginProtection(victim)) return false;
|
||||
@@ -165,9 +182,8 @@ public class PlayerManager {
|
||||
if (PreventStabby.worldGuardHookEnabled() && attackerPlayer != null && WorldGuardHook.isPlayerForcedToPvp(attackerPlayer))
|
||||
return true;
|
||||
|
||||
if (sendDenyMessage) {
|
||||
ConfigCache config = PreventStabby.getPlugin().getConfigCache();
|
||||
PluginMessages.sendActionBar(attacker, config.getCannot_attack_attacker());
|
||||
if (attackerDenyMessage != null) {
|
||||
PluginMessages.sendActionBar(attacker, attackerDenyMessage);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -176,7 +192,7 @@ public class PlayerManager {
|
||||
if (PreventStabby.worldGuardHookEnabled() && victimPlayer != null && WorldGuardHook.isPlayerForcedToPvp(victimPlayer))
|
||||
return true;
|
||||
|
||||
if (sendDenyMessage) {
|
||||
if (victimDenyMessage != null) {
|
||||
ConfigCache config = PreventStabby.getPlugin().getConfigCache();
|
||||
PluginMessages.sendActionBar(attacker, config.getCannot_attack_victim());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user