custom yml get enum

Discussion in 'Plugin Development' started by Devil0s, Sep 30, 2012.

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

    Devil0s

    Does anyone know how to get the value of an enum object from a custom yml file?

    Code:
    public static QuestTyp getQuestQuestTyp(String questname) {
            String questTyp = FileHandler.quests.getString(questname+".QuestTyp");
            QuestTyp quest = QuestTyp.valueOf(questTyp);
            return quest;
        }
    I want to get the typ of the quest with the methode.
    The methode should return the value of an enum object (QuestTyp).
    How can I read a simple input in the yml file.
    Thats my yml file:

    Code:
    quest1:
      Name: 'Killer'
      Description: 'We was conquered by Creepers. Kill them!'
      QuestTyp: 'kill' //Thats an enum value.
      ObjectTyp: 'Creeper' // Thats an enum value too
      reqAmount: 10
      MoneyReward: 40
    quest2:
    Does anyone know a good way to "read" and return it??
    My code doesnt work. There is a Nullpointer error.
    Hope you understand my problem. :D
    Thanks.
     
  2. Offline

    MrFigg

    Unfortunately YamlConfiguration doesn't have built in support for Enum values. You can use something like this;
    Code:JAVA
    1. public Enum getEnum(Class clazz, String string) throws Exception {
    2. if(!clazz.isEnum()) throw new Exception("Class "+clazz.getName()+" is not an enum.");
    3. for(Object constant : clazz.getEnumConstants()) {
    4. if(((Enum) constant).toString().equals(string)) {
    5. return (Enum) constant;
    6. }
    7. }
    8. throw new Exception("String "+string+" not a valid enum constant for "+clazz.getName());
    9. }

    That's the method I use in my SuperEasyConfig. To use it with your example it would be;
    Code:JAVA
    1. public static QuestTyp getQuestQuestTyp(String questname) {
    2. String questTyp = FileHandler.quests.getString(questname+".QuestTyp");
    3. QuestTyp quest = null;
    4. try {
    5. quest = getEnum(QuestTyp.class, questTyp);
    6. } catch(Exception ex) {
    7. ex.printStackTrace();
    8. return null;
    9. }
    10. return quest;
    11. }
     
    Devil0s likes this.
  3. Offline

    Devil0s

    Thank you very much. I'll test it.

    Im very worry about it but it doesnt work.
    There is still a Nullpointer error. I casted
    quest = getEnum(QuestTyp.class, questTyp);
    to
    quest = (QuestTyp) getEnum(QuestTyp.class, questTyp);
    Please help me! :D

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

    MrFigg

    Sorry, forgot about the reflection trickiness I used around this. Try;
    Code:JAVA
    1. public static QuestTyp getQuestQuestTyp(String questname) {
    2. String questTyp = FileHandler.quests.getString(questname+".QuestTyp");
    3. QuestTyp quest = null;
    4. try {
    5. quest = QuestTyp.values()[getEnum(QuestTyp.class, questTyp).ordinal()];
    6. } catch(Exception ex) {
    7. ex.printStackTrace();
    8. return null;
    9. }
    10. return quest;
    11. }
     
  5. Offline

    Devil0s

    Sorry. It worked with my original code. I used a wrong variable to call the methode. I used not the username but a not inialized variable. :D Very nooby mistake. xDDD
    But thanks for your source code examples.
     
Thread Status:
Not open for further replies.

Share This Page