Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup Commands #3935

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,9 +30,9 @@ public SlimefunTabCompleter(@Nonnull SlimefunCommand command) {
this.command = command;
}

@Nullable
@Override
public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] args) {
@ParametersAreNonnullByDefault
public @Nullable List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] args) {
if (args.length == 1) {
return createReturnList(command.getSubCommandNames(), args[0]);
} else if (args.length == 2) {
Expand Down Expand Up @@ -77,8 +78,8 @@ public List<String> onTabComplete(CommandSender sender, Command cmd, String labe
* The typed string
* @return Sublist if string is not empty
*/
@Nonnull
private List<String> createReturnList(@Nonnull List<String> list, @Nonnull String string) {
@ParametersAreNonnullByDefault
private @Nonnull List<String> createReturnList(List<String> list, String string) {
if (string.length() == 0) {
return list;
}
Expand All @@ -101,8 +102,7 @@ private List<String> createReturnList(@Nonnull List<String> list, @Nonnull Strin
return returnList;
}

@Nonnull
private List<String> getSlimefunItems() {
private @Nonnull List<String> getSlimefunItems() {
List<SlimefunItem> items = Slimefun.getRegistry().getEnabledSlimefunItems();
List<String> list = new ArrayList<>(items.size());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.thebusybiscuit.slimefun4.core.commands.subcommands;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

import org.bukkit.Bukkit;
Expand Down Expand Up @@ -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 <Player> <ID>"));
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 <Player> <ID>"));
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");
});
});
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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> 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 <Player> <Slimefun Item> [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 <Player> <Slimefun Item> [Amount]"));
return;
}

Optional<Player> 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) {
JustAHuman-xD marked this conversation as resolved.
Show resolved Hide resolved
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<Integer, ItemStack> 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<Integer, ItemStack> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}
Loading