    001    /**
    002     * This software is provided under the terms of the Minecraft Forge Public
    003     * License v1.0.
    004     */
    005
    006    package net.minecraftforge.common;
    007
    008    import java.util.Arrays;
    009    import java.util.HashSet;
    010    import java.util.LinkedList;
    011    import java.util.List;
    012    import java.util.Set;
    013
    014    import net.minecraft.src.Achievement;
    015
    016    public class AchievementPage
    017    {
    018        private String name;
    019        private LinkedList<Achievement> achievements;
    020
    021        public AchievementPage(String name, Achievement... achievements)
    022        {
    023            this.name = name;
    024            this.achievements = new LinkedList<Achievement>(Arrays.asList(achievements));
    025        }
    026
    027        public String getName()
    028        {
    029            return name;
    030        }
    031
    032        public List<Achievement> getAchievements()
    033        {
    034            return achievements;
    035        }
    036
    037        private static LinkedList<AchievementPage> achievementPages = new LinkedList<AchievementPage>();
    038
    039        /**
    040         * Registers an achievement page.
    041         * @param page The page.
    042         */
    043        public static void registerAchievementPage(AchievementPage page)
    044        {
    045            if (getAchievementPage(page.getName()) != null)
    046            {
    047                throw new RuntimeException("Duplicate achievement page name \"" + page.getName() + "\"!");
    048            }
    049            achievementPages.add(page);
    050        }
    051
    052        /**
    053         * Will return an achievement page by its index on the list.
    054         * @param index The page's index.
    055         * @return the achievement page corresponding to the index or null if invalid index
    056         */
    057        public static AchievementPage getAchievementPage(int index)
    058        {
    059            return achievementPages.get(index);
    060        }
    061
    062        /**
    063         * Will return an achievement page by its name.
    064         * @param name The page's name.
    065         * @return the achievement page with the given name or null if no such page
    066         */
    067        public static AchievementPage getAchievementPage(String name)
    068        {
    069            for (AchievementPage page : achievementPages)
    070            {
    071                if (page.getName().equals(name))
    072                {
    073                    return page;
    074                }
    075            }
    076            return null;
    077        }
    078
    079        /**
    080         * Will return the list of achievement pages.
    081         * @return the list's size
    082         */
    083        public static Set<AchievementPage> getAchievementPages()
    084        {
    085            return new HashSet<AchievementPage>(achievementPages);
    086        }
    087
    088        /**
    089         * Will return whether an achievement is in any page or not.
    090         * @param achievement The achievement.
    091         */
    092        public static boolean isAchievementInPages(Achievement achievement)
    093        {
    094            for (AchievementPage page : achievementPages)
    095            {
    096                if (page.getAchievements().contains(achievement))
    097                {
    098                    return true;
    099                }
    100            }
    101            return false;
    102        }
    103
    104        public static String getTitle(int index)
    105        {
    106            return index == -1 ? "Minecraft" : getAchievementPage(index).getName();
    107        }
    108    }