Commits

feildmaster authored 01ec789162f
Add Effect command.

Adds BUKKIT-3763
No tags

src/main/java/org/bukkit/command/defaults/EffectCommand.java

Added
1 +package org.bukkit.command.defaults;
2 +
3 +import com.google.common.collect.ImmutableList;
4 +import java.util.ArrayList;
5 +import java.util.List;
6 +import org.bukkit.ChatColor;
7 +import org.bukkit.command.CommandSender;
8 +import org.bukkit.entity.Player;
9 +import org.bukkit.potion.PotionEffect;
10 +import org.bukkit.potion.PotionEffectType;
11 +import org.bukkit.util.StringUtil;
12 +
13 +public class EffectCommand extends VanillaCommand {
14 + private static final List<String> effects;
15 +
16 + public EffectCommand() {
17 + super("effect");
18 + this.description = "Adds/Removes effects on players";
19 + this.usageMessage = "/effect <player> <effect> [seconds] [amplifier]";
20 + this.setPermission("bukkit.command.effect");
21 + }
22 +
23 + static {
24 + ImmutableList.Builder<String> builder = ImmutableList.<String>builder();
25 +
26 + for (PotionEffectType type : PotionEffectType.values()) {
27 + if (type != null) {
28 + builder.add(type.getName());
29 + }
30 + }
31 +
32 + effects = builder.build();
33 + }
34 +
35 + @Override
36 + public boolean execute(CommandSender sender, String commandLabel, String[] args) {
37 + if (!testPermission(sender)) {
38 + return true;
39 + }
40 +
41 + if (args.length < 2) {
42 + sender.sendMessage(getUsage());
43 + return true;
44 + }
45 +
46 + final Player player = sender.getServer().getPlayer(args[0]);
47 +
48 + if (player == null) {
49 + sender.sendMessage(ChatColor.RED + String.format("Player, %s, not found", args[0]));
50 + return true;
51 + }
52 +
53 + PotionEffectType effect = PotionEffectType.getByName(args[1]);
54 +
55 + if (effect == null) {
56 + effect = PotionEffectType.getById(getInteger(sender, args[1], 0));
57 + }
58 +
59 + if (effect == null) {
60 + sender.sendMessage(ChatColor.RED + String.format("Effect, %s, not found", args[1]));
61 + return true;
62 + }
63 +
64 + int duration = 600;
65 + int duration_temp = 30;
66 + int amplification = 0;
67 +
68 + if (args.length >= 3) {
69 + duration_temp = getInteger(sender, args[2], 0, 1000000);
70 + if (effect.isInstant()) {
71 + duration = duration_temp;
72 + } else {
73 + duration = duration_temp * 20;
74 + }
75 + } else if (effect.isInstant()) {
76 + duration = 1;
77 + }
78 +
79 + if (args.length >= 4) {
80 + amplification = getInteger(sender, args[3], 0, 255);
81 + }
82 +
83 + if (duration_temp == 0) {
84 + if (!player.hasPotionEffect(effect)) {
85 + sender.sendMessage(String.format("Couldn't take %s from %s as they do not have the effect", effect.getName(), args[0]));
86 + return true;
87 + }
88 +
89 + player.removePotionEffect(effect);
90 + broadcastCommandMessage(sender, String.format("Took %s from %s", effect.getName(), args[0]));
91 + } else {
92 + final PotionEffect applyEffect = new PotionEffect(effect, duration, amplification);
93 +
94 + player.addPotionEffect(applyEffect, true);
95 + broadcastCommandMessage(sender, String.format("Given %s (ID %d) * %d to %s for %d seconds", effect.getName(), effect.getId(), amplification, args[0], duration));
96 + }
97 +
98 + return true;
99 + }
100 +
101 + @Override
102 + public List<String> tabComplete(CommandSender sender, String commandLabel, String[] args) {
103 + if (args.length == 1) {
104 + return super.tabComplete(sender, commandLabel, args);
105 + } else if (args.length == 2) {
106 + return StringUtil.copyPartialMatches(args[1], effects, new ArrayList<String>(effects.size()));
107 + }
108 +
109 + return ImmutableList.of();
110 + }
111 +}

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

Add shortcut