Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
EffectCommand.java
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;
8 import org.bukkit.entity.Player;
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|clear> [seconds] [amplifier]";
20  this.setPermission("bukkit.command.effect");
21  }
22 
23  static {
24  ImmutableList.Builder<String> builder = ImmutableList.<String>builder();
25 
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  if ("clear".equalsIgnoreCase(args[1])) {
54  for (PotionEffect effect : player.getActivePotionEffects()) {
55  player.removePotionEffect(effect.getType());
56  }
57  sender.sendMessage(String.format("Took all effects from %s", args[0]));
58  return true;
59  }
60 
62 
63  if (effect == null) {
64  effect = PotionEffectType.getById(getInteger(sender, args[1], 0));
65  }
66 
67  if (effect == null) {
68  sender.sendMessage(ChatColor.RED + String.format("Effect, %s, not found", args[1]));
69  return true;
70  }
71 
72  int duration = 600;
73  int duration_temp = 30;
74  int amplification = 0;
75 
76  if (args.length >= 3) {
77  duration_temp = getInteger(sender, args[2], 0, 1000000);
78  if (effect.isInstant()) {
79  duration = duration_temp;
80  } else {
81  duration = duration_temp * 20;
82  }
83  } else if (effect.isInstant()) {
84  duration = 1;
85  }
86 
87  if (args.length >= 4) {
88  amplification = getInteger(sender, args[3], 0, 255);
89  }
90 
91  if (duration_temp == 0) {
92  if (!player.hasPotionEffect(effect)) {
93  sender.sendMessage(String.format("Couldn't take %s from %s as they do not have the effect", effect.getName(), args[0]));
94  return true;
95  }
96 
97  player.removePotionEffect(effect);
98  broadcastCommandMessage(sender, String.format("Took %s from %s", effect.getName(), args[0]));
99  } else {
100  final PotionEffect applyEffect = new PotionEffect(effect, duration, amplification);
101 
102  player.addPotionEffect(applyEffect, true);
103  broadcastCommandMessage(sender, String.format("Given %s (ID %d) * %d to %s for %d seconds", effect.getName(), effect.getId(), amplification, args[0], duration_temp));
104  }
105 
106  return true;
107  }
108 
109  @Override
110  public List<String> tabComplete(CommandSender sender, String commandLabel, String[] args) {
111  if (args.length == 1) {
112  return super.tabComplete(sender, commandLabel, args);
113  } else if (args.length == 2) {
114  return StringUtil.copyPartialMatches(args[1], effects, new ArrayList<String>(effects.size()));
115  }
116 
117  return ImmutableList.of();
118  }
119 }
boolean testPermission(CommandSender target)
void removePotionEffect(PotionEffectType type)
static< TextendsCollection<?superString > T copyPartialMatches(final String token, final Iterable< String > originals, final T collection)
Definition: StringUtil.java:24
static PotionEffectType getById(int id)
void sendMessage(String message)
boolean hasPotionEffect(PotionEffectType type)
Collection< PotionEffect > getActivePotionEffects()
void setPermission(String permission)
Player getPlayer(String name)
static PotionEffectType[] values()
boolean addPotionEffect(PotionEffect effect)
static PotionEffectType getByName(String name)