001 package org.bukkit.command.defaults; 002 003 import com.google.common.collect.ImmutableList; 004 import java.util.ArrayList; 005 import java.util.List; 006 import org.bukkit.ChatColor; 007 import org.bukkit.command.CommandSender; 008 import org.bukkit.entity.Player; 009 import org.bukkit.potion.PotionEffect; 010 import org.bukkit.potion.PotionEffectType; 011 import org.bukkit.util.StringUtil; 012 013 public class EffectCommand extends VanillaCommand { 014 private static final List<String> effects; 015 016 public EffectCommand() { 017 super("effect"); 018 this.description = "Adds/Removes effects on players"; 019 this.usageMessage = "/effect <player> <effect|clear> [seconds] [amplifier]"; 020 this.setPermission("bukkit.command.effect"); 021 } 022 023 static { 024 ImmutableList.Builder<String> builder = ImmutableList.<String>builder(); 025 026 for (PotionEffectType type : PotionEffectType.values()) { 027 if (type != null) { 028 builder.add(type.getName()); 029 } 030 } 031 032 effects = builder.build(); 033 } 034 035 @Override 036 public boolean execute(CommandSender sender, String commandLabel, String[] args) { 037 if (!testPermission(sender)) { 038 return true; 039 } 040 041 if (args.length < 2) { 042 sender.sendMessage(getUsage()); 043 return true; 044 } 045 046 final Player player = sender.getServer().getPlayer(args[0]); 047 048 if (player == null) { 049 sender.sendMessage(ChatColor.RED + String.format("Player, %s, not found", args[0])); 050 return true; 051 } 052 053 if ("clear".equalsIgnoreCase(args[1])) { 054 for (PotionEffect effect : player.getActivePotionEffects()) { 055 player.removePotionEffect(effect.getType()); 056 } 057 sender.sendMessage(String.format("Took all effects from %s", args[0])); 058 return true; 059 } 060 061 PotionEffectType effect = PotionEffectType.getByName(args[1]); 062 063 if (effect == null) { 064 effect = PotionEffectType.getById(getInteger(sender, args[1], 0)); 065 } 066 067 if (effect == null) { 068 sender.sendMessage(ChatColor.RED + String.format("Effect, %s, not found", args[1])); 069 return true; 070 } 071 072 int duration = 600; 073 int duration_temp = 30; 074 int amplification = 0; 075 076 if (args.length >= 3) { 077 duration_temp = getInteger(sender, args[2], 0, 1000000); 078 if (effect.isInstant()) { 079 duration = duration_temp; 080 } else { 081 duration = duration_temp * 20; 082 } 083 } else if (effect.isInstant()) { 084 duration = 1; 085 } 086 087 if (args.length >= 4) { 088 amplification = getInteger(sender, args[3], 0, 255); 089 } 090 091 if (duration_temp == 0) { 092 if (!player.hasPotionEffect(effect)) { 093 sender.sendMessage(String.format("Couldn't take %s from %s as they do not have the effect", effect.getName(), args[0])); 094 return true; 095 } 096 097 player.removePotionEffect(effect); 098 broadcastCommandMessage(sender, String.format("Took %s from %s", effect.getName(), args[0])); 099 } 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 }