Gui.java

1
package sh.okx.rankup.gui;
2
3
import java.util.Arrays;
4
import java.util.Objects;
5
import java.util.stream.Collectors;
6
import lombok.AccessLevel;
7
import lombok.Getter;
8
import lombok.RequiredArgsConstructor;
9
import org.bukkit.Bukkit;
10
import org.bukkit.ChatColor;
11
import org.bukkit.Material;
12
import org.bukkit.configuration.ConfigurationSection;
13
import org.bukkit.entity.Player;
14
import org.bukkit.inventory.Inventory;
15
import org.bukkit.inventory.InventoryHolder;
16
import org.bukkit.inventory.ItemStack;
17
import org.bukkit.inventory.meta.ItemMeta;
18
import sh.okx.rankup.RankupPlugin;
19
import sh.okx.rankup.messages.Message;
20
import sh.okx.rankup.messages.MessageBuilder;
21
import sh.okx.rankup.prestige.Prestige;
22
import sh.okx.rankup.ranks.Rank;
23
import sh.okx.rankup.ranks.RankElement;
24
import sh.okx.rankup.util.Colour;
25
import sh.okx.rankup.util.ItemUtil;
26
27
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
28
public class Gui implements InventoryHolder {
29
30 2 1. isReturnToRanksGui : replaced boolean return with false for sh/okx/rankup/gui/Gui::isReturnToRanksGui → NO_COVERAGE
2. isReturnToRanksGui : replaced boolean return with true for sh/okx/rankup/gui/Gui::isReturnToRanksGui → NO_COVERAGE
  @Getter
31
  private final boolean returnToRanksGui;
32 1 1. getInventory : replaced return value with null for sh/okx/rankup/gui/Gui::getInventory → NO_COVERAGE
  @Getter
33
  private Inventory inventory;
34 1 1. getRankup : replaced return value with null for sh/okx/rankup/gui/Gui::getRankup → NO_COVERAGE
  @Getter
35
  private ItemStack rankup;
36 1 1. getCancel : replaced return value with null for sh/okx/rankup/gui/Gui::getCancel → NO_COVERAGE
  @Getter
37
  private ItemStack cancel;
38 2 1. isPrestige : replaced boolean return with false for sh/okx/rankup/gui/Gui::isPrestige → NO_COVERAGE
2. isPrestige : replaced boolean return with true for sh/okx/rankup/gui/Gui::isPrestige → NO_COVERAGE
  @Getter
39
  private boolean prestige;
40
41
  public static Gui of(Player player, Rank oldRank, Rank rank, RankupPlugin plugin,
42
      boolean returnToRanksGui) {
43
    Gui gui = new Gui(returnToRanksGui);
44
    gui.prestige = oldRank instanceof Prestige;
45
46 1 1. of : negated conditional → NO_COVERAGE
    String type = gui.prestige ? "prestige" : "rankup";
47
    String basePath = type + ".gui";
48
    ConfigurationSection config = plugin.getSection(oldRank, basePath);
49 1 1. of : negated conditional → NO_COVERAGE
    if (config == null) {
50
      plugin.getLogger().severe(
51
          "You must update your config.yml and locale/en.yml to be able to use the GUI! Your configuration files are outdated.");
52
      return null;
53
    }
54
55
    Integer rows = Gui.getInt(config, "rows");
56 2 1. of : Replaced integer multiplication with division → NO_COVERAGE
2. of : negated conditional → NO_COVERAGE
    ItemStack[] items = new ItemStack[(rows == null ? 1 : rows) * 9];
57
58
    ItemStack fill = getItem(plugin, plugin.getSection(oldRank, basePath + ".fill"), player,
59
        oldRank, rank);
60
    ItemStack cancel = getItem(plugin, plugin.getSection(oldRank, basePath + ".cancel"), player,
61
        oldRank, rank);
62
    ItemStack rankup = getItem(plugin, plugin.getSection(oldRank, basePath + ".rankup"), player,
63
        oldRank, rank);
64
65 1 1. of : removed call to sh/okx/rankup/gui/Gui::addItem → NO_COVERAGE
    addItem(items, plugin.getSection(oldRank, basePath + ".rankup"), rankup);
66 1 1. of : removed call to sh/okx/rankup/gui/Gui::addItem → NO_COVERAGE
    addItem(items, plugin.getSection(oldRank, basePath + ".cancel"), cancel);
67 1 1. of : removed call to sh/okx/rankup/gui/Gui::addItem → NO_COVERAGE
    addItem(items, plugin.getSection(oldRank, basePath + ".fill"), fill);
68
69
    gui.rankup = rankup;
70
    gui.cancel = cancel;
71
72
    Inventory inventory = Bukkit.createInventory(gui, items.length,
73
        Colour.translate(
74 1 1. of : negated conditional → NO_COVERAGE
            plugin.getMessage(oldRank, gui.prestige ? Message.PRESTIGE_TITLE : Message.TITLE)
75
                .replacePlayer(player)
76
                .replaceOldRank(oldRank)
77
                .replaceRank(rank).toString(player)));
78 1 1. of : removed call to org/bukkit/inventory/Inventory::setContents → NO_COVERAGE
    inventory.setContents(items);
79
    gui.inventory = inventory;
80 1 1. of : replaced return value with null for sh/okx/rankup/gui/Gui::of → NO_COVERAGE
    return gui;
81
  }
82
83
  public static ItemStack getItem(RankupPlugin plugin, ConfigurationSection section, Player player,
84
      RankElement<Rank> element) {
85 1 1. getItem : negated conditional → KILLED
    if (element == null) {
86 1 1. getItem : replaced return value with null for sh/okx/rankup/gui/Gui::getItem → SURVIVED
      return getItem(plugin, section, player, null, null);
87
    } else {
88
      RankElement<Rank> next = element.getNext();
89 1 1. getItem : replaced return value with null for sh/okx/rankup/gui/Gui::getItem → SURVIVED
      return getItem(plugin, section, player, element.getRank(),
90 1 1. getItem : negated conditional → SURVIVED
          (next == null ? element : next).getRank());
91
    }
92
  }
93
94
  @SuppressWarnings("deprecation")
95
  public static ItemStack getItem(RankupPlugin plugin, ConfigurationSection section, Player player,
96
      Rank oldRank, Rank rank) {
97 1 1. getItem : negated conditional → KILLED
    if (section == null) {
98
      return null;
99
    }
100
    String materialName = section.getString("material").toUpperCase();
101
102
    ItemStack item;
103 1 1. getItem : negated conditional → KILLED
    if (ItemUtil.isServerFlattened()) {
104
      Material material = Material.valueOf(materialName);
105
      item = new ItemStack(material);
106
    } else {
107
      // handle default material correctly on older versions
108 1 1. getItem : negated conditional → NO_COVERAGE
      if (materialName.equals("BLACK_STAINED_GLASS_PANE")) {
109
        materialName = "STAINED_GLASS_PANE:15";
110
      }
111
112
      String[] parts = materialName.split(":");
113
      Material material = Material.valueOf(parts[0]);
114
115 2 1. getItem : changed conditional boundary → NO_COVERAGE
2. getItem : negated conditional → NO_COVERAGE
      short type = parts.length > 1 ? Short.parseShort(parts[1]) : 0;
116
      item = new ItemStack(material, 1, type);
117
    }
118
119 2 1. getItem : negated conditional → SURVIVED
2. getItem : negated conditional → NO_COVERAGE
    if (item.getType() == Material.AIR && section.getName().equalsIgnoreCase("fill")) {
120 1 1. getItem : replaced return value with null for sh/okx/rankup/gui/Gui::getItem → NO_COVERAGE
      return item;
121
    }
122
123
    ItemMeta meta = item.getItemMeta();
124 1 1. getItem : negated conditional → KILLED
    if (section.contains("lore")) {
125 1 1. getItem : removed call to org/bukkit/inventory/meta/ItemMeta::setLore → NO_COVERAGE
      meta.setLore(Arrays
126
          .stream(format(plugin, section.getString("lore"), player, oldRank, rank).split("\n"))
127 1 1. lambda$getItem$0 : replaced return value with "" for sh/okx/rankup/gui/Gui::lambda$getItem$0 → NO_COVERAGE
          .map(string -> ChatColor.RESET + string)
128
          .collect(Collectors.toList()));
129
    }
130 1 1. getItem : negated conditional → SURVIVED
    if (section.contains("name")) {
131 1 1. getItem : removed call to org/bukkit/inventory/meta/ItemMeta::setDisplayName → SURVIVED
      meta.setDisplayName(
132
          ChatColor.RESET + format(plugin, section.getString("name"), player, oldRank, rank));
133
    }
134
    item.setItemMeta(meta);
135 1 1. getItem : replaced return value with null for sh/okx/rankup/gui/Gui::getItem → SURVIVED
    return item;
136
  }
137
138
  private static String format(RankupPlugin plugin, String message, Player player, Rank oldRank,
139
      Rank rank) {
140
    MessageBuilder builder = plugin.newMessageBuilder(message);
141 2 1. format : negated conditional → SURVIVED
2. format : negated conditional → SURVIVED
    if (oldRank != null && rank != null) {
142
      builder = builder.replacePlayer(player).replaceOldRank(oldRank).replaceRank(rank);
143
    }
144 1 1. format : replaced return value with "" for sh/okx/rankup/gui/Gui::format → SURVIVED
    return builder.toString(player);
145
  }
146
147
  private static void addItem(ItemStack[] items, ConfigurationSection section, ItemStack item) {
148
    Objects.requireNonNull(section, "GUI configuration section not found");
149 1 1. addItem : negated conditional → NO_COVERAGE
    if (section.getName().equalsIgnoreCase("fill")) {
150 2 1. addItem : changed conditional boundary → NO_COVERAGE
2. addItem : negated conditional → NO_COVERAGE
      for (int i = 0; i < items.length; i++) {
151 1 1. addItem : negated conditional → NO_COVERAGE
        if (items[i] == null) {
152
          items[i] = item;
153
        }
154
      }
155
      return;
156
    }
157
158
    String[] locations = section.getString("index").split(" ");
159
    for (String location : locations) {
160
      String[] parts = location.split("-");
161 1 1. addItem : negated conditional → NO_COVERAGE
      if (parts.length == 1) {
162
        items[Integer.parseInt(parts[0])] = item;
163
      } else {
164 3 1. addItem : changed conditional boundary → NO_COVERAGE
2. addItem : Changed increment from 1 to -1 → NO_COVERAGE
3. addItem : negated conditional → NO_COVERAGE
        for (int i = Integer.parseInt(parts[0]); i <= Integer.parseInt(parts[1]); i++) {
165
          items[i] = item;
166
        }
167
      }
168
    }
169
  }
170
171
  public static Integer getInt(ConfigurationSection section, String key) {
172
    String string = section.getString(key);
173 1 1. getInt : negated conditional → KILLED
    if (string == null) {
174 1 1. getInt : replaced Integer return value with 0 for sh/okx/rankup/gui/Gui::getInt → SURVIVED
      return null;
175
    } else {
176
      try {
177 1 1. getInt : replaced Integer return value with 0 for sh/okx/rankup/gui/Gui::getInt → KILLED
        return Integer.parseInt(string);
178
      } catch (IllegalArgumentException ex) {
179 1 1. getInt : replaced Integer return value with 0 for sh/okx/rankup/gui/Gui::getInt → NO_COVERAGE
        return null;
180
      }
181
    }
182
  }
183
184
  public void open(Player player) {
185
    player.openInventory(inventory);
186
  }
187
}

Mutations

30

1.1
Location : isReturnToRanksGui
Killed by : none
replaced boolean return with false for sh/okx/rankup/gui/Gui::isReturnToRanksGui → NO_COVERAGE

2.2
Location : isReturnToRanksGui
Killed by : none
replaced boolean return with true for sh/okx/rankup/gui/Gui::isReturnToRanksGui → NO_COVERAGE

32

1.1
Location : getInventory
Killed by : none
replaced return value with null for sh/okx/rankup/gui/Gui::getInventory → NO_COVERAGE

34

1.1
Location : getRankup
Killed by : none
replaced return value with null for sh/okx/rankup/gui/Gui::getRankup → NO_COVERAGE

36

1.1
Location : getCancel
Killed by : none
replaced return value with null for sh/okx/rankup/gui/Gui::getCancel → NO_COVERAGE

38

1.1
Location : isPrestige
Killed by : none
replaced boolean return with false for sh/okx/rankup/gui/Gui::isPrestige → NO_COVERAGE

2.2
Location : isPrestige
Killed by : none
replaced boolean return with true for sh/okx/rankup/gui/Gui::isPrestige → NO_COVERAGE

46

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

49

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

56

1.1
Location : of
Killed by : none
Replaced integer multiplication with division → NO_COVERAGE

2.2
Location : of
Killed by : none
negated conditional → NO_COVERAGE

65

1.1
Location : of
Killed by : none
removed call to sh/okx/rankup/gui/Gui::addItem → NO_COVERAGE

66

1.1
Location : of
Killed by : none
removed call to sh/okx/rankup/gui/Gui::addItem → NO_COVERAGE

67

1.1
Location : of
Killed by : none
removed call to sh/okx/rankup/gui/Gui::addItem → NO_COVERAGE

74

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

78

1.1
Location : of
Killed by : none
removed call to org/bukkit/inventory/Inventory::setContents → NO_COVERAGE

80

1.1
Location : of
Killed by : none
replaced return value with null for sh/okx/rankup/gui/Gui::of → NO_COVERAGE

85

1.1
Location : getItem
Killed by : sh.okx.rankup.ranksgui.RanksGuiTest.[engine:junit-jupiter]/[class:sh.okx.rankup.ranksgui.RanksGuiTest]/[method:testRowsWithoutGroup()]
negated conditional → KILLED

86

1.1
Location : getItem
Killed by : none
replaced return value with null for sh/okx/rankup/gui/Gui::getItem → SURVIVED

89

1.1
Location : getItem
Killed by : none
replaced return value with null for sh/okx/rankup/gui/Gui::getItem → SURVIVED

90

1.1
Location : getItem
Killed by : none
negated conditional → SURVIVED

97

1.1
Location : getItem
Killed by : sh.okx.rankup.ranksgui.RanksGuiTest.[engine:junit-jupiter]/[class:sh.okx.rankup.ranksgui.RanksGuiTest]/[method:testRowsWithGroup()]
negated conditional → KILLED

103

1.1
Location : getItem
Killed by : sh.okx.rankup.ranksgui.RanksGuiTest.[engine:junit-jupiter]/[class:sh.okx.rankup.ranksgui.RanksGuiTest]/[method:testRowsWithGroup()]
negated conditional → KILLED

108

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

115

1.1
Location : getItem
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : getItem
Killed by : none
negated conditional → NO_COVERAGE

119

1.1
Location : getItem
Killed by : none
negated conditional → SURVIVED

2.2
Location : getItem
Killed by : none
negated conditional → NO_COVERAGE

120

1.1
Location : getItem
Killed by : none
replaced return value with null for sh/okx/rankup/gui/Gui::getItem → NO_COVERAGE

124

1.1
Location : getItem
Killed by : sh.okx.rankup.ranksgui.RanksGuiTest.[engine:junit-jupiter]/[class:sh.okx.rankup.ranksgui.RanksGuiTest]/[method:testRowsWithGroup()]
negated conditional → KILLED

125

1.1
Location : getItem
Killed by : none
removed call to org/bukkit/inventory/meta/ItemMeta::setLore → NO_COVERAGE

127

1.1
Location : lambda$getItem$0
Killed by : none
replaced return value with "" for sh/okx/rankup/gui/Gui::lambda$getItem$0 → NO_COVERAGE

130

1.1
Location : getItem
Killed by : none
negated conditional → SURVIVED

131

1.1
Location : getItem
Killed by : none
removed call to org/bukkit/inventory/meta/ItemMeta::setDisplayName → SURVIVED

135

1.1
Location : getItem
Killed by : none
replaced return value with null for sh/okx/rankup/gui/Gui::getItem → SURVIVED

141

1.1
Location : format
Killed by : none
negated conditional → SURVIVED

2.2
Location : format
Killed by : none
negated conditional → SURVIVED

144

1.1
Location : format
Killed by : none
replaced return value with "" for sh/okx/rankup/gui/Gui::format → SURVIVED

149

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

150

1.1
Location : addItem
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : addItem
Killed by : none
negated conditional → NO_COVERAGE

151

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

161

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

164

1.1
Location : addItem
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : addItem
Killed by : none
Changed increment from 1 to -1 → NO_COVERAGE

3.3
Location : addItem
Killed by : none
negated conditional → NO_COVERAGE

173

1.1
Location : getInt
Killed by : sh.okx.rankup.ranksgui.RanksGuiTest.[engine:junit-jupiter]/[class:sh.okx.rankup.ranksgui.RanksGuiTest]/[method:testRowsWithGroup()]
negated conditional → KILLED

174

1.1
Location : getInt
Killed by : none
replaced Integer return value with 0 for sh/okx/rankup/gui/Gui::getInt → SURVIVED

177

1.1
Location : getInt
Killed by : sh.okx.rankup.ranksgui.RanksGuiTest.[engine:junit-jupiter]/[class:sh.okx.rankup.ranksgui.RanksGuiTest]/[method:testRowsWithGroup()]
replaced Integer return value with 0 for sh/okx/rankup/gui/Gui::getInt → KILLED

179

1.1
Location : getInt
Killed by : none
replaced Integer return value with 0 for sh/okx/rankup/gui/Gui::getInt → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.7.0