use of org.bukkit.inventory.meta.BookMeta in project Prism-Bukkit by prism.
the class ItemStackAction method setItem.
/**
*
* @param action_type
* @param block
* @param player
*/
public void setItem(ItemStack item, int quantity, int slot, Map<Enchantment, Integer> enchantments) {
actionData = new ItemStackActionData();
if (enchantments != null) {
this.enchantments = enchantments;
}
if (item == null || item.getAmount() <= 0) {
this.setCanceled(true);
return;
}
this.item = item;
if (enchantments == null) {
this.enchantments = item.getEnchantments();
}
// Set basics
this.block_id = item.getTypeId();
this.block_subid = item.getDurability();
actionData.amt = quantity;
if (slot >= 0) {
actionData.slot = slot;
}
// Set additional data all items may have
final ItemMeta meta = item.getItemMeta();
if (meta != null && meta.getDisplayName() != null) {
actionData.name = meta.getDisplayName();
}
// Leather Coloring
if (meta != null && item.getType().name().contains("LEATHER_")) {
final LeatherArmorMeta lam = (LeatherArmorMeta) meta;
if (lam.getColor() != null) {
actionData.color = lam.getColor().asRGB();
}
} else // Skull Owner
if (meta != null && item.getType().equals(Material.SKULL_ITEM)) {
final SkullMeta skull = (SkullMeta) meta;
if (skull.hasOwner()) {
actionData.owner = skull.getOwner();
}
}
// Written books
if (meta != null && meta instanceof BookMeta) {
final BookMeta bookMeta = (BookMeta) meta;
actionData.by = bookMeta.getAuthor();
actionData.title = bookMeta.getTitle();
actionData.content = bookMeta.getPages().toArray(new String[0]);
}
// Lore
if (meta != null && meta.getLore() != null) {
actionData.lore = meta.getLore().toArray(new String[0]);
}
// Enchantments
if (!this.enchantments.isEmpty()) {
final String[] enchs = new String[this.enchantments.size()];
int i = 0;
for (final Entry<Enchantment, Integer> ench : this.enchantments.entrySet()) {
enchs[i] = ench.getKey().getId() + ":" + ench.getValue();
i++;
}
actionData.enchs = enchs;
} else // Book enchantments
if (meta != null && item.getType().equals(Material.ENCHANTED_BOOK)) {
final EnchantmentStorageMeta bookEnchantments = (EnchantmentStorageMeta) meta;
if (bookEnchantments.hasStoredEnchants()) {
if (bookEnchantments.getStoredEnchants().size() > 0) {
final String[] enchs = new String[bookEnchantments.getStoredEnchants().size()];
int i = 0;
for (final Entry<Enchantment, Integer> ench : bookEnchantments.getStoredEnchants().entrySet()) {
enchs[i] = ench.getKey().getId() + ":" + ench.getValue();
i++;
}
actionData.enchs = enchs;
}
}
}
// Fireworks
if (meta != null && block_id == 402) {
final FireworkEffectMeta fireworkMeta = (FireworkEffectMeta) meta;
if (fireworkMeta.hasEffect()) {
final FireworkEffect effect = fireworkMeta.getEffect();
if (!effect.getColors().isEmpty()) {
final int[] effectColors = new int[effect.getColors().size()];
int i = 0;
for (final Color effectColor : effect.getColors()) {
effectColors[i] = effectColor.asRGB();
i++;
}
actionData.effectColors = effectColors;
}
if (!effect.getFadeColors().isEmpty()) {
final int[] fadeColors = new int[effect.getColors().size()];
final int i = 0;
for (final Color fadeColor : effect.getFadeColors()) {
fadeColors[i] = fadeColor.asRGB();
}
actionData.fadeColors = fadeColors;
}
if (effect.hasFlicker()) {
actionData.hasFlicker = true;
}
if (effect.hasTrail()) {
actionData.hasTrail = true;
}
}
}
}
use of org.bukkit.inventory.meta.BookMeta in project Denizen-For-Bukkit by DenizenScript.
the class BookScriptContainer method writeBookTo.
public dItem writeBookTo(dItem book, dPlayer player, dNPC npc) {
// Get current ItemMeta from the book
BookMeta bookInfo = (BookMeta) book.getItemStack().getItemMeta();
if (contains("TITLE")) {
String title = getString("TITLE");
title = TagManager.tag(title, new BukkitTagContext(player, npc, false, null, shouldDebug(), new dScript(this)));
bookInfo.setTitle(title);
}
if (contains("SIGNED")) {
if (getString("SIGNED").equalsIgnoreCase("false")) {
book.getItemStack().setType(Material.BOOK_AND_QUILL);
}
}
if (contains("AUTHOR")) {
String author = getString("AUTHOR");
author = TagManager.tag(author, new BukkitTagContext(player, npc, false, null, shouldDebug(), new dScript(this)));
bookInfo.setAuthor(author);
}
if (contains("TEXT")) {
List<String> pages = getStringList("TEXT");
for (String page : pages) {
page = TagManager.tag(page, new BukkitTagContext(player, npc, false, null, shouldDebug(), new dScript(this)));
bookInfo.addPage(page);
}
}
book.getItemStack().setItemMeta(bookInfo);
return book;
}
use of org.bukkit.inventory.meta.BookMeta in project Denizen-For-Bukkit by DenizenScript.
the class ItemBook method getPropertyString.
@Override
public String getPropertyString() {
StringBuilder output = new StringBuilder();
BookMeta bookInfo = (BookMeta) item.getItemStack().getItemMeta();
if (item.getItemStack().getType().equals(Material.WRITTEN_BOOK) && bookInfo.hasAuthor() && bookInfo.hasTitle()) {
output.append("author|").append(EscapeTags.Escape(bookInfo.getAuthor())).append("|title|").append(EscapeTags.Escape(bookInfo.getTitle())).append("|");
}
output.append("pages|");
if (bookInfo.hasPages()) {
for (String page : bookInfo.getPages()) {
output.append(EscapeTags.Escape(page)).append("|");
}
}
if (output.length() <= 6) {
return null;
} else {
return output.substring(0, output.length() - 1);
}
}
use of org.bukkit.inventory.meta.BookMeta in project Denizen-For-Bukkit by DenizenScript.
the class ItemBook method adjust.
@Override
public void adjust(Mechanism mechanism) {
if (mechanism.matches("book")) {
BookMeta meta = (BookMeta) item.getItemStack().getItemMeta();
dList data = mechanism.getValue().asType(dList.class);
if (data.size() < 2) {
dB.echoError("Invalid book input!");
} else {
if (data.size() > 4 && data.get(0).equalsIgnoreCase("author") && data.get(2).equalsIgnoreCase("title")) {
if (!item.getItemStack().getType().equals(Material.WRITTEN_BOOK)) {
dB.echoError("That type of book cannot have title or author!");
} else {
meta.setAuthor(EscapeTags.unEscape(data.get(1)));
meta.setTitle(EscapeTags.unEscape(data.get(3)));
for (int i = 0; i < 4; i++) {
// No .removeRange?
data.remove(0);
}
}
}
if (!data.get(0).equalsIgnoreCase("pages")) {
dB.echoError("Invalid book input!");
} else {
ArrayList<String> newPages = new ArrayList<String>();
for (int i = 1; i < data.size(); i++) {
newPages.add(EscapeTags.unEscape(data.get(i)));
}
meta.setPages(newPages);
}
item.getItemStack().setItemMeta(meta);
}
}
}
use of org.bukkit.inventory.meta.BookMeta in project Glowstone by GlowstoneMC.
the class PluginMessageHandler method handleInternal.
private void handleInternal(GlowSession session, String channel, byte... data) {
/*
MC|Brand
entire data: string of client's brand (e.g. "vanilla")
MC|BEdit
item stack: new book item (should be verified)
MC|BSign
item stack: new book item (should be verified)
MC|TrSel
int: villager trade to select
MC|AdvCdm
byte: mode
if 0:
int x, int y, int z (command block in world)
if 1:
int entity (command block minecart)
string: command to set
MC|Beacon
two ints, presumably the selected enchants
MC|ItemName
entire data: name to apply to item in anvil
*/
ByteBuf buf = Unpooled.wrappedBuffer(data);
switch(channel) {
case "MC|Brand":
// vanilla server doesn't handle this, for now just log it
String brand = null;
try {
brand = ByteBufUtils.readUTF8(buf);
} catch (IOException e) {
GlowServer.logger.log(Level.WARNING, "Error reading client brand of " + session, e);
}
if (brand != null && !brand.equals("vanilla")) {
GlowServer.logger.info("Client brand of " + session.getPlayer().getName() + " is: " + brand);
}
break;
case "MC|BEdit":
{
// read and verify stack
ItemStack item = GlowBufUtils.readSlot(buf);
//GlowServer.logger.info("BookEdit [" + session.getPlayer().getName() + "]: " + item);
if (item == null || item.getType() != Material.BOOK_AND_QUILL) {
return;
}
ItemMeta meta = item.getItemMeta();
if (!(meta instanceof BookMeta)) {
return;
}
BookMeta book = (BookMeta) meta;
if (!book.hasPages()) {
return;
}
// verify item in hand
ItemStack inHand = session.getPlayer().getItemInHand();
if (inHand == null || inHand.getType() != Material.BOOK_AND_QUILL) {
return;
}
ItemMeta handMeta = inHand.getItemMeta();
if (!(handMeta instanceof BookMeta)) {
return;
}
BookMeta handBook = (BookMeta) handMeta;
// apply pages to book
handBook.setPages(book.getPages());
inHand.setItemMeta(handBook);
session.getPlayer().setItemInHand(inHand);
break;
}
case "MC|BSign":
// read and verify stack
ItemStack item = GlowBufUtils.readSlot(buf);
//GlowServer.logger.info("BookSign [" + session.getPlayer().getName() + "]: " + item);
if (item == null || item.getType() != Material.WRITTEN_BOOK) {
return;
}
ItemMeta meta = item.getItemMeta();
if (!(meta instanceof BookMeta)) {
return;
}
BookMeta book = (BookMeta) meta;
if (!book.hasPages() || !book.hasTitle()) {
return;
}
// verify item in hand
ItemStack inHand = session.getPlayer().getItemInHand();
if (inHand == null || inHand.getType() != Material.BOOK_AND_QUILL) {
return;
}
ItemMeta handMeta = inHand.getItemMeta();
if (!(handMeta instanceof BookMeta)) {
return;
}
BookMeta handBook = (BookMeta) handMeta;
// apply pages, title, and author to book
handBook.setAuthor(session.getPlayer().getName());
handBook.setTitle(book.getTitle());
handBook.setPages(book.getPages());
inHand.setType(Material.WRITTEN_BOOK);
inHand.setItemMeta(handBook);
session.getPlayer().setItemInHand(inHand);
break;
case "MC|ItemName":
if (session.getPlayer().getOpenInventory() == null) {
break;
}
// check if player is in an anvil inventory
if (session.getPlayer().getOpenInventory().getType() != InventoryType.ANVIL) {
break;
}
// get the new name for the item
String name;
try {
name = ByteBufUtils.readUTF8(buf);
} catch (IOException e) {
GlowServer.logger.log(Level.WARNING, "Error reading anvil item name by " + session, e);
break;
}
((GlowAnvilInventory) session.getPlayer().getOpenInventory().getTopInventory()).setRenameText(name);
break;
default:
GlowServer.logger.info(session + " used unknown Minecraft channel: " + channel);
break;
}
buf.release();
}
Aggregations