use of org.bukkit.inventory.meta.BookMeta in project Denizen-For-Bukkit by DenizenScript.
the class dInventory method getAttribute.
////////////////////////
// Attributes
/////////////////////
public String getAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
// -->
if (attribute.startsWith("empty_slots")) {
int full = new dInventory(inventory).count(null, true);
return new Element(inventory.getSize() - full).getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("can_fit") && attribute.hasContext(1) && dItem.matches(attribute.getContext(1))) {
dItem item = dItem.valueOf(attribute.getContext(1));
if (item == null) {
return null;
}
int attribs = 1;
int qty = 1;
InventoryType type = inventory.getType();
dInventory dummyInv = new dInventory(Bukkit.createInventory(null, type == InventoryType.PLAYER ? InventoryType.CHEST : type, inventory.getTitle()));
ItemStack[] contents = getStorageContents();
if (dummyInv.getInventoryType() == InventoryType.CHEST) {
dummyInv.setSize(contents.length);
}
if (contents.length != dummyInv.getSize()) {
contents = Arrays.copyOf(contents, dummyInv.getSize());
}
dummyInv.setContents(contents);
// -->
if ((attribute.getAttribute(2).startsWith("quantity") || attribute.getAttribute(2).startsWith("qty")) && attribute.hasContext(2) && aH.matchesInteger(attribute.getContext(2))) {
qty = attribute.getIntContext(2);
attribs = 2;
}
item.setAmount(qty);
List<ItemStack> leftovers = dummyInv.addWithLeftovers(0, true, item.getItemStack());
return new Element(leftovers.isEmpty()).getAttribute(attribute.fulfill(attribs));
}
// -->
if (attribute.startsWith("include") && attribute.hasContext(1) && dItem.matches(attribute.getContext(1))) {
dItem item = dItem.valueOf(attribute.getContext(1));
if (item == null) {
return null;
}
int attribs = 1;
int qty = 1;
dInventory dummyInv = new dInventory(Bukkit.createInventory(null, inventory.getType(), inventory.getTitle()));
if (inventory.getType() == InventoryType.CHEST) {
dummyInv.setSize(inventory.getSize());
}
dummyInv.setContents(getContents());
// -->
if ((attribute.getAttribute(2).startsWith("quantity") || attribute.getAttribute(2).startsWith("qty")) && attribute.hasContext(2) && aH.matchesInteger(attribute.getContext(2))) {
qty = attribute.getIntContext(2);
attribs = 2;
}
item.setAmount(qty);
dummyInv.add(0, item.getItemStack());
return dummyInv.getAttribute(attribute.fulfill(attribs));
}
// -->
if (attribute.startsWith("is_empty")) {
boolean empty = true;
for (ItemStack item : getStorageContents()) {
if (item != null && item.getType() != Material.AIR) {
empty = false;
break;
}
}
return new Element(empty).getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("is_full")) {
boolean full = true;
for (ItemStack item : getStorageContents()) {
if ((item == null) || (item.getType() == Material.AIR) || (item.getAmount() < item.getMaxStackSize())) {
full = false;
break;
}
}
return new Element(full).getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("contains.display") && attribute.hasContext(2)) {
String search_string = attribute.getContext(2);
boolean strict = false;
if (CoreUtilities.toLowerCase(search_string).startsWith("strict:") && search_string.length() > 7) {
strict = true;
search_string = search_string.substring(7);
}
if (search_string.length() == 0) {
return null;
}
int qty = 1;
int attribs = 2;
// -->
if ((attribute.getAttribute(3).startsWith("quantity") || attribute.getAttribute(3).startsWith("qty")) && attribute.hasContext(3) && aH.matchesInteger(attribute.getContext(3))) {
qty = attribute.getIntContext(3);
attribs = 3;
}
int found_items = 0;
if (strict) {
for (ItemStack item : getContents()) {
if (item != null && item.getType() == Material.WRITTEN_BOOK && ((BookMeta) item.getItemMeta()).getTitle().equalsIgnoreCase(search_string)) {
found_items += item.getAmount();
if (found_items >= qty) {
break;
}
} else if (item != null && item.hasItemMeta() && item.getItemMeta().hasDisplayName() && item.getItemMeta().getDisplayName().equalsIgnoreCase(search_string)) {
found_items += item.getAmount();
if (found_items >= qty) {
break;
}
}
}
} else {
for (ItemStack item : getContents()) {
if (item != null && item.getType() == Material.WRITTEN_BOOK && CoreUtilities.toLowerCase(((BookMeta) item.getItemMeta()).getTitle()).contains(CoreUtilities.toLowerCase(search_string))) {
found_items += item.getAmount();
if (found_items >= qty) {
break;
}
} else if (item != null && item.hasItemMeta() && item.getItemMeta().hasDisplayName() && CoreUtilities.toLowerCase(item.getItemMeta().getDisplayName()).contains(CoreUtilities.toLowerCase(search_string))) {
found_items += item.getAmount();
if (found_items >= qty) {
break;
}
}
}
}
return new Element(found_items >= qty).getAttribute(attribute.fulfill(attribs));
}
// -->
if (attribute.startsWith("contains.lore") && attribute.hasContext(2)) {
String search_string = attribute.getContext(2);
boolean strict = false;
if (CoreUtilities.toLowerCase(search_string).startsWith("strict:")) {
strict = true;
search_string = search_string.substring(7);
}
if (search_string.length() == 0) {
return null;
}
dList lore = dList.valueOf(search_string);
int qty = 1;
int attribs = 2;
// -->
if ((attribute.getAttribute(3).startsWith("quantity") || attribute.getAttribute(3).startsWith("qty")) && attribute.hasContext(3) && aH.matchesInteger(attribute.getContext(3))) {
qty = attribute.getIntContext(3);
attribs = 3;
}
int found_items = 0;
if (strict) {
strict_items: for (ItemStack item : getContents()) {
if (item != null && item.hasItemMeta() && item.getItemMeta().hasLore()) {
List<String> item_lore = item.getItemMeta().getLore();
if (lore.size() != item_lore.size()) {
continue;
}
for (int i = 0; i < item_lore.size(); i++) {
if (lore.get(i).equalsIgnoreCase(item_lore.get(i))) {
if (i == lore.size()) {
found_items += item.getAmount();
if (found_items >= qty) {
break strict_items;
}
}
} else {
continue strict_items;
}
}
}
}
} else {
for (ItemStack item : getContents()) {
if (item != null && item.hasItemMeta() && item.getItemMeta().hasLore()) {
List<String> item_lore = item.getItemMeta().getLore();
int loreCount = 0;
lines: for (String line : lore) {
for (String item_line : item_lore) {
if (CoreUtilities.toLowerCase(item_line).contains(CoreUtilities.toLowerCase(line))) {
loreCount++;
continue lines;
}
}
}
if (loreCount == lore.size()) {
found_items += item.getAmount();
if (found_items >= qty) {
break;
}
}
}
}
}
return new Element(found_items >= qty).getAttribute(attribute.fulfill(attribs));
}
// -->
if (attribute.startsWith("contains.material") && attribute.hasContext(2) && dMaterial.matches(attribute.getContext(2))) {
dMaterial material = dMaterial.valueOf(attribute.getContext(2));
int qty = 1;
int attribs = 2;
// -->
if ((attribute.getAttribute(3).startsWith("quantity") || attribute.getAttribute(3).startsWith("qty")) && attribute.hasContext(3) && aH.matchesInteger(attribute.getContext(3))) {
qty = attribute.getIntContext(3);
attribs = 3;
}
int found_items = 0;
for (ItemStack item : getContents()) {
if (item != null && item.getType() == material.getMaterial()) {
found_items += item.getAmount();
if (found_items >= qty) {
break;
}
}
}
return new Element(found_items >= qty).getAttribute(attribute.fulfill(attribs));
}
// -->
if (attribute.startsWith("contains_any") && attribute.hasContext(1)) {
dList list = dList.valueOf(attribute.getContext(1));
if (list.isEmpty()) {
return null;
}
int qty = 1;
int attribs = 1;
// -->
if ((attribute.getAttribute(2).startsWith("quantity") || attribute.getAttribute(2).startsWith("qty")) && attribute.hasContext(2) && aH.matchesInteger(attribute.getContext(2))) {
qty = attribute.getIntContext(2);
attribs = 2;
}
List<dItem> contains = list.filter(dItem.class, attribute.getScriptEntry());
if (!contains.isEmpty()) {
for (dItem item : contains) {
if (containsItem(item, qty)) {
return Element.TRUE.getAttribute(attribute.fulfill(attribs));
}
}
}
return Element.FALSE.getAttribute(attribute.fulfill(attribs));
}
// -->
if (attribute.startsWith("contains") && attribute.hasContext(1)) {
dList list = dList.valueOf(attribute.getContext(1));
if (list.isEmpty()) {
return null;
}
int qty = 1;
int attribs = 1;
// -->
if ((attribute.getAttribute(2).startsWith("quantity") || attribute.getAttribute(2).startsWith("qty")) && attribute.hasContext(2) && aH.matchesInteger(attribute.getContext(2))) {
qty = attribute.getIntContext(2);
attribs = 2;
}
// TODO: Fix logic
List<dItem> contains = list.filter(dItem.class, attribute.getScriptEntry());
if (!contains.isEmpty()) {
for (dItem item : contains) {
if (containsItem(item, qty)) {
return Element.TRUE.getAttribute(attribute.fulfill(attribs));
}
}
}
return Element.FALSE.getAttribute(attribute.fulfill(attribs));
}
// -->
if (attribute.startsWith("first_empty")) {
return new Element(firstEmpty(0)).getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("find.material") && attribute.hasContext(2) && dMaterial.matches(attribute.getContext(2))) {
dMaterial material = dMaterial.valueOf(attribute.getContext(2));
if (material == null) {
return null;
}
int slot = -1;
for (int i = 0; i < inventory.getSize(); i++) {
if (inventory.getItem(i) != null && inventory.getItem(i).getType() == material.getMaterial()) {
slot = i + 1;
break;
}
}
return new Element(slot).getAttribute(attribute.fulfill(2));
}
// -->
if (attribute.startsWith("find_imperfect") && attribute.hasContext(1) && dItem.matches(attribute.getContext(1))) {
dItem item = dItem.valueOf(attribute.getContext(1), attribute.getScriptEntry() != null ? ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getPlayer() : null, attribute.getScriptEntry() != null ? ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getNPC() : null);
item.setAmount(1);
int slot = -1;
for (int i = 0; i < inventory.getSize(); i++) {
if (inventory.getItem(i) != null) {
dItem compare_to = new dItem(inventory.getItem(i).clone());
compare_to.setAmount(1);
if (item.identify().equalsIgnoreCase(compare_to.identify())) {
slot = i + 1;
break;
}
}
}
return new Element(slot).getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("find") && attribute.hasContext(1) && dItem.matches(attribute.getContext(1))) {
dItem item = dItem.valueOf(attribute.getContext(1), attribute.getScriptEntry() != null ? ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getPlayer() : null, attribute.getScriptEntry() != null ? ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getNPC() : null);
item.setAmount(1);
int slot = -1;
for (int i = 0; i < inventory.getSize(); i++) {
if (inventory.getItem(i) != null) {
dItem compare_to = new dItem(inventory.getItem(i).clone());
compare_to.setAmount(1);
if (item.getFullString().equalsIgnoreCase(compare_to.getFullString())) {
slot = i + 1;
break;
}
}
}
return new Element(slot).getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("id_type")) {
return new Element(idType).getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("notable_name")) {
String notname = NotableManager.getSavedId(this);
if (notname == null) {
return null;
}
return new Element(notname).getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("location")) {
dLocation location = getLocation();
if (location == null) {
return null;
}
return location.getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("quantity") || attribute.startsWith("qty")) {
if (attribute.hasContext(1) && dItem.matches(attribute.getContext(1))) {
return new Element(count(dItem.valueOf(attribute.getContext(1), ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getPlayer(), ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getNPC()).getItemStack(), false)).getAttribute(attribute.fulfill(1));
} else {
return new Element(count(null, false)).getAttribute(attribute.fulfill(1));
}
}
// -->
if (attribute.startsWith("stacks")) {
if (attribute.hasContext(1) && dItem.matches(attribute.getContext(1))) {
return new Element(count(dItem.valueOf(attribute.getContext(1), ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getPlayer(), ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getNPC()).getItemStack(), true)).getAttribute(attribute.fulfill(1));
} else {
return new Element(count(null, true)).getAttribute(attribute.fulfill(1));
}
}
// -->
if (attribute.startsWith("slot") && attribute.hasContext(1) && aH.matchesInteger(attribute.getContext(1))) {
int slot = new Element(attribute.getContext(1)).asInt() - 1;
if (slot < 0) {
slot = 0;
} else if (slot > getInventory().getSize() - 1) {
slot = getInventory().getSize() - 1;
}
return new dItem(getInventory().getItem(slot)).getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("inventory_type")) {
return new Element(inventory instanceof HorseInventory ? "HORSE" : getInventory().getType().name()).getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("equipment")) {
dList equipment = getEquipment();
if (equipment == null) {
return null;
}
return equipment.getAttribute(attribute.fulfill(1));
}
if (inventory instanceof CraftingInventory) {
CraftingInventory craftingInventory = (CraftingInventory) inventory;
// -->
if (attribute.startsWith("matrix")) {
dList recipeList = new dList();
for (ItemStack item : craftingInventory.getMatrix()) {
if (item != null) {
recipeList.add(new dItem(item).identify());
} else {
recipeList.add(new dItem(Material.AIR).identify());
}
}
return recipeList.getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("result")) {
ItemStack result = craftingInventory.getResult();
if (result == null) {
return null;
}
return new dItem(result).getAttribute(attribute.fulfill(1));
}
}
// -->
if (attribute.startsWith("type")) {
return new Element("Inventory").getAttribute(attribute.fulfill(1));
}
// Iterate through this object's properties' attributes
for (Property property : PropertyParser.getProperties(this)) {
String returned = property.getAttribute(attribute);
if (returned != null) {
return returned;
}
}
return new Element(identify()).getAttribute(attribute);
}
use of org.bukkit.inventory.meta.BookMeta in project Denizen-For-Bukkit by DenizenScript.
the class dInventory method removeBook.
/**
* Remove a book from this inventory, comparing
* only its title and author with books in the
* inventory, but ignoring its text, thus having
* Denizen support for updatable quest journals
* and their like
*
* @param title The title of the book
* @param author The author of the book
* @param quantity The number of books to remove
* @return The resulting dInventory
*/
public dInventory removeBook(String title, String author, int quantity) {
if (inventory == null || (title == null && author == null)) {
return this;
}
for (ItemStack invStack : inventory) {
if (quantity == 0) {
break;
}
if (invStack != null && invStack.getItemMeta() instanceof BookMeta) {
BookMeta invMeta = (BookMeta) invStack.getItemMeta();
String invTitle = invMeta.getTitle();
String invAuthor = invMeta.getAuthor();
if ((invTitle == null && title != null) || (invAuthor == null && author != null)) {
continue;
} else if (invTitle == null || invAuthor == null) {
continue;
}
if (invAuthor.equalsIgnoreCase(author) && invTitle.equalsIgnoreCase(title)) {
// need to
if (quantity - invStack.getAmount() < 0) {
invStack.setAmount((quantity - invStack.getAmount()) * -1);
} else {
inventory.removeItem(invStack);
// Update the quantity we still have to remove
quantity -= invStack.getAmount();
}
}
}
}
return this;
}
use of org.bukkit.inventory.meta.BookMeta in project Denizen-For-Bukkit by DenizenScript.
the class ItemBook method getAttribute.
@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
if (attribute.startsWith("book")) {
BookMeta bookInfo = (BookMeta) item.getItemStack().getItemMeta();
attribute = attribute.fulfill(1);
if (item.getItemStack().getType() == Material.WRITTEN_BOOK) {
// -->
if (attribute.startsWith("author")) {
return new Element(bookInfo.getAuthor()).getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("title")) {
return new Element(bookInfo.getTitle()).getAttribute(attribute.fulfill(1));
}
}
// -->
if (attribute.startsWith("page_count")) {
return new Element(bookInfo.getPageCount()).getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("get_page") && aH.matchesInteger(attribute.getContext(1))) {
return new Element(bookInfo.getPage(attribute.getIntContext(1))).getAttribute(attribute.fulfill(1));
}
// Deprecated in favor of pages.escape_contents
if (attribute.startsWith("pages.escaped")) {
StringBuilder output = new StringBuilder();
for (String page : bookInfo.getPages()) {
output.append(EscapeTags.Escape(page)).append("|");
}
return new dList(output.length() > 0 ? output.substring(0, output.length() - 1) : output.toString()).getAttribute(attribute.fulfill(2));
}
// -->
if (attribute.startsWith("pages")) {
return new dList(bookInfo.getPages()).getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <i@item.book>
// @returns Element
// @mechanism dItem.book
// @group properties
// @description
// Returns full information on the book item, in the format
// author|AUTHOR|title|TITLE|pages|PAGE_ONE|PAGE_TWO|...
// or as pages|PAGE_ONE|PAGE_TWO|...
// Pre-escaped to prevent issues.
// See <@link language Property Escaping>
// -->
String output = getPropertyString();
if (output == null) {
output = "null";
}
return new Element(output).getAttribute(attribute);
}
return null;
}
use of org.bukkit.inventory.meta.BookMeta in project Essentials by drtshock.
the class Commandbook method run.
//TODO: Translate this
@Override
public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
final ItemStack item = user.getItemInHand();
final String player = user.getName();
if (item.getType() == Material.WRITTEN_BOOK) {
BookMeta bmeta = (BookMeta) item.getItemMeta();
if (args.length > 1 && args[0].equalsIgnoreCase("author")) {
if (user.isAuthorized("essentials.book.author") && (isAuthor(bmeta, player) || user.isAuthorized("essentials.book.others"))) {
bmeta.setAuthor(args[1]);
item.setItemMeta(bmeta);
user.sendMessage(tl("bookAuthorSet", getFinalArg(args, 1)));
} else {
throw new Exception(tl("denyChangeAuthor"));
}
} else if (args.length > 1 && args[0].equalsIgnoreCase("title")) {
if (user.isAuthorized("essentials.book.title") && (isAuthor(bmeta, player) || user.isAuthorized("essentials.book.others"))) {
bmeta.setTitle(args[1]);
item.setItemMeta(bmeta);
user.sendMessage(tl("bookTitleSet", getFinalArg(args, 1)));
} else {
throw new Exception(tl("denyChangeTitle"));
}
} else {
if (isAuthor(bmeta, player) || user.isAuthorized("essentials.book.others")) {
ItemStack newItem = new ItemStack(Material.BOOK_AND_QUILL, item.getAmount());
newItem.setItemMeta(bmeta);
InventoryWorkaround.setItemInMainHand(user.getBase(), newItem);
user.sendMessage(tl("editBookContents"));
} else {
throw new Exception(tl("denyBookEdit"));
}
}
} else if (item.getType() == Material.BOOK_AND_QUILL) {
BookMeta bmeta = (BookMeta) item.getItemMeta();
if (!user.isAuthorized("essentials.book.author")) {
bmeta.setAuthor(player);
}
ItemStack newItem = new ItemStack(Material.WRITTEN_BOOK, item.getAmount());
newItem.setItemMeta(bmeta);
InventoryWorkaround.setItemInMainHand(user.getBase(), newItem);
user.sendMessage(tl("bookLocked"));
} else {
throw new Exception(tl("holdBook"));
}
}
use of org.bukkit.inventory.meta.BookMeta in project Prism-Bukkit by prism.
the class ItemStackAction method setItemStackFromNewDataFormat.
/**
*
*/
protected void setItemStackFromNewDataFormat() {
if (data == null || !data.startsWith("{"))
return;
actionData = gson.fromJson(data, ItemStackActionData.class);
item = new ItemStack(this.block_id, actionData.amt, (short) this.block_subid);
// Restore enchantment
if (actionData.enchs != null && actionData.enchs.length > 0) {
for (final String ench : actionData.enchs) {
final String[] enchArgs = ench.split(":");
final Enchantment enchantment = Enchantment.getById(Integer.parseInt(enchArgs[0]));
// Restore book enchantment
if (item.getType().equals(Material.ENCHANTED_BOOK)) {
final EnchantmentStorageMeta bookEnchantments = (EnchantmentStorageMeta) item.getItemMeta();
bookEnchantments.addStoredEnchant(enchantment, Integer.parseInt(enchArgs[1]), false);
item.setItemMeta(bookEnchantments);
} else // Restore item enchantment
{
item.addUnsafeEnchantment(enchantment, Integer.parseInt(enchArgs[1]));
}
}
}
// Leather color
if (item.getType().name().contains("LEATHER_") && actionData.color > 0) {
final LeatherArmorMeta lam = (LeatherArmorMeta) item.getItemMeta();
lam.setColor(Color.fromRGB(actionData.color));
item.setItemMeta(lam);
} else // Skulls
if (item.getType().equals(Material.SKULL_ITEM) && actionData.owner != null) {
final SkullMeta meta = (SkullMeta) item.getItemMeta();
meta.setOwner(actionData.owner);
item.setItemMeta(meta);
} else // Written books
if (item.getItemMeta() instanceof BookMeta) {
final BookMeta bookMeta = (BookMeta) item.getItemMeta();
bookMeta.setAuthor(actionData.by);
bookMeta.setTitle(actionData.title);
bookMeta.setPages(actionData.content);
item.setItemMeta(bookMeta);
}
// Fireworks
if (block_id == 402 && actionData.effectColors != null && actionData.effectColors.length > 0) {
final FireworkEffectMeta fireworkMeta = (FireworkEffectMeta) item.getItemMeta();
final Builder effect = FireworkEffect.builder();
if (actionData.effectColors != null) {
for (int i = 0; i < actionData.effectColors.length; i++) {
effect.withColor(Color.fromRGB(actionData.effectColors[i]));
}
fireworkMeta.setEffect(effect.build());
}
if (actionData.fadeColors != null) {
for (int i = 0; i < actionData.fadeColors.length; i++) {
effect.withFade(Color.fromRGB(actionData.fadeColors[i]));
}
fireworkMeta.setEffect(effect.build());
}
if (actionData.hasFlicker) {
effect.flicker(true);
}
if (actionData.hasTrail) {
effect.trail(true);
}
fireworkMeta.setEffect(effect.build());
item.setItemMeta(fireworkMeta);
}
// Item display names
final ItemMeta meta = item.getItemMeta();
if (actionData.name != null) {
meta.setDisplayName(actionData.name);
}
if (actionData.lore != null) {
meta.setLore(Arrays.asList(actionData.lore));
}
item.setItemMeta(meta);
}
Aggregations