What event?

Discussion in 'Spigot Plugin Development' started by TheDiamondWorm, May 21, 2015.

  1. What event is called when a player moves an item from their inventory into another inventory, essentially storing it there? (Not necessarily a chest, an inventory created by my plugin for example).
     
  2. The only way I can think of would be the inventory click event. I'm not entirely sure if there is a way to check if an item has been moved, but there are ways to check. I think it's possible to discern which inventory the slot was in with the getRawSlot() method.
     
    • Informative Informative x 1
  3. I am trying this:
    Code (Java):
        @EventHandler
        public void onMove(InventoryMoveItemEvent e) {
            if (e.getDestination() != null && e.getInitiator() != null) {
                if (e.getInitiator() instanceof Player) {
                    Player p = (Player) e.getInitiator();
                    File f = new File(getDataFolder(), File.separator
                            + p.getUniqueId().toString() + ".yml");
                    FileConfiguration pf = YamlConfiguration.loadConfiguration(f);
                    if (!f.exists()) {
                        try {
                            f.createNewFile();
                        } catch (IOException ex) {
                        }
                        pf.set(p.getUniqueId().toString(), e.getItem());
                        try {
                            pf.save(f);

                        } catch (IOException ex) {

                        }

                    }
                }
            }
        }
    I want it to store the itemstack they moved into the inventory into their file. But, this is the result:
    Code (Text):
    2218cc3f-8b4f-46b0-96d9-8a296548ec83:
      ==: org.bukkit.inventory.ItemStack
      type: AIR
      damage: -1
      amount: 0
     
     
  4. Code (Text):
     File f = new File(getDataFolder(), File.separator
                            + p.getUniqueId().toString() + ".yml");
                    FileConfiguration pf = YamlConfiguration.loadConfiguration(f);
                    if (!f.exists()) {
                        try {
                            f.createNewFile();
                        } catch (IOException ex) {
                        }
                        pf.set(p.getUniqueId().toString(), e.getItem());
                        try {
                            pf.save(f);

                        } catch (IOException ex) {

                        }

                    }
    Logic error here; see if you can spot it.
     
  5. PHP:
    pf.set(p.getUniqueId().toString(), e.getItem().toString());
    That's one error. Solved
     
  6. I'm fairly sure you can save items to yaml files because they extend a certain class (I forget which one though). I think it's the same thing with locations and offline players too.
     
    • Like Like x 1

  7. Really? ItemStack is configuration serializable, an ItemStack can be grabbed from the config with getItemStack(path). I've been able to store a players entire inventory to a yml file and load it from there fine, so I assumed I could do it with a single ItemStack.
     
  8. You could easily just save all its attributes as a single string
     
  9. No - not what I was talking about, guys. Look closer.
    Code (Text):
     if (!f.exists()) {
                        try {
                            f.createNewFile();
                        } catch (IOException ex) {
                        }
                        pf.set(p.getUniqueId().toString(), e.getItem());
                        try {
                            pf.save(f);

                        } catch (IOException ex) {

                        }

                    }
    If f does not exist, the code will run. But if it does exist... then what? Nothing happens!

    Also, you're never assigning a value to pf again after you check if f doesn't exist; which will cause a NPE.
     
    • Like Like x 2
  10. I have this now:
    Code (Java):
        @EventHandler
        public void onMove(InventoryMoveItemEvent e) {
            if (e.getDestination() != null && e.getInitiator() != null) {
                if (e.getInitiator() instanceof Player) {
                    Player p = (Player) e.getInitiator();
                    File f = new File(getDataFolder(), File.separator
                            + p.getUniqueId().toString() + ".yml");
                    FileConfiguration pf = YamlConfiguration.loadConfiguration(f);
                    if (!f.exists()) {
                        try {
                            f.createNewFile();
                        } catch (IOException ex) {
                        }
                    }
                    pf.set(p.getUniqueId().toString(), e.getItem());
                    try {
                        pf.save(f);

                    } catch (IOException ex) {

                    }
                }
            }
        }
    However, it still does not work properly.
     
  11. pf will be null if f doesn't exist still. What's your main issue though?
     

  12. If I move an item from my inventory into my custom inventory, I want the itemstack to be saved to the file, but nothing is happening.
     
  13. I got a big part of it working via InventoryClickEvent:
    Code (Java):
        @EventHandler
        public void onClick(InventoryClickEvent e) {
            if (e.getWhoClicked() instanceof Player) {
                Player p = (Player) e.getWhoClicked();
                File f = new File(getDataFolder(), File.separator
                        + p.getUniqueId().toString() + ".yml");
                if (!f.exists()) {
                    try {
                        f.createNewFile();
                    } catch (IOException ex) {
                    }
                }
                FileConfiguration pf = YamlConfiguration.loadConfiguration(f);
                if (e.getClickedInventory() != null) {
                    Inventory top = e.getView().getTopInventory();
                    Inventory bottom = e.getView().getBottomInventory();
                    if (top.getType() == InventoryType.CHEST
                            && bottom.getType() == InventoryType.PLAYER) {
                        if (e.getClickedInventory()
                                .getTitle()
                                .equalsIgnoreCase(
                                        ChatColor.DARK_RED + "Reset Chest")) {
                            if (e.getCurrentItem() != null
                                    && e.getCurrentItem().getType() != Material.AIR) {
                                pf.set(p.getUniqueId().toString(),
                                        e.getCurrentItem());
                                try {
                                    pf.save(f);
                                } catch (IOException ex) {

                                }
                            }
                        }
                    }

                }
            }
        }
    Now, the YML looks like this:
    Code (Text):
    2218cc3f-8b4f-46b0-96d9-8a296548ec83:
      ==: org.bukkit.inventory.ItemStack
      type: STONE
      amount: 64
     
    But, here are the following problems:
    If I take the 64 stone out of the inventory, it is still in the YML file.
    If I replace it with 1 stone, the YML file will not update until I place the 1 stone, then click it.
     
  14. Do you mind telling us what you're actually trying to accomplish, that way we could possibly give you a better method
     
  15. Basically, a personal vault plugin. The vault is an inventory created belonging to the player that sent the command, titled (players) Vault, with 9 slots. However, the 8 slots of the vault are replaced with barriers which can not be clicked, and are "unusable", so only one slot of the vault is free. When a player stores an item in the vault, I want to store it into their file.
     
    • Like Like x 1
  16. Code (Java):
        @EventHandler
        public void onVaultClose(InventoryCloseEvent e) {
            if (e.getPlayer() instanceof Player) {
                Player p = (Player) e.getPlayer();
                File f = new File(getDataFolder(), File.separator
                        + p.getUniqueId().toString() + ".yml");
                if (!f.exists()) {
                    try {
                        f.createNewFile();
                    } catch (IOException ex) {
                    }
                }

                FileConfiguration pf = YamlConfiguration.loadConfiguration(f);
                if (e.getInventory() != null) {
                    if (e.getInventory()
                            .getTitle()
                            .equalsIgnoreCase(
                                    ChatColor.DARK_RED + p.getName()
                                            + "s Reset Chest")) {
                        for (ItemStack i : e.getInventory().getContents()) {
                            if (i != null) {
                                if (i.getType() != Material.THIN_GLASS
                                        && i.hasItemMeta() && !i.getItemMeta()
                                                .getDisplayName()
                                                .equalsIgnoreCase(
                                                        ChatColor.RED
                                                                + "You can not store here!")) {
                                    // The above code is supposed to check if the
                                    // item is not a "slot-blocking" item, and if
                                    // so, add it to the file
                                    pf.set(p.getUniqueId().toString(), i);
                                }
                            }
                        }
                    }
                    try {
                        pf.save(f);
                    } catch (IOException ex) {

                    }
                }
            }

        }
    The method is being called as Notepad++ asks me to refresh the file, which means the file has been saved. Unfortunately, no itemstacks are added to the file.
     
  17. You should put all the file loading and saving parts into separate methods.

    what if the item doesnt have ItemMeta, but it isnt the glass, it wouldnt store it...
     
  18. I believe:
    Code (Text):
    !i.getItemMeta()
                                                .getDisplayName()
                                                .equalsIgnoreCase(
                                                        ChatColor.RED
                                                                + "You can not store here!")
    can give you an error if the item does not have a display name. But I don't think this would be an issue because you said it is still saving.

    Could this be a typo: ChatColor.DARK_RED+ p.getName()+"s Reset Chest"
    You might have put an apostrophe in the inventory's title.