-
Notifications
You must be signed in to change notification settings - Fork 429
Expand file tree
/
Copy pathEffectCommand.java
More file actions
128 lines (122 loc) · 5.53 KB
/
EffectCommand.java
File metadata and controls
128 lines (122 loc) · 5.53 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package cn.nukkit.command.defaults;
import cn.nukkit.Player;
import cn.nukkit.command.Command;
import cn.nukkit.command.CommandSender;
import cn.nukkit.command.data.CommandEnum;
import cn.nukkit.command.data.CommandParamType;
import cn.nukkit.command.data.CommandParameter;
import cn.nukkit.event.entity.EntityPotionEffectEvent;
import cn.nukkit.lang.TranslationContainer;
import cn.nukkit.potion.Effect;
import cn.nukkit.potion.InstantEffect;
import cn.nukkit.utils.ServerException;
import cn.nukkit.utils.TextFormat;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Snake1999 and Pub4Game on 2016/1/23.
* Package cn.nukkit.command.defaults in project nukkit.
*/
public class EffectCommand extends VanillaCommand {
public EffectCommand(String name) {
super(name, "%nukkit.command.effect.description", "%commands.effect.usage");
this.setPermission("nukkit.command.effect");
this.commandParameters.clear();
List<String> effects = new ArrayList<>();
for (Field field : Effect.class.getDeclaredFields()) {
if (field.getType() == int.class && field.getModifiers() == (Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL)) {
effects.add(field.getName().toLowerCase());
}
}
this.commandParameters.put("default", new CommandParameter[]{
CommandParameter.newType("player", CommandParamType.TARGET),
CommandParameter.newEnum("effect", new CommandEnum("Effect", effects)),
CommandParameter.newType("seconds", true, CommandParamType.INT),
CommandParameter.newType("amplifier", true, CommandParamType.INT),
CommandParameter.newEnum("hideParticle", true, CommandEnum.ENUM_BOOLEAN)
});
this.commandParameters.put("clear", new CommandParameter[]{
CommandParameter.newType("player", CommandParamType.TARGET),
CommandParameter.newEnum("clear", new CommandEnum("ClearEffects", "clear"))
});
}
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
if (!this.testPermission(sender)) {
return true;
}
if (args.length < 2) {
sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
return true;
}
Player player = sender.getServer().getPlayer(args[0].replace("@s", sender.getName()));
if (player == null) {
sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.player.notFound"));
return true;
}
if (args[1].equalsIgnoreCase("clear")) {
player.removeAllEffects(EntityPotionEffectEvent.Cause.COMMAND);
sender.sendMessage(new TranslationContainer("commands.effect.success.removed.all", player.getDisplayName()));
return true;
}
Effect effect;
try {
effect = Effect.getEffect(Integer.parseInt(args[1]));
} catch (NumberFormatException | ServerException a) {
try {
effect = Effect.getEffectByName(args[1]);
} catch (Exception e) {
sender.sendMessage(new TranslationContainer("commands.effect.notFound", args[1]));
return true;
}
}
int duration = 300;
int amplification = 0;
if (args.length >= 3) {
try {
duration = Integer.parseInt(args[2]);
} catch (NumberFormatException a) {
sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
return true;
}
if (!(effect instanceof InstantEffect)) {
duration *= 20;
}
} else if (effect instanceof InstantEffect) {
duration = 1;
}
if (args.length >= 4) {
try {
amplification = Integer.parseInt(args[3]);
} catch (NumberFormatException a) {
sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
return true;
}
}
if (args.length >= 5) {
String v = args[4].toLowerCase();
if (v.matches("(?i)|on|true|t|1")) {
effect.setVisible(false);
}
}
if (duration == 0) {
if (!player.hasEffect(effect.getId())) {
if (player.getEffects().size() == 0) {
sender.sendMessage(new TranslationContainer("commands.effect.failure.notActive.all", player.getDisplayName()));
} else {
sender.sendMessage(new TranslationContainer("commands.effect.failure.notActive", effect.getName(), player.getDisplayName()));
}
return true;
}
player.removeEffect(effect.getId(), EntityPotionEffectEvent.Cause.COMMAND);
sender.sendMessage(new TranslationContainer("commands.effect.success.removed", effect.getName(), player.getDisplayName()));
} else {
effect.setDuration(duration).setAmplifier(amplification);
player.addEffect(effect, EntityPotionEffectEvent.Cause.COMMAND);
Command.broadcastCommandMessage(sender, new TranslationContainer("%commands.effect.success", effect.getName(), String.valueOf(effect.getAmplifier()), player.getDisplayName(), String.valueOf(effect.getDuration() / 20)));
}
return true;
}
}