use of org.bukkit.enchantments.Enchantment in project TotalFreedomMod by TotalFreedom.
the class Command_enchant method run.
@Override
public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) {
if (args.length < 1) {
return false;
}
ItemStack item = playerSender.getEquipment().getItemInMainHand();
if (item == null || item.getType() == Material.AIR) {
msg("You have to hold an item to enchant it");
return true;
}
if (args[0].equalsIgnoreCase("list")) {
boolean has_enchantments = false;
StringBuilder possible_ench = new StringBuilder("Possible enchantments for held item: ");
for (Enchantment ench : Enchantment.values()) {
if (ench.canEnchantItem(item)) {
has_enchantments = true;
possible_ench.append(ench.getName()).append(", ");
}
}
if (has_enchantments) {
msg(possible_ench.toString());
} else {
msg("The held item has no enchantments.");
}
} else if (args[0].equalsIgnoreCase("addall")) {
for (Enchantment ench : Enchantment.values()) {
try {
if (ench.canEnchantItem(item)) {
item.addEnchantment(ench, ench.getMaxLevel());
}
} catch (Exception ex) {
msg("Could not add enchantment: " + ench.getName());
}
}
msg("Added all possible enchantments for this item.");
} else if (args[0].equalsIgnoreCase("reset")) {
for (Enchantment ench : item.getEnchantments().keySet()) {
item.removeEnchantment(ench);
}
msg("Removed all enchantments.");
} else {
if (args.length < 2) {
return false;
}
Enchantment ench = null;
try {
ench = Enchantment.getByName(args[1]);
} catch (Exception ex) {
}
if (ench == null) {
msg(args[1] + " is an invalid enchantment for the held item. Type \"/enchant list\" for valid enchantments for this item.");
return true;
}
if (args[0].equalsIgnoreCase("add")) {
if (ench.canEnchantItem(item)) {
item.addEnchantment(ench, ench.getMaxLevel());
msg("Added enchantment: " + ench.getName());
} else {
msg("Can't use this enchantment on held item.");
}
} else if (args[0].equals("remove")) {
item.removeEnchantment(ench);
msg("Removed enchantment: " + ench.getName());
}
}
return true;
}
use of org.bukkit.enchantments.Enchantment in project Denizen-For-Bukkit by DenizenScript.
the class ItemScriptContainer method getItemFrom.
public dItem getItemFrom(dPlayer player, dNPC npc) {
// Try to use this script to make an item.
dItem stack = null;
try {
boolean debug = true;
if (contains("DEBUG")) {
debug = Boolean.valueOf(getString("DEBUG"));
}
// Check validity of material
if (contains("MATERIAL")) {
String material = TagManager.tag(getString("MATERIAL"), new BukkitTagContext(player, npc, false, null, debug, new dScript(this)));
if (material.startsWith("m@")) {
material = material.substring(2);
}
stack = dItem.valueOf(material);
}
// Make sure we're working with a valid base ItemStack
if (stack == null) {
return null;
}
ItemMeta meta = stack.getItemStack().getItemMeta();
List<String> lore = meta.hasLore() ? meta.getLore() : new ArrayList<String>();
// Set Id of the first, invisible lore
boolean hideLore = false;
boolean pureNbtId = false;
if (contains("NO_ID")) {
hideLore = Boolean.valueOf(getString("NO_ID"));
}
if (!hideLore) {
if (!Settings.packetInterception()) {
lore.add(0, hash);
} else {
pureNbtId = true;
}
}
// Set Display Name
if (contains("DISPLAY NAME")) {
String displayName = TagManager.tag(getString("DISPLAY NAME"), new BukkitTagContext(player, npc, false, null, debug, new dScript(this)));
meta.setDisplayName(displayName);
}
// Set if the object is bound to the player
if (contains("BOUND")) {
bound = Boolean.valueOf(TagManager.tag(getString("BOUND"), new BukkitTagContext(player, npc, false, null, debug, new dScript(this))));
}
// Set Lore
if (contains("LORE")) {
for (String l : getStringList("LORE")) {
l = TagManager.tag(l, new BukkitTagContext(player, npc, false, null, debug, new dScript(this)));
lore.add(l);
}
}
meta.setLore(lore);
stack.getItemStack().setItemMeta(meta);
// Set Durability
if (contains("DURABILITY")) {
short durability = Short.valueOf(getString("DURABILITY"));
stack.setDurability(durability);
}
// Set Enchantments
if (contains("ENCHANTMENTS")) {
for (String enchantment : getStringList("ENCHANTMENTS")) {
enchantment = TagManager.tag(enchantment, new BukkitTagContext(player, npc, false, null, debug, new dScript(this)));
try {
// Build enchantment context
int level = 1;
String[] split = enchantment.split(":");
if (split.length > 1) {
level = Integer.valueOf(split[1].replace(" ", ""));
enchantment = split[0].replace(" ", "");
}
// Add enchantment
Enchantment ench = Enchantment.getByName(enchantment.toUpperCase());
stack.getItemStack().addUnsafeEnchantment(ench, level);
} catch (Exception e) {
dB.echoError("While constructing '" + getName() + "', encountered error: '" + enchantment + "' is an invalid enchantment!");
}
}
}
// Set Color
if (contains("COLOR")) {
String color = TagManager.tag(getString("COLOR"), new BukkitTagContext(player, npc, false, null, debug, new dScript(this)));
LeatherColorer.colorArmor(stack, color);
}
// Set Book
if (contains("BOOK")) {
BookScriptContainer book = ScriptRegistry.getScriptContainer(TagManager.tag(getString("BOOK"), new BukkitTagContext(player, npc, false, null, debug, new dScript(this))).replace("s@", ""));
stack = book.writeBookTo(stack, player, npc);
}
if (pureNbtId) {
stack.setItemStack(NMSHandler.getInstance().getItemHelper().addNbtData(stack.getItemStack(), "Denizen Item Script", new StringTag(hash)));
}
} catch (Exception e) {
dB.echoError("Woah! An exception has been called with this item script!");
dB.echoError(e);
stack = null;
}
return stack;
}
use of org.bukkit.enchantments.Enchantment in project xAuth by CypherX.
the class PlayerDataHandler method buildItemStack.
private ItemStack[] buildItemStack(String str) {
ItemStack[] items = null;
String[] itemSplit = str.split(";");
String[] itemid = itemSplit[0].split(",");
String[] amount = itemSplit[1].split(",");
String[] durability = itemSplit[2].split(",");
String[] enchants = itemSplit[3].split(",", -1);
items = new ItemStack[itemid.length];
for (int i = 0; i < items.length; i++) {
items[i] = new ItemStack(Integer.parseInt(itemid[i]), Integer.parseInt(amount[i]), Short.parseShort(durability[i]));
if (!enchants[i].isEmpty()) {
String[] itemEnchants = enchants[i].split("-");
for (String enchant : itemEnchants) {
String[] enchantSplit = enchant.split(":");
int id = Integer.parseInt(enchantSplit[0]);
int level = Integer.parseInt(enchantSplit[1]);
Enchantment e = new EnchantmentWrapper(id);
items[i].addUnsafeEnchantment(e, level);
}
}
}
return items;
}
use of org.bukkit.enchantments.Enchantment in project Glowstone by GlowstoneMC.
the class EnchantmentManager method getAllPossibleEnchants.
private static List<LeveledEnchant> getAllPossibleEnchants(ItemStack item, int modifier, int cost) {
List<LeveledEnchant> enchantments = new ArrayList<>();
boolean isBook = item.getType() == Material.BOOK;
for (Enchantment enchantment : Enchantment.values()) {
if (isBook || enchantment.canEnchantItem(item)) {
for (int level = enchantment.getStartLevel(); level <= enchantment.getMaxLevel(); level++) {
if (((GlowEnchantment) enchantment).isInRange(level, modifier)) {
enchantments.add(new LeveledEnchant(enchantment, level, cost));
}
}
}
}
return enchantments;
}
use of org.bukkit.enchantments.Enchantment in project Glowstone by GlowstoneMC.
the class EnchantmentManager method removeConflicting.
/////////////////////////////////////
// Internal stuff / helper functions
private static void removeConflicting(List<LeveledEnchant> enchants, List<LeveledEnchant> toReduce) {
Iterator<LeveledEnchant> it = toReduce.iterator();
while (it.hasNext()) {
Enchantment currentEnchantment = it.next().getEnchantment();
boolean conflicts = false;
for (LeveledEnchant entry : enchants) {
if (entry.getEnchantment().conflictsWith(currentEnchantment)) {
conflicts = true;
break;
}
}
if (conflicts)
it.remove();
}
}
Aggregations