package me.loganb1max.minions.command; import com.google.common.collect.ImmutableList; import me.loganb1max.minions.model.Minion; import me.loganb1max.minions.model.minions.FarmerMinion; import me.loganb1max.minions.model.minions.FisherMinion; import me.loganb1max.minions.model.minions.MinerMinion; import me.loganb1max.minions.util.ItemUtil; import me.loganb1max.minions.util.Text; import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.command.TabCompleter; import org.bukkit.entity.Player; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class MinionCommand implements CommandExecutor, TabCompleter { @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { if (!sender.hasPermission("minions.command")) { sender.sendMessage(Text.color("&cYou don't have permission for this command.")); return true; } if (args.length < 1) { sendHelp(sender); return true; } if (args.length == 1) { if (args[0].equalsIgnoreCase("reload")) { } else { sendHelp(sender); } } if (args.length >= 3) { if (!args[0].equalsIgnoreCase("give")) { sendHelp(sender); return true; } final Player target = Bukkit.getPlayer(args[1]); if (target == null) { sender.sendMessage(Text.color("&cPlayer Not Found.")); return true; } Minion minion = null; final String type = args[2].toLowerCase(); switch (type) { case "miner": minion = new MinerMinion(target); break; case "farmer": minion = new FarmerMinion(target); break; case "fisher": minion = new FisherMinion(target); break; } if (minion == null) { sender.sendMessage(Text.color("&cInvalid Minion Type.")); return true; } ItemUtil.giveAndDrop(target, minion.toItemStack()); target.sendMessage(Text.color("&aYou have received a " + minion.getName())); sender.sendMessage(Text.color("&aYou gave " + target.getName() + " a " + minion.getName())); } return true; } @Override public List onTabComplete(CommandSender commandSender, Command command, String label, String[] args) { List toReturn = new ArrayList<>(); switch (args.length) { case 0: toReturn = ImmutableList.of( "give", "reload" ); break; case 1: toReturn = Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList()); break; case 2: toReturn = ImmutableList.of( "miner", "farmer", "fisher" ); break; } return toReturn; } private void sendHelp(final CommandSender sender) { sender.sendMessage(Text.color("&7============================================")); sender.sendMessage(Text.color("&6 Minions Help Menu")); sender.sendMessage(Text.color("&7============================================")); sender.sendMessage(Text.color("&6 /minions give ")); sender.sendMessage(Text.color("&6 /minions reload")); } }