Rideable Ender Pearls

Discussion in 'Plugin Development' started by GamerzKing, Dec 5, 2015.

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

    GamerzKing

    Hey guys!

    I was just wondering if there was a smoother way, to ride EnderPearl's, other than teleporting the player, or setting the passenger. I have tried many different methods to create something similar to the Mineplex ethereal pearl, for a game I am making, however when a player is riding the EnderPearl, it glitches, the player, the player is standing up, etc.


    Code:
    package com.gamerzking.stacker.events;
    
    import org.bukkit.entity.EnderPearl;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.ProjectileLaunchEvent;
    import org.bukkit.event.player.PlayerTeleportEvent;
    import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
    
    public class ProjectileManipulation implements Listener {
    
        @EventHandler
        public void onProjectileLaunch(ProjectileLaunchEvent event) {
    
            if(event.getEntity().getShooter() instanceof Player && event.getEntity() instanceof EnderPearl) {
               
                Player player = (Player) event.getEntity().getShooter();
                EnderPearl enderPearl = (EnderPearl) event.getEntity();
    
                enderPearl.setPassenger(player);
            }
        }
       
        @EventHandler
        public void onPlayerTeleport(PlayerTeleportEvent event) {
           
            if(event.getCause().equals(TeleportCause.ENDER_PEARL)) {
                event.setCancelled(true);
            }
        }
    }
    Mineplex's EnderPearl's:


    My EnderPearls:


    Any suggestions would be greatly appreciated! Thanks everyone!
     
  2. Offline

    Scimiguy

    They're not throwing the pearls, they're just spawning them, setting you as the passenger, then allowing your movement to control them
     
  3. Offline

    GamerzKing

    Sorry for the late response, but when I use PlayerInteractEvent to check if the item in hand is an EnderPearl, and I spawn the EnderPearl, it spawns two. The method you suggested works perfectly, I'm just wondering how to prevent the second EnderPearl from spawning.

    I was also wondering, how would I make it so the player cannot get off the EnderPearl (by sneaking or other sources)? I know I would use PlayerToggleSneakEvent, but how?

    Code:
    Code:
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
    
            Player player = event.getPlayer();
            Action action = event.getAction();
    
            if(player.getItemInHand().getType().equals(Material.ENDER_PEARL)) {
                if(action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK) {
    
                    EnderPearl pearl = player.launchProjectile(EnderPearl.class);
    
                    pearl.setVelocity(player.getLocation().getDirection());
                    pearl.setPassenger(player);
                }
            }
        }

    Thanks,
    - GamerzKing
     
  4. Offline

    567legodude

    @GamerzKing Probably because you are not cancelling the event, so the one that they throw is still thrown along with the one you spawn.
     
  5. Offline

    GamerzKing

    But if I cancel the ProjectileLaunchEvent, neither EnderPearl will spawn, soo
     
  6. Offline

    567legodude

    @GamerzKing If neither of them spawn then you are doing something wrong, you can cancel their throw and still spawn your own.
     
  7. Offline

    GamerzKing

    Well for some reason, neither spawn for me.

    Code:

    Code:
    public class ProjectileManipulation implements Listener {
    
        @EventHandler
        public void onPlayerTeleport(PlayerTeleportEvent event) {
    
            if(event.getCause().equals(TeleportCause.ENDER_PEARL)) {
                event.setCancelled(true);
            }
        }
    
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
    
            Player player = event.getPlayer();
            Action action = event.getAction();
    
            if(player.getItemInHand().getType().equals(Material.ENDER_PEARL)) {
                if(action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK) {
    
                    EnderPearl pearl = player.launchProjectile(EnderPearl.class);
    
                    pearl.setVelocity(player.getLocation().getDirection());
                    pearl.setPassenger(player);
                }
            }
        }
     
        @EventHandler
        public void onProjectileLaunch(ProjectileLaunchEvent event) {
            event.setCancelled(true);
        }
    }
    I also tried replacing
    Code:
    player.launchProjectile(EnderPearl.class);
    with
    Code:
     EnderPearl pearl = (EnderPearl) player.getLocation().getWorld().spawnEntity(player.getLocation(), EntityType.ENDER_PEARL);
    but no result.
     
  8. Offline

    Scimiguy

    Wow many things wrong there

    1. You're listening to ProjectileLaunchEvent -- Which wouldn't happen if you intercept the interactevent
    2. You're also listening to PlayerTeleportEvent -- Which definitely wouldn't happen if you intercepted either the interactevent, or the projectilelaunchevent
    3. You're not checking if the player even has an item in their hand (NullPointerExceptions ahoy!)
    4. You're not even cancelling the PlayerInteractEvent when you need to
    5. You're launching your own projectile?
    6. You're not spawning a pearl entity at all
    7. You're not doing anything about PlayerControl - not that you'd get to that yet
     
  9. Offline

    567legodude

    @GamerzKing You don't need the teleport event or the launch event. Just in the interact event cancel it when you spawn your own.

    @Scimiguy He has 5 and 6. Look at his fourth post. But everything else is still wrong.
     
Thread Status:
Not open for further replies.

Share This Page