TF-EssentialsX/Essentials/src/main/java/com/earth2me/essentials/commands/Commandpotion.java

108 lines
4.7 KiB
Java
Raw Normal View History

2013-02-04 21:47:26 +00:00
package com.earth2me.essentials.commands;
import com.earth2me.essentials.MetaItemStack;
import com.earth2me.essentials.Potions;
import com.earth2me.essentials.User;
2013-06-08 21:31:19 +00:00
import com.earth2me.essentials.utils.StringUtil;
import com.google.common.collect.Lists;
import net.ess3.nms.refl.ReflUtil;
2013-02-04 21:47:26 +00:00
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.PotionMeta;
2013-02-08 23:13:36 +00:00
import org.bukkit.potion.PotionEffect;
2013-02-04 21:47:26 +00:00
import org.bukkit.potion.PotionEffectType;
2020-10-03 17:46:05 +00:00
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
2015-04-15 04:06:16 +00:00
import static com.earth2me.essentials.I18n.tl;
public class Commandpotion extends EssentialsCommand {
public Commandpotion() {
super("potion");
}
2013-02-04 21:47:26 +00:00
2015-04-15 04:06:16 +00:00
@Override
protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
final ItemStack stack = user.getItemInHand();
2015-04-15 04:06:16 +00:00
if (args.length == 0) {
2015-06-03 20:11:56 +00:00
final Set<String> potionslist = new TreeSet<>();
2020-10-03 17:46:05 +00:00
for (final Map.Entry<String, PotionEffectType> entry : Potions.entrySet()) {
2015-04-15 04:06:16 +00:00
final String potionName = entry.getValue().getName().toLowerCase(Locale.ENGLISH);
2020-10-03 17:46:05 +00:00
if (potionslist.contains(potionName) || user.isAuthorized("essentials.potion." + potionName)) {
2015-04-15 04:06:16 +00:00
potionslist.add(entry.getKey());
}
}
throw new NotEnoughArgumentsException(tl("potions", StringUtil.joinList(potionslist.toArray())));
}
2020-10-03 17:46:05 +00:00
boolean holdingPotion = stack.getType() == Material.POTION;
if (!holdingPotion && ReflUtil.getNmsVersionObject().isHigherThanOrEqualTo(ReflUtil.V1_9_R1)) {
holdingPotion = stack.getType() == Material.SPLASH_POTION || stack.getType() == Material.LINGERING_POTION;
}
if (holdingPotion) {
2015-04-15 04:06:16 +00:00
PotionMeta pmeta = (PotionMeta) stack.getItemMeta();
if (args[0].equalsIgnoreCase("clear")) {
pmeta.clearCustomEffects();
stack.setItemMeta(pmeta);
} else if (args[0].equalsIgnoreCase("apply") && user.isAuthorized("essentials.potion.apply")) {
2020-10-03 17:46:05 +00:00
for (final PotionEffect effect : pmeta.getCustomEffects()) {
effect.apply(user.getBase());
}
} else if (args.length < 3) {
throw new NotEnoughArgumentsException();
} else {
final MetaItemStack mStack = new MetaItemStack(stack);
2020-10-03 17:46:05 +00:00
for (final String arg : args) {
mStack.addPotionMeta(user.getSource(), true, arg, ess);
}
if (mStack.completePotion()) {
pmeta = (PotionMeta) mStack.getItemStack().getItemMeta();
2015-04-15 04:06:16 +00:00
stack.setItemMeta(pmeta);
} else {
user.sendMessage(tl("invalidPotion"));
throw new NotEnoughArgumentsException();
2015-04-15 04:06:16 +00:00
}
}
} else {
throw new Exception(tl("holdPotion"));
}
}
@Override
2020-10-03 17:46:05 +00:00
protected List<String> getTabCompleteOptions(final Server server, final User user, final String commandLabel, final String[] args) {
// Note: this enforces an order of effect power duration splash, which the actual command doesn't have. But that's fine.
if (args.length == 1) {
2020-10-03 17:46:05 +00:00
final List<String> options = Lists.newArrayList();
options.add("clear");
if (user.isAuthorized("essentials.potion.apply")) {
options.add("apply");
}
2020-10-03 17:46:05 +00:00
for (final Map.Entry<String, PotionEffectType> entry : Potions.entrySet()) {
final String potionName = entry.getValue().getName().toLowerCase(Locale.ENGLISH);
if (user.isAuthorized("essentials.potion." + potionName)) {
options.add("effect:" + entry.getKey());
}
}
return options;
} else if (args.length == 2 && args[0].startsWith("effect:")) {
return Lists.newArrayList("power:1", "power:2", "power:3", "power:4", "amplifier:0", "amplifier:1", "amplifier:2", "amplifier:3");
} else if (args.length == 3 && args[0].startsWith("effect:")) {
2020-10-03 17:46:05 +00:00
final List<String> options = Lists.newArrayList();
for (final String duration : COMMON_DURATIONS) {
options.add("duration:" + duration);
}
return options;
} else if (args.length == 4 && args[0].startsWith("effect:")) {
return Lists.newArrayList("splash:true", "splash:false");
} else {
return Collections.emptyList();
}
}
2013-02-04 21:47:26 +00:00
}