Solved Vehicle events

Discussion in 'Spigot Plugin Development' started by torpkev, Jul 9, 2020.

  1. I'm having some trouble registering my vehicle events. I'd like to run some code when a vehicle (minecart/boat) are hit or destroyed

    I thought VehicleDamageEvent or VehicleDestroyEvent would be a good place to start, but neither seems to fire when I hit a boat until it breaks.

    I added in VehicleEvent just to see if that would fire, and nothing. My VehicleEnter code DOES fire though

    upload_2020-7-9_11-56-58.png

    I am registering the events in main (also, vehicle enter works fine)

    Code (Text):
    // Main class
    Bukkit.getPluginManager().registerEvents(new VehicleEvents(), this);

    Code (Java):

    // VehicleEvents class
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.vehicle.VehicleEvent;
    import org.bukkit.event.vehicle.VehicleDamageEvent;
    import org.bukkit.event.vehicle.VehicleDestroyEvent;

    public class VehicleEvents implements Listener {
        @EventHandler
        public void onVehicle(VehicleEvent evt) {
            Logging.debug("VehicleEvents", "onVehicle()", "Vehicle Event Fired");
            Logging.debug("VehicleEvents", "onVehicle()", "Event: " + evt.getEventName());
        }
        @EventHandler
        public void onVehicleDamage(VehicleDamageEvent evt) {
            Logging.debug("VehicleEvents", "onVehicleDamage()", "Vehicle Damage Event Fired");
            Logging.debug("VehicleEvents", "onVehicleDamage()", "Event: " + evt.getEventName());
        }
        @EventHandler
        public void onVehicleDestroy(VehicleDestroyEvent evt) {
            Logging.debug("VehicleEvents", "onVehicleDestroy()", "Vehicle Destroy Event Fired");
            Logging.debug("VehicleEvents", "onVehicleDestroy()", "Event: " + evt.getEventName());
        }
        @EventHandler
        public void onVehicleEnter(VehicleEnterEvent evt) {
            Logging.debug("VehicleEvents", "onVehicleEnter()", "Vehicle entered");
        }
    }
    Am I barking up the wrong tree?
     

    Attached Files:

  2. Is this only a problem with boats? I know it works for minecarts cause I use a system for it in 1.12 unless it was changed in 1.16 but I doubt it.
    I haven't personally tested with boats to know but maybe you could try the EntityDamageByEntityEvent since it would techinally be a player (entity) hitting a boat (entity)?
     
  3. Please use the JavaDocs or Google. The EntityDamageEvent is what you're looking for.

    1. Check if the entity is a BOAT.
    2. Run your code.
    3. Just use vehicle enter event and run your code for when it's destroyed.
    4. Additional information is to use DamageCause and see if it's a valid way you want to detect or not when it's damaged.
    5. VehicleDestroyEvent may be better, but I don't believe it works for Boats (haven't tested it recently).

    If you furthermore need help and don't want to always make a thread add me on discord $olo#0001.

    Hopefully what I mentioned helped to fix what you were looking for.

    Best Regards,
    Solo
     
  4. Thanks all - Bizarrely I restarted my computer and now VehicleDamage is firing.. not sure why, I don't think I changed anything. VehicleEvent and VehicleDestroy still aren't - but I think I'm good to go on just VehicleDamage

    I did use both Google and JavaDocs - not sure I can see a clear reasoning between why we'd use EntityDamage rather VehicleDamage/VehicleDestroy - but - I can get by with what I have for now..

    Thanks again!