Solved Inventory check

Discussion in 'Plugin Development' started by Darris, Feb 17, 2018.

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

    Darris

    Hello! Anyway, what do I need? - checking the inventory. For example, if a player has an item in their inventory that has a specific name, the player gets an effect. Example
    The player in the inventory has a book with any title, this book is in the inventory and the player damage effect power. Well, something like that, who can help - help!
     
  2. Offline

    Proxygames14

    I can help you Darris, Here is an example of what you want:
    You must also add a event handler that triggers the code.
    Such as Inventory events.

    I hope this helps you out :)

    Example:
    Code:
            Player p;
            Material item = Material.BOOK;
    
            String name = "magic food";
    
            for(ItemStack item_stack : p.getInventory().getContents()) {
                if(item_stack.getType().equals(item) && item_stack.getItemMeta().getDisplayName().equals(name)) {
                    p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 60, 1, true));
                }
            }
     
  3. Offline

    Darris

    So. I'll check.

    @Proxygames14 Your code does not work :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 17, 2018
  4. Offline

    Proxygames14

    Can you give me your Bukkit version or Spigot?
    And can you give me the error log?
     
  5. Offline

    Darris

    Spigot version: 1.8.
    In the console, no errors there, I registered Your code in the event "InventoryEvent".
     
  6. Offline

    Proxygames14

    I get it, The problem is, is that I meant 'Events that has to deal with the inventory' not exactly InventoryEvent.
    Here are some examples you must add to activate the method:

    Code:
        @EventHandler
        public void onItemPickup(InventoryPickupItemEvent event) {
            Player p = (Player) event.getInventory().getHolder();
        }
    
        @EventHandler
        public void onItemPickup(InventoryMoveItemEvent event) {
            Player p = (Player) event.getDestination().getHolder();
        }
     
  7. Offline

    Darris

    Add them to the class Eventos "InventoryEvent"?
     
  8. Offline

    Proxygames14

    You must add these methods to the class that is registered as Events in the Plugin, (Which Implements 'Listener').
    If you paste them in that class and register the events with.
    Code:
    YourPluginClass plugin = YourPluginInstance;
    YourEventClass event = YourEventInstance;
    
    Bukkit.getPluginManager().registerEvents(event , plugin);
     
  9. Offline

    Darris

    upload_2018-2-17_21-58-36.png
    Hmm, It's not working.
     
  10. Offline

    timtower Moderator Moderator

    @Darris Please post your code.
     
  11. Offline

    Lorinthio

    Also including a full stack trace can help us, help you. Just knowing you're getting a certain exception doesn't help us know where the exception is happening.
     
  12. Offline

    Proxygames14

    Ok can you send me your entire class?

    Sent from my SM-G920F using Tapatalk

    Here is the upgraded you send me, Through the app

    Code:
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryMoveItemEvent;
    import org.bukkit.event.inventory.InventoryPickupItemEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class onInventoryCheck implements Listener {
    
        @EventHandler
        public void onItemPickup(InventoryPickupItemEvent event) {
            check_player((Player) event.getInventory().getHolder());
        }
    
        @EventHandler
        public void onItemPickup(InventoryMoveItemEvent event) {
            check_player((Player) event.getDestination().getHolder());
        }
    
        public void check_player(Player p) {
            Material item = Material.BOOK;
    
            String name = "magic food";
    
            for(ItemStack item_stack : p.getInventory().getContents()) {
                if(item_stack.getType().equals(item) && item_stack.getItemMeta().getDisplayName().equals(name)) {
                    p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 60, 1, true));
                }
            }
        }
    }
    
     
    Last edited by a moderator: Feb 17, 2018
  13. Offline

    Darris

    Anyway doesn't work. No errors in the console..
     
  14. @Darris
    Post your main class and events class please
     
  15. Offline

    Darris

    Event class:
    Code:
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryMoveItemEvent;
    import org.bukkit.event.inventory.InventoryPickupItemEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class onInventoryCheck implements Listener {
    
        @EventHandler
        public void onItemPickup(InventoryPickupItemEvent event) {
            check_player((Player) event.getInventory().getHolder());
        }
    
        @EventHandler
        public void onItemPickup(InventoryMoveItemEvent event) {
            check_player((Player) event.getDestination().getHolder());
        }
    
        public void check_player(Player p) {
            Material item = Material.BOOK;
    
            String name = "Test book";
    
            for(ItemStack item_stack : p.getInventory().getContents()) {
                if(item_stack.getType().equals(item) && item_stack.getItemMeta().getDisplayName().equals(name)) {
                    p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 60, 1, true));
                }
            }
        }
    }
    Main class:
    Code:
    public class Main extends JavaPlugin{
     
        @Override
        public void onEnable() {
            Bukkit.getConsoleSender().sendMessage(UtilChat.c("&7Plugin> &aEnabled"));
            Bukkit.getPluginManager().registerEvents(new onInventoryCheck(), this);
        }
       
        @Override
        public void onDisable() {
            Bukkit.getConsoleSender().sendMessage(UtilChat.c("&7Plugin> &cDisabled"));
        }
    }
     
  16. Offline

    Proxygames14

    This code does work, You just need to change the variables
    Code:
        private void createTask() {
    
            new BukkitRunnable() {
    
                @Override
                public void run() {
                    for(Player p : Bukkit.getOnlinePlayers()) {
                        Material item = Material.BOOK;
    
                        String name = "magic food";
    
                        for(ItemStack item_stack : p.getInventory().getContents()) {
    
    
                            if(item_stack == null) continue;
                            if(!item_stack.hasItemMeta()) continue;
                            if(item_stack.getType().equals(item) && item_stack.getItemMeta().getDisplayName().equals(name)) {
                                Bukkit.broadcastMessage("book found, Effect has been added");
                                p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 60, 1, true));
                            }
                        }
                    }
                }
            }.runTaskTimer(plugin, 0, 10);
    
    
        }
    
     
  17. Offline

    Darris

    So, can I have the full code of all classes?)
     
  18. Offline

    Proxygames14

    That is everything you need, just replace (plugin) with the main class and put this in the listener
     
  19. Offline

    Darris

    When you type the name, the main class is highlighted in red. What to do?
     
  20. Offline

    Proxygames14

    ok add this in the main class, in the method 'onEnable()' and class.

    Code:
       
            public static Plugin plugin;
            public void onEnable() {
            plugin = this;
            Bukkit.getServer().getPluginManager().registerEvents(this, new onInventoryCheck ());
        }
    And then in the .runTaskTimer(plugin, 0, 10); replace plugin to (name of main class).plugin

    I'm going offline for now. If you got any more questions then stack them up and ask them tommorow because then I will take my time to finnish this.
     
  21. Offline

    Darris

    @Proxygames14 Thank you very much!

    Well, as strange as it may sound.. Not working, code that is throwing @Proxygames14
    Tried myself to fix it - failed. Still need help..

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 18, 2018
  22. Offline

    Proxygames14

    I'm was coding in 1.12 and Tomorrow I will make a brand new code in Bukkit 1.8.9.
    But I will do that tomorrow

    Sent from my SM-G920F using Tapatalk
     
  23. Offline

    Darris

    Okay. If you do, please notify me. )
     
  24. Offline

    Proxygames14

    I may live in an other time zone but you can give me your discord or Skype, to communicate better.

    Sent from my SM-G920F using Tapatalk

    Dear @Darris

    It took me some time but this is my best result.
    Instructions: Put this content into your main plugin class,
    And put the method I put in the 'onEnable()' in your existing 'onEnable()' method.
    I also made up some editable variables where you can customize the result.
    and I have made this in Spigot-1.8 So it should definitely work.

    I hope this solved your problem, If you got any more questoins you know how to contact me. :)


    Code:
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.scheduler.BukkitRunnable;
    
    public class PluginClass extends JavaPlugin {
    
        public static Plugin plugin;
    
        public void onEnable() {
            plugin = this;
            setRunnables();
        }
    
        private void setRunnables() {
            new BukkitRunnable() {
    
                @Override
                public void run() {
                    for(Player p : Bukkit.getOnlinePlayers()) {
                        for(ItemStack item : p.getInventory().getContents()) {
                            if(run_item_check(p, item)) break;
                        }
                    }
                }
            }.runTaskTimer(this, 10, 10);
        }
    
        public boolean run_item_check(Player p, ItemStack item_stack) {
    
            if(item_stack == null) return false;
            else if(!item_stack.hasItemMeta()) return false;
    
            String name = "my book";
            Material item = Material.BOOK;
            PotionEffectType potion = PotionEffectType.INCREASE_DAMAGE;
            int durationInSeconds = 1;
            int potionAmplifier = 3;
            boolean hide_particles = false;
    
            if(item_stack.getItemMeta().getDisplayName().equals(name) && item_stack.getType().equals(item)) {
                    p.addPotionEffect(new PotionEffect(potion, durationInSeconds*20, potionAmplifier-1, hide_particles));
                    return true;
            }
            return false;
        }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 18, 2018
  25. Offline

    Darris

    @Proxygames14 Thank you so much! You helped me a lot, I test and it works! However found a small bug, if you put an infinite number of seconds in the inventory you won't have a subject, the effect is not lost. If you can fix it, it'll be really cool!
     
  26. Offline

    Proxygames14

    Ah I get it, I placed 1 because you remove the book the effect will disappear by itself.
    This should solve your question, I hope I did you a great serive.

    Here is the updated version:

    Instructions: Replace both methods
    Code:
       private void setRunnables() {
    
            String name = "my book";
            Material item = Material.BOOK;
            PotionEffectType potion = PotionEffectType.INCREASE_DAMAGE;
            int durationInSeconds = 1;
            int potionAmplifier = 3;
            boolean hide_particles = false;
    
            new BukkitRunnable() {
    
                @Override
                public void run() {
                    for(Player p : Bukkit.getOnlinePlayers()) {
                        if(has_item(p, item, name)) {
                            p.addPotionEffect(new PotionEffect(potion, durationInSeconds*20, potionAmplifier-1, hide_particles));
                            continue;
                        } else {
                            if(p.hasPotionEffect(potion)) {
                                p.removePotionEffect(potion);
                            }
                        }
                    }
                }
            }.runTaskTimer(this, 10, 10);
        }
    
        public boolean has_item(Player p, Material item, String name) {
            for(ItemStack i : p.getInventory().getContents()) {
                if(i.getType().equals(item) && i.getItemMeta().getDisplayName().equals(name)) {
                    return true;
                }
            }
            return false;
        }
     
  27. Offline

    Darris

    Thank you very much! The issue has been resolved =)
     
Thread Status:
Not open for further replies.

Share This Page