Launch-Pad Movement

Discussion in 'Plugin Development' started by Eisi05, Jul 31, 2022.

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

    Eisi05

    Hello,
    i am trying to make a Launch-Pad plugin. With some research i found this:
    Code:
    private static final MaterialData PISTON = new MaterialData(Material.MOVING_PISTON);
    
        @EventHandler
        public void onMove(PlayerMoveEvent event) {
            Block block = event.getTo().clone().subtract(0,1,0).getBlock();
            JumpPadManager manager = new JumpPadManager(block);
            if(block.getType() != Material.SLIME_BLOCK)
                return;
            if(!manager.isJumpPad())
                return;
    
            FallingBlock fallingBlock = event.getTo().getWorld().spawnFallingBlock(event.getTo(), PISTON.getItemType(), PISTON.getData());
            fallingBlock.setDropItem(false);
            fallingBlock.setCustomName("JumpPadEntity");
    
            Location launchLocation = event.getFrom().setDirection(event.getTo().toVector().subtract(event.getFrom().toVector())).clone();
            launchLocation.setPitch((float) -(manager.getMultiplier().getAngle()));
            launchLocation.setYaw(event.getTo().getYaw());
            fallingBlock.setVelocity(launchLocation.getDirection().normalize().multiply(manager.getMultiplier().getMultiplier()));
    
            launchPlayer(event.getPlayer(), fallingBlock);
        }
    
        private void launchPlayer(final Player player, final FallingBlock fallingBlock) {
            if (!player.getAllowFlight()) {
                player.setAllowFlight(true);
            }
            (new BukkitRunnable() {
                private boolean endFlight = false;
    
                private boolean ignoreFirstPlayerOnGround = true;
    
                public void run() {
                    player.setFallDistance(0.0F);
                    if (fallingBlock != null) {
                        fallingBlock.setTicksLived(1);
                        if (!player.getLocation().getWorld().equals(fallingBlock.getWorld()) || fallingBlock.getLocation().clone().add(fallingBlock.getLocation().getDirection().clone().multiply(0.5D)).getBlock().isLiquid())
                            endFlight = true;
                        if (!fallingBlock.isDead())
                            player.setVelocity(fallingBlock.getVelocity());
                    }
                    if(player.isSneaking())
                        endFlight = true;
                    if ((player.isOnGround() && !ignoreFirstPlayerOnGround) || fallingBlock == null || fallingBlock.isOnGround() || fallingBlock.isDead() || fallingBlock.getLocation().getY() < -10.0D || fallingBlock.getVelocity().length() == 0.0D || endFlight || player.getLocation().getBlock().getType() == Material.LADDER) {
                        if (fallingBlock != null)
                            fallingBlock.remove();
                        if(player.getGameMode() != GameMode.CREATIVE && player.getGameMode() != GameMode.SPECTATOR)
                            player.setAllowFlight(false);
                        player.setFlying(false);
                        cancel();
                    }
                    ignoreFirstPlayerOnGround = false;
                    fallingBlock.getLocation().setYaw(player.getLocation().getYaw());
                }
            }).runTaskTimer(Main.plugin, 0L, 1L);
        }
    it also works, but i can't move/change direction when i'm in the air.
    Is there any way to fix this?
    (I thought of changing the direction of the falling block in that direction i look, but i don't know how to do it)

    Thanks for help
     
  2. Offline

    Tim_M

    As a good start you can do: player.getLocation().getDirection() which will give you a vector of where the player is looking.
     
Thread Status:
Not open for further replies.

Share This Page