Solved Per player custom GUI

Discussion in 'Plugin Development' started by mateo226, Mar 14, 2015.

Thread Status:
Not open for further replies.
  1. Hello everyone! I need some help with a plugin that I am making... It is a virtual /chest plugin but I don't know how to make the /chest private because when I open it and put something in it it saves it BUT when someone else opens it it shows him my chest... For example if I opened my chest and put something in it and closed it, and another person wanted to see his chest and he wrote /chest he would see my chest because I think I programmed it that way.... I don't want it to work like that... This is my code.... I just started making an arraylist of inventories because I think it might be the first thing to start with....
    Code:
    public class Main extends JavaPlugin implements Listener {
    
        public final Logger logger = Bukkit.getServer().getLogger();
        private static Inventory inv;
        public static List<Inventory> invList = new ArrayList<Inventory>();
       
        @Override
        public void onEnable() {
            this.getServer().getPluginManager().registerEvents(this, this);
    
        }
    
        @Override
        public void onDisable() {
    
        }
       
        @EventHandler
        public static void onPlayerJoin(PlayerJoinEvent e){
            invList.add(0, Bukkit.getServer().createInventory(e.getPlayer(), 54, "Private Chest"));
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String t,
                String[] args) {
            if (!(sender instanceof Player))
                return true;
            Player p = (Player) sender;
    
            if (cmd.getName().equalsIgnoreCase("chest")
                    && p.hasPermission("chest.use.one")) {
    
                File playerSave = new File("plugins/APChests/" + p.getName() + ".invsave");
                inv = Bukkit.getServer().createInventory(p, 54, "Private Chest");
                if (new File("plugins/APChests/" + p.getName() + ".invsave")
                        .exists()) {
                    inv = getInventoryFromFile(playerSave, inv, p);
                    p.sendMessage("The /chest file for you exists, opening it!");
                } else {
                    saveInventoryToFile(inv, new File("plugins/APChests/"),
                            p.getName());
                    p.sendMessage("The /chest file for you doesn't exist, creating it!");
                }
    
                p.openInventory(inv);
    
            }
           
    
            return false;
        }
    
        public static Inventory getInventoryFromFile(File file, Inventory inv, Player p) {
            if (file == null)
                return null;
            if (!file.exists() || file.isDirectory()
                    || !file.getAbsolutePath().endsWith(".invsave")) {
                saveInventoryToFile(inv, new File("plugins/APChests/"), p.getName());
                p.sendMessage("Tried opening inv but it does not exist! Creating one for you!");
            }
            ;
            try {
                FileConfiguration invConfig = YamlConfiguration
                        .loadConfiguration(file);
                Inventory inventory = null;
                String invTitle = invConfig.getString("Title", "Private Chest");
                int invSize = invConfig.getInt("Size", 54);
                int invMaxStackSize = invConfig.getInt("Max stack size", 64);
                InventoryHolder invHolder = null;
                if (invConfig.contains("Holder"))
                    invHolder = Bukkit.getPlayer(invConfig.getString("Holder"));
                inventory = Bukkit.getServer().createInventory(invHolder, invSize,
                        ChatColor.translateAlternateColorCodes('&', invTitle));
                inventory.setMaxStackSize(invMaxStackSize);
                try {
                    ItemStack[] invContents = new ItemStack[invSize];
                    for (int i = 0; i < invSize; i++) {
                        if (invConfig.contains("Slot " + i))
                            invContents[i] = invConfig.getItemStack("Slot " + i);
                        else
                            invContents[i] = new ItemStack(Material.AIR);
                    }
                    inventory.setContents(invContents);
                } catch (Exception ex) {
                }
                return inventory;
            } catch (Exception ex) {
                return null;
            }
        }
    
        @EventHandler
        public static void onInventoryClose(InventoryCloseEvent e) {
            if (inv != null) {
                saveInventoryToFile(inv, new File("plugins/APChests/"), e.getPlayer().getName());
            }
        }
    
        public static boolean saveInventoryToFile(Inventory inventory, File path,
                String fileName) {
            if (inventory == null || path == null || fileName == null)
                return false;
            try {
                File invFile = new File(path, fileName + ".invsave");
                if (invFile.exists())
                    invFile.delete();
                FileConfiguration invConfig = YamlConfiguration
                        .loadConfiguration(invFile);
    
                invConfig.set("Title", inventory.getTitle());
                invConfig.set("Size", inventory.getSize());
                invConfig.set("Max stack size", inventory.getMaxStackSize());
                if (inventory.getHolder() instanceof Player)
                    invConfig.set("Holder",
                            ((Player) inventory.getHolder()).getName());
    
                ItemStack[] invContents = inventory.getContents();
                for (int i = 0; i < invContents.length; i++) {
                    ItemStack itemInInv = invContents[i];
                    if (itemInInv != null)
                        if (itemInInv.getType() != Material.AIR)
                            invConfig.set("Slot " + i, itemInInv);
                }
    
                invConfig.save(invFile);
                return true;
            } catch (Exception ex) {
                return false;
            }
        }
    
    }
    
    So yeah... Any help would be great! Thank you!
     
  2. Offline

    BrickBoy55

    Try a HashMap using UUID and Inventory
     
  3. @BrickBoy55 Hey, someone told me that once but I don't know how to really use HashMaps since I never needed them before.... Can you please explain it to me how I could implement it into my own code? Thanks! EDIT: I tried implementing them into my code and this is what I have come up with:
    Code:
    
    public class Main extends JavaPlugin implements Listener {
    
        public final Logger logger = Bukkit.getServer().getLogger();
        private static Inventory inv;
        public static HashMap<UUID, Inventory> invList = new HashMap<UUID,Inventory>();
        @Override
        public void onEnable() {
            this.getServer().getPluginManager().registerEvents(this, this);
    
        }
    
        @Override
        public void onDisable() {
    
        }
       
        @EventHandler
        public static void onPlayerJoin(PlayerJoinEvent e){
            invList.put(e.getPlayer().getUniqueId(), Bukkit.getServer().createInventory(e.getPlayer(), 54, "Private Chest"));
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String t,
                String[] args) {
            if (!(sender instanceof Player))
                return true;
            Player p = (Player) sender;
    
            if (cmd.getName().equalsIgnoreCase("chest")
                    && p.hasPermission("chest.use.one")) {
    
                File playerSave = new File("plugins/APChests/" + p.getName() + ".invsave");
                inv = Bukkit.getServer().createInventory(p, 54, "Private Chest");
                if (new File("plugins/APChests/" + p.getName() + ".invsave")
                        .exists()) {
                    inv = getInventoryFromFile(playerSave, invList.get(p.getUniqueId()), p);
                    p.sendMessage("The /chest file for you exists, opening it!");
                } else {
                    saveInventoryToFile(invList.get(p.getUniqueId()), new File("plugins/APChests/"),
                            p.getName());
                    p.sendMessage("The /chest file for you doesn't exist, creating it!");
                }
    
                p.openInventory(invList.get(p.getUniqueId()));
    
            }
           
    
            return false;
        }
    
        public static Inventory getInventoryFromFile(File file, Inventory inv, Player p) {
            if (file == null)
                return null;
            if (!file.exists() || file.isDirectory()
                    || !file.getAbsolutePath().endsWith(".invsave")) {
                saveInventoryToFile(inv, new File("plugins/APChests/"), p.getName());
                p.sendMessage("Tried opening inv but it does not exist! Creating one for you!");
            }
            ;
            try {
                FileConfiguration invConfig = YamlConfiguration
                        .loadConfiguration(file);
                Inventory inventory = null;
                String invTitle = invConfig.getString("Title", "Private Chest");
                int invSize = invConfig.getInt("Size", 54);
                int invMaxStackSize = invConfig.getInt("Max stack size", 64);
                InventoryHolder invHolder = null;
                if (invConfig.contains("Holder"))
                    invHolder = Bukkit.getPlayer(invConfig.getString("Holder"));
                inventory = Bukkit.getServer().createInventory(invHolder, invSize,
                        ChatColor.translateAlternateColorCodes('&', invTitle));
                inventory.setMaxStackSize(invMaxStackSize);
                try {
                    ItemStack[] invContents = new ItemStack[invSize];
                    for (int i = 0; i < invSize; i++) {
                        if (invConfig.contains("Slot " + i))
                            invContents[i] = invConfig.getItemStack("Slot " + i);
                        else
                            invContents[i] = new ItemStack(Material.AIR);
                    }
                    inventory.setContents(invContents);
                } catch (Exception ex) {
                }
                return inventory;
            } catch (Exception ex) {
                return null;
            }
        }
    
        @EventHandler
        public static void onInventoryClose(InventoryCloseEvent e) {
            if (inv != null) {
                saveInventoryToFile(invList.get(e.getPlayer().getUniqueId()), new File("plugins/APChests/"), e.getPlayer().getName());
            }
        }
    
        public static boolean saveInventoryToFile(Inventory inventory, File path,
                String fileName) {
            if (inventory == null || path == null || fileName == null)
                return false;
            try {
                File invFile = new File(path, fileName + ".invsave");
                if (invFile.exists())
                    invFile.delete();
                FileConfiguration invConfig = YamlConfiguration
                        .loadConfiguration(invFile);
    
                invConfig.set("Title", inventory.getTitle());
                invConfig.set("Size", inventory.getSize());
                invConfig.set("Max stack size", inventory.getMaxStackSize());
                if (inventory.getHolder() instanceof Player)
                    invConfig.set("Holder",
                            ((Player) inventory.getHolder()).getName());
    
                ItemStack[] invContents = inventory.getContents();
                for (int i = 0; i < invContents.length; i++) {
                    ItemStack itemInInv = invContents[i];
                    if (itemInInv != null)
                        if (itemInInv.getType() != Material.AIR)
                            invConfig.set("Slot " + i, itemInInv);
                }
    
                invConfig.save(invFile);
                return true;
            } catch (Exception ex) {
                return false;
            }
        }
    
    }
    
    I am about to test it out hopefully it will work :)

    @BrickBoy55 Hey, just wanted to say that it works now thanks :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  4. Offline

    BrickBoy55

    No problem! I used to not know how to use HashMaps also and got yelled at for not knowing java. :p
     
  5. Offline

    Funergy

    @mateo226 I see you learned from my post from the last thread hehe xD
     
Thread Status:
Not open for further replies.

Share This Page