Vehicle Auto-Pickup

Discussion in 'Archived: Plugin Requests' started by bobacadodl, Apr 10, 2013.

  1. Offline

    bobacadodl

    Plugin Description:
    This is a simple plugin that I need. All it needs to do is when a player leaves a vehicle (boat or minecart), it should automatically destroy the entity and put the itemstack back into their inventory.

    No permissions or commands are necessary in my situation, but you could add a permission for other users.
     
  2. Offline

    StaticE

    bobacadodl This sounds kinda cool, I'll give it a shot.
     
  3. Offline

    bobacadodl

    Thanks! :)

    Lemme know when you're done :) I need it pretty soon. You dont need to test or anything If i find any errors ill fix them myself.

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

    StaticE

    bobacadodl Ok I know I sound like a noob because this is super easy to make, but I'm done all the code and the event won't register. I googled around and saw that a lot of people are having problems with using the same event. Maybe another dev can figure it out.
     
  5. Offline

    SainttX

    I'm new to creating plugins and have been playing with this snippet of code:
    Code:
    public void destroyVehicle(VehicleDestroyEvent event, Player player) {
            if (event.getVehicle() instanceof Minecart) {
                player.getInventory().addItem(
                        new ItemStack[] { new ItemStack(Material.MINECART, 1) });
        }
    }
    But it isn't working or detecting like you said. Just thought I'd contribute to the discussion and get some opinions or any corrections in my function.
     
  6. Offline

    StaticE

    SainttX I was actually using VehicleExitEvent, because he said he wanted it to be destroyed when the player exits. But good job contributing!
     
  7. Offline

    SainttX

    Oops!

    Anyway, I tried VehicleExitEvent and it yields the same results... Doesn't seem to do anything, here's the code I used if anybody wants to help:

    Code:
    public void destroyVehicle(VehicleExitEvent event, Player player) {
        if (event.getVehicle() instanceof Minecart) {
            vehicle = event.getVehicle();
            vehicle.remove();
            player.getInventory().addItem(
                    new ItemStack[] { new ItemStack(Material.MINECART, 1) });
     
        }
    }
     
  8. Offline

    StaticE

    SainttX Yup, that's pretty much what I did. You have the annotation "@EventHandler" above that code right? Also, did you remember to initialize the event in the onEnable()? I did but still didn't work lol.
     
  9. Offline

    SpaceManiac

    You have an extra "Player player" argument - you can only have one argument to event handlers, the VehicleExitEvent. It probably has a .getPlayer() method you can use.
     
    devilquak likes this.
  10. Offline

    SainttX

    SE Plugins Hm, I just added @EventHandler, but how do I initialize the event in onEnable ()? I've implemented the Listener if that's what you're referring too.
    Code:
    public class BreakTest extends JavaPlugin implements Listener {
    SpaceManiac I've removed Player player and have access to the .getExited() method, however when trying to add items to the player's inventory, I need to wrap the whole statement with (HumanEntity) as in:
    Code:
    ((HumanEntity) event.getExited()).getInventory().addItem(
                        new ItemStack[] { new ItemStack(Material.MINECART, 1) });
    Is this acceptable? Either way it doesn't seem to be working at the moment.

    Here's my code for reference to what I've done so far.
     
  11. Offline

    StaticE

    SainttX No, you need to initialize the event in the onEnable(). If your event is in the same class as the onEnable() do:
    Code:
    this.getServer().getPluginManager().registerEvents(this, this);
     
  12. Offline

    SainttX

    SE Plugins Hm, put it in but nothing changes in game.
     
  13. Offline

    StaticE

    SainttX Yeah, same. I have no idea why it isn't working.
     
  14. Offline

    devilquak

    SainttX

    Cleaned that up for you. Few more things:

    a.) Like SE Plugins said, you need to register any and all events with Bukkit in onEnable.

    b.) Don't ever create your own logger in a plugin, and especially don't ever call it "Minecraft". Java has its own logger, tailor-made just for you and your plugin, use plugin.getLogger() to access it.

    c.) Don't print messages saying the plugin is enabled/disabled. They are generated automatically by Bukkit in the first place, and any more than that one line can begin to clog up both your code and the console of the server running it.

    d.) There's absolutely no reason you should define your main class using a static reference, especially in a project with a single class and a dozen or so lines of code. The same goes for the Vehicle declaration, it doesn't even make sense. If you really must assign the vehicle to a variable, you want to put this inside the event: "Vehicle v = event.getVehicle();" That way, you can use "v" to access the vehicle in the event. What you were doing before was taking one, static instance of the event, and telling the plugin to think of the minecart as nonexistent.

    e.) Don't know what you were aiming for with the ItemStack dealio, but all you need is .addItem(new ItemStack(Material.MINECART).

    f.) You should check to see if the Entity exiting the minecart is actually a player or not, especially before casting HumanEntity to a method later on. If you don't check, anything besides a player that leaves a minecart will break the function and trigger a huge, nasty error-tastic surprise for your consumption in the console.

    Hope this helped somewhat.


    Code:
    package me.SainttX.BreakTest;
     
    import java.util.logging.Logger;
     
    import org.bukkit.Material;
    import org.bukkit.entity.HumanEntity;
    import org.bukkit.entity.Minecart;
    import org.bukkit.entity.Vehicle;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.vehicle.VehicleExitEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class BreakTest extends JavaPlugin implements Listener {
     
        this.plugin = plugin;
     
        public void OnEnable() {
                    this.getServer().getPluginManager().registerEvents(this, this);
        }
     
        @EventHandler
        public void destroyVehicle(VehicleExitEvent event) {
            if (event.getVehicle() instanceof Minecart&&event.getEntity().getType() == EntityType.PLAYER) {
                event.getVehicle().remove();
                ((HumanEntity) event.getExited()).getInventory().addItem(new ItemStack(Material.MINECART));
            }
        }
    }
     
    drtshock likes this.
  15. Offline

    SainttX

    devilquak Thanks for your help! I have some reading to do about static variables and such.

    I'm getting an error in eclipse with the "this.plugin = plugin;" statement you've done. Shows up as "Syntax error on token(s), misplaced construct(s)". When I move it inside the OnEnable() method it gives me "plugin cannot be resolved or is not a field".
     
  16. Offline

    bobacadodl

    thx for the code, I'll compile it myself :)
     
  17. Offline

    devilquak

    Forgive me, that line is to be used in a method when accessing the main class from a different class, copied the wrong piece of code. You were actually close the first time, just replace my line with "public <MainClassName> plugin;". Same thing you had before, just without the static declaration.

    Edit: But remember, you don't even need that for this plugin if you won't be accessing any of its methods, which you haven't so far.

    Edit 2: One more mistake I noticed, I apologize. Inside onEnable, replace "this.getServer()..." with "Bukkit.getServer()...".
     
    SainttX likes this.
  18. Offline

    bobacadodl

    Are you sure you can't just do this.getServer()? I use that all the time in my plugins and it seems to work fine...
     
  19. Offline

    devilquak

    Turns out that they're essentially the same when used in the main class, just in any other class Bukkit.getServer() must be used. I guess I forgot that this.getServer() functioned as well, and as a force-of-habit assumed it needed to be Bukkit. Apologies, this.getServer() should work fine if you want to use it. I just use Bukkit all the time anyways, as I don't make many methods in the main class.
     
    bobacadodl likes this.
  20. Offline

    bobacadodl

    Same, i know what you mean ;)

    EDIT: Now these events dont seem to be triggering...

    Code:
        public void OnEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
     
        @EventHandler
        public void exitVehicle(VehicleExitEvent event) {
            getServer().getLogger().log(Level.INFO,"Exited vehicle");
            if (event.getVehicle().getType()==EntityType.MINECART&&event.getExited().getType() == EntityType.PLAYER) {
                event.getVehicle().remove();
                ItemStack car = new ItemStack(Material.MINECART);
                ItemMeta cm = car.getItemMeta();
                cm.setDisplayName(ChatColor.GREEN+"Car");
                car.setItemMeta(cm);
                ((HumanEntity) event.getExited()).getInventory().addItem(car);
            }
        }
        @EventHandler
        public void destroyVehicle(VehicleDestroyEvent event) {
            getServer().getLogger().log(Level.INFO,"Destroyec vehicle");
            if(event.getVehicle().getType()==EntityType.MINECART&&event.getVehicle().getPassenger().getType() == EntityType.PLAYER){
                event.setCancelled(true);
                ItemStack car = new ItemStack(Material.MINECART);
                ItemMeta cm = car.getItemMeta();
                cm.setDisplayName(ChatColor.GREEN+"Car");
                car.setItemMeta(cm);
                ((HumanEntity) event.getVehicle().getPassenger()).getInventory().addItem(car);
                event.getVehicle().remove();
            }
        }
    Nothings getting logged.. even though the plugin is being enbled on startup. No errors either... wierd

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

    devilquak

    Did the plugin load correctly? Are there any errors in the console? Is the plugin.yml file all good and dandy?
     
  22. Offline

    bobacadodl

    devilquak :p you missed my previous post
    edit: g2g for the night I'll be back on tomorrow morning... hopefully figure out whats wrong then
     
  23. Offline

    devilquak

    Ah, didn't see that, sorry. Nothing looks wrong here, so I'd need to see the whole class and your plugin.yml to try and help you more.
     
  24. Offline

    bobacadodl

    Nothing's wrong.. I know how to make a plugin YML and thats pretty much my whole class. I think its a bug with bukkit.
     
  25. Offline

    devilquak

    If you're asking for help, and nothing looks wrong with what you've shown us so far, the only way you can get more help is by posting the only stuff that could be causing the problem, which is your whole class and your plugin.yml. I'm not accusing you of making an error, or trying to steal your code, or anything like that. It sounds like an issue of your events just not firing in the first place, and the only way we can help you is by seeing what you're doing.
     

Share This Page