Thread: Help with usage of Enums

Results 1 to 6 of 6
  1. #1 Help with usage of Enums 
    Registered Member
    Join Date
    Mar 2016
    Posts
    40
    Thanks given
    10
    Thanks received
    7
    Rep Power
    13
    Hello, I am trying to find the int[] runes and level integers with the use of componentIds,

    This is the current enum im using:
    Code:
    public enum LunarSpell {
    		BAKE_PIE(38, 65, 60, new int[] { Magic.ASTRAL_RUNE, 1, FIRE, 5, WATER, 4 }),
    		CURE_PLANT(SpellType.ON_OBJECT, 28180535, 66, 60, new int[] { ASTRAL, 1, EARTH, 8 }),
    		MONSTER_EXAMINE(SpellType.ON_NPC, 28, 66, 61, new int[] { COSMIC, 1, ASTRAL, 1, MIND, 1 }),
    		NPC_CONTACT(26, 67, 63, new int[] { COSMIC, 1, ASTRAL, 1, AIR, 2 }),
    		CURE_OTHER(SpellType.ON_PLAYER, 23, 68, 65, new int[] { ASTRAL, 1, EARTH, 10 }),
    		SPELLBOOK_SWAP(34, 96, 130, new int[] { LAW, 1, COSMIC, 2, ASTRAL, 3 });
    
    		private final int componentId;
    		private final SpellType type;
    		private final int requiredLevel;
    		private final int experience;
    		private final int[] runes;
    
    		private LunarSpell(int componentId, int requiredLevel, int experience, int... runes) {
    			this(SpellType.STANDARD, componentId, requiredLevel, experience, runes);
    		}
    
    		private LunarSpell(SpellType type, int componentId, int requiredLevel, int experience, int... runes) {
    			this.type = type;
    			this.componentId = componentId;
    			this.requiredLevel = requiredLevel;
    			this.experience = experience;
    			this.runes = runes;
    		}
    
    		public SpellType getType() {
    			return type;
    		}
    
    		public int getExperience() {
    			return experience;
    		}
    
    		public int[] getRunes() {
    			return runes;
    		}
    
    		public int getRequiredLevel() {
    			return requiredLevel;
    		}
    
    		public int getComponentId() {
    			return componentId;
    		}
    
    		public String getName() {
    			String name = name().toLowerCase().replace("_", " ");
    			return name.substring(0, 1).toUpperCase() + name.substring(1);
    		}
    
    	}
    this is the calling method im trying to use:
    Code:
    public static boolean checkReqs (Player p, int componentId){
    if (!Magic.checkRunes(player, true, getRunesForSpell(componentId))
    return false;
    }
    the only problem is i don't know the correct method for
    Code:
    getRunesForSpell(componentId)
    (trying to find the int[] values in the enum related to the componentId)

    thanks
    rep++
    Reply With Quote  
     

  2. #2  
    Contributor

    clem585's Avatar
    Join Date
    Sep 2013
    Posts
    3,788
    Thanks given
    706
    Thanks received
    702
    Rep Power
    570
    Quote Originally Posted by spawning718 View Post
    Hello, I am trying to find the int[] runes and level integers with the use of componentIds,

    This is the current enum im using:
    Code:
    public enum LunarSpell {
    		BAKE_PIE(38, 65, 60, new int[] { Magic.ASTRAL_RUNE, 1, FIRE, 5, WATER, 4 }),
    		CURE_PLANT(SpellType.ON_OBJECT, 28180535, 66, 60, new int[] { ASTRAL, 1, EARTH, 8 }),
    		MONSTER_EXAMINE(SpellType.ON_NPC, 28, 66, 61, new int[] { COSMIC, 1, ASTRAL, 1, MIND, 1 }),
    		NPC_CONTACT(26, 67, 63, new int[] { COSMIC, 1, ASTRAL, 1, AIR, 2 }),
    		CURE_OTHER(SpellType.ON_PLAYER, 23, 68, 65, new int[] { ASTRAL, 1, EARTH, 10 }),
    		SPELLBOOK_SWAP(34, 96, 130, new int[] { LAW, 1, COSMIC, 2, ASTRAL, 3 });
    
    		private final int componentId;
    		private final SpellType type;
    		private final int requiredLevel;
    		private final int experience;
    		private final int[] runes;
    
    		private LunarSpell(int componentId, int requiredLevel, int experience, int... runes) {
    			this(SpellType.STANDARD, componentId, requiredLevel, experience, runes);
    		}
    
    		private LunarSpell(SpellType type, int componentId, int requiredLevel, int experience, int... runes) {
    			this.type = type;
    			this.componentId = componentId;
    			this.requiredLevel = requiredLevel;
    			this.experience = experience;
    			this.runes = runes;
    		}
    
    		public SpellType getType() {
    			return type;
    		}
    
    		public int getExperience() {
    			return experience;
    		}
    
    		public int[] getRunes() {
    			return runes;
    		}
    
    		public int getRequiredLevel() {
    			return requiredLevel;
    		}
    
    		public int getComponentId() {
    			return componentId;
    		}
    
    		public String getName() {
    			String name = name().toLowerCase().replace("_", " ");
    			return name.substring(0, 1).toUpperCase() + name.substring(1);
    		}
    
    	}
    this is the calling method im trying to use:
    Code:
    public static boolean checkReqs (Player p, int componentId){
    if (!Magic.checkRunes(player, true, getRunesForSpell(componentId))
    return false;
    }
    the only problem is i don't know the correct method for
    Code:
    getRunesForSpell(componentId)
    (trying to find the int[] values in the enum related to the componentId)

    thanks
    rep++
    I made a "getRunesFromComponent" method instead of a "getRunesForSpell" one. It goes in the enum somewhere. Add the 2 other along with it:

    Code:
    private static ArrayList<LunarSpell> spells = new ArrayList<LunarSpell>();
    
    static {
        for (LunarSpell s : LunarSpell.values()) {
    	spells.add(s);
        }
    }
    
    public static int[] getRunesFromComponent(int componentId) {
        for (LunarSpell s : spells) {
            if (s.getComponent() == componentId) {
                return s.getRunes();
            }
        }
        return null;
    }
    Project thread
    Reply With Quote  
     

  3. #3  
    Registered Member
    Join Date
    Mar 2016
    Posts
    40
    Thanks given
    10
    Thanks received
    7
    Rep Power
    13
    Quote Originally Posted by clem585 View Post
    I made a "getRunesFromComponent" method instead of a "getRunesForSpell" one. It goes in the enum somewhere. Add the 2 other along with it:

    Code:
    private static ArrayList<LunarSpell> spells = new ArrayList<LunarSpell>();
    
    static {
        for (LunarSpell s : LunarSpell.values()) {
    	spells.add(s);
        }
    }
    
    public static int[] getRunesFromComponent(int componentId) {
        for (LunarSpell s : spells) {
            if (s.getComponent() == componentId) {
                return s.getRunes();
            }
        }
        return null;
    }
    Thanks, though i thought there was a more simpler way than looping all the spells?
    Reply With Quote  
     

  4. #4  
    Registered Member
    Shamon King's Avatar
    Join Date
    Aug 2007
    Posts
    3,335
    Thanks given
    90
    Thanks received
    228
    Rep Power
    1363
    Quote Originally Posted by spawning718 View Post
    Thanks, though i thought there was a more simpler way than looping all the spells?
    There is. Store them in a map.
    Code:
    	private static final Map<Integer, LunarSpell> LUNAR_SPELLS = Arrays.stream(LunarSpell.values()).collect(
    			Collectors.toMap(LunarSpell::getComponentId, Function.identity()));
    Code:
    LunarSpell spell = LUNAR_SPELLS.get(componentId)
    Reply With Quote  
     

  5. #5  
    Contributor

    clem585's Avatar
    Join Date
    Sep 2013
    Posts
    3,788
    Thanks given
    706
    Thanks received
    702
    Rep Power
    570
    Quote Originally Posted by Shamon King View Post
    There is. Store them in a map.
    Code:
    	private static final Map<Integer, LunarSpell> LUNAR_SPELLS = Arrays.stream(LunarSpell.values()).collect(
    			Collectors.toMap(LunarSpell::getComponentId, Function.identity()));
    Code:
    LunarSpell spell = LUNAR_SPELLS.get(componentId)
    Oh you're right it's more efficient as long as he only uses component id. Then, he can use ArrayList if he also has different search params than CID.
    Project thread
    Reply With Quote  
     

  6. #6  
    Banned Help with usage of Enums Market Banned

    -3clipse-'s Avatar
    Join Date
    May 2015
    Posts
    839
    Thanks given
    101
    Thanks received
    311
    Rep Power
    389
    Quote Originally Posted by Shamon King View Post
    There is. Store them in a map.
    Code:
    	private static final Map<Integer, LunarSpell> LUNAR_SPELLS = Arrays.stream(LunarSpell.values()).collect(
    			Collectors.toMap(LunarSpell::getComponentId, Function.identity()));
    Code:
    LunarSpell spell = LUNAR_SPELLS.get(componentId)
    After that, I would make an optional just in case the ID he adds nulls the spell.
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Replies: 12
    Last Post: 11-08-2010, 06:35 PM
  2. Help with Altar of Guthix
    By infinity in forum Help
    Replies: 3
    Last Post: 09-22-2010, 11:44 AM
  3. Help with altar of guthix
    By HenryL in forum Help
    Replies: 6
    Last Post: 01-30-2010, 10:50 AM
  4. [HELP] Need help with alot of things.
    By munch in forum Help
    Replies: 35
    Last Post: 10-03-2009, 04:25 PM
  5. Help with piece of code!
    By harraj128 in forum Help
    Replies: 12
    Last Post: 03-24-2009, 10:32 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •