mirror of
https://github.com/YouHaveTrouble/PreventStabby.git
synced 2026-05-12 05:16:55 +00:00
potential fix for #15
This commit is contained in:
+15
-17
@@ -2,15 +2,14 @@ package me.youhavetrouble.preventstabby.listeners.pets;
|
|||||||
|
|
||||||
import me.youhavetrouble.preventstabby.PreventStabby;
|
import me.youhavetrouble.preventstabby.PreventStabby;
|
||||||
import me.youhavetrouble.preventstabby.config.ConfigCache;
|
import me.youhavetrouble.preventstabby.config.ConfigCache;
|
||||||
import me.youhavetrouble.preventstabby.players.SmartCache;
|
|
||||||
import me.youhavetrouble.preventstabby.util.CombatTimer;
|
import me.youhavetrouble.preventstabby.util.CombatTimer;
|
||||||
import me.youhavetrouble.preventstabby.util.PluginMessages;
|
|
||||||
import me.youhavetrouble.preventstabby.util.PreventStabbyListener;
|
import me.youhavetrouble.preventstabby.util.PreventStabbyListener;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.entity.Tameable;
|
import org.bukkit.entity.Tameable;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@@ -18,10 +17,10 @@ import java.util.UUID;
|
|||||||
public class PlayerAttackPetListener implements Listener {
|
public class PlayerAttackPetListener implements Listener {
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
@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)) return;
|
||||||
|
|
||||||
if (event.getDamager() instanceof Player && event.getEntity() instanceof Tameable) {
|
|
||||||
SmartCache smartCache = PreventStabby.getPlugin().getSmartCache();
|
|
||||||
Tameable tameable = (Tameable) event.getEntity();
|
Tameable tameable = (Tameable) event.getEntity();
|
||||||
if (tameable.getOwner() == null) return;
|
if (tameable.getOwner() == null) return;
|
||||||
|
|
||||||
@@ -31,19 +30,18 @@ public class PlayerAttackPetListener implements Listener {
|
|||||||
if (damager.equals(victim)) return;
|
if (damager.equals(victim)) return;
|
||||||
|
|
||||||
ConfigCache config = PreventStabby.getPlugin().getConfigCache();
|
ConfigCache config = PreventStabby.getPlugin().getConfigCache();
|
||||||
boolean damagerPvpState = PreventStabby.getPlugin().getPlayerManager().getPlayerPvPState(damager);
|
|
||||||
if (!damagerPvpState) {
|
if (PreventStabby.getPlugin().getPlayerManager()
|
||||||
PluginMessages.sendActionBar(damager, config.getCannot_attack_pets_attacker());
|
.canDamage(
|
||||||
event.setCancelled(true);
|
damager,
|
||||||
return;
|
victim,
|
||||||
}
|
config.getCannot_attack_pets_attacker(),
|
||||||
if (!smartCache.getPlayerData(victim).isPvpEnabled()) {
|
config.getCannot_attack_pets_victim(),
|
||||||
PluginMessages.sendActionBar(damager, config.getCannot_attack_pets_victim());
|
false
|
||||||
event.setCancelled(true);
|
))
|
||||||
return;
|
|
||||||
}
|
|
||||||
CombatTimer.refreshPlayersCombatTime(damager);
|
CombatTimer.refreshPlayersCombatTime(damager);
|
||||||
|
else
|
||||||
|
event.setCancelled(true);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import org.bukkit.Bukkit;
|
|||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.scheduler.BukkitTask;
|
import org.bukkit.scheduler.BukkitTask;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
@@ -142,6 +143,22 @@ public class PlayerManager {
|
|||||||
* @return Whenever attacker can harm the victim.
|
* @return Whenever attacker can harm the victim.
|
||||||
*/
|
*/
|
||||||
public boolean canDamage(UUID attacker, UUID victim, boolean sendDenyMessage, boolean checkVictimSpawnProtection) {
|
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 (hasLoginProtection(attacker) || hasTeleportProtection(attacker)) return false;
|
||||||
if (checkVictimSpawnProtection && hasLoginProtection(victim)) return false;
|
if (checkVictimSpawnProtection && hasLoginProtection(victim)) return false;
|
||||||
@@ -165,9 +182,8 @@ public class PlayerManager {
|
|||||||
if (PreventStabby.worldGuardHookEnabled() && attackerPlayer != null && WorldGuardHook.isPlayerForcedToPvp(attackerPlayer))
|
if (PreventStabby.worldGuardHookEnabled() && attackerPlayer != null && WorldGuardHook.isPlayerForcedToPvp(attackerPlayer))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
if (sendDenyMessage) {
|
if (attackerDenyMessage != null) {
|
||||||
ConfigCache config = PreventStabby.getPlugin().getConfigCache();
|
PluginMessages.sendActionBar(attacker, attackerDenyMessage);
|
||||||
PluginMessages.sendActionBar(attacker, config.getCannot_attack_attacker());
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -176,7 +192,7 @@ public class PlayerManager {
|
|||||||
if (PreventStabby.worldGuardHookEnabled() && victimPlayer != null && WorldGuardHook.isPlayerForcedToPvp(victimPlayer))
|
if (PreventStabby.worldGuardHookEnabled() && victimPlayer != null && WorldGuardHook.isPlayerForcedToPvp(victimPlayer))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
if (sendDenyMessage) {
|
if (victimDenyMessage != null) {
|
||||||
ConfigCache config = PreventStabby.getPlugin().getConfigCache();
|
ConfigCache config = PreventStabby.getPlugin().getConfigCache();
|
||||||
PluginMessages.sendActionBar(attacker, config.getCannot_attack_victim());
|
PluginMessages.sendActionBar(attacker, config.getCannot_attack_victim());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user