more fixes for #15

This commit is contained in:
2023-05-20 12:06:16 +02:00
parent aac1f99374
commit 8fcf34e310
3 changed files with 41 additions and 37 deletions
@@ -3,6 +3,7 @@ package me.youhavetrouble.preventstabby.listeners.pets;
import me.youhavetrouble.preventstabby.PreventStabby;
import me.youhavetrouble.preventstabby.players.SmartCache;
import me.youhavetrouble.preventstabby.util.PreventStabbyListener;
import me.youhavetrouble.preventstabby.util.PvpState;
import me.youhavetrouble.preventstabby.util.Util;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Tameable;
@@ -14,8 +15,12 @@ import org.bukkit.event.Listener;
@PreventStabbyListener
public class PetTargettingMountListener implements Listener {
/**
* TODO - this needs to pass canDamage() in the future, for now it just checks forced pvp state directly
*/
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onWolfAttackMount(org.bukkit.event.entity.EntityDamageByEntityEvent event) {
if (PreventStabby.getPlugin().getPlayerManager().getForcedPvpState() == PvpState.ENABLED) return;
if (!(event.getDamager() instanceof Tameable) || event.getEntity().getPassengers().isEmpty()) return;
Tameable damager = (Tameable) event.getDamager();
Entity victim = event.getEntity();
@@ -1,7 +1,9 @@
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.PreventStabbyListener;
import org.bukkit.entity.Tameable;
import org.bukkit.entity.Wolf;
@@ -14,23 +16,21 @@ public class PetTargettingPetListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onWolfAttackWolf(org.bukkit.event.entity.EntityDamageByEntityEvent event) {
if (event.getDamager() instanceof Tameable && event.getEntity() instanceof Tameable) {
if (!(event.getDamager() instanceof Tameable) || !(event.getEntity() instanceof Tameable)) return;
Tameable damager = (Tameable) event.getDamager();
Tameable victim = (Tameable) event.getEntity();
if (damager.getOwner() == null || victim.getOwner() == null) return;
SmartCache smartCache = PreventStabby.getPlugin().getSmartCache();
boolean damagerPvpEnabled = smartCache.getPlayerData(damager.getOwner().getUniqueId()).isPvpEnabled();
boolean victimPvpEnabled = smartCache.getPlayerData(victim.getOwner().getUniqueId()).isPvpEnabled();
if (!victimPvpEnabled || !damagerPvpEnabled) {
if (damager instanceof Wolf) {
Wolf wolf = (Wolf) damager;
wolf.setAngry(false);
}
if (!PreventStabby.getPlugin().getPlayerManager()
.canDamage(
damager.getUniqueId(),
victim.getUniqueId(),
false,
false
))
event.setCancelled(true);
}
}
}
}
@@ -23,7 +23,7 @@ public class PlayerHitPetWithProjectileListener implements Listener {
*/
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPlayerHitPetWithProjectile(org.bukkit.event.entity.EntityDamageByEntityEvent event) {
if (event.getEntity() instanceof Tameable && event.getDamager() instanceof Projectile) {
if (!(event.getEntity() instanceof Tameable) || !(event.getDamager() instanceof Projectile)) return;
Projectile projectile = (Projectile) event.getDamager();
if (!(projectile.getShooter() instanceof Player)) return;
Tameable tameable = (Tameable) event.getEntity();
@@ -35,20 +35,19 @@ public class PlayerHitPetWithProjectileListener implements Listener {
if (damager.equals(victim)) return;
ConfigCache config = PreventStabby.getPlugin().getConfigCache();
SmartCache smartCache = PreventStabby.getPlugin().getSmartCache();
if (!smartCache.getPlayerData(damager).isPvpEnabled()) {
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 (PreventStabby.getPlugin().getPlayerManager()
.canDamage(
damager,
victim,
config.getCannot_attack_pets_attacker(),
config.getCannot_attack_pets_victim(),
false
))
CombatTimer.refreshPlayersCombatTime(damager);
else
event.setCancelled(true);
}
}
}