use of org.bukkit.enchantments.Enchantment in project Bukkit by Bukkit.
the class ItemStack method deserialize.
/**
* Required method for configuration serialization
*
* @param args map to deserialize
* @return deserialized item stack
* @see ConfigurationSerializable
*/
public static ItemStack deserialize(Map<String, Object> args) {
Material type = Material.getMaterial((String) args.get("type"));
short damage = 0;
int amount = 1;
if (args.containsKey("damage")) {
damage = ((Number) args.get("damage")).shortValue();
}
if (args.containsKey("amount")) {
amount = (Integer) args.get("amount");
}
ItemStack result = new ItemStack(type, amount, damage);
if (args.containsKey("enchantments")) {
// Backward compatiblity, @deprecated
Object raw = args.get("enchantments");
if (raw instanceof Map) {
Map<?, ?> map = (Map<?, ?>) raw;
for (Map.Entry<?, ?> entry : map.entrySet()) {
Enchantment enchantment = Enchantment.getByName(entry.getKey().toString());
if ((enchantment != null) && (entry.getValue() instanceof Integer)) {
result.addUnsafeEnchantment(enchantment, (Integer) entry.getValue());
}
}
}
} else if (args.containsKey("meta")) {
// We cannot and will not have meta when enchantments (pre-ItemMeta) exist
Object raw = args.get("meta");
if (raw instanceof ItemMeta) {
result.setItemMeta((ItemMeta) raw);
}
}
return result;
}
use of org.bukkit.enchantments.Enchantment in project Bukkit by Bukkit.
the class EnchantCommand method execute.
@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]);
int minLevel = enchantment.getStartLevel();
int maxLevel = force ? Short.MAX_VALUE : enchantment.getMaxLevel();
if (integer != null) {
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;
}
if ("max".equals(args[2])) {
level = maxLevel;
}
}
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;
}
use of org.bukkit.enchantments.Enchantment in project xAuth by CypherX.
the class PlayerDataHandler method buildItemString.
private String buildItemString(ItemStack[] items) {
StringBuilder sbItems = new StringBuilder();
StringBuilder sbAmount = new StringBuilder();
StringBuilder sbDurability = new StringBuilder();
StringBuilder sbEnchants = new StringBuilder();
for (ItemStack item : items) {
int itemId = 0;
int amount = 0;
short durability = 0;
Map<Enchantment, Integer> enchants = null;
if (item != null) {
itemId = item.getTypeId();
amount = item.getAmount();
durability = item.getDurability();
enchants = item.getEnchantments();
if (!enchants.keySet().isEmpty()) {
for (Enchantment enchant : enchants.keySet()) {
int id = enchant.getId();
int level = enchants.get(enchant);
sbEnchants.append(id + ":" + level + "-");
}
sbEnchants.deleteCharAt(sbEnchants.lastIndexOf("-"));
}
}
sbItems.append(itemId).append(",");
sbAmount.append(amount).append(",");
sbDurability.append(durability).append(",");
sbEnchants.append(",");
}
sbItems.deleteCharAt(sbItems.lastIndexOf(","));
sbAmount.deleteCharAt(sbAmount.lastIndexOf(","));
sbDurability.deleteCharAt(sbDurability.lastIndexOf(","));
sbEnchants.deleteCharAt(sbEnchants.lastIndexOf(","));
return sbItems.append(";").append(sbAmount).append(";").append(sbDurability).append(";").append(sbEnchants).toString();
}
use of org.bukkit.enchantments.Enchantment in project Minigames by AddstarMC.
the class BlockData method toString.
@SuppressWarnings("deprecation")
@Override
public String toString() {
String ret = "{";
ret += "mat:" + state.getType().toString() + ";";
ret += "data:" + state.getData().getData() + ";";
ret += "x:" + state.getX() + ";";
ret += "y:" + state.getY() + ";";
ret += "z:" + state.getZ() + ";";
ret += "world:" + state.getWorld().getName();
if (items != null) {
ret += ";";
int c = 0;
ret += "items:";
for (ItemStack i : items) {
if (i != null) {
ret += "(";
ret += "item-" + i.getType().toString() + "|";
ret += "dur-" + i.getDurability() + "|";
ret += "c-" + i.getAmount() + "|";
if (!i.getEnchantments().isEmpty()) {
ret += "enc-";
for (Enchantment e : i.getEnchantments().keySet()) {
ret += "[";
ret += e.getName() + ",";
ret += i.getEnchantments().get(e);
ret += "]";
}
ret += "|";
}
ret += "slot-" + c;
ret += ")";
}
c++;
}
}
ret += "}";
return ret;
}
use of org.bukkit.enchantments.Enchantment in project EliteMobs by MagmaGuy.
the class EliteDropsHandler method superDropParser.
public void superDropParser() {
List<String> lootCount = lootCounter();
for (String lootEntry : lootCount) {
int itemPower = 0;
StringBuilder path = new StringBuilder();
path.append(lootEntry);
String previousPath = path.toString();
String itemType = itemTypeHandler(previousPath);
itemPower += itemTypePower(Material.getMaterial(itemType));
Bukkit.getLogger().info("Adding: " + previousPath);
String itemName = itemNameHandler(previousPath);
List itemLore = itemLoreHandler(previousPath);
List itemEnchantments = itemEnchantmentHandler(previousPath);
List potionEffects = itemPotionEffectHandler(previousPath);
ItemStack itemStack = new ItemStack(Material.getMaterial(itemType), 1);
ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.setDisplayName(itemName);
itemMeta.setLore(itemLore);
if (itemEnchantments != null) {
for (Object object : itemEnchantments) {
String string = object.toString();
String[] parsedString = string.split(",");
String enchantmentName = parsedString[0];
Enchantment enchantmentType = Enchantment.getByName(enchantmentName);
int enchantmentLevel = Integer.parseInt(parsedString[1]);
itemPower += enchantmentLevel;
itemMeta.addEnchant(enchantmentType, enchantmentLevel, true);
}
}
itemStack.setItemMeta(itemMeta);
lootList.add(itemStack);
List<PotionEffect> parsedPotionEffect = new ArrayList();
//Add potion effects to a separate list to reduce i/o operations
if (potionEffects != null) {
for (Object object : potionEffects) {
String string = object.toString();
String[] parsedString = string.split(",");
String potionEffectTypeString = parsedString[0];
PotionEffectType potionEffectType = PotionEffectType.getByName(potionEffectTypeString);
//this is a really bad way of doing things, two wrongs make a right
if (parsedString.length % 2 != 0) {
getLogger().info("Your item " + itemName + " has a problematic potions effect entry.");
}
int potionEffectAmplifier = Integer.parseInt(parsedString[1]);
itemPower += potionEffectAmplifier;
PotionEffect potionEffect = new PotionEffect(potionEffectType, 40, potionEffectAmplifier);
parsedPotionEffect.add(potionEffect);
}
potionEffectItemList.put(itemStack, parsedPotionEffect);
}
rankedItemMapCreator(itemPower, itemStack);
}
}
Aggregations