/say if player has a block in their inventory

Discussion in 'Plugin Development' started by JazzaG, May 12, 2012.

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

    JazzaG

    How would I go about the server issuing a /say command when a player has a block in their inventory. For example, if they have a diamond sword in their inventory, the server would /say Player has a diamond sword!

    I don't know how to make a plugin issue a server command, but I have tried making it say the user that just logged in (just for starting out), by altering the code found on the bukkit wiki tutorial:

    Code:
    public final Logger logger = Logger.getLogger("Minecraft");
       
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            Player player = event.getPlayer(); // The player who joined
           
            logger.info(player.getName());
           
        }
    When I joined my server, I expected it to say my username in the console, which it didn't. How would I go about doing this?

    Thanks!
     
  2. Offline

    Craftiii4

    You would need to check the inventories of every player currently on-line on the server.
     
  3. Offline

    Digi

    JazzaG
    First, you don't need to make the server use "say", just do: getServer().broadcastMessage("...").
    Then, don't asociate logger in the header, if you call the class in the header as well you will have errors, and use getLogger() instead.

    About that code, did you implement Listener and registerEvents() on that class ?

    About the bocks... you could make a task and check every player's inventory, but that's verry costly, what exacly do you want to do ? Maybe there's a better way.
     
  4. Offline

    JazzaG

    I am trying to make it so that if a player has the only type of block in the server, the server will broadcast he has it, so everyone else can try and steal it from him. For example, there will only be one diamond sword, and when somebody has it, everyone will know about it.

    That code is all that's in my class. I'm just starting to make these plugins. Thanks for the reply!
     
  5. Offline

    Digi

    You need to think about some "rules" for this... do you allow diamond swords to be crafted ? Can you get them via creative inventory ? If you can *only* get a diamond sword by picking it up there's a pick up event which you can use to detect when the diamond sword was picked up.

    If you can do the rest (craft, get via inventory, chests, etc) you could hook all the events regarding transition or you can just make that task I said in my previous post, if there aren't that many players it should look pretty fast through them... also, make it run every 5 or more seconds.
     
  6. Offline

    JazzaG

    Since the one off item starts off in a chest somewhere, I was thinking I could do a "onInventoryChange" event (if that exists) then if the one off item is found using "inventory.contains()", broadcast that the player has the item. What I was also hoping to do was if the player no longer has the item (killed, dropped it), then the server would broadcast that they don't have it anymore. Am I looking at this the right way?
     
  7. Offline

    Digi

    No that event does not exist... but the InventoryClick event exists and it *might* get you the item, you should also try with shift+click to be sure it triggers for every ocasion.
    You can see a complete list of events here: http://jd.bukkit.org/apidocs/org/bukkit/event/Event.html (be sure to click the items in the Direct Known Subclasses: as well)
     
  8. Offline

    xyberviri

    You should have to register the listener in the onenable part of your plugin, something like this:
    Code:
        public void onEnable(){
            getServer().getPluginManager().registerEvents(new HelloWorldListener(), this);
        }
    You can check if the player is in possession of the item with the following:

    player.getInventory().contains(int)

    player.getItemInHand().getTypeId()


    You can also check when someone picks up and drops the item your looking for by listening for "PlayerPickupItemEvent" & "PlayerDropItemEvent"

    If the player opens a chest and places the item in their inventory you can check there inventory on the "InventoryCloseEvent", however if they grab the item and dont drop it into their inventory it won't show up their.
    and also dont forget to check for "
    PlayerQuitEvent" so if your player leaves the server the item being limited isn't an issue.
     
  9. Offline

    JazzaG

    I don't really understand how to use listeners. I have read the section on the wiki about it, but I don't know how I would use it for what I'm doing. (http://wiki.bukkit.org/Introduction_to_the_New_Event_System#Adding_the_listener)

    I have updated my code so my class extends JavaPlugin and implements Listener, and here is my code onEnable():
    Code:
    getServer().getPluginManager().registerEvents(PlayerPickupItemEvent(null), this); // line 15
            getServer().getPluginManager().registerEvents(PlayerDropItemEvent(null), this);
    ...and my events

    Code:
    @EventHandler
        public Listener PlayerPickupItemEvent(PlayerEvent event) {
            Player player = event.getPlayer(); // line 25
            if(player.getInventory().contains(276)) {
                getServer().broadcast("say", "picked up");
            }
            return null;
        }
       
        @EventHandler
        public Listener PlayerDropItemEvent(PlayerEvent event) {
            Player player = event.getPlayer();
            if(!player.getInventory().contains(79)) {
                getServer().broadcast("say", "dropped");
            }
            return null;
        }
    It all compiles fine, but when I start up the server it throws some NullPointerExceptions with reference to lines 15 and 25.
    Sorry if I'm being a nuisance, but this is something I'm finding hard to grasp.
     
  10. Offline

    Digi

    JazzaG
    And what's line 15 ?

    Line 25 seems fine... unless the event.getPlayer() can return null, you'll have to read the Bukkit API javadocs on that one.
     
  11. Offline

    JazzaG

    Line 15 is
    Code:
    getServer().getPluginManager().registerEvents(PlayerPickupItemEvent(null), this);
    Eclipse told me that my event had to have a Listener type, and so I made it return null. Now I've just been playing around trying to get it to work, so I've done some changes :)
    Anyway here's my full code
    Code:
    public class Checker extends JavaPlugin implements Listener {
     
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
        }
     
        public static Checker plugin;
        public final Logger logger = Logger.getLogger("Minecraft");
     
     
        @EventHandler
        public void PlayerPickup(PlayerPickupItemEvent event) {
            Player player = event.getPlayer();
            if(player.getInventory().contains(276)) {
                getServer().broadcast("say", "picked up");
            }
            //return null;
        }
     
        @EventHandler
        public void PlayerDrop(PlayerDropItemEvent event) {
            Player player = event.getPlayer();
            if(!player.getInventory().contains(276)) {
                getServer().broadcast("say", "dropped");
            }
            //return null;
        }
    } 
    No errors in compiling or the console, but it doesn't do anything. :(
     
  12. Offline

    Craftiii4

    Well, for a start you can get the exact items picked up or dropped.

    And i am confused about the way you are registering your events?
     
  13. Offline

    JazzaG

    I've just been playing around trying to get this to work. On the wiki (http://wiki.bukkit.org/Introduction_to_the_New_Event_System#Adding_the_listener) it registered events with the way I did in the above code, under "Registering Events with Plugin as Listener".




    Is this doing that, or do I need something more complex :S

    Code:
    if(player.getInventory().contains(276)) {
                getServer().broadcast("say", "picked up");
            }
    Thanks :)
     
  14. Offline

    Digi

    JazzaG
    contains() doesn't accept numbers, you'll need to make a ItemStack I belive.
    But you DON'T need to use those, the event will tell you what item was picked up or dropped, see the event. methods!

    Also, your logger is still invalid, asign it to getLogger() in onEnable().
     
  15. Offline

    JazzaG

    Thanks for the help people, I have figured it all out.
    Code:
    @EventHandler
    public void onPickup(PlayerPickupItemEvent event) {
    Player player = event.getPlayer();
    int item = event.getItem().getItemStack().getTypeId();
    if (item == 276) {
    getServer().broadcastMessage(player.getDisplayName() + " picked up the sword!");
    }
     
    }
    When the player picks up the dsword, if they didn't already have one in their inv, it will say "Player picked up sword!".

    Thanks a lot!
     
Thread Status:
Not open for further replies.

Share This Page