Player left click other player in creative

Discussion in 'Plugin Development' started by MaTaMoR_, May 3, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    MaTaMoR_

    So i want to know if there's any way to know when a player left click other player on creative, with my code if a player is on creative it does not work .

    My code :
    Show Spoiler

    Code:
    @EventHandler
    public void onPlayerClickLeft(EntityDamageByEntityEvent event) {
        //Check if damager is a player\\
        if(!(event.getDamager() instanceof Player)) return;
     
        //Check if players is on right world\\
        Player player = (Player) event.getDamager();
        String world = player.getWorld().getName();
        if(!QOMain.getInstance().getWorld().equals(world)) return;
     
        //Check if target is a player\\
        if(!(event.getEntity() instanceof Player)) return;
     
        Player target = (Player) event.getEntity();
     
        Bukkit.getServer().getPluginManager().callEvent(new PlayerClickPlayerEvent(player, target, ClickType.LEFT_CLICK));
    }
    
     
  2. Offline

    Konato_K

    @MaTaMoR_ The problem is that players can't take damage in creative in most situations.

    Have you tried using PlayerInteractEvent?
     
  3. Offline

    MaTaMoR_

    Yes i know that, I thought about use it but for some reason i didnt try it.

    Now im using this :
    Show Spoiler

    Code:
    @EventHandler
    public void onPlayerLeftClick(PlayerInteractEvent event) {
        //Check if is right click\\
        Action action = event.getAction();
        if(!action.equals(Action.LEFT_CLICK_AIR) || !action.equals(Action.LEFT_CLICK_BLOCK)) return;
    
        Player player = event.getPlayer();
    
        //Check if player has target\\
        Player target = Targeter.getTargetPlayer(player);
        if(target == null) return;
    
        double distance = Util.getDistance(player.getLocation(), target.getLocation());
    
        if(distance > 3) return;
    
        Bukkit.getServer().getPluginManager().callEvent(new PlayerClickPlayerEvent(player, target, ClickType.LEFT_CLICK));
    }
    
    Targeter class //This class is not main
    Code:
    public class Targeter {
    
        public static Player getTargetPlayer(final Player player) {
            return getTarget(player, player.getWorld().getPlayers());
        }
    
        public static Entity getTargetEntity(final Entity entity) {
            return getTarget(entity, entity.getWorld().getEntities());
        }
    
        public static <T extends Entity> T getTarget(final Entity entity,
                final Iterable<T> entities) {
            if (entity == null)
                return null;
            T target = null;
            final double threshold = 1;
            for (final T other : entities) {
                final Vector n = other.getLocation().toVector()
                        .subtract(entity.getLocation().toVector());
                if (entity.getLocation().getDirection().normalize().crossProduct(n)
                        .lengthSquared() < threshold
                        && n.normalize().dot(
                                entity.getLocation().getDirection().normalize()) >= 0) {
                    if (target == null
                            || target.getLocation().distanceSquared(
                                    entity.getLocation()) > other.getLocation()
                                    .distanceSquared(entity.getLocation()))
                        target = other;
                }
            }
            return target;
        }
    }
    
    getDistance
    Code:
    public static final double getDistance(Location one, Location two) {
        double x = one.getX() - two.getX();
        double y = one.getY() - two.getY();
        double z = one.getZ() - two.getZ();
    
        return Math.sqrt((x * x) + (y * y) + (z * z));
    }
    

    I'm doing it right ? (i didnt event test it [i can't test it])

    Edit: sorry for double post i didnt notice, mods plz.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
Thread Status:
Not open for further replies.

Share This Page