org.bukkit.inventory.meta.EnchantmentStorageMeta Java Examples

The following examples show how to use org.bukkit.inventory.meta.EnchantmentStorageMeta. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UMaterial.java    From TradePlus with GNU General Public License v3.0 6 votes vote down vote up
public ItemStack getItemStack() {
  final String v = getVersionName();
  final Material m = v != null ? Material.valueOf(v) : null;
  ItemStack is = m != null ? EIGHT || NINE || TEN || ELEVEN || TWELVE ? new ItemStack(m, 1, data) : new ItemStack(m) : null;
  if(is != null && attributes != null) {
    for(String s : attributes.split(";")) {
      if(s.startsWith("color=")) {
        final String[] a = s.split(":");
        final LeatherArmorMeta me = (LeatherArmorMeta) is.getItemMeta();
        me.setColor(Color.fromRGB(Integer.parseInt(a[0]), Integer.parseInt(a[1]), Integer.parseInt(a[2])));
        is.setItemMeta(me);
      } else if(s.startsWith("enchant=")) {
        final String[] e = s.split("=")[1].split(":");
        final EnchantmentStorageMeta sm = (EnchantmentStorageMeta) is.getItemMeta();
        sm.addStoredEnchant(Sounds.version < 114 ? Enchantment.getByName(e[0]) : ItemUtils1_14.getEnchantment(e[0]), Integer.parseInt(e[1]), true);
        is.setItemMeta(sm);
      } else if(s.startsWith("upotion=")) {
        final String[] p = s.split("=")[1].split(":");
        is = new UPotion(PotionBase.valueOf(p[0]), p[1], Boolean.parseBoolean(p[2]), Boolean.parseBoolean(p[3])).getItemStack();
      }
    }
  }
  return is;
}
 
Example #2
Source File: AutoDisenchanter.java    From Slimefun4 with GNU General Public License v3.0 6 votes vote down vote up
private void transferEnchantments(ItemStack item, ItemStack book, Map<Enchantment, Integer> enchantments) {
    ItemMeta itemMeta = item.getItemMeta();
    ItemMeta bookMeta = book.getItemMeta();
    ((Repairable) bookMeta).setRepairCost(((Repairable) itemMeta).getRepairCost());
    ((Repairable) itemMeta).setRepairCost(0);
    item.setItemMeta(itemMeta);
    book.setItemMeta(bookMeta);

    EnchantmentStorageMeta meta = (EnchantmentStorageMeta) book.getItemMeta();

    for (Map.Entry<Enchantment, Integer> entry : enchantments.entrySet()) {
        item.removeEnchantment(entry.getKey());
        meta.addStoredEnchant(entry.getKey(), entry.getValue(), true);
    }

    book.setItemMeta(meta);
}
 
Example #3
Source File: Util.java    From QuickShop-Reremake with GNU General Public License v3.0 5 votes vote down vote up
@NotNull
public static String getItemStackName(@NotNull ItemStack itemStack) {
    if (useEnchantmentForEnchantedBook() && itemStack.getType() == Material.ENCHANTED_BOOK) {
        ItemMeta meta = itemStack.getItemMeta();
        if (meta instanceof EnchantmentStorageMeta && ((EnchantmentStorageMeta) meta).hasStoredEnchants()) {
            return getFirstEnchantmentName((EnchantmentStorageMeta) meta);
        }
    }
    if (itemStack.hasItemMeta()
            && Objects.requireNonNull(itemStack.getItemMeta()).hasDisplayName()) {
        return itemStack.getItemMeta().getDisplayName();
    }
    return MsgUtil.getItemi18n(itemStack.getType().name());
}
 
Example #4
Source File: Util.java    From QuickShop-Reremake with GNU General Public License v3.0 5 votes vote down vote up
@NotNull
public static String getFirstEnchantmentName(@NotNull EnchantmentStorageMeta meta) {
    if (meta == null || !meta.hasStoredEnchants()) {
        throw new IllegalArgumentException("Item does not have an enchantment!");
    }
    Map.Entry<Enchantment, Integer> entry = meta.getStoredEnchants().entrySet().iterator().next();
    String name = MsgUtil.getEnchi18n(entry.getKey());
    if (entry.getValue() == 1 && entry.getKey().getMaxLevel() == 1) {
        return name;
    } else {
        return name + " " + RomanNumber.toRoman(entry.getValue());
    }
}
 
Example #5
Source File: UMaterial.java    From TradePlus with GNU General Public License v3.0 5 votes vote down vote up
public static ItemStack getEnchantmentBook(LinkedHashMap<Enchantment, Integer> enchants, int amount) {
  final ItemStack s = new ItemStack(Material.ENCHANTED_BOOK, amount);
  final EnchantmentStorageMeta sm = (EnchantmentStorageMeta) s;
  for(Enchantment enchant : enchants.keySet()) {
    sm.addStoredEnchant(enchant, enchants.get(enchant), true);
  }
  s.setItemMeta(sm);
  return s;
}
 
Example #6
Source File: UMaterial.java    From TradePlus with GNU General Public License v3.0 5 votes vote down vote up
public static UMaterial matchEnchantedBook(ItemStack book) {
  if(book != null && book.getItemMeta() instanceof EnchantmentStorageMeta) {
    final EnchantmentStorageMeta m = (EnchantmentStorageMeta) book.getItemMeta();
    final Map<Enchantment, Integer> s = m.getStoredEnchants();
    if(s.size() == 1) {
      final Enchantment e = (Enchantment) s.keySet().toArray()[0];
      final int l = s.get(e);
      final String n = e.getName(), name =
                                        n.equals("PROTECTION_ENVIRONMENTAL") ? "PROTECTION"
                                            : n.equals("PROTECTION_FIRE") ? "FIRE_PROTECTION"
                                                  : n.equals("PROTECTION_FALL") ? "FEATHER_FALLING"
                                                        : n.equals("PROTECTION_EXPLOSIONS") ? "BLAST_PROTECTION"
                                                              : n.equals("PROTECTION_PROJECTILE") ? "PROJECTILE_PROTECTION"
                                                                    : n.equals("OXYGEN") ? "AQUA_AFFINITY"
                                                                          : n.equals("WATER_WORKER") ? "RESPIRATION"
                                                                                : n.equals("DAMAGE_ALL") ? "SHARPNESS"
                                                                                      : n.equals("DAMAGE_UNDEAD") ? "SMITE"
                                                                                            : n.equals("DAMAGE_ARTHROPODS") ? "BANE_OF_ARTHROPODS"
                                                                                                  : n.equals("LOOT_BONUS_MOBS") ? "LOOTING"
                                                                                                        : n.equals("SWEEPING_EDGE") ? "SWEEPING"
                                                                                                              : n.equals("DIG_SPEED") ? "EFFICIENCY"
                                                                                                                    : n.equals("DURABILITY") ? "UNBREAKING"
                                                                                                                          : n.equals("LOOT_BONUS_BLOCKS") ? "FORTUNE"
                                                                                                                                : n.equals("ARROW_DAMAGE") ? "POWER"
                                                                                                                                      : n.equals("ARROW_KNOCKBACK") ? "PUNCH"
                                                                                                                                            : n.equals("ARROW_FIRE") ? "FLAME"
                                                                                                                                                  : n.equals("ARROW_INFINITE") ? "INFINITY"
                                                                                                                                                        : n.equals("LUCK") ? "LUCK_OF_THE_SEA"
                                                                                                                                                              : n;
      return match("ENCHANTED_BOOK_" + (e.getMaxLevel() != 1 ? name + "_" + l : name));
    }
  }
  return null;
}
 
Example #7
Source File: ItemUtils.java    From ShopChest with MIT License 5 votes vote down vote up
public static Map<Enchantment, Integer> getEnchantments(ItemStack itemStack) {
    if (itemStack.getItemMeta() instanceof EnchantmentStorageMeta) {
        EnchantmentStorageMeta esm = (EnchantmentStorageMeta) itemStack.getItemMeta();
        return esm.getStoredEnchants();
    } else {
        return itemStack.getEnchantments();
    }
}
 
Example #8
Source File: SchematicUtil.java    From PlotMe-Core with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
private void setTag(ItemStack is, ItemTag itemtag) {
    List<Ench> enchants = itemtag.getEnchants();
    //Integer repaircost = itemtag.getRepairCost();
    List<String> pages = itemtag.getPages();
    String author = itemtag.getAuthor();
    String title = itemtag.getTitle();
    Display display = itemtag.getDisplay();

    ItemMeta itemmeta = is.getItemMeta();

    if (display != null) {
        List<String> lores = display.getLore();
        String name = display.getName();

        itemmeta.setLore(lores);
        itemmeta.setDisplayName(name);
    }

    if (itemmeta instanceof BookMeta) {
        BookMeta bookmeta = (BookMeta) itemmeta;
        bookmeta.setAuthor(author);
        bookmeta.setTitle(title);
        bookmeta.setPages(pages);
    }

    if (itemmeta instanceof EnchantmentStorageMeta) {
        EnchantmentStorageMeta enchantmentstoragemeta = (EnchantmentStorageMeta) itemmeta;

        for (Ench enchant : enchants) {
            enchantmentstoragemeta.addEnchant(Enchantment.getById(enchant.getId()), enchant.getLvl(), true);
        }
    }

    is.setItemMeta(itemmeta);
}
 
Example #9
Source File: PlayerListener.java    From MineTinker with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Updates the HashMap BLOCKFACE with the clicked face of the Block
 *
 * @param event PlayerInteractEvent
 */
@EventHandler(priority = EventPriority.LOW)
public void onInteract(PlayerInteractEvent event) {
	if (Lists.WORLDS.contains(event.getPlayer().getWorld().getName())) {
		return;
	}

	if (!event.getBlockFace().equals(BlockFace.SELF)) {
		Lists.BLOCKFACE.replace(event.getPlayer(), event.getBlockFace());
	}

	if (!modManager.allowBookToModifier()) {
		return;
	}

	if (event.getClickedBlock() == null || event.getClickedBlock().getType() != Material.BOOKSHELF) {
		return;
	}

	if (event.getItem() == null || event.getItem().getType() != Material.ENCHANTED_BOOK) {
		return;
	}

	if (event.getItem().getItemMeta() == null || !(event.getItem().getItemMeta() instanceof EnchantmentStorageMeta)) {
		return;
	}

	EnchantmentStorageMeta meta = (EnchantmentStorageMeta) event.getItem().getItemMeta();

	for (Map.Entry<Enchantment, Integer> entry : meta.getStoredEnchants().entrySet()) {
		Modifier modifier = modManager.getModifierFromEnchantment(entry.getKey());

		if (modifier == null) {
			continue;
		}

		ItemStack modDrop = modifier.getModItem();
		modDrop.setAmount(entry.getValue());

		event.getClickedBlock().getWorld().dropItem(event.getClickedBlock().getLocation(), modDrop);

		meta.removeStoredEnchant(entry.getKey());
	}

	if (meta.getStoredEnchants().isEmpty()) {
		event.getPlayer().getInventory().removeItem(event.getItem());
	} else {
		event.getItem().setItemMeta(meta);
	}
}
 
Example #10
Source File: EnchantCommands.java    From NyaaUtils with MIT License 4 votes vote down vote up
@SubCommand(value = "info", permission = "nu.enchantinfo")
public void commandEnchantInfo(CommandSender sender, Arguments args) {
    Player p = asPlayer(sender);
    ItemStack item = getItemInOffHand(sender);

    if (!BasicItemMatcher.containsMatch(NyaaUtils.instance.cfg.enchantSrcConfig.enchantSrc, item)) {
        sender.sendMessage(I18n.format("user.enchant.invalid_src"));
        return;
    }

    Map<Enchantment, Integer> enchant;
    if (item.getType().equals(Material.ENCHANTED_BOOK)) {
        EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta();
        enchant = meta.getStoredEnchants();
    } else {
        enchant = item.getEnchantments();
    }

    sender.sendMessage(I18n.format("user.enchant.list_ench_header"));
    printEnchant(p, enchant.keySet().toArray(new Enchantment[0]));
    long cooldown = 0;
    if (enchantCooldown.containsKey(p.getUniqueId())) {
        cooldown = enchantCooldown.get(p.getUniqueId()) + (plugin.cfg.enchantCooldown / 20 * 1000);
    }
    float percent;
    msg(sender, "user.enchantinfo.info_0");
    if (cooldown > System.currentTimeMillis()) {
        percent = (plugin.cfg.chanceModerate +
                plugin.cfg.chanceFail + plugin.cfg.chanceDestroy) / 100.0F;
        msg(sender, "user.enchantinfo.info_1", 0);
    } else {
        percent = (plugin.cfg.chanceSuccess + plugin.cfg.chanceModerate +
                plugin.cfg.chanceFail + plugin.cfg.chanceDestroy) / 100.0F;
        msg(sender, "user.enchantinfo.info_1", (int) (plugin.cfg.chanceSuccess / percent));
    }
    msg(sender, "user.enchantinfo.info_2", (int) (plugin.cfg.chanceModerate / percent));
    msg(sender, "user.enchantinfo.info_3", (int) (plugin.cfg.chanceFail / percent));
    msg(sender, "user.enchantinfo.info_4", (int) (plugin.cfg.chanceDestroy / percent));
    if (cooldown > System.currentTimeMillis()) {
        msg(sender, "user.enchantinfo.info_cooldown", (int) ((cooldown - System.currentTimeMillis()) / 1000));
    }
}
 
Example #11
Source File: AutoEnchanter.java    From Slimefun4 with GNU General Public License v3.0 4 votes vote down vote up
private MachineRecipe findRecipe(BlockMenu menu) {
    for (int slot : getInputSlots()) {
        ItemStack target = menu.getItemInSlot(slot == getInputSlots()[0] ? getInputSlots()[1] : getInputSlots()[0]);

        // Check if the item is enchantable
        if (!isEnchantable(target)) {
            return null;
        }

        ItemStack item = menu.getItemInSlot(slot);

        // Enchant
        if (item != null && item.getType() == Material.ENCHANTED_BOOK && target != null) {
            Map<Enchantment, Integer> enchantments = new HashMap<>();
            Set<ItemEnchantment> emeraldEnchantments = new HashSet<>();
            int amount = 0;
            int specialAmount = 0;
            EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta();

            for (Map.Entry<Enchantment, Integer> e : meta.getStoredEnchants().entrySet()) {
                if (e.getKey().canEnchantItem(target)) {
                    amount++;
                    enchantments.put(e.getKey(), e.getValue());
                }
            }

            if (SlimefunPlugin.getThirdPartySupportService().isEmeraldEnchantsInstalled()) {
                for (ItemEnchantment enchantment : EmeraldEnchants.getInstance().getRegistry().getEnchantments(item)) {
                    if (EmeraldEnchants.getInstance().getRegistry().isApplicable(target, enchantment.getEnchantment()) && EmeraldEnchants.getInstance().getRegistry().getEnchantmentLevel(target, enchantment.getEnchantment().getName()) < enchantment.getLevel()) {
                        amount++;
                        specialAmount++;
                        emeraldEnchantments.add(enchantment);
                    }
                }
                specialAmount += EmeraldEnchants.getInstance().getRegistry().getEnchantments(target).size();
            }

            if (amount > 0 && specialAmount <= emeraldEnchantsLimit) {
                ItemStack enchantedItem = target.clone();
                enchantedItem.setAmount(1);

                for (Map.Entry<Enchantment, Integer> entry : enchantments.entrySet()) {
                    enchantedItem.addUnsafeEnchantment(entry.getKey(), entry.getValue());
                }

                for (ItemEnchantment ench : emeraldEnchantments) {
                    EmeraldEnchants.getInstance().getRegistry().applyEnchantment(enchantedItem, ench.getEnchantment(), ench.getLevel());
                }

                return new MachineRecipe(75 * amount, new ItemStack[] { target, item }, new ItemStack[] { enchantedItem, new ItemStack(Material.BOOK) });
            }

            return null;
        }
    }

    return null;
}