<commit_before>package net.piratjsk.saddles.listeners;

import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Horse;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.inventory.ItemStack;

import net.piratjsk.saddles.Saddles;

public final class HorseListener implements Listener {

    @EventHandler
    public void onHorseAccess(final PlayerInteractEntityEvent event) {
        if (event.getRightClicked() instanceof Horse) {
            final Horse horse = (Horse) event.getRightClicked();
            final ItemStack saddle = horse.getInventory().getSaddle();
            if (saddle!=null && Saddles.isSigned(saddle)) {
                if (!Saddles.hasAccess(saddle, event.getPlayer())) {
                    event.setCancelled(true);
                    final OfflinePlayer owner = Saddles.getOwner(saddle);
                    String name = "undefined";
                    if (owner!=null) {
                        if (owner.hasPlayedBefore()) {
                            name = owner.getName();
                            if (owner.isOnline())
                                name = ((Player) owner).getDisplayName();
                        } else {
                            name = owner.getUniqueId().toString();
                        }
                    }
                    event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', Saddles.noAccessMsg.replaceAll("%owner%", name)));
                }
            }
        }
    }

    @EventHandler
    public void onHorseDamage(final EntityDamageEvent event) {
        if (event.getEntityType().equals(EntityType.HORSE)) {
            final Horse horse = (Horse) event.getEntity();
            final ItemStack saddle = horse.getInventory().getSaddle();
            if (saddle!=null && Saddles.isSigned(saddle)) {
                Entity damager = null;
                if (event instanceof EntityDamageByEntityEvent)
                    damager = ((EntityDamageByEntityEvent) event).getDamager();
                if (horse.getPassenger()!=null) {
                    if (damager != null && damager instanceof Player)
                        if (!Saddles.hasAccess(saddle,damager))
                            event.setCancelled(true);
                } else {
                    if (!Saddles.hasAccess(saddle,damager))
                        event.setCancelled(true);
                }
            }
        }
    }

}<commit_msg>fixed bug with projectiles<commit_after>package net.piratjsk.saddles.listeners;

import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Horse;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.inventory.ItemStack;

import net.piratjsk.saddles.Saddles;

public final class HorseListener implements Listener {

    @EventHandler
    public void onHorseAccess(final PlayerInteractEntityEvent event) {
        if (event.getRightClicked() instanceof Horse) {
            final Horse horse = (Horse) event.getRightClicked();
            final ItemStack saddle = horse.getInventory().getSaddle();
            if (saddle!=null && Saddles.isSigned(saddle)) {
                if (!Saddles.hasAccess(saddle, event.getPlayer())) {
                    event.setCancelled(true);
                    final OfflinePlayer owner = Saddles.getOwner(saddle);
                    String name = "undefined";
                    if (owner!=null) {
                        if (owner.hasPlayedBefore()) {
                            name = owner.getName();
                            if (owner.isOnline())
                                name = ((Player) owner).getDisplayName();
                        } else {
                            name = owner.getUniqueId().toString();
                        }
                    }
                    event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', Saddles.noAccessMsg.replaceAll("%owner%", name)));
                }
            }
        }
    }

    @EventHandler
    public void onHorseDamage(final EntityDamageEvent event) {
        if (event.getEntityType().equals(EntityType.HORSE)) {
            final Horse horse = (Horse) event.getEntity();
            final ItemStack saddle = horse.getInventory().getSaddle();
            if (saddle!=null && Saddles.isSigned(saddle)) {
                Entity damager = null;
                if (event instanceof EntityDamageByEntityEvent) {
                    damager = ((EntityDamageByEntityEvent) event).getDamager();
                    if (damager instanceof Projectile)
                        if (((Projectile)damager).getShooter() instanceof Player)
                            damager = (Player) ((Projectile) damager).getShooter();
                    if (!(damager instanceof Player)) damager = null;
                }
                if (damager!=null)
                    if (!Saddles.hasAccess(saddle,damager))
                        event.setCancelled(true);
                else
                    if (horse.getPassenger()==null)
                        event.setCancelled(true);
            }
        }
    }

}
{'fraction_non_alphanumeric': 0.1022884882108183, 'fraction_numerical': 0.0, 'mean_word_length': 1.8816183816183816, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3632536', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1503, 'n_words': 275}