Commits

Black Hole authored 9a45d4139d4
Initial commit
No tags
No parent commit
master

src/main/java/com/github/blackhole/statisticstest/commands/StatsCommand.java

Added
1 +package com.github.blackhole.statisticstest.commands;
2 +
3 +import com.google.common.collect.ImmutableList;
4 +import com.google.common.collect.Lists;
5 +import net.md_5.bungee.api.ChatColor;
6 +import org.bukkit.Statistic;
7 +import org.bukkit.command.Command;
8 +import org.bukkit.command.CommandSender;
9 +import org.bukkit.command.TabExecutor;
10 +import org.bukkit.entity.Player;
11 +import org.bukkit.util.StringUtil;
12 +
13 +import java.util.Arrays;
14 +import java.util.List;
15 +import java.util.stream.Collectors;
16 +
17 +public class StatsCommand implements TabExecutor {
18 +
19 + @Override
20 + public boolean onCommand(CommandSender sender, Command command, String alias, String[] args) {
21 + if (!(sender instanceof Player)) {
22 + sender.sendMessage(ChatColor.RED + "Command is for players only!");
23 + return true;
24 + }
25 +
26 + Player player = (Player) sender;
27 +
28 + if (args.length == 2) {
29 + onAdd(player, args);
30 + return true;
31 + }
32 +
33 + sender.sendMessage(ChatColor.RED + "Syntax: /" + alias + " <statistics> <value>");
34 + return true;
35 + }
36 +
37 + @Override
38 + public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
39 + String lastArg = args[args.length - 1];
40 +
41 + if (args.length == 1) {
42 + return StringUtil.copyPartialMatches(lastArg, Arrays.stream(Statistic.values()).filter(statistic -> statistic.getType() == Statistic.Type.UNTYPED)
43 + .map(statistic -> statistic.name().toLowerCase()).collect(Collectors.toList()), Lists.newArrayList());
44 + }
45 +
46 + return ImmutableList.of();
47 + }
48 +
49 + private void onAdd(Player player, String[] args) {
50 + String statisticName = args[0];
51 + Statistic statistic;
52 + try {
53 + statistic = Statistic.valueOf(statisticName.toUpperCase());
54 + } catch (IllegalArgumentException exc) {
55 + player.sendMessage(ChatColor.RED + "Unknown statistic " + ChatColor.WHITE + statisticName + ChatColor.RED + "!");
56 + return;
57 + }
58 +
59 + if (statistic.getType() != Statistic.Type.UNTYPED) {
60 + player.sendMessage(ChatColor.RED + "Statistic " + ChatColor.WHITE + statistic.name() + ChatColor.RED + " is typed!");
61 + return;
62 + }
63 +
64 + String valueString = args[1];
65 + int value;
66 + try {
67 + value = Integer.parseInt(valueString);
68 + } catch (NumberFormatException exc) {
69 + player.sendMessage(ChatColor.RED + "Invalid value " + ChatColor.WHITE + valueString + ChatColor.RED + "!");
70 + return;
71 + }
72 +
73 + player.incrementStatistic(statistic, value);
74 + player.sendMessage(ChatColor.GREEN + "Statistic " + ChatColor.GOLD + statistic.name() + ChatColor.GREEN + " was increased by " + ChatColor.GOLD + value
75 + + ChatColor.GREEN + ".");
76 + }
77 +
78 +}

Everything looks good. We'll let you know here if there's anything you should know about.

Add shortcut