summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/bukkit/command/defaults/EffectCommand.java
blob: bfa5b2a57bb4966a43181d17c78cd80d350642cc (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package org.bukkit.command.defaults;

import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.util.StringUtil;

public class EffectCommand extends VanillaCommand {
    private static final List<String> effects;

    public EffectCommand() {
        super("effect");
        this.description = "Adds/Removes effects on players";
        this.usageMessage = "/effect <player> <effect> [seconds] [amplifier]";
        this.setPermission("bukkit.command.effect");
    }

    static {
        ImmutableList.Builder<String> builder = ImmutableList.<String>builder();

        for (PotionEffectType type : PotionEffectType.values()) {
            if (type != null) {
                builder.add(type.getName());
            }
        }

        effects = builder.build();
    }

    @Override
    public boolean execute(CommandSender sender, String commandLabel, String[] args) {
        if (!testPermission(sender)) {
            return true;
        }

        if (args.length < 2) {
            sender.sendMessage(getUsage());
            return true;
        }

        final Player player = sender.getServer().getPlayer(args[0]);

        if (player == null) {
            sender.sendMessage(ChatColor.RED + String.format("Player, %s, not found", args[0]));
            return true;
        }

        PotionEffectType effect = PotionEffectType.getByName(args[1]);

        if (effect == null) {
            effect = PotionEffectType.getById(getInteger(sender, args[1], 0));
        }

        if (effect == null) {
            sender.sendMessage(ChatColor.RED + String.format("Effect, %s, not found", args[1]));
            return true;
        }

        int duration = 600;
        int duration_temp = 30;
        int amplification = 0;

        if (args.length >= 3) {
            duration_temp = getInteger(sender, args[2], 0, 1000000);
            if (effect.isInstant()) {
                duration = duration_temp;
            } else {
                duration = duration_temp * 20;
            }
        } else if (effect.isInstant()) {
            duration = 1;
        }

        if (args.length >= 4) {
            amplification = getInteger(sender, args[3], 0, 255);
        }

        if (duration_temp == 0) {
            if (!player.hasPotionEffect(effect)) {
                sender.sendMessage(String.format("Couldn't take %s from %s as they do not have the effect", effect.getName(), args[0]));
                return true;
            }

            player.removePotionEffect(effect);
            broadcastCommandMessage(sender, String.format("Took %s from %s", effect.getName(), args[0]));
        } else {
            final PotionEffect applyEffect = new PotionEffect(effect, duration, amplification);

            player.addPotionEffect(applyEffect, true);
            broadcastCommandMessage(sender, String.format("Given %s (ID %d) * %d to %s for %d seconds", effect.getName(), effect.getId(), amplification, args[0], duration));
        }

        return true;
    }

    @Override
    public List<String> tabComplete(CommandSender sender, String commandLabel, String[] args) {
        if (args.length == 1) {
            return super.tabComplete(sender, commandLabel, args);
        } else if (args.length == 2) {
            return StringUtil.copyPartialMatches(args[1], effects, new ArrayList<String>(effects.size()));
        }

        return ImmutableList.of();
    }
}