StringMessageBuilder.java

1
package sh.okx.rankup.messages;
2
3
import java.util.Objects;
4
import java.util.regex.Matcher;
5
import java.util.regex.Pattern;
6
import me.clip.placeholderapi.PlaceholderAPI;
7
import org.bukkit.Bukkit;
8
import org.bukkit.command.CommandSender;
9
import org.bukkit.configuration.ConfigurationSection;
10
import org.bukkit.entity.Player;
11
import sh.okx.rankup.prestige.Prestige;
12
import sh.okx.rankup.prestige.Prestiges;
13
import sh.okx.rankup.ranks.Rank;
14
import sh.okx.rankup.util.Colour;
15
16
public class StringMessageBuilder implements MessageBuilder {
17
18
  private String message;
19
20
  public StringMessageBuilder(String message) {
21
    this.message = message;
22
  }
23
24
  public static StringMessageBuilder of(ConfigurationSection config, Message message) {
25 1 1. of : replaced return value with null for sh/okx/rankup/messages/StringMessageBuilder::of → NO_COVERAGE
    return StringMessageBuilder.of(config, message.getName());
26
  }
27
28
  private static StringMessageBuilder of(ConfigurationSection config, String message) {
29
    String string = config.getString(message);
30
    Objects.requireNonNull(string, "Configuration message '" + message + "' not found!");
31 1 1. of : replaced return value with null for sh/okx/rankup/messages/StringMessageBuilder::of → NO_COVERAGE
    return new StringMessageBuilder(Colour.translate(string));
32
  }
33
34
  public StringMessageBuilder replace(Variable variable, Object value) {
35 1 1. replace : replaced return value with null for sh/okx/rankup/messages/StringMessageBuilder::replace → NO_COVERAGE
    return replaceKey(variable.name(), value);
36
  }
37
38
  @Override
39
  public StringMessageBuilder replaceKey(String name, Object value) {
40 1 1. replaceKey : negated conditional → NO_COVERAGE
    if (value == null) {
41 1 1. replaceKey : replaced return value with null for sh/okx/rankup/messages/StringMessageBuilder::replaceKey → NO_COVERAGE
      return this;
42
    }
43
    Pattern pattern = Pattern.compile("\\{" + name + "}", Pattern.CASE_INSENSITIVE);
44
    Matcher matcher = pattern.matcher(message);
45
    this.message = matcher.replaceAll(String.valueOf(value));
46 1 1. replaceKey : replaced return value with null for sh/okx/rankup/messages/StringMessageBuilder::replaceKey → NO_COVERAGE
    return this;
47
  }
48
49
  public StringMessageBuilder replaceFirstPrestige(Rank rank, Prestiges prestiges, String with) {
50 2 1. replaceFirstPrestige : negated conditional → NO_COVERAGE
2. replaceFirstPrestige : negated conditional → NO_COVERAGE
    if (prestiges != null && prestiges.getFirst().equals(rank)) {
51
      replace(Variable.OLD_RANK, with);
52
    }
53 1 1. replaceFirstPrestige : replaced return value with null for sh/okx/rankup/messages/StringMessageBuilder::replaceFirstPrestige → NO_COVERAGE
    return this;
54
  }
55
56
  /**
57
   * Fails the MessageBuilder if the message is empty. if this fails, all subsequent calls to that
58
   * MessageBuilder will do nothing
59
   *
60
   * @return a NullMessageBuilder if the message is empty, itself otherwise
61
   */
62
  public MessageBuilder failIfEmpty() {
63 1 1. failIfEmpty : replaced return value with null for sh/okx/rankup/messages/StringMessageBuilder::failIfEmpty → KILLED
    return failIf(message.isEmpty());
64
  }
65
66
  public MessageBuilder failIf(boolean value) {
67 1 1. failIf : negated conditional → KILLED
    if (value) {
68 1 1. failIf : replaced return value with null for sh/okx/rankup/messages/StringMessageBuilder::failIf → KILLED
      return new NullMessageBuilder();
69
    } else {
70 1 1. failIf : replaced return value with null for sh/okx/rankup/messages/StringMessageBuilder::failIf → NO_COVERAGE
      return this;
71
    }
72
  }
73
74
  @Override
75
  public MessageBuilder replacePlayer(CommandSender sender) {
76 1 1. replacePlayer : replaced return value with null for sh/okx/rankup/messages/StringMessageBuilder::replacePlayer → NO_COVERAGE
    return replace(Variable.PLAYER, sender.getName());
77
  }
78
79
  @Override
80
  public MessageBuilder replaceRank(Rank rank) {
81 1 1. replaceRank : replaced return value with null for sh/okx/rankup/messages/StringMessageBuilder::replaceRank → NO_COVERAGE
    return replace(Variable.RANK, rank.getRank())
82
        .replace(Variable.RANK_NAME, rank.getDisplayName());
83
  }
84
85
  @Override
86
  public MessageBuilder replaceOldRank(Rank rank) {
87 1 1. replaceOldRank : negated conditional → NO_COVERAGE
    if (rank instanceof Prestige) {
88
      Prestige prestige = (Prestige) rank;
89
      replace(Variable.FROM, prestige.getFrom());
90
      replace(Variable.TO, prestige.getTo());
91
    }
92 1 1. replaceOldRank : replaced return value with null for sh/okx/rankup/messages/StringMessageBuilder::replaceOldRank → NO_COVERAGE
    return replace(Variable.OLD_RANK, rank.getRank())
93
        .replace(Variable.OLD_RANK_NAME, rank.getDisplayName());
94
  }
95
96
  @Override
97
  public MessageBuilder replaceSeconds(long seconds, long secondsLeft) {
98 1 1. replaceSeconds : replaced return value with null for sh/okx/rankup/messages/StringMessageBuilder::replaceSeconds → NO_COVERAGE
    return replace(Variable.SECONDS, seconds)
99
        .replace(Variable.SECONDS_LEFT, secondsLeft);
100
  }
101
102
  public void send(CommandSender sender) {
103
    String msg = message;
104 2 1. send : negated conditional → NO_COVERAGE
2. send : negated conditional → NO_COVERAGE
    if (sender instanceof Player && Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
105
      msg = PlaceholderAPI.setPlaceholders((Player) sender, msg);
106
    }
107 1 1. send : removed call to org/bukkit/command/CommandSender::sendMessage → NO_COVERAGE
    sender.sendMessage(msg);
108
  }
109
110
  /**
111
   * Sends the message to all players ie, calls MessageBuilder#send(Player) for all players online,
112
   * and sends the message in the console.
113
   */
114
  public void broadcast() {
115
    for (Player player : Bukkit.getOnlinePlayers()) {
116 1 1. broadcast : removed call to sh/okx/rankup/messages/StringMessageBuilder::send → NO_COVERAGE
      send(player);
117
    }
118 1 1. broadcast : removed call to sh/okx/rankup/messages/StringMessageBuilder::send → NO_COVERAGE
    send(Bukkit.getConsoleSender());
119
  }
120
121
  @Override
122
  public String toString() {
123 1 1. toString : replaced return value with "" for sh/okx/rankup/messages/StringMessageBuilder::toString → NO_COVERAGE
    return message;
124
  }
125
}

Mutations

25

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

31

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

35

1.1
Location : replace
Killed by : none
replaced return value with null for sh/okx/rankup/messages/StringMessageBuilder::replace → NO_COVERAGE

40

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

41

1.1
Location : replaceKey
Killed by : none
replaced return value with null for sh/okx/rankup/messages/StringMessageBuilder::replaceKey → NO_COVERAGE

46

1.1
Location : replaceKey
Killed by : none
replaced return value with null for sh/okx/rankup/messages/StringMessageBuilder::replaceKey → NO_COVERAGE

50

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

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

53

1.1
Location : replaceFirstPrestige
Killed by : none
replaced return value with null for sh/okx/rankup/messages/StringMessageBuilder::replaceFirstPrestige → NO_COVERAGE

63

1.1
Location : failIfEmpty
Killed by : sh.okx.rankup.messages.MessageBuilderTest.[engine:junit-jupiter]/[class:sh.okx.rankup.messages.MessageBuilderTest]/[method:testFailIfEmpty()]
replaced return value with null for sh/okx/rankup/messages/StringMessageBuilder::failIfEmpty → KILLED

67

1.1
Location : failIf
Killed by : sh.okx.rankup.messages.MessageBuilderTest.[engine:junit-jupiter]/[class:sh.okx.rankup.messages.MessageBuilderTest]/[method:testFailIfEmpty()]
negated conditional → KILLED

68

1.1
Location : failIf
Killed by : sh.okx.rankup.messages.MessageBuilderTest.[engine:junit-jupiter]/[class:sh.okx.rankup.messages.MessageBuilderTest]/[method:testFailIfEmpty()]
replaced return value with null for sh/okx/rankup/messages/StringMessageBuilder::failIf → KILLED

70

1.1
Location : failIf
Killed by : none
replaced return value with null for sh/okx/rankup/messages/StringMessageBuilder::failIf → NO_COVERAGE

76

1.1
Location : replacePlayer
Killed by : none
replaced return value with null for sh/okx/rankup/messages/StringMessageBuilder::replacePlayer → NO_COVERAGE

81

1.1
Location : replaceRank
Killed by : none
replaced return value with null for sh/okx/rankup/messages/StringMessageBuilder::replaceRank → NO_COVERAGE

87

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

92

1.1
Location : replaceOldRank
Killed by : none
replaced return value with null for sh/okx/rankup/messages/StringMessageBuilder::replaceOldRank → NO_COVERAGE

98

1.1
Location : replaceSeconds
Killed by : none
replaced return value with null for sh/okx/rankup/messages/StringMessageBuilder::replaceSeconds → NO_COVERAGE

104

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

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

107

1.1
Location : send
Killed by : none
removed call to org/bukkit/command/CommandSender::sendMessage → NO_COVERAGE

116

1.1
Location : broadcast
Killed by : none
removed call to sh/okx/rankup/messages/StringMessageBuilder::send → NO_COVERAGE

118

1.1
Location : broadcast
Killed by : none
removed call to sh/okx/rankup/messages/StringMessageBuilder::send → NO_COVERAGE

123

1.1
Location : toString
Killed by : none
replaced return value with "" for sh/okx/rankup/messages/StringMessageBuilder::toString → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.7.0