How do I destroy a cart?

Discussion in 'Plugin Development' started by Xstasy, Jan 25, 2011.

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

    Xstasy

    Hi!

    I thought this could destroy a cart, but obviously it doesn't work.
    I want to delete the cart from the tracks on exit.

    Code:
    package com.bukkit.hardwork.Hardwork;
    
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Minecart;
    import org.bukkit.entity.Player;
    import org.bukkit.event.vehicle.VehicleDamageEvent;
    import org.bukkit.event.vehicle.VehicleEntityCollisionEvent;
    import org.bukkit.event.vehicle.VehicleExitEvent;
    import org.bukkit.event.vehicle.VehicleListener;
    import org.bukkit.event.block.BlockListener;
    import org.bukkit.event.vehicle.VehicleMoveEvent;
    
    @SuppressWarnings("unused")
    public class HardworkVehicleListener extends VehicleListener{
    
      private final Hardwork plugin;
    
      public HardworkVehicleListener(Hardwork instance) {
        this.plugin = instance;
      }
    
      public void onVehicleExit(VehicleExitEvent event) {
        if (event.getVehicle() instanceof Minecart) {
          Minecart cart = (Minecart)event.getVehicle();
          cart.setDamage(0);
          this.plugin.log("Killed a cart.");
        }
      }
    
    }
     
  2. Offline

    Afforess

    Yeah, it doesn't work. I've just recently come up with a workaround, but you need to include CraftBukkit into your library:

    CraftMinecart cart = (CraftMinecart)minecart;
    EntityMinecart em = (EntityMinecart) cart.getHandle();
    em.q();
     
  3. Offline

    Xstasy

    Hmm, this didn't work either:
    And yes, I included craftbukkit as well.
    Code:
    package com.bukkit.hardwork.Hardwork;
    
    import net.minecraft.server.EntityMinecart;
    
    import org.bukkit.craftbukkit.entity.CraftMinecart;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Minecart;
    import org.bukkit.entity.Player;
    import org.bukkit.event.vehicle.VehicleDamageEvent;
    import org.bukkit.event.vehicle.VehicleEntityCollisionEvent;
    import org.bukkit.event.vehicle.VehicleExitEvent;
    import org.bukkit.event.vehicle.VehicleListener;
    import org.bukkit.event.block.BlockListener;
    import org.bukkit.event.vehicle.VehicleMoveEvent;
    
    @SuppressWarnings("unused")
    public class HardworkVehicleListener extends VehicleListener{
    
      private final Hardwork plugin;
    
      public HardworkVehicleListener(Hardwork instance) {
    
        this.plugin = instance;
      }
    
      public void onVehicleExit(VehicleExitEvent event) {
        if (event.getVehicle() instanceof Minecart) {
          Minecart minecart = (Minecart)event.getVehicle();
          CraftMinecart cart = (CraftMinecart)minecart;
          EntityMinecart em = (EntityMinecart) cart.getHandle();
          em.q();
          this.plugin.log("Killed a minecart.");
        }
      }
    
    }
     
  4. Offline

    Afforess

    VehicleExitEvent hasn't been implemented yet. I've got a workaround for that one too, but it's much more complex.

    If you're really interested though - you'd be better off including MMC in your libraries.
     
  5. Offline

    Xstasy

    Would you be willing to share that?
     
  6. Offline

    Afforess

    Well, the solution involves storing data to a minecart, so you'd need to create a wrapper to save data, then check the values on each minecart update. It'd be ~150 lines of code. My source code is on Github.

    If you include MMC in your libraries, the events would work out of the box, like they are supposed to.

    Edit: I should mention that I only recently added this to MMC, you need to compile an unreleased copy from GitHub, or ask me for a compiled one until I push the changes out to the public

    Nothing's easy eh?
     
Thread Status:
Not open for further replies.

Share This Page