-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGiveItemCommand.java
More file actions
38 lines (33 loc) · 1.26 KB
/
Copy pathGiveItemCommand.java
File metadata and controls
38 lines (33 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Register this executor for the "give" command in your plugin.yml.
// Command: /give <player> <material> [amount]
public class GiveItemCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (args.length < 2) {
sender.sendMessage("§cUsage: /give <player> <material> [amount]");
return true;
}
Player target = Bukkit.getPlayer(args[0]);
if (target == null) {
sender.sendMessage("§cPlayer not found!");
return true;
}
Material material = Material.getMaterial(args[1].toUpperCase());
if (material == null) {
sender.sendMessage("§cInvalid material!");
return true;
}
int amount = 1;
if (args.length >= 3) {
try {
amount = Integer.parseInt(args[2]);
} catch (NumberFormatException ex) {
sender.sendMessage("§cInvalid amount!");
return true;
}
}
target.getInventory().addItem(new ItemStack(material, amount));
target.sendMessage("§aYou received §e" + amount + "x " + material.name() + "§a!");
return true;
}
}