Skip to content

Commit cbb876d

Browse files
committed
Using java.util.Optional the value for player's name can not throw an exception anymore.
1 parent aa58a19 commit cbb876d

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/main/java/pro/cloudnode/smp/cloudnodemsg/command/ToggleMessageCommand.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package pro.cloudnode.smp.cloudnodemsg.command;
22

3+
import org.bukkit.OfflinePlayer;
34
import org.bukkit.command.CommandSender;
45
import org.bukkit.entity.Player;
56
import org.jetbrains.annotations.NotNull;
@@ -13,6 +14,7 @@
1314

1415
import java.util.List;
1516
import java.util.Objects;
17+
import java.util.Optional;
1618

1719
public class ToggleMessageCommand extends Command {
1820
public static final @NotNull String usage = "<player>";
@@ -21,18 +23,19 @@ public class ToggleMessageCommand extends Command {
2123
public boolean run(@NotNull CommandSender sender, @NotNull String label, @NotNull String[] args) {
2224
if (!sender.hasPermission(Permission.TOGGLE) || (args.length == 1 && !sender.hasPermission(Permission.TOGGLE_OTHER))) return new NoPermissionError().send(sender);
2325
if (args.length == 1) {
24-
final @NotNull Player recipient = Objects.requireNonNull(CloudnodeMSG.getInstance().getServer().getPlayer(args[0]));
26+
final @NotNull OfflinePlayer recipient = CloudnodeMSG.getInstance().getServer().getOfflinePlayer(args[0]);
2527

26-
if (Message.isIncomeEnabled(recipient)) {
27-
Message.incomeDisable(recipient);
28-
sendMessage(sender, CloudnodeMSG.getInstance().config().toggleDisableOther(Objects.requireNonNull(recipient.getName())));
2928
if (recipient.getPlayer() == null) return new NeverJoinedError(Optional.ofNullable(recipient.getName()).orElse("Unknown Player")).send(sender);
3029

30+
if (Message.isIncomeEnabled(recipient.getPlayer())) {
31+
Message.incomeDisable(recipient.getPlayer());
32+
sendMessage(sender, CloudnodeMSG.getInstance().config().toggleDisableOther(Optional.of(recipient.getPlayer().getName()).orElse("Unknown Player")));
33+
3134
return true;
3235
}
3336

34-
Message.incomeEnable(recipient);
35-
sendMessage(sender, CloudnodeMSG.getInstance().config().toggleEnableOther(Objects.requireNonNull(recipient.getName())));
37+
Message.incomeEnable(recipient.getPlayer());
38+
sendMessage(sender, CloudnodeMSG.getInstance().config().toggleEnableOther(Optional.of(recipient.getPlayer().getName()).orElse("Unknown Player")));
3639

3740
return true;
3841
}

0 commit comments

Comments
 (0)