having 1 inventory per player

Discussion in 'Plugin Development' started by ninjastar1011, Feb 21, 2013.

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

    ninjastar1011

    So iv'e been searching around looking for a bit of code that does this but really couldn't find one!
    all i really want is something that lets the player have an inventory that doesn't link to the entire server. Ive got the code for creating an inventory and opening it and all that, but i cant for the life of me find a way to set it to one player only, hers the code so far :) :

    Code:
    package me.ninjastar101.vchest;
     
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Sound;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class VirtualChest extends JavaPlugin implements Listener{
       
    public final Logger logger = Logger.getLogger("Minecraft");
    public static Inventory VirtualChest;
       
       
        public void onDisable(){
            PluginDescriptionFile pdfFile = this.getDescription();
           
            this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Dis-abled!");
     
        }
           
           
           
        public void onEnable(){
       
        PluginDescriptionFile pdfFile = this.getDescription();
        this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Enabled!");
        VirtualChest = Bukkit.createInventory(null, 27, "Virtual Chest");
        getServer().getPluginManager().registerEvents(this, this);
        }
       
       
       
     
            public boolean onCommand(CommandSender sender, Command command,
                    String label, String[] args) {
                if(label.equalsIgnoreCase("virtualchest")){
                    if(sender instanceof Player){
                        ((Player)sender).openInventory(VirtualChest);
                       
                    }
                   
                   
                   
                }if(label.equalsIgnoreCase("getender")){
                    if(sender instanceof Player){
                        Player player = (Player)sender;
                        Inventory inventory = ((Player)sender).getEnderChest();
                        player.playSound(player.getLocation(), Sound.CHEST_OPEN, 5.0F, 1.0F);
                       
                        ((    Player)sender).openInventory(inventory);
                       
                    }
                   
                   
                   
                }
               
               
               
                return false;
            }   
        }
    
    thanks, any help is appreciated

    -ninja
     
  2. Offline

    vildaberper

    Make a HashMap with the players names as keys and the inventories as values.

    So create a function like:
    Code:
    static Map<Player, Inventory> inventories = new HashMap<Player, Inventory>();
     
    Inventory getInventory(Player player){
      Inventory i = inventories.get(player);
     
      if(i == null){
          i = Bukkit.createInventory(null, 27, player.getName() + "'s virtual chest");
          inventories.put(player, i);
      }
      return i;
    }
     
  3. Offline

    ZachBora

    Don't put players in a hashmap, put the player name. If you store players you can cause memory leaks.
     
  4. Offline

    ninjastar1011

    well thanks, but im kind of a noob :p so where would i put that within the code?
     
Thread Status:
Not open for further replies.

Share This Page