Solved How to save an inventory slot then set something in that inventory slot;

Discussion in 'Spigot Plugin Development' started by Kingskew, Jun 8, 2021.

  1. I want to save the inventory slot that someone is currently in, then later set an item in that saved inventory slot. Example: if someone is currently holding a diamond in slot 3, then i want to remove that diamond, but then 3 seconds later i want to give that diamond back in slot 3. I hope this makes sense.

    my code so far:
    Code (Java):
        public void clickEvent(PlayerInteractEvent e){
            if(this.plugin.started = true) {
                Player p = e.getPlayer();
                Material m = e.getItem().getType();
                BukkitScheduler s = p.getServer().getScheduler();
                Action act = e.getAction();
                if(m == Material.DIAMOND && ( act == Action.RIGHT_CLICK_AIR || act == Action.RIGHT_CLICK_BLOCK || act == Action.LEFT_CLICK_AIR || act == Action.LEFT_CLICK_BLOCK)) {
                    p.sendMessage("---- §bStop §bFlexing §f----");
                    p.getInventory().setItemInMainHand(new ItemStack(Material.COAL));
                    s.scheduleSyncRepeatingTask(this.plugin, new Runnable() {
                        int cnt = 1;
                        public void run() {
                            if(cnt == 3) {
                                p.sendMessage("|||||| §bSIKE §f||||||");
                                p.getInventory().setItemInMainHand(new ItemStack(Material.DIAMOND));
                            }else {
                    }, 0L, 20L);
  2. save p.getInventory().getHeldItemSlot() in a variable and use that in the runnable
  3. You could easily set a variable with that slot and later set the item back with the provided slot. That would be possible.
  4. thanks

    question: How would I do that?
    I tried to do PlayerInventory slot = p.getInventory().getHeldItemSlot(); but that didn't work.
    #4 Kingskew, Jun 9, 2021
    Last edited: Jun 9, 2021
  5. Inventory#getHeldItemSlot() return int value
    Code (Text):
    int slot = e.getPlayer().getInventory().getHeldItemSlot();
  6. oh yeah im dumb.