Minecart Help

Discussion in 'Plugin Development' started by EspoDev, Sep 4, 2011.

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

    EspoDev

    How to you make the Minecart move in your target Cross hairs?
     
  2. Offline

    bergerkiller

    Hehe I did this already so I can give some tips. Also, this is untested and written from scratch, you may need to fix/improve it.

    Code:
    public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
        if (event.getEntity() instanceof Minecart) {
            minecart = (Minecart) event.getEntity();
        }
    }
    
    public Minecart minecart = null;
    
    public void onPlayerInteract(PlayerInteractEvent event) {
        if (minecart == null) return;
        Block target = event.getPlayer().getTargetBlock(null, 200);
        //get the difference vector
        Location from = minecart.getLocation();
        Location to = target.getLocation();
        Vector diff = new Vector();
        diff.setX(to.getX() - from.getX());
        diff.setY(to.getY() - from.getY());
        diff.setZ(to.getZ() - from.getZ());
        //set the diff to be 1 in length
        double length = diff.length();
        if (length < 0.001) return;
        diff.multiply(1 / length);
        //apply a force to it
        double force = 0.5;
        diff = diff.multiply(force);
        //Apply the diff as velocity to the minecart
        minecart.setVelocity(diff);
    }
    I originally did it differently, but to implement that would take a whole new TrainCarts project.. :)

    (it consisted of targets, yaw calculations, lots of if-statements, checks, time delay, and more, would be overkill for this idea)
     
Thread Status:
Not open for further replies.

Share This Page