connect a custom item to a player

Discussion in 'Plugin Development' started by Shmobi, Mar 30, 2015.

Thread Status:
Not open for further replies.
  1. Hello,
    i created my own itemstack for a new item i want to include (backpack)
    BackPackStack (open)
    Code:
    public class BackPackStack extends ItemStack {
    
       private final static String name = "Backpack";
       private final static List<String> lore = generateLore();
    
       private UUID owner;
       private Inventory inventory;
    
       public BackPackStack() {
         super(Material.CHEST, 1);
         inventory = Bukkit.getServer().createInventory(null, 18, name);
         ItemMeta meta = getItemMeta();
         meta.setDisplayName(name);
         meta.setLore(lore);
         setItemMeta(meta);
       }
    
       public void setOwner(final UUID pOwner) {
         owner = pOwner;
       }
    
       public UUID getOwner() {
         return owner;
       }
    
       public boolean showInventory(final Player pPlayer) {
         if (pPlayer != null)
           if (pPlayer.getUniqueId().equals(owner)) {
             pPlayer.openInventory(inventory);
             return true;
           }
         return false;
       }
    
       private static List<String> generateLore() {
         List<String> list = new ArrayList<String>();
         list.add("Extends your inventory");
         return list;
       }
    
    }
    

    - every player can only have 1 backpack
    - every backpack has an owner
    - a backpack can only be opened by the owner

    So far so good, everything works fine. But when a reload or a restart happens i get some problems. i save the inventory of the backpack and after that i do the reload. how can my plugin figure out which backpackitem belongs to which player after the reload?

    I am also thinking about removing all backpackitems with every reload/restart and safing the backpackinventory for every player. then i load all inventories in my onEnable and add to every player who had a backpackinventory a new backpackitem and set the inventory. but this would mean to remove and add on every reload items to the players inv. kind of ugly.

    also what to do if the backpack is laing arround in a chest instead of the players inventory?
     
  2. Offline

    sablednah

    How are you saving the backpack? Can't you add saving the uuid along with the contents when you save it?

    And yes - bukkikt isn't going to save your extended data on reload/restart = you'll have to save it all and reload it each time.
     
  3. @sablednah do items have uuids? and even if, can i load an item via its uuid through the serverobject like i can with players? the uuid doesnt help me at all when i cant load the item with it. also my question was "what to do..." and not "does bukkit..."

    i think i will go with safing the inventory and adding/removing backpacks with every reload/restart
     
  4. Offline

    ferrago

    Sounds like you don't want to allow the Player to take the backpack out of their inventory. I recommend listening to inventory events and ensuring if the item being moved from one inventory to another is an instance of your custom backpack then cancel the event.

    You can save the player's UUID when you save the inventory. Then when player joins check if they have a previously loaded inventory, if they do load it up. If they generate them a new one.

    This is how I would recommend designing this plugin.
    Player logs into server - check if there is a config file for that player, if there is then load the inventory and give them their backpack which is linked to that inventory. (Recommend maybe a HashMap<UUID, Inventory>). If not create a new pairing for that player and give them the "empty" backpack.

    When the player logs out save the backpack inventory to a config file specific to the player.

    If you want something a bit faster instead of using a .yml config file you can use MySql.

    Here is an example that I use in my old backpack plugin. You won't understand some of the custom methods not shown, however you should understand the "gist" (pun) of it.
    https://gist.github.com/ultiferrago/12ded4f72c2d9f4b6ad3
     
    Last edited by a moderator: Mar 30, 2015
  5. Offline

    sablednah

    I meant save the players UUID. And was pointing out if you want your data to persist across restarts then you will need to save it some way your self and reload.

    Id look at loading/saving a players backback on playerjoin and the quick and kicked events.
     
  6. @ferrago nono, i want them to be able to drop it or put it in a chest. thats the problem. how can i get the itemstack after a reload from a random chest i dont know?!

    @ferrago is there a function in the bukkit api to save an inventory or do i have to do that on my own? because i dont want to lose things like enchantments etc through the safe/load process.
     
    Last edited by a moderator: Mar 30, 2015
  7. Offline

    ferrago

    You shouldn't need to. The only time you care about what information that specific pack has is whenever it is opened. So for example the way I do mine. I don't actually load the backpack until the first time the player "interacts" with the pack. IE right click.

    Look at - https://gist.github.com/ultiferrago/12ded4f72c2d9f4b6ad3
    I use the FileConfiguration class provided by Bukkit API, and cycle through and write all the relevant information to the .yml file. If you want to see the Config class I use for my stuff it is here - https://gist.github.com/ultiferrago/7f11e46c1080dc977929

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: Mar 30, 2015
  8. Offline

    NathanWolf

Thread Status:
Not open for further replies.

Share This Page