13
0
geforkt von Mirrors/Paper

Add default EnchantCommand. Tim, The Enchanter - I AM DEATH! Adds BUKKIT-2961

By: EvilSeph <evilseph@gmail.com>
Dieser Commit ist enthalten in:
Bukkit/Spigot 2012-11-16 16:11:30 -06:00
Ursprung cf99b7e756
Commit 0a9f6c2cc2
4 geänderte Dateien mit 177 neuen und 0 gelöschten Zeilen

Datei anzeigen

@ -54,6 +54,7 @@ public class SimpleCommandMap implements CommandMap {
fallbackCommands.add(new SpawnpointCommand());
fallbackCommands.add(new ClearCommand());
fallbackCommands.add(new GameRuleCommand());
fallbackCommands.add(new EnchantCommand());
}
public SimpleCommandMap(final Server server) {

Datei anzeigen

@ -0,0 +1,166 @@
package org.bukkit.command.defaults;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.Validate;
import org.apache.commons.lang.WordUtils;
import com.google.common.collect.ImmutableList;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.StringUtil;
public class EnchantCommand extends VanillaCommand {
private static final List<String> ENCHANTMENT_NAMES = new ArrayList<String>();
public EnchantCommand() {
super("enchant");
this.description = "Adds enchantments to the item the player is currently holding. Specify 0 for the level to remove an enchantment. Specify force to ignore normal enchantment restrictions";
this.usageMessage = "/enchant <player> <enchantment> [level|0] [force]";
this.setPermission("bukkit.command.enchant");
}
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
if (!testPermission(sender)) return true;
if (args.length < 2) {
sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
return false;
}
boolean force = false;
if (args.length > 2) {
force = args[args.length > 3 ? 3 : 2].equalsIgnoreCase("force");
}
Player player = Bukkit.getPlayerExact(args[0]);
if (player == null) {
sender.sendMessage("Can't find player " + args[0]);
} else {
ItemStack item = player.getItemInHand();
if (item.getType() == Material.AIR) {
sender.sendMessage("The player isn't holding an item");
} else {
String itemName = item.getType().toString().replaceAll("_", " ");
itemName = WordUtils.capitalizeFully(itemName);
Enchantment enchantment = getEnchantment(args[1].toUpperCase());
if (enchantment == null) {
sender.sendMessage(String.format("Enchantment does not exist: %s", args[1]));
} else {
String enchantmentName = enchantment.getName().replaceAll("_", " ");
enchantmentName = WordUtils.capitalizeFully(enchantmentName);
if (!force && !enchantment.canEnchantItem(item)) {
sender.sendMessage(String.format("%s cannot be applied to %s", enchantmentName, itemName));
} else {
int level = 1;
if (args.length > 2) {
Integer integer = getInteger(args[2]);
if (integer != null) {
int minLevel = enchantment.getStartLevel();
int maxLevel = force ? Short.MAX_VALUE : enchantment.getMaxLevel();
if (integer == 0) {
item.removeEnchantment(enchantment);
Command.broadcastCommandMessage(sender, String.format("Removed %s on %s's %s", enchantmentName, player.getName(), itemName));
return true;
}
if (integer < minLevel || integer > maxLevel) {
sender.sendMessage(String.format("Level for enchantment %s must be between %d and %d", enchantmentName, minLevel, maxLevel));
sender.sendMessage("Specify 0 for level to remove an enchantment");
return true;
}
level = integer;
}
}
Map<Enchantment, Integer> enchantments = item.getEnchantments();
boolean conflicts = false;
if (!force && !enchantments.isEmpty()) { // TODO: Improve this to use a "hasEnchantments" call
for (Map.Entry<Enchantment, Integer> entry : enchantments.entrySet()) {
Enchantment enchant = entry.getKey();
if (enchant.equals(enchantment)) continue;
if (enchant.conflictsWith(enchantment)) {
sender.sendMessage(String.format("Can't apply the enchantment %s on an item with the enchantment %s", enchantmentName, WordUtils.capitalizeFully(enchant.getName().replaceAll("_", " "))));
conflicts = true;
break;
}
}
}
if (!conflicts) {
item.addUnsafeEnchantment(enchantment, level);
Command.broadcastCommandMessage(sender, String.format("Applied %s (Lvl %d) on %s's %s", enchantmentName, level, player.getName(), itemName), false);
sender.sendMessage(String.format("Enchanting succeeded, applied %s (Lvl %d) onto your %s", enchantmentName, level, itemName));
}
}
}
}
}
return true;
}
@Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
Validate.notNull(sender, "Sender cannot be null");
Validate.notNull(args, "Arguments cannot be null");
Validate.notNull(alias, "Alias cannot be null");
if (args.length == 1) {
return super.tabComplete(sender, alias, args);
}
if (args.length == 2) {
return StringUtil.copyPartialMatches(args[1], ENCHANTMENT_NAMES, new ArrayList<String>(ENCHANTMENT_NAMES.size()));
}
if (args.length == 3 || args.length == 4) {
if (!args[args.length - 2].equalsIgnoreCase("force")) {
return ImmutableList.of("force");
}
}
return ImmutableList.of();
}
private Enchantment getEnchantment(String lookup) {
Enchantment enchantment = Enchantment.getByName(lookup);
if (enchantment == null) {
Integer id = getInteger(lookup);
if (id != null) {
enchantment = Enchantment.getById(id);
}
}
return enchantment;
}
public static void buildEnchantments() {
if (!ENCHANTMENT_NAMES.isEmpty()) {
throw new IllegalStateException("Enchantments have already been built!");
}
for (Enchantment enchantment : Enchantment.values()) {
ENCHANTMENT_NAMES.add(enchantment.getName());
}
Collections.sort(ENCHANTMENT_NAMES);
}
}

Datei anzeigen

@ -42,6 +42,14 @@ public abstract class VanillaCommand extends Command {
return i;
}
Integer getInteger(String value) {
try {
return Integer.valueOf(value);
} catch (NumberFormatException ex) {
return null;
}
}
public static double getDouble(CommandSender sender, String input) {
try {
return Double.parseDouble(input);

Datei anzeigen

@ -2,6 +2,7 @@ package org.bukkit.enchantments;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.command.defaults.EnchantCommand;
import org.bukkit.inventory.ItemStack;
/**
@ -233,6 +234,7 @@ public abstract class Enchantment {
*/
public static void stopAcceptingRegistrations() {
acceptingNew = false;
EnchantCommand.buildEnchantments();
}
/**