use of org.bukkit.enchantments.Enchantment in project Glowstone by GlowstoneMC.
the class GlowMetaItem method readNbtEnchants.
protected static Map<Enchantment, Integer> readNbtEnchants(String name, CompoundTag tag) {
Map<Enchantment, Integer> result = null;
if (tag.isList(name, TagType.COMPOUND)) {
Iterable<CompoundTag> enchs = tag.getCompoundList(name);
for (CompoundTag enchantmentTag : enchs) {
if (enchantmentTag.isShort("id") && enchantmentTag.isShort("lvl")) {
Enchantment enchantment = Enchantment.getById(enchantmentTag.getShort("id"));
if (result == null)
result = new HashMap<>(4);
result.put(enchantment, (int) enchantmentTag.getShort("lvl"));
}
}
}
return result;
}
use of org.bukkit.enchantments.Enchantment in project Glowstone by GlowstoneMC.
the class GlowMetaItem method writeNbtEnchants.
protected static void writeNbtEnchants(String name, CompoundTag to, Map<Enchantment, Integer> enchants) {
List<CompoundTag> ench = new ArrayList<>();
for (Entry<Enchantment, Integer> enchantment : enchants.entrySet()) {
CompoundTag enchantmentTag = new CompoundTag();
enchantmentTag.putShort("id", enchantment.getKey().getId());
enchantmentTag.putShort("lvl", enchantment.getValue());
ench.add(enchantmentTag);
}
to.putCompoundList(name, ench);
}
use of org.bukkit.enchantments.Enchantment in project Glowstone by GlowstoneMC.
the class EnchantmentManager method onPlayerEnchant.
public void onPlayerEnchant(int clicked) {
if (enchLevelCosts[clicked] <= 0 || isMaliciousClicked(clicked))
return;
ItemStack item = inventory.getItem();
List<LeveledEnchant> enchants = calculateCurrentEnchants(item, clicked, enchLevelCosts[clicked]);
if (enchants == null)
enchants = new ArrayList<>();
EnchantItemEvent event = EventFactory.callEvent(new EnchantItemEvent(player, player.getOpenInventory(), inventory.getLocation().getBlock(), item.clone(), enchLevelCosts[clicked], toMap(enchants), clicked));
if (event.isCancelled() || player.getGameMode() != GameMode.CREATIVE && event.getExpLevelCost() > player.getLevel())
return;
boolean isBook = item.getType() == Material.BOOK;
if (isBook)
item.setType(Material.ENCHANTED_BOOK);
Map<Enchantment, Integer> toAdd = event.getEnchantsToAdd();
if (toAdd == null || toAdd.isEmpty()) {
return;
}
for (Entry<Enchantment, Integer> enchantment : toAdd.entrySet()) {
try {
if (isBook) {
EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta();
//TODO is true correct here?
meta.addStoredEnchant(enchantment.getKey(), enchantment.getValue(), true);
item.setItemMeta(meta);
} else {
item.addUnsafeEnchantment(enchantment.getKey(), enchantment.getValue());
}
} catch (IllegalArgumentException e) {
//ignore, since plugins are allowed to add enchantments that can't be applied
}
}
player.enchanted(clicked);
if (player.getGameMode() != GameMode.CREATIVE) {
ItemStack res = inventory.getSecondary();
res.setAmount(res.getAmount() - clicked + 1);
if (res.getAmount() <= 0)
inventory.setSecondary(null);
}
xpSeed = player.getXpSeed();
update();
}
use of org.bukkit.enchantments.Enchantment in project Essentials by drtshock.
the class YamlStorageWriter method writeItemStack.
private void writeItemStack(final Object data) {
final ItemStack itemStack = (ItemStack) data;
writeMaterialData(itemStack.getData());
writer.print(' ');
writer.print(itemStack.getAmount());
for (Entry<Enchantment, Integer> entry : itemStack.getEnchantments().entrySet()) {
writer.print(' ');
writeEnchantmentLevel(entry);
}
}
use of org.bukkit.enchantments.Enchantment in project Essentials by drtshock.
the class SignEnchant method onSignCreate.
@Override
protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException {
final ItemStack stack;
try {
stack = sign.getLine(1).equals("*") || sign.getLine(1).equalsIgnoreCase("any") ? null : getItemStack(sign.getLine(1), 1, ess);
} catch (SignException e) {
sign.setLine(1, "§c<item|any>");
throw e;
}
final String[] enchantLevel = sign.getLine(2).split(":");
if (enchantLevel.length != 2) {
sign.setLine(2, "§c<enchant>");
throw new SignException(tl("invalidSignLine", 3));
}
final Enchantment enchantment = Enchantments.getByName(enchantLevel[0]);
if (enchantment == null) {
sign.setLine(2, "§c<enchant>");
throw new SignException(tl("enchantmentNotFound"));
}
int level;
try {
level = Integer.parseInt(enchantLevel[1]);
} catch (NumberFormatException ex) {
sign.setLine(2, "§c<enchant>");
throw new SignException(ex.getMessage(), ex);
}
final boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments() && player.isAuthorized("essentials.enchantments.allowunsafe") && player.isAuthorized("essentials.signs.enchant.allowunsafe");
if (level < 0 || (!allowUnsafe && level > enchantment.getMaxLevel())) {
level = enchantment.getMaxLevel();
sign.setLine(2, enchantLevel[0] + ":" + level);
}
try {
if (stack != null) {
if (allowUnsafe) {
stack.addUnsafeEnchantment(enchantment, level);
} else {
stack.addEnchantment(enchantment, level);
}
}
} catch (Throwable ex) {
throw new SignException(ex.getMessage(), ex);
}
getTrade(sign, 3, ess);
return true;
}
Aggregations