Minecart cars being weird

Discussion in 'Plugin Development' started by Tim_M, Jul 26, 2022.

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

    Tim_M

    I made a simple cars plugin, you enter it and go in whatever direction you look. I started working on adding acceleration, but it literally seems to be ignoring the acceleration. Please take a look:
    Code:
    @EventHandlervoid vehicleMoveEvent(VehicleMoveEvent event)
    {
       Minecart vehicle = (Minecart) event.getVehicle();
       Player player = (Player) vehicle.getPassenger();
    
       Vector playerVector = player.getLocation().getDirection();
    
       double x = Math.round(playerVector.getX());
       double z = Math.round(playerVector.getZ());
    
       Vector velocity = new Vector(x, 0, z);
       Vector velocityMod = new Vector(Math.max(carStartSpeed, vehicle.getDerailedVelocityMod().getX()), 0, Math.max(carStartSpeed, vehicle.getDerailedVelocityMod().getZ()));
    
       velocityMod = new Vector(Math.min(velocityMod.getX() + carAcceleration/20, carMaxSpeed),    velocity.getY(), Math.min(velocityMod.getZ() + carAcceleration/20, carMaxSpeed));
    
       if (vehicle.getFallDistance() > 0)
       {
          velocity = new Vector(x, -1, z);
          velocityMod = new Vector(1, -1, 1);
       }
    
       if (!vehicle.isOnGround() && vehicle.getLocation().getBlock().getType().name().toLowerCase().contains("rail"))
       {
          try {
             Method getHandle = Reflect.getOBCClass("entity.CraftEntity").getMethod("getHandle");
             Object entity = getHandle.invoke(vehicle);
             Method setPosition;
             if (Reflect.getVersion().contains("1_17") || !Reflect.NEW_NMS)
                setPosition = entity.getClass().getMethod("setPosition", Double.TYPE, Double.TYPE, Double.TYPE);
          else
             setPosition = entity.getClass().getMethod("c", Double.TYPE, Double.TYPE, Double.TYPE);
          Location position = vehicle.getLocation().clone().add(velocity);
    
          setPosition.invoke(entity, position.getX(), position.getY(), position.getZ());
       } catch (IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | IllegalAccessException e) {
          e.printStackTrace();
       }
    
       vehicle.setDerailedVelocityMod(velocityMod);
       vehicle.setVelocity(velocity);
    }
    
    
    @EventHandlervoid vehicleUpdateEvent(VehicleUpdateEvent event)
    {
       Minecart vehicle = (Minecart) event.getVehicle();
       Player player = (Player) vehicle.getPassenger();
    
       Vector playerVector = player.getLocation().getDirection();
    
       double x = Math.round(playerVector.getX());
       double z = Math.round(playerVector.getZ());
    
       Vector velocity = new Vector(x, 0, z);
       Vector velocityMod = new Vector(Math.max(carStartSpeed, vehicle.getDerailedVelocityMod().getX()), 0, Math.max(carStartSpeed, vehicle.getDerailedVelocityMod().getZ()));
    
       velocityMod = new Vector(Math.min(velocityMod.getX() + carAcceleration/20, carMaxSpeed),    velocity.getY(), Math.min(velocityMod.getZ() + carAcceleration/20, carMaxSpeed));
    
       vehicle.setDerailedVelocityMod(velocityMod);
       vehicle.setVelocity(velocity);
    }
    
    
    This whole thing with velocity and derailedvelocitymod really confuses me.

    I removed a lot of code, which although important, does not matter to this question.
     
    Last edited: Jul 26, 2022
Thread Status:
Not open for further replies.

Share This Page