Solved Checking players inventory

Discussion in 'Plugin Development' started by HeyAwesomePeople, May 23, 2013.

Thread Status:
Not open for further replies.
  1. I need some help. I have it so they when a player does /leave or /lobby it gives them a written book, but what I am trying to do is that if they preform those commands and they already have the book in their inventory, it would not give it to them. Help?

    Code:
    //Starting of Book Giving
                   
                    ItemStack book = new ItemStack(Material.WRITTEN_BOOK);
                    BookMeta bm = (BookMeta) book.getItemMeta();
                    bm.setPages(Arrays.asList(ChatColor.DARK_RED + "RULES" +
                            "\nTHIS IS THE SECOND PAGE"));
                    bm.setAuthor("Player vs Player");
                    bm.setTitle("Rules");
                    book.setItemMeta(bm);
                    ((Player) sender).getInventory().addItem(book);
                    //Ending of book giving
                    
    Thanks!
    -HeyAwesomePeople
     
  2. Offline

    CubixCoders

    Code:java
    1.  
    2. if(player.getInventory().contains(Material.WRITTEN_BOOK)){
    3. //They have a written book
    4. }
    5.  
     
  3. Offline

    CubixCoders

  4. CubixCoders
    One more thing
    If a player has the book in their inventory already, and they try to pick the same book up, how do I cancel the picking up of the book? Like make the book disappear instead of go into thier inventory(Cause they already have one on them).
     
  5. Offline

    Garris0n

    PlayerPickupItemEvent
    event.setCancelled(true), and event.getEntity(or is it something else, not sure, look it up).remove()
     

  6. I am new to coding, how were would I place this?
     
  7. Offline

    Garris0n

    Code:
    @EventHandler
    public void onPickup(PlayerPickupItemEvent event){
        if(player.getInventory().contains(Material.WRITTEN_BOOK){
            event.getItem().remove();
            event.setCancelled(true);
        }
    }
    Don't forget to register your events(search bukkit event system for that info).
    Untested. Not sure if that cancel is even needed, if you have to cancel first, etc. Test it out for yourself.
     
  8. Offline

    AstramG

    Since you should know how to do it already this is just a tip. You should check the displayname of the book because if someone picks up another book then it will conflict with the book that your talking about.
     
  9. Offline

    Minnymin3

    Pro tip: Learn java before making plugins.
     
    CakePvP and Ultimate_n00b like this.
  10. New to coding, I currently don't know how to check for the book name. I could use all the help I can get.

    "player" in player.getInventory is underlined red with the error: player cannot be resolved.

    Here is my code, I just want to make sure I am using the event handler and registering the events right.

    Code:
    package me.heyawesomepeople.main;
     
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.MemorySection;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.player.PlayerPickupItemEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.BookMeta;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class lobby extends JavaPlugin {
        public boolean freeze = false;
        public final StartUp blo = new StartUp(this);
     
        @Override
        public void onDisable() {
            blo.enable(false);
            getServer().getPluginManager().removePermission(new permissions().canLeave);
            getServer().getPluginManager().removePermission(new permissions().canJoin);
            getServer().getPluginManager().removePermission(new permissions().canJoinRed);
            getServer().getPluginManager().removePermission(new permissions().canJoinBlue);
            getServer().getPluginManager().removePermission(new permissions().canMap);
        }
     
        @Override
        public void onEnable() {
            blo.enable(true);
            getConfig().options().copyDefaults(true);
            PluginManager pm = getServer().getPluginManager();
            saveConfig();
           
            pm.addPermission(new permissions().canLeave);
            pm.addPermission(new permissions().canJoin);
            pm.addPermission(new permissions().canJoinRed);
            pm.addPermission(new permissions().canJoinBlue);
            pm.addPermission(new permissions().canMap);
        }
       
        public class LoginListener implements Listener {
            public LoginListener(lobby plugin) {
                plugin.getServer().getPluginManager().registerEvents(this, plugin);
            }
       
        @EventHandler
        public void onPickup(PlayerPickupItemEvent event){
            if(player.getInventory().contains(Material.WRITTEN_BOOK)){
                event.getItem().remove();
                event.setCancelled(true);
            }
        }
     
      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if(cmd.getName().equalsIgnoreCase("leave") || cmd.getName().equalsIgnoreCase("lobby")){
                if (sender.hasPermission(new permissions().canLeave)) {
                    Player player = (Player) sender;
                    player.teleport(new Location(Bukkit.getWorld(getConfig().getString("lobbyworld")), this.getConfig().getInt("coordX"), this.getConfig().getInt("coordY"), this.getConfig().getInt("coordZ")));
                    if(player.getInventory().contains(Material.WRITTEN_BOOK)){
                        setCancelled(true);
                        } else{
                    //Starting of Book Giving
                   
                    ItemStack book = new ItemStack(Material.WRITTEN_BOOK);
                    BookMeta bm = (BookMeta) book.getItemMeta();
                    bm.setPages(Arrays.asList(ChatColor.DARK_RED + "RULES" + ChatColor.BLUE + "\nWelcome to the rules book!" + ChatColor.RED + "\n1. No Spam\n2. No Hacks\n3. Respect Admins\n4. Keep Cussing Low\n5. Don't harm your own team!"));
                    bm.setAuthor("Player vs Player");
                    bm.setTitle("Rules");
                    book.setItemMeta(bm);
                    ((Player) sender).getInventory().addItem(book);
                    //Ending of book giving
                        }
                    return true;
                } else{
                    sender.sendMessage(ChatColor.DARK_RED + "You do not have permissions to preform this command!");
                }
            }
           
           
            if(cmd.getName().equalsIgnoreCase("join")){
                if (args.length == 0) {
                    if (sender.hasPermission(new permissions().canJoin)) {
                        Player player = (Player) sender;
                        sender.sendMessage(ChatColor.BLUE + "No Game In Progress");
                        return true;
                    } else{
                        sender.sendMessage(ChatColor.DARK_RED + "You do not have permissions to preform this command!");
                    }   
                }
            }
           
           
            if(cmd.getName().equalsIgnoreCase("maps")){
                    if (sender.hasPermission(new permissions().canMap)) {
                        Player player = (Player) sender;
                        player.sendMessage(ChatColor.AQUA + "Maps in Cycle: " + ChatColor.RED + getConfig().getString("listofmaps"));
                    } else{
                        sender.sendMessage(ChatColor.DARK_RED + "You do not have permissions to preform this command!");
                    }   
            }
     
            return false;
        }
     
    private MemorySection getConfig() {
        // TODO Auto-generated method stub
        return null;
    }
     
    private void setCancelled(boolean b) {
        // TODO Auto-generated method stub
       
    }
       
    }
    }
    
    Flash News, not going to currently. Don't know anyone that could train me properly. And I am starting to get the hang of it, and I came here for help.

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

    AstramG

    Okay well you need to change your PickupItemEvent section of your code to this:
    Code:
    @EventHandler
        public void onPickup(PlayerPickupItemEvent event){
            if(player.getInventory().contains(Material.WRITTEN_BOOK)){
                if (event.getItem().getItemMeta().getDisplayName().equals("Bookname here with color codes") {
                  event.getItem().remove();
                  event.setCancelled(true);
                } else {
                  // Do something here if it's not the same, such as arranging the item in the inventory
                  return;
                }
            }
        }
    Also I'm not sure if this is 100% correct because I'm doing it all from memory but I'm pretty sure that'll work.
     
  12. Offline

    Minnymin3

    Theres hundreds of tutorials online and hundreds more books on Java. Take a look at a few and make some basic programs like a calculator, a useless program that displays what you type in, or whatever you want. Learning java will make your life easier in the future and then you wont have jack***** like me yelling at you to learn java.
     
  13. Offline

    AstramG

    I approve the post, but the ending wasn't necessary :p
     
    Minnymin3 likes this.
  14. Offline

    Minnymin3

    Well its true :p
     


  15. Okay yeah, I will get a book. I guess learning java could help me.. But I am doing fairly well. Do you suggest a book/software?(Assuming something like Eclipse). I really want to make this plugin I have in mind. It will take a ton of work from me.
    Still, player in player.getInventory is underlined red.

    I also exported this to my server, reloaded, and no commands work. I think I messed up the listener somehow
     
  16. Offline

    AstramG

    I'm pretty sure it's because of the error with player. All errors could lead you to a faulty plugin. Change that method to this:
    Code:
    @EventHandler
        public void onPickup(PlayerPickupItemEvent event){
            Player player = event.getPlayer(); //This gets a player object from the event which is triggered by picking up an item
            if(player.getInventory().contains(Material.WRITTEN_BOOK)){
                if (event.getItem().getItemMeta().getDisplayName().equals("Bookname here with color codes") {
                  event.getItem().remove();
                  event.setCancelled(true);
                } else {
                  // Do something here if it's not the same, such as arranging the item in the inventory
                  return;
                }
            }
        }
     
  17. Offline

    Minnymin3

    Instead of getting a book take at http://docs.oracle.com/javase/tutorial/java/index.html it can be very useful for learning java.
     
  18. Code:
            @EventHandler
            public void onPickup(PlayerPickupItemEvent event){
                Player player = event.getPlayer(); //This gets a player object from the event which is triggered by picking up an item
                if(player.getInventory().contains(Material.WRITTEN_BOOK)){
                    if (event.getItem().getItemMeta().getDisplayName().equals("Rules")) {
                      event.getItem().remove();
                      event.setCancelled(true);
                    } else {
                      // Do something here if it's not the same, such as arranging the item in the inventory
                      return;
                    }
                }
            }
    getItemMeta is underlined with: The method getItemMeta() is undefined for the type Item

    I will check it out, thanks.
     
  19. Offline

    Ultimate_n00b

    <Edit by Moderator: Redacted bit url>
     
    Last edited by a moderator: Feb 16, 2017
    Minnymin3 likes this.
  20. Okay Fine, I will leave you guys alone.
     
  21. Offline

    Sheepii

    Hey mate,

    If the first thing that you say isn't constructive in a conversation, then you need to learn how to be social. Learning how to be social will make your life easier in the future and then you won't have jackasses like myself yelling at you to learn how to be social. Plus, 90% of the threads here in Bukkit development is in regards to Java Programming, and not Bukkit API. Are you going to sit around and yell at every individual that doesn't understand Java? If so, you have a very poor life and should take up a hobby, like for an example brushing your teeth. Also, before you retaliate, just know, I don't give a shit.

    On a side note, thank you OP this helped me a lot. Also, I learned Java due to Bukkit programming. I didn't learn Java, and then do Minecraft. I learned it from multiple failures, asking questions, scouring millions of coding snips and eventually succeeding. I've made very large scale projects and worked on very well known projects, as a main developer.
     
  22. Offline

    Minnymin3

    I agree with you that the best way of learning is doing but the place for questions that have stuff to do with java should be asked in places like StackTrace instead on the forum for bukkit help. And all the oracle trails are very helpful. Plus googling stuff is a really good thing to do as well. HeyAwesomePeople don't feel bad about asking a question though, just go to places like StackTrace to ask questions if you think they are questions to do with java in general.
     
  23. Offline

    Ultimate_n00b

    Go to StackOverflow if you have coding questions.
    This forum is for bukkit related questions.
     
    Minnymin3 likes this.
  24. Got it. I am currently going through 80+ videos on Java beginner and intermediate by TheNewBoston. And, I did try to learn by trial and error.
     
Thread Status:
Not open for further replies.

Share This Page