McMMOSkillUtil.java

1
package sh.okx.rankup.requirements.requirement.mcmmo;
2
3
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
4
import com.gmail.nossr50.util.player.UserManager;
5
import java.lang.reflect.InvocationTargetException;
6
import java.lang.reflect.Method;
7
import org.bukkit.entity.Player;
8
9
/**
10
 * Because mcMMO like changing the name of their skill types.
11
 * Singleton class to access different mcMMO versions.
12
 */
13
public class McMMOSkillUtil {
14
  private static McMMOSkillUtil instance;
15
16
  private Class<?> skillTypeClass;
17
  private Method values;
18
  private Method valueOf;
19
  //private Class<?> userManagerClass;
20
  private Method getSkillLevel;
21
22
  private McMMOSkillUtil() {
23
    final String pckg = "com.gmail.nossr50.datatypes.skills.";
24
    try {
25
      skillTypeClass = Class.forName(pckg + "PrimarySkillType");
26
    } catch (ClassNotFoundException e0) {
27
      try {
28
        skillTypeClass = Class.forName(pckg + "PrimarySkill");
29
      } catch (ClassNotFoundException e1) {
30
        try {
31
          skillTypeClass = Class.forName(pckg + "SkillType");
32
        } catch (ClassNotFoundException e2) {
33
          throw new UnsupportedOperationException("mcMMO Skill Type class not found");
34
        }
35
      }
36
    }
37
    try {
38
      values = skillTypeClass.getMethod("values");
39
    } catch (NoSuchMethodException e) {
40
      throw new UnsupportedOperationException("mcMMO " + skillTypeClass + ".values() not found");
41
    }
42
    try {
43
      valueOf = skillTypeClass.getMethod("valueOf", String.class);
44
    } catch (NoSuchMethodException e) {
45
      throw new UnsupportedOperationException("mcMMO" + skillTypeClass + ".valueOf(String) not found");
46
    }
47
48
    /*try {
49
      userManagerClass = Class.forName("com.gmail.nossr50.util.player.UserManager");
50
    } catch (ClassNotFoundException e) {
51
      throw new RuntimeException("mcMMO UserManager class not found");
52
    }*/
53
54
    try {
55
      getSkillLevel = McMMOPlayer.class.getMethod("getSkillLevel", skillTypeClass);
56
    } catch (NoSuchMethodException e) {
57
      throw new UnsupportedOperationException("mcMMO UserManager.getSkillLevel(" + skillTypeClass + ") not found");
58
    }
59
  }
60
61
  public static McMMOSkillUtil getInstance() {
62 1 1. getInstance : negated conditional → NO_COVERAGE
    if (instance == null) {
63
      instance = new McMMOSkillUtil();
64
    }
65 1 1. getInstance : replaced return value with null for sh/okx/rankup/requirements/requirement/mcmmo/McMMOSkillUtil::getInstance → NO_COVERAGE
    return instance;
66
  }
67
68
  /*public String[] getSkills() {
69
    try {
70
      Enum<?>[] skills = (Enum<?>[]) values.invoke(null);
71
      String[] stringSkills = new String[skills.length];
72
      for (int i = 0; i < skills.length; i++) {
73
        stringSkills[i] = skills[i].name();
74
      }
75
      return stringSkills;
76
    } catch (IllegalAccessException | InvocationTargetException e) {
77
      throw new RuntimeException(e);
78
    }
79
  }*/
80
81
  public int getSkillLevel(Player player, String skill) {
82
    McMMOPlayer mcMMOPlayer = UserManager.getPlayer(player);
83
    try {
84
      Object skillType = skillTypeClass.cast(valueOf.invoke(null, skill.toUpperCase()));
85 1 1. getSkillLevel : replaced int return with 0 for sh/okx/rankup/requirements/requirement/mcmmo/McMMOSkillUtil::getSkillLevel → NO_COVERAGE
      return (int) getSkillLevel.invoke(mcMMOPlayer, skillType);
86
    } catch (IllegalAccessException | InvocationTargetException e) {
87
      throw new RuntimeException(e);
88
    }
89
  }
90
}

Mutations

62

1.1
Location : getInstance
Killed by : none
negated conditional → NO_COVERAGE

65

1.1
Location : getInstance
Killed by : none
replaced return value with null for sh/okx/rankup/requirements/requirement/mcmmo/McMMOSkillUtil::getInstance → NO_COVERAGE

85

1.1
Location : getSkillLevel
Killed by : none
replaced int return with 0 for sh/okx/rankup/requirements/requirement/mcmmo/McMMOSkillUtil::getSkillLevel → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.7.0