[SOLVED]Launch a Minecart?

Discussion in 'Plugin Development' started by HappyPikachu, Nov 8, 2011.

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

    HappyPikachu

    I'm working on a small plugin. Amongst the many tasks involved, I need a player in a minecart to be able to left-click either the minecart or the "air" in a given direction and then start traveling in that direction if possible, similar to MinecartMania.

    In other words, I want to "launch" the minecart by having the player left-click. I can handle what happens to it after it starts moving. Any ideas?

    Sorry for the lack of source, but I haven't been able to figure how this would work... at all.
     
  2. Offline

    nisovin

    I believe you would use setVelocity() on the minecart entity.
     
  3. Offline

    HappyPikachu

    Never tried that. I figured it wouldn't work on something that isn't already moving.

    I'll try it and get back to you.

    Yeah, I was right. I can't .setvelocity() through onVehicleEnter, only via onVehicleMove. Thus, I can't change the velocity of a still cart given my current knowledge.

    There has to be a way to do this. I'll post source for the sake of getting the thread moving, but I haven't gotten too far with the plugin.

    Here's two parts of my code:

    Code:java
    1. public class RandomVehicleListener extends VehicleListener{
    2.  
    3. public void onVehicleEnter(VehicleEnterEvent event) {
    4. if (event.getVehicle() instanceof Minecart) {
    5. Player p = (Player)event.getEntered();
    6. p.sendMessage(ChatColor.RED + "You have entered the minecart.");
    7. }
    8. }

    Code:java
    1. public class RandomPlayerListener extends PlayerListener{
    2.  
    3. public void onPlayerInteract(PlayerInteractEvent event){
    4. if (event.getClickedBlock() != null){
    5. //Stuck...
    6. }
    7. }
    8. }

    I realize the message in onVehicleEnter gets fired when the player exits a Minecart as per here, but that's low priority atm.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 21, 2016
  4. Offline

    bergerkiller

    @HappyPikachu you need to use onPlayerInteractEntity, then check if the clicked entity is a minecart, and if so, use the player direction (location.getDirection()) to push the minecart into that direction.

    Could be that event is for right-clicking only, in that case use onVehicleDamage and listen for Minecarts getting damaged by a player. Then repeat the above.
     
  5. Offline

    HappyPikachu

    It is right-click only, which is a problem because that causes the player to exit the Minecart. I'm messing around with the onVehicleDamage idea... never woulda thought of that. I'll post results when I have 'em.
     
  6. Offline

    Butkicker12

    Right click with a item in hand? That would work think.
     
  7. Offline

    HappyPikachu

    Needs to work w/o an item. Excellent point, though.
     
  8. Offline

    HappyPikachu

    @bergerkiller I got your second suggestion working! For those reading, here's all the code I used:

    Code:java
    1. public void onVehicleDamage(VehicleDamageEvent event) {
    2. if (event.getVehicle() instanceof Minecart) {
    3. Player p = (Player)event.getAttacker();
    4. Vector facing = p.getLocation().getDirection();
    5. event.getVehicle().setVelocity(facing);
    6. }
    7. }

    This was surprisingly simpler than I thought it'd be. I still need to set it so it only fires if the player is in the vehicle, but I can handle that part. Thanks a bunch!
     
    olsyboy and r3Fuze like this.
Thread Status:
Not open for further replies.

Share This Page