use of org.bukkit.enchantments.Enchantment in project Essentials by drtshock.
the class SignEnchant method onSignInteract.
@Override
protected boolean onSignInteract(ISign sign, User player, String username, IEssentials ess) throws SignException, ChargeException {
final ItemStack search = sign.getLine(1).equals("*") || sign.getLine(1).equalsIgnoreCase("any") ? null : getItemStack(sign.getLine(1), 1, ess);
final Trade charge = getTrade(sign, 3, ess);
charge.isAffordableFor(player);
final String[] enchantLevel = sign.getLine(2).split(":");
if (enchantLevel.length != 2) {
throw new SignException(tl("invalidSignLine", 3));
}
final Enchantment enchantment = Enchantments.getByName(enchantLevel[0]);
if (enchantment == null) {
throw new SignException(tl("enchantmentNotFound"));
}
int level;
try {
level = Integer.parseInt(enchantLevel[1]);
} catch (NumberFormatException ex) {
level = enchantment.getMaxLevel();
}
final ItemStack playerHand = player.getBase().getItemInHand();
if (playerHand == null || playerHand.getAmount() != 1 || (playerHand.containsEnchantment(enchantment) && playerHand.getEnchantmentLevel(enchantment) == level)) {
throw new SignException(tl("missingItems", 1, sign.getLine(1)));
}
if (search != null && playerHand.getType() != search.getType()) {
throw new SignException(tl("missingItems", 1, search.getType().toString().toLowerCase(Locale.ENGLISH).replace('_', ' ')));
}
final ItemStack toEnchant = playerHand;
try {
if (level == 0) {
toEnchant.removeEnchantment(enchantment);
} else {
if (ess.getSettings().allowUnsafeEnchantments() && player.isAuthorized("essentials.signs.enchant.allowunsafe")) {
toEnchant.addUnsafeEnchantment(enchantment, level);
} else {
toEnchant.addEnchantment(enchantment, level);
}
}
} catch (Exception ex) {
throw new SignException(ex.getMessage(), ex);
}
final String enchantmentName = enchantment.getName().toLowerCase(Locale.ENGLISH);
if (level == 0) {
player.sendMessage(tl("enchantmentRemoved", enchantmentName.replace('_', ' ')));
} else {
player.sendMessage(tl("enchantmentApplied", enchantmentName.replace('_', ' ')));
}
charge.charge(player);
Trade.log("Sign", "Enchant", "Interact", username, charge, username, charge, sign.getBlock().getLocation(), ess);
player.getBase().updateInventory();
return true;
}
use of org.bukkit.enchantments.Enchantment in project Essentials by drtshock.
the class ItemDb method serialize.
@Override
public String serialize(ItemStack is) {
String mat = is.getType().name();
if (is.getData().getData() != 0) {
mat = mat + ":" + is.getData().getData();
}
int quantity = is.getAmount();
// Add space AFTER you add something. We can trim at end.
StringBuilder sb = new StringBuilder();
sb.append(mat).append(" ").append(quantity).append(" ");
// ItemMeta applies to anything.
if (is.hasItemMeta()) {
ItemMeta meta = is.getItemMeta();
if (meta.hasDisplayName()) {
sb.append("name:").append(meta.getDisplayName().replaceAll(" ", "_")).append(" ");
}
if (meta.hasLore()) {
sb.append("lore:");
boolean first = true;
for (String s : meta.getLore()) {
// to do this since we need each line separated by |
if (!first) {
sb.append("|");
}
first = false;
sb.append(s.replaceAll(" ", "_"));
}
sb.append(" ");
}
if (meta.hasEnchants()) {
for (Enchantment e : meta.getEnchants().keySet()) {
sb.append(e.getName().toLowerCase()).append(":").append(meta.getEnchantLevel(e)).append(" ");
}
}
}
switch(is.getType()) {
case WRITTEN_BOOK:
// Everything from http://wiki.ess3.net/wiki/Item_Meta#Books in that order.
// Interesting as I didn't see a way to do pages or chapters.
BookMeta bookMeta = (BookMeta) is.getItemMeta();
if (bookMeta.hasTitle()) {
sb.append("title:").append(bookMeta.getTitle()).append(" ");
}
if (bookMeta.hasAuthor()) {
sb.append("author:").append(bookMeta.getAuthor()).append(" ");
}
// Only other thing it could have is lore but that's done up there ^^^
break;
case ENCHANTED_BOOK:
EnchantmentStorageMeta enchantmentStorageMeta = (EnchantmentStorageMeta) is.getItemMeta();
for (Enchantment e : enchantmentStorageMeta.getStoredEnchants().keySet()) {
sb.append(e.getName().toLowerCase()).append(":").append(enchantmentStorageMeta.getStoredEnchantLevel(e)).append(" ");
}
break;
case FIREWORK:
// Everything from http://wiki.ess3.net/wiki/Item_Meta#Fireworks in that order.
FireworkMeta fireworkMeta = (FireworkMeta) is.getItemMeta();
if (fireworkMeta.hasEffects()) {
for (FireworkEffect effect : fireworkMeta.getEffects()) {
if (effect.getColors() != null && !effect.getColors().isEmpty()) {
sb.append("color:");
boolean first = true;
for (Color c : effect.getColors()) {
if (!first) {
// same thing as above.
sb.append(",");
}
sb.append(c.toString());
first = false;
}
sb.append(" ");
}
sb.append("shape: ").append(effect.getType().name()).append(" ");
if (effect.getFadeColors() != null && !effect.getFadeColors().isEmpty()) {
sb.append("fade:");
boolean first = true;
for (Color c : effect.getFadeColors()) {
if (!first) {
// same thing as above.
sb.append(",");
}
sb.append(c.toString());
first = false;
}
sb.append(" ");
}
}
sb.append("power: ").append(fireworkMeta.getPower()).append(" ");
}
break;
case POTION:
Potion potion = Potion.fromItemStack(is);
for (PotionEffect e : potion.getEffects()) {
// long but needs to be effect:speed power:2 duration:120 in that order.
sb.append("splash:").append(potion.isSplash()).append(" ").append("effect:").append(e.getType().getName().toLowerCase()).append(" ").append("power:").append(e.getAmplifier()).append(" ").append("duration:").append(e.getDuration() / 20).append(" ");
}
break;
case SKULL_ITEM:
// item stack with meta
SkullMeta skullMeta = (SkullMeta) is.getItemMeta();
if (skullMeta != null && skullMeta.hasOwner()) {
sb.append("player:").append(skullMeta.getOwner()).append(" ");
}
break;
case LEATHER_HELMET:
case LEATHER_CHESTPLATE:
case LEATHER_LEGGINGS:
case LEATHER_BOOTS:
LeatherArmorMeta leatherArmorMeta = (LeatherArmorMeta) is.getItemMeta();
int rgb = leatherArmorMeta.getColor().asRGB();
sb.append("color:").append(rgb).append(" ");
break;
case BANNER:
BannerMeta bannerMeta = (BannerMeta) is.getItemMeta();
if (bannerMeta != null) {
int basecolor = bannerMeta.getBaseColor().getColor().asRGB();
sb.append("basecolor:").append(basecolor).append(" ");
for (org.bukkit.block.banner.Pattern p : bannerMeta.getPatterns()) {
String type = p.getPattern().getIdentifier();
int color = p.getColor().getColor().asRGB();
sb.append(type).append(",").append(color).append(" ");
}
}
break;
case SHIELD:
// Hacky fix for accessing Shield meta - https://github.com/drtshock/Essentials/pull/745#issuecomment-234843795
BlockStateMeta shieldMeta = (BlockStateMeta) is.getItemMeta();
Banner shieldBannerMeta = (Banner) shieldMeta.getBlockState();
int basecolor = shieldBannerMeta.getBaseColor().getColor().asRGB();
sb.append("basecolor:").append(basecolor).append(" ");
for (org.bukkit.block.banner.Pattern p : shieldBannerMeta.getPatterns()) {
String type = p.getPattern().getIdentifier();
int color = p.getColor().getColor().asRGB();
sb.append(type).append(",").append(color).append(" ");
}
break;
}
return sb.toString().trim().replaceAll("ยง", "&");
}
use of org.bukkit.enchantments.Enchantment in project Essentials by drtshock.
the class MetaItemStack method getEnchantment.
public Enchantment getEnchantment(final User user, final String name) throws Exception {
final Enchantment enchantment = Enchantments.getByName(name);
if (enchantment == null) {
return null;
}
final String enchantmentName = enchantment.getName().toLowerCase(Locale.ENGLISH);
if (!hasMetaPermission(user, "enchantments." + enchantmentName, true, false)) {
throw new Exception(tl("enchantmentPerm", enchantmentName));
}
return enchantment;
}
use of org.bukkit.enchantments.Enchantment in project Essentials by drtshock.
the class Commandenchant method run.
//TODO: Implement charge costs: final Trade charge = new Trade("enchant-" + enchantmentName, ess);
@Override
protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
final ItemStack stack = user.getItemInHand();
if (stack == null || stack.getType() == Material.AIR) {
throw new Exception(tl("nothingInHand"));
}
if (args.length == 0) {
final Set<String> enchantmentslist = new TreeSet<>();
for (Map.Entry<String, Enchantment> entry : Enchantments.entrySet()) {
final String enchantmentName = entry.getValue().getName().toLowerCase(Locale.ENGLISH);
if (enchantmentslist.contains(enchantmentName) || (user.isAuthorized("essentials.enchantments." + enchantmentName) && entry.getValue().canEnchantItem(stack))) {
enchantmentslist.add(entry.getKey());
//enchantmentslist.add(enchantmentName);
}
}
throw new NotEnoughArgumentsException(tl("enchantments", StringUtil.joinList(enchantmentslist.toArray())));
}
int level = -1;
if (args.length > 1) {
try {
level = Integer.parseInt(args[1]);
} catch (NumberFormatException ex) {
level = -1;
}
}
final boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments() && user.isAuthorized("essentials.enchantments.allowunsafe");
final MetaItemStack metaStack = new MetaItemStack(stack);
final Enchantment enchantment = metaStack.getEnchantment(user, args[0]);
metaStack.addEnchantment(user.getSource(), allowUnsafe, enchantment, level);
InventoryWorkaround.setItemInMainHand(user.getBase(), metaStack.getItemStack());
user.getBase().updateInventory();
final String enchantmentName = enchantment.getName().toLowerCase(Locale.ENGLISH);
if (level == 0) {
user.sendMessage(tl("enchantmentRemoved", enchantmentName.replace('_', ' ')));
} else {
user.sendMessage(tl("enchantmentApplied", enchantmentName.replace('_', ' ')));
}
}
use of org.bukkit.enchantments.Enchantment in project InfernalMobs by NyaaCat.
the class LootConfig method dump.
public void dump(File f) {
YamlConfiguration cfg = new YamlConfiguration();
ConfigurationSection items = cfg.createSection("lootItems");
ConfigurationSection dropMap = cfg.createSection("dropMap");
for (String name : lootItems.keySet()) {
LootItem item = lootItems.get(name);
ConfigurationSection s = items.createSection(name);
s.set("item", item.item);
if (item.amountRange != null)
s.set("amountRange", item.amountRange.toString());
if (item.damageRange != null)
s.set("damageRange", item.damageRange.toString());
if (item.commands != null)
s.set("commands", item.commands);
if (item.extraEnchants != null) {
ConfigurationSection sec = s.createSection("extraEnchants");
for (Enchantment e : item.extraEnchants.keySet()) {
sec.set(e.getName(), item.extraEnchants.get(e).toString());
}
}
}
for (Integer level : this.dropMap.keySet()) {
dropMap.createSection("level-" + level.toString(), this.dropMap.get(level));
}
try {
cfg.save(f);
} catch (Exception ex) {
ex.printStackTrace();
System.out.print(cfg.saveToString());
}
}
Aggregations