1.14.4 Trying to push player away from blocks

Discussion in 'Spigot Plugin Development' started by PartyMC, Mar 15, 2020.

  1. Right now Im trying to create a simple gate/door only accessible for certain players. When players wont have access to this place, they will be pushed back. This is what I've got for now:



    This is my code:
    Code (Java):
    Vector v = sl.getDirection();
                        v.setY(0);
                        v.multiply(-1);
                        p.setVelocity(v.multiply(0.5));
    "sl" is the location of the block im trying to push players away from. But as you can see in the video, when looking down they can go right through. Does anyone have an idea of how to fix this?
     
  2. That the vector for the velocity reacts to the player‘s direction gives the impression that sl.getDirection() actually is the player’s direction. And when the player looks down there will be no velocity in x or z direction.
    Do you mind posting your full code for the whole pushing away part?
     
  3. Here's the full code:
    Code (Java):
        @EventHandler
        public void onMove(PlayerMoveEvent e) {
            Player p = e.getPlayer();
            Location l = p.getLocation();
            Location sl = l;
            sl.setY(l.getY() - 2);
            if(l.getBlock().getState().getType().equals(Material.OAK_SIGN)) {
                Sign b = (Sign) sl.getBlock().getState();
                if(b.getLine(0).equalsIgnoreCase("[permissiongate]")) {
                    if(!p.hasPermission(b.getLine(1))) {
                       
                        p.spawnParticle(Particle.BARRIER, l.getBlockX()+0.5, l.getBlockY()+3.5, l.getBlockZ()+0.5, 0, 0, 0, 0.1, 1, null);
                        p.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(Color.color("&c&lNo Access! &7Buy &a" + b.getLine(1) + " &7to get access to this area!")));
                       
                        Vector v = sl.getDirection();
                        v.setY(0);
                        v.multiply(-1);
                        p.setVelocity(v.multiply(0.5));
                        //Location l2 = sl.subtract(l);
                        //Vector v2 = l2.getDirection().multiply(-1);
                        //p.setVelocity(v2.setY(0).multiply(0.4));
                       
                    }
                }
                if(b.getLine(0).equalsIgnoreCase("[onewaygate]")) {
                    float pv = l.getYaw();
                    p.setVelocity(b.getLocation().getDirection().multiply(-1).multiply(0.3));
                }
            }
        }
       
    }
    Don't look at the [onewaygate] part, something else I'm working on but not important for now.
     
  4. There you go:
    Code (Text):
    Location l = p.getLocation();
    Location sl = l;
    If you simply use the player’s location as your blocks‘ location (Even without cloning it) the direction is the player’s direction. If you walk backwards into the gate you’ll even be pushed in.

    Maybe use the distance from the player to the middle of the sign block‘s location in order to calculate a velocity
     
  5. Okay, so my code is like this now (With the difference between the player and the block):
    Code (Java):
                        Vector v3 = b.getLocation().getDirection().subtract(p.getLocation().getDirection());
                        p.setVelocity(v3.multiply(0.5));
    But now this starts happening:
     
  6. Code (Text):
    Vector v = b.getLocation().add(.5, .5, .5).subtract(p.getLocation());
    v.setY(0);
    v.normalize().multiply(.5)
    That’s what I mean with middle of the block. You now have the difference in space between these two locations.
     
  7. I just found something; It's getting a Vector to the sign, thats why I'm always launched in a certain direction. How can I get around getting the vector from the sign?