diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/SlimefunCommand.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/SlimefunCommand.java index 4ef1a411a5..bc9d298e3f 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/SlimefunCommand.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/SlimefunCommand.java @@ -7,6 +7,7 @@ import java.util.stream.Collectors; import javax.annotation.Nonnull; +import javax.annotation.ParametersAreNonnullByDefault; import org.apache.commons.lang.Validate; import org.bukkit.command.Command; @@ -68,6 +69,7 @@ public void register() { } @Override + @ParametersAreNonnullByDefault public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { if (args.length > 0) { for (SubCommand command : commands) { diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/SlimefunTabCompleter.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/SlimefunTabCompleter.java index 50a665f308..374ec65b52 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/SlimefunTabCompleter.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/SlimefunTabCompleter.java @@ -9,6 +9,7 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -29,9 +30,9 @@ public SlimefunTabCompleter(@Nonnull SlimefunCommand command) { this.command = command; } - @Nullable @Override - public List onTabComplete(CommandSender sender, Command cmd, String label, String[] args) { + @ParametersAreNonnullByDefault + public @Nullable List onTabComplete(CommandSender sender, Command cmd, String label, String[] args) { if (args.length == 1) { return createReturnList(command.getSubCommandNames(), args[0]); } else if (args.length == 2) { @@ -77,8 +78,8 @@ public List onTabComplete(CommandSender sender, Command cmd, String labe * The typed string * @return Sublist if string is not empty */ - @Nonnull - private List createReturnList(@Nonnull List list, @Nonnull String string) { + @ParametersAreNonnullByDefault + private @Nonnull List createReturnList(List list, String string) { if (string.length() == 0) { return list; } @@ -101,8 +102,7 @@ private List createReturnList(@Nonnull List list, @Nonnull Strin return returnList; } - @Nonnull - private List getSlimefunItems() { + private @Nonnull List getSlimefunItems() { List items = Slimefun.getRegistry().getEnabledSlimefunItems(); List list = new ArrayList<>(items.size()); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/BackpackCommand.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/BackpackCommand.java index 431d47cb66..6326a153a1 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/BackpackCommand.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/BackpackCommand.java @@ -1,5 +1,6 @@ package io.github.thebusybiscuit.slimefun4.core.commands.subcommands; +import javax.annotation.Nonnull; import javax.annotation.ParametersAreNonnullByDefault; import org.bukkit.Bukkit; @@ -34,52 +35,53 @@ class BackpackCommand extends SubCommand { } @Override - protected String getDescription() { + protected @Nonnull String getDescription() { return "commands.backpack.description"; } @Override + @ParametersAreNonnullByDefault public void onExecute(CommandSender sender, String[] args) { - if (sender instanceof Player player) { - if (sender.hasPermission("slimefun.command.backpack")) { - if (args.length != 3) { - Slimefun.getLocalization().sendMessage(sender, "messages.usage", true, msg -> msg.replace("%usage%", "/sf backpack ")); - return; - } + if (!(sender instanceof Player player)) { + Slimefun.getLocalization().sendMessage(sender, "messages.only-players", true); + return; + } - if (!CommonPatterns.NUMERIC.matcher(args[2]).matches()) { - Slimefun.getLocalization().sendMessage(sender, "commands.backpack.invalid-id"); - return; - } + if (!sender.hasPermission("slimefun.command.backpack")) { + Slimefun.getLocalization().sendMessage(sender, "messages.no-permission", true); + return; + } - @SuppressWarnings("deprecation") - OfflinePlayer backpackOwner = Bukkit.getOfflinePlayer(args[1]); + if (args.length != 3) { + Slimefun.getLocalization().sendMessage(sender, "messages.usage", true, msg -> msg.replace("%usage%", "/sf backpack ")); + return; + } - if (!(backpackOwner instanceof Player) && !backpackOwner.hasPlayedBefore()) { - Slimefun.getLocalization().sendMessage(sender, "commands.backpack.player-never-joined"); - return; - } + if (!CommonPatterns.NUMERIC.matcher(args[2]).matches()) { + Slimefun.getLocalization().sendMessage(sender, "commands.backpack.invalid-id"); + return; + } - int id = Integer.parseInt(args[2]); + OfflinePlayer backpackOwner = Bukkit.getOfflinePlayer(args[1]); + if (!(backpackOwner instanceof Player) && !backpackOwner.hasPlayedBefore()) { + Slimefun.getLocalization().sendMessage(sender, "commands.backpack.player-never-joined"); + return; + } - PlayerProfile.get(backpackOwner, profile -> { - if (!profile.getBackpack(id).isPresent()) { - Slimefun.getLocalization().sendMessage(sender, "commands.backpack.backpack-does-not-exist"); - return; - } + int id = Integer.parseInt(args[2]); - Slimefun.runSync(() -> { - ItemStack item = SlimefunItems.RESTORED_BACKPACK.clone(); - Slimefun.getBackpackListener().setBackpackId(backpackOwner, item, 2, id); - player.getInventory().addItem(item); - Slimefun.getLocalization().sendMessage(sender, "commands.backpack.restored-backpack-given"); - }); - }); - } else { - Slimefun.getLocalization().sendMessage(sender, "messages.no-permission", true); + PlayerProfile.get(backpackOwner, profile -> { + if (!profile.getBackpack(id).isPresent()) { + Slimefun.getLocalization().sendMessage(sender, "commands.backpack.backpack-does-not-exist"); + return; } - } else { - Slimefun.getLocalization().sendMessage(sender, "messages.only-players", true); - } + + Slimefun.runSync(() -> { + ItemStack item = SlimefunItems.RESTORED_BACKPACK.clone(); + Slimefun.getBackpackListener().setBackpackId(backpackOwner, item, 2, id); + player.getInventory().addItem(item); + Slimefun.getLocalization().sendMessage(sender, "commands.backpack.restored-backpack-given"); + }); + }); } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/ChargeCommand.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/ChargeCommand.java index 119091e1e6..ec3815c78e 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/ChargeCommand.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/ChargeCommand.java @@ -1,5 +1,6 @@ package io.github.thebusybiscuit.slimefun4.core.commands.subcommands; +import javax.annotation.Nonnull; import javax.annotation.ParametersAreNonnullByDefault; import org.bukkit.command.CommandSender; @@ -27,28 +28,31 @@ class ChargeCommand extends SubCommand { } @Override - protected String getDescription() { + protected @Nonnull String getDescription() { return "commands.charge.description"; } @Override + @ParametersAreNonnullByDefault public void onExecute(CommandSender sender, String[] args) { - if (sender instanceof Player player) { - if (sender.hasPermission("slimefun.command.charge")) { - ItemStack item = player.getInventory().getItemInMainHand(); - SlimefunItem slimefunItem = SlimefunItem.getByItem(item); - - if (slimefunItem instanceof Rechargeable rechargeableItem) { - rechargeableItem.setItemCharge(item, rechargeableItem.getMaxItemCharge(item)); - Slimefun.getLocalization().sendMessage(sender, "commands.charge.charge-success", true); - } else { - Slimefun.getLocalization().sendMessage(sender, "commands.charge.not-rechargeable", true); - } - } else { - Slimefun.getLocalization().sendMessage(sender, "messages.no-permission", true); - } - } else { + if (!(sender instanceof Player player)) { Slimefun.getLocalization().sendMessage(sender, "messages.only-players", true); + return; + } + + if (!sender.hasPermission("slimefun.command.charge")) { + Slimefun.getLocalization().sendMessage(sender, "messages.no-permission", true); + return; + } + + ItemStack item = player.getInventory().getItemInMainHand(); + SlimefunItem slimefunItem = SlimefunItem.getByItem(item); + + if (slimefunItem instanceof Rechargeable rechargeableItem) { + rechargeableItem.setItemCharge(item, rechargeableItem.getMaxItemCharge(item)); + Slimefun.getLocalization().sendMessage(sender, "commands.charge.charge-success", true); + } else { + Slimefun.getLocalization().sendMessage(sender, "commands.charge.not-rechargeable", true); } } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/CheatCommand.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/CheatCommand.java index 973ed1ee39..1342c1f2a6 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/CheatCommand.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/CheatCommand.java @@ -18,15 +18,17 @@ class CheatCommand extends SubCommand { } @Override + @ParametersAreNonnullByDefault public void onExecute(CommandSender sender, String[] args) { - if (sender instanceof Player player) { - if (sender.hasPermission("slimefun.cheat.items")) { - SlimefunGuide.openCheatMenu(player); - } else { - Slimefun.getLocalization().sendMessage(sender, "messages.no-permission", true); - } - } else { + if (!(sender instanceof Player player)) { Slimefun.getLocalization().sendMessage(sender, "messages.only-players", true); + return; + } + + if (sender.hasPermission("slimefun.cheat.items")) { + SlimefunGuide.openCheatMenu(player); + } else { + Slimefun.getLocalization().sendMessage(sender, "messages.no-permission", true); } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/DebugCommand.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/DebugCommand.java index 12c567cb14..0d9b59f962 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/DebugCommand.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/DebugCommand.java @@ -1,6 +1,7 @@ package io.github.thebusybiscuit.slimefun4.core.commands.subcommands; import javax.annotation.Nonnull; +import javax.annotation.ParametersAreNonnullByDefault; import org.bukkit.command.CommandSender; @@ -27,7 +28,8 @@ protected DebugCommand(@Nonnull Slimefun plugin, @Nonnull SlimefunCommand cmd) { } @Override - public void onExecute(@Nonnull CommandSender sender, @Nonnull String[] args) { + @ParametersAreNonnullByDefault + public void onExecute(CommandSender sender, String[] args) { if (!sender.hasPermission("slimefun.command.debug")) { Slimefun.getLocalization().sendMessage(sender, "messages.no-permission", true); return; diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/DebugFishCommand.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/DebugFishCommand.java index cc79b113e6..92acb319e5 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/DebugFishCommand.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/DebugFishCommand.java @@ -18,6 +18,7 @@ class DebugFishCommand extends SubCommand { } @Override + @ParametersAreNonnullByDefault public void onExecute(CommandSender sender, String[] args) { if (sender instanceof Player player && sender.hasPermission("slimefun.debugging")) { player.getInventory().addItem(SlimefunItems.DEBUG_FISH.clone()); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/GiveCommand.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/GiveCommand.java index 5d56959e84..4b3ef1ff74 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/GiveCommand.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/GiveCommand.java @@ -4,6 +4,7 @@ import java.util.Map; import java.util.Optional; +import javax.annotation.Nonnull; import javax.annotation.ParametersAreNonnullByDefault; import org.bukkit.command.CommandSender; @@ -31,55 +32,59 @@ class GiveCommand extends SubCommand { } @Override + @ParametersAreNonnullByDefault public void onExecute(CommandSender sender, String[] args) { - if (sender.hasPermission("slimefun.cheat.items") || !(sender instanceof Player)) { - if (args.length > 2) { - Optional player = PlayerList.findByName(args[1]); - - if (player.isPresent()) { - Player p = player.get(); - - SlimefunItem sfItem = SlimefunItem.getById(args[2].toUpperCase(Locale.ROOT)); - - if (sfItem != null) { - giveItem(sender, p, sfItem, args); - } else { - Slimefun.getLocalization().sendMessage(sender, "messages.invalid-item", true, msg -> msg.replace(PLACEHOLDER_ITEM, args[2])); - } - } else { - Slimefun.getLocalization().sendMessage(sender, "messages.not-online", true, msg -> msg.replace(PLACEHOLDER_PLAYER, args[1])); - } - } else { - Slimefun.getLocalization().sendMessage(sender, "messages.usage", true, msg -> msg.replace("%usage%", "/sf give [Amount]")); - } - } else { + if (!sender.hasPermission("slimefun.cheat.items") && sender instanceof Player) { Slimefun.getLocalization().sendMessage(sender, "messages.no-permission", true); + return; + } + + if (args.length <= 2) { + Slimefun.getLocalization().sendMessage(sender, "messages.usage", true, msg -> msg.replace("%usage%", "/sf give [Amount]")); + return; + } + + Optional player = PlayerList.findByName(args[1]); + if (player.isEmpty()) { + Slimefun.getLocalization().sendMessage(sender, "messages.not-online", true, msg -> msg.replace(PLACEHOLDER_PLAYER, args[1])); + return; + } + + Player p = player.get(); + SlimefunItem sfItem = SlimefunItem.getById(args[2].toUpperCase(Locale.ROOT)); + if (sfItem == null) { + Slimefun.getLocalization().sendMessage(sender, "messages.invalid-item", true, msg -> msg.replace(PLACEHOLDER_ITEM, args[2])); + return; } + + giveItem(sender, p, sfItem, args); } + @ParametersAreNonnullByDefault private void giveItem(CommandSender sender, Player p, SlimefunItem sfItem, String[] args) { if (sfItem instanceof MultiBlockMachine) { Slimefun.getLocalization().sendMessage(sender, "guide.cheat.no-multiblocks"); - } else { - int amount = parseAmount(args); - - if (amount > 0) { - Slimefun.getLocalization().sendMessage(p, "messages.given-item", true, msg -> msg.replace(PLACEHOLDER_ITEM, sfItem.getItemName()).replace(PLACEHOLDER_AMOUNT, String.valueOf(amount))); - Map excess = p.getInventory().addItem(new CustomItemStack(sfItem.getItem(), amount)); - if (Slimefun.getCfg().getBoolean("options.drop-excess-sf-give-items") && !excess.isEmpty()) { - for (ItemStack is : excess.values()) { - p.getWorld().dropItem(p.getLocation(), is); - } - } - - Slimefun.getLocalization().sendMessage(sender, "messages.give-item", true, msg -> msg.replace(PLACEHOLDER_PLAYER, args[1]).replace(PLACEHOLDER_ITEM, sfItem.getItemName()).replace(PLACEHOLDER_AMOUNT, String.valueOf(amount))); - } else { - Slimefun.getLocalization().sendMessage(sender, "messages.invalid-amount", true, msg -> msg.replace(PLACEHOLDER_AMOUNT, args[3])); + return; + } + + int amount = parseAmount(args); + if (amount <= 0) { + Slimefun.getLocalization().sendMessage(sender, "messages.invalid-amount", true, msg -> msg.replace(PLACEHOLDER_AMOUNT, args[3])); + return; + } + + Slimefun.getLocalization().sendMessage(p, "messages.given-item", true, msg -> msg.replace(PLACEHOLDER_ITEM, sfItem.getItemName()).replace(PLACEHOLDER_AMOUNT, String.valueOf(amount))); + Map excess = p.getInventory().addItem(new CustomItemStack(sfItem.getItem(), amount)); + if (Slimefun.getCfg().getBoolean("options.drop-excess-sf-give-items") && !excess.isEmpty()) { + for (ItemStack is : excess.values()) { + p.getWorld().dropItem(p.getLocation(), is); } } + + Slimefun.getLocalization().sendMessage(sender, "messages.give-item", true, msg -> msg.replace(PLACEHOLDER_PLAYER, args[1]).replace(PLACEHOLDER_ITEM, sfItem.getItemName()).replace(PLACEHOLDER_AMOUNT, String.valueOf(amount))); } - private int parseAmount(String[] args) { + private int parseAmount(@Nonnull String[] args) { int amount = 1; if (args.length == 4) { diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/GuideCommand.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/GuideCommand.java index 006744d6c7..306700ed58 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/GuideCommand.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/GuideCommand.java @@ -19,17 +19,20 @@ class GuideCommand extends SubCommand { } @Override + @ParametersAreNonnullByDefault public void onExecute(CommandSender sender, String[] args) { - if (sender instanceof Player player) { - if (sender.hasPermission("slimefun.command.guide")) { - SlimefunGuideMode design = SlimefunGuide.getDefaultMode(); - player.getInventory().addItem(SlimefunGuide.getItem(design).clone()); - } else { - Slimefun.getLocalization().sendMessage(sender, "messages.no-permission", true); - } - } else { + if (!(sender instanceof Player player)) { Slimefun.getLocalization().sendMessage(sender, "messages.only-players", true); + return; + } + + if (!sender.hasPermission("slimefun.command.guide")) { + Slimefun.getLocalization().sendMessage(sender, "messages.no-permission", true); + return; } + + SlimefunGuideMode design = SlimefunGuide.getDefaultMode(); + player.getInventory().addItem(SlimefunGuide.getItem(design).clone()); } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/HelpCommand.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/HelpCommand.java index b3e411d388..271a7d2d55 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/HelpCommand.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/HelpCommand.java @@ -16,6 +16,7 @@ class HelpCommand extends SubCommand { } @Override + @ParametersAreNonnullByDefault public void onExecute(CommandSender sender, String[] args) { cmd.sendHelp(sender); } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/OpenGuideCommand.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/OpenGuideCommand.java index 83db842925..e23a1b9b8e 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/OpenGuideCommand.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/OpenGuideCommand.java @@ -21,14 +21,15 @@ class OpenGuideCommand extends SubCommand { @Override @ParametersAreNonnullByDefault public void onExecute(CommandSender sender, String[] args) { - if (sender instanceof Player player) { - if (sender.hasPermission("slimefun.command.open_guide")) { - SlimefunGuide.openGuide(player, SlimefunGuideMode.SURVIVAL_MODE); - } else { - Slimefun.getLocalization().sendMessage(sender, "messages.no-permission", true); - } - } else { + if (!(sender instanceof Player player)) { Slimefun.getLocalization().sendMessage(sender, "messages.only-players", true); + return; + } + + if (sender.hasPermission("slimefun.command.open_guide")) { + SlimefunGuide.openGuide(player, SlimefunGuideMode.SURVIVAL_MODE); + } else { + Slimefun.getLocalization().sendMessage(sender, "messages.no-permission", true); } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/ResearchCommand.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/ResearchCommand.java index 8b81c87b53..d3b7c7b3ab 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/ResearchCommand.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/ResearchCommand.java @@ -27,11 +27,12 @@ class ResearchCommand extends SubCommand { } @Override - protected String getDescription() { + protected @Nonnull String getDescription() { return "commands.research.description"; } @Override + @ParametersAreNonnullByDefault public void onExecute(CommandSender sender, String[] args) { // Check if researching is even enabled if (!Slimefun.getRegistry().isResearchingEnabled()) { @@ -39,46 +40,48 @@ public void onExecute(CommandSender sender, String[] args) { return; } - if (args.length == 3) { - if (!(sender instanceof Player) || sender.hasPermission("slimefun.cheat.researches")) { - Optional player = PlayerList.findByName(args[1]); - - if (player.isPresent()) { - Player p = player.get(); - - // Getting the PlayerProfile async - PlayerProfile.get(p, profile -> { - if (args[2].equalsIgnoreCase("all")) { - researchAll(sender, profile, p); - } else if (args[2].equalsIgnoreCase("reset")) { - reset(profile, p); - } else { - giveResearch(sender, p, args[2]); - } - }); - } else { - Slimefun.getLocalization().sendMessage(sender, "messages.not-online", true, msg -> msg.replace(PLACEHOLDER_PLAYER, args[1])); - } - } else { - Slimefun.getLocalization().sendMessage(sender, "messages.no-permission", true); - } - } else { + if (args.length != 3) { Slimefun.getLocalization().sendMessage(sender, "messages.usage", true, msg -> msg.replace("%usage%", "/sf research ")); + return; } + + if (sender instanceof Player && !sender.hasPermission("slimefun.cheat.researches")) { + Slimefun.getLocalization().sendMessage(sender, "messages.no-permission", true); + return; + } + + Optional player = PlayerList.findByName(args[1]); + if (player.isEmpty()) { + Slimefun.getLocalization().sendMessage(sender, "messages.not-online", true, msg -> msg.replace(PLACEHOLDER_PLAYER, args[1])); + return; + } + + // Getting the PlayerProfile async + Player p = player.get(); + PlayerProfile.get(p, profile -> { + if (args[2].equalsIgnoreCase("all")) { + researchAll(sender, profile, p); + } else if (args[2].equalsIgnoreCase("reset")) { + reset(profile, p); + } else { + giveResearch(sender, p, args[2]); + } + }); } @ParametersAreNonnullByDefault private void giveResearch(CommandSender sender, Player p, String input) { Optional research = getResearchFromString(input); - if (research.isPresent()) { - research.get().unlock(p, true, player -> { - UnaryOperator variables = msg -> msg.replace(PLACEHOLDER_PLAYER, player.getName()).replace(PLACEHOLDER_RESEARCH, research.get().getName(player)); - Slimefun.getLocalization().sendMessage(player, "messages.give-research", true, variables); - }); - } else { + if (research.isEmpty()) { Slimefun.getLocalization().sendMessage(sender, "messages.invalid-research", true, msg -> msg.replace(PLACEHOLDER_RESEARCH, input)); + return; } + + research.get().unlock(p, true, player -> { + UnaryOperator variables = msg -> msg.replace(PLACEHOLDER_PLAYER, player.getName()).replace(PLACEHOLDER_RESEARCH, research.get().getName(player)); + Slimefun.getLocalization().sendMessage(player, "messages.give-research", true, variables); + }); } @ParametersAreNonnullByDefault @@ -101,8 +104,7 @@ private void reset(PlayerProfile profile, Player p) { Slimefun.getLocalization().sendMessage(p, "commands.research.reset", true, msg -> msg.replace(PLACEHOLDER_PLAYER, p.getName())); } - @Nonnull - private Optional getResearchFromString(@Nonnull String input) { + private @Nonnull Optional getResearchFromString(@Nonnull String input) { if (!input.contains(":")) { return Optional.empty(); } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/SearchCommand.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/SearchCommand.java index 9783518b17..b4fd77863c 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/SearchCommand.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/SearchCommand.java @@ -22,21 +22,25 @@ class SearchCommand extends SubCommand { } @Override + @ParametersAreNonnullByDefault public void onExecute(CommandSender sender, String[] args) { - if (sender instanceof Player player) { - if (sender.hasPermission("slimefun.command.search")) { - if (args.length > 1) { - String query = String.join(" ", Arrays.copyOfRange(args, 1, args.length)); - PlayerProfile.get(player, profile -> SlimefunGuide.openSearch(profile, query, SlimefunGuideMode.SURVIVAL_MODE, true)); - } else { - Slimefun.getLocalization().sendMessage(sender, "messages.usage", true, msg -> msg.replace("%usage%", "/sf search ")); - } - } else { - Slimefun.getLocalization().sendMessage(sender, "messages.no-permission", true); - } - } else { + if (!(sender instanceof Player player)) { Slimefun.getLocalization().sendMessage(sender, "messages.only-players", true); + return; + } + + if (!sender.hasPermission("slimefun.command.search")) { + Slimefun.getLocalization().sendMessage(sender, "messages.no-permission", true); + return; } + + if (args.length <= 1) { + Slimefun.getLocalization().sendMessage(sender, "messages.usage", true, msg -> msg.replace("%usage%", "/sf search ")); + return; + } + + String query = String.join(" ", Arrays.copyOfRange(args, 1, args.length)); + PlayerProfile.get(player, profile -> SlimefunGuide.openSearch(profile, query, SlimefunGuideMode.SURVIVAL_MODE, true)); } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/SlimefunSubCommands.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/SlimefunSubCommands.java index 17d70bce3e..fa02cba436 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/SlimefunSubCommands.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/SlimefunSubCommands.java @@ -22,8 +22,7 @@ public final class SlimefunSubCommands { private SlimefunSubCommands() {} - @Nonnull - public static Collection getAllCommands(@Nonnull SlimefunCommand cmd) { + public static @Nonnull Collection getAllCommands(@Nonnull SlimefunCommand cmd) { Slimefun plugin = cmd.getPlugin(); List commands = new LinkedList<>(); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/StatsCommand.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/StatsCommand.java index cb9069eb6a..e6f6b6e966 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/StatsCommand.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/StatsCommand.java @@ -22,6 +22,7 @@ class StatsCommand extends SubCommand { } @Override + @ParametersAreNonnullByDefault public void onExecute(CommandSender sender, String[] args) { // Check if researching is even enabled if (!Slimefun.getRegistry().isResearchingEnabled()) { @@ -29,23 +30,27 @@ public void onExecute(CommandSender sender, String[] args) { return; } - if (args.length > 1) { - if (sender.hasPermission("slimefun.stats.others") || sender instanceof ConsoleCommandSender) { - Optional player = PlayerList.findByName(args[1]); - - if (player.isPresent()) { - PlayerProfile.get(player.get(), profile -> profile.sendStats(sender)); - } else { - Slimefun.getLocalization().sendMessage(sender, "messages.not-online", true, msg -> msg.replace("%player%", args[1])); - } + if (args.length <= 1) { + if (sender instanceof Player player) { + PlayerProfile.get(player, profile -> profile.sendStats(sender)); } else { - Slimefun.getLocalization().sendMessage(sender, "messages.no-permission", true); + Slimefun.getLocalization().sendMessage(sender, "messages.only-players", true); } - } else if (sender instanceof Player player) { - PlayerProfile.get(player, profile -> profile.sendStats(sender)); - } else { - Slimefun.getLocalization().sendMessage(sender, "messages.only-players", true); + return; + } + + if (!sender.hasPermission("slimefun.stats.others") && !(sender instanceof ConsoleCommandSender)) { + Slimefun.getLocalization().sendMessage(sender, "messages.no-permission", true); + return; } + + Optional player = PlayerList.findByName(args[1]); + if (player.isEmpty()) { + Slimefun.getLocalization().sendMessage(sender, "messages.not-online", true, msg -> msg.replace("%player%", args[1])); + return; + } + + PlayerProfile.get(player.get(), profile -> profile.sendStats(sender)); } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/TeleporterCommand.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/TeleporterCommand.java index 891ba85059..d2d7ecd203 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/TeleporterCommand.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/TeleporterCommand.java @@ -20,30 +20,34 @@ class TeleporterCommand extends SubCommand { } @Override + @ParametersAreNonnullByDefault public void onExecute(CommandSender sender, String[] args) { - if (sender instanceof Player player) { - if (sender.hasPermission("slimefun.command.teleporter")) { - if (args.length == 1) { - Slimefun.getGPSNetwork().getTeleportationManager().openTeleporterGUI(player, player.getUniqueId(), player.getLocation().getBlock().getRelative(BlockFace.DOWN), 999999999); - } else if (args.length == 2) { - - @SuppressWarnings("deprecation") - OfflinePlayer targetPlayer = Bukkit.getOfflinePlayer(args[1]); - - if (targetPlayer.getName() != null) { - Slimefun.getGPSNetwork().getTeleportationManager().openTeleporterGUI(player, targetPlayer.getUniqueId(), player.getLocation().getBlock().getRelative(BlockFace.DOWN), 999999999); - } else { - Slimefun.getLocalization().sendMessage(sender, "messages.unknown-player", msg -> msg.replace("%player%", args[1])); - } - } else { - Slimefun.getLocalization().sendMessage(sender, "messages.usage", msg -> msg.replace("%usage%", "/sf teleporter [Player]")); - } - } else { - Slimefun.getLocalization().sendMessage(sender, "messages.no-permission"); - } - } else { + if (!(sender instanceof Player player)) { Slimefun.getLocalization().sendMessage(sender, "messages.only-players"); + return; + } + + if (!sender.hasPermission("slimefun.command.teleporter")) { + Slimefun.getLocalization().sendMessage(sender, "messages.no-permission"); + return; + } + + if (args.length != 1 && args.length != 2) { + Slimefun.getLocalization().sendMessage(sender, "messages.usage", msg -> msg.replace("%usage%", "/sf teleporter [Player]")); } + + if (args.length == 1) { + Slimefun.getGPSNetwork().getTeleportationManager().openTeleporterGUI(player, player.getUniqueId(), player.getLocation().getBlock().getRelative(BlockFace.DOWN), 999999999); + return; + } + + OfflinePlayer targetPlayer = Bukkit.getOfflinePlayer(args[1]); + if (targetPlayer.getName() == null) { + Slimefun.getLocalization().sendMessage(sender, "messages.unknown-player", msg -> msg.replace("%player%", args[1])); + return; + } + + Slimefun.getGPSNetwork().getTeleportationManager().openTeleporterGUI(player, targetPlayer.getUniqueId(), player.getLocation().getBlock().getRelative(BlockFace.DOWN), 999999999); } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/TimingsCommand.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/TimingsCommand.java index 3dfd8a78cc..22e0bce896 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/TimingsCommand.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/TimingsCommand.java @@ -36,7 +36,8 @@ class TimingsCommand extends SubCommand { } @Override - public void onExecute(CommandSender sender, @Nonnull String[] args) { + @ParametersAreNonnullByDefault + public void onExecute(CommandSender sender, String[] args) { if (!sender.hasPermission("slimefun.command.timings") && !(sender instanceof ConsoleCommandSender)) { Slimefun.getLocalization().sendMessage(sender, "messages.no-permission", true); return; @@ -95,14 +96,13 @@ private boolean hasFlag(String[] args, String flag) { return false; } - - @Nonnull + @ParametersAreNonnullByDefault - private PerformanceInspector inspectorOf(CommandSender sender, boolean verbose, SummaryOrderType orderType) { + private @Nonnull PerformanceInspector inspectorOf(CommandSender sender, boolean verbose, SummaryOrderType orderType) { if (sender instanceof Player player) { return new PlayerPerformanceInspector(player, orderType); } else { return new ConsolePerformanceInspector(sender, verbose, orderType); } } -} +} \ No newline at end of file diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/VersionsCommand.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/VersionsCommand.java index 34ff1f4a20..56c46bcd6c 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/VersionsCommand.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/subcommands/VersionsCommand.java @@ -50,79 +50,82 @@ class VersionsCommand extends SubCommand { } @Override - public void onExecute(@Nonnull CommandSender sender, @Nonnull String[] args) { - if (sender.hasPermission("slimefun.command.versions") || sender instanceof ConsoleCommandSender) { - /* - * After all these years... Spigot still displays as "CraftBukkit". - * so we will just fix this inconsistency for them :) - */ - String serverSoftware = PaperLib.isSpigot() && !PaperLib.isPaper() ? "Spigot" : Bukkit.getName(); - ComponentBuilder builder = new ComponentBuilder(); + @ParametersAreNonnullByDefault + public void onExecute(CommandSender sender, String[] args) { + if (!sender.hasPermission("slimefun.command.versions") && !(sender instanceof ConsoleCommandSender)) { + Slimefun.getLocalization().sendMessage(sender, "messages.no-permission", true); + return; + } - // @formatter:off - builder.append("This Server uses the following setup of Slimefun:\n") + /* + * After all these years... Spigot still displays as "CraftBukkit". + * so we will just fix this inconsistency for them :) + */ + String serverSoftware = PaperLib.isSpigot() && !PaperLib.isPaper() ? "Spigot" : Bukkit.getName(); + ComponentBuilder builder = new ComponentBuilder(); + + // @formatter:off + builder.append("This Server uses the following setup of Slimefun:\n") .color(ChatColor.GRAY) .append(serverSoftware) .color(ChatColor.GREEN) .append(" " + Bukkit.getVersion() + '\n') .color(ChatColor.DARK_GREEN); + builder + .append("Slimefun ") + .color(ChatColor.GREEN) + .append(Slimefun.getVersion()) + .color(ChatColor.DARK_GREEN); + + if (!Slimefun.getUpdater().isLatestVersion()) { builder - .append("Slimefun ") - .color(ChatColor.GREEN) - .append(Slimefun.getVersion()) - .color(ChatColor.DARK_GREEN); - if (!Slimefun.getUpdater().isLatestVersion()) { - builder - .append(" (").color(ChatColor.GRAY) - .append("Update available").color(ChatColor.RED).event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text( - "Your Slimefun version is out of date!\n" + - "Please update to get the latest bug fixes and performance improvements.\n" + - "Please do not report any bugs without updating first." - ))) - .append(")").color(ChatColor.GRAY); - } + .append(" (").color(ChatColor.GRAY) + .append("Update available").color(ChatColor.RED).event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text( + "Your Slimefun version is out of date!\n" + + "Please update to get the latest bug fixes and performance improvements.\n" + + "Please do not report any bugs without updating first." + ))) + .append(")").color(ChatColor.GRAY); + } - builder.append("\n").event((HoverEvent) null); - // @formatter:on + builder.append("\n").event((HoverEvent) null); + // @formatter:on - if (Slimefun.getMetricsService().getVersion() != null) { - // @formatter:off - builder.append("Metrics-Module ") + if (Slimefun.getMetricsService().getVersion() != null) { + // @formatter:off + builder.append("Metrics-Module ") .color(ChatColor.GREEN) .append("#" + Slimefun.getMetricsService().getVersion() + '\n') .color(ChatColor.DARK_GREEN); - // @formatter:on - } - - addJavaVersion(builder); + // @formatter:on + } - builder.append("\n").event((HoverEvent) null); - addPluginVersions(builder); + addJavaVersion(builder); + builder.append("\n").event((HoverEvent) null); + addPluginVersions(builder); - sender.spigot().sendMessage(builder.create()); - } else { - Slimefun.getLocalization().sendMessage(sender, "messages.no-permission", true); - } + sender.spigot().sendMessage(builder.create()); } private void addJavaVersion(@Nonnull ComponentBuilder builder) { int version = NumberUtils.getJavaVersion(); - if (version < RECOMMENDED_JAVA_VERSION) { - // @formatter:off - builder.append("Java " + version).color(ChatColor.RED) + if (version >= RECOMMENDED_JAVA_VERSION) { + builder.append("Java ").color(ChatColor.GREEN).append(version + "\n").color(ChatColor.DARK_GREEN); + return; + } + + // @formatter:off + builder.append("Java " + version).color(ChatColor.RED) .event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text( - "Your Java version is out of date!\n!" + - "You should use Java " + RECOMMENDED_JAVA_VERSION + " or higher.\n" + - JAVA_VERSION_NOTICE + "Your Java version is out of date!\n!" + + "You should use Java " + RECOMMENDED_JAVA_VERSION + " or higher.\n" + + JAVA_VERSION_NOTICE ))) .append("\n") .event((HoverEvent) null); - // @formatter:on - } else { - builder.append("Java ").color(ChatColor.GREEN).append(version + "\n").color(ChatColor.DARK_GREEN); - } + // @formatter:on } private void addPluginVersions(@Nonnull ComponentBuilder builder) {