Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************************************************************************
- * Copyright (c) 2012 turt2live (Travis Ralston).
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the GNU Lesser Public License v2.1
- * which accompanies this distribution, and is available at
- * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
- *
- * Contributors:
- * turt2live (Travis Ralston) - initial API and implementation
- ******************************************************************************/
- package com.turt2live.xmail.mail.attachment;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import org.bukkit.ChatColor;
- import org.bukkit.Color;
- import org.bukkit.configuration.serialization.ConfigurationSerialization;
- import org.bukkit.craftbukkit.libs.com.google.gson.Gson;
- import org.bukkit.craftbukkit.libs.com.google.gson.reflect.TypeToken;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.inventory.meta.BookMeta;
- import org.bukkit.inventory.meta.ItemMeta;
- import com.turt2live.xmail.api.AttachmentName;
- import com.turt2live.xmail.player.XMailEntity;
- public class ItemAttachment extends Attachment implements AttachmentName {
- protected ItemStack item;
- public ItemAttachment(ItemStack item){
- super(AttachmentType.ITEM2);
- if(item != null){
- this.item = item.clone();
- }
- }
- public ItemStack getItem(){
- return item;
- }
- @SuppressWarnings ("unchecked")
- public void setValue(String value){
- Gson gson = new Gson();
- Map<String, Object> keys = gson.fromJson(value, new TypeToken<Map<String, Object>>(){}.getType());
- // Repair Gson thinking int == double
- if(keys.get("amount") != null){
- Double d = (Double) keys.get("amount");
- Integer i = d.intValue();
- keys.put("amount", i);
- }
- // Create item
- this.item = ItemStack.deserialize(keys);
- // Handle item meta
- if(keys.containsKey("meta")){
- Map<String, Object> itemmeta = (Map<String, Object>) keys.get("meta");
- // Fix Gson thinking double == int
- if(itemmeta.containsKey("repair-cost")){
- Double d = (Double) itemmeta.get("repair-cost");
- Integer i = d.intValue();
- itemmeta.put("repair-cost", i);
- }
- // Fix Gson thinking double == int
- if(itemmeta.containsKey("color")){
- Map<String, Object> raw = (Map<String, Object>) itemmeta.get("color");
- Map<String, Object> colorMap = new HashMap<String, Object>();
- // We have to transfer the map because under rare cases "RED" is actually "red" (for example)
- for(String key : raw.keySet()){
- Double d = (Double) raw.get(key);
- Integer b = d.intValue();
- colorMap.put(key.toUpperCase(), b);
- }
- Color color = Color.deserialize(colorMap);
- itemmeta.put("color", color);
- }
- // Create the Item Meta
- ItemMeta meta = (ItemMeta) ConfigurationSerialization.deserializeObject(itemmeta, ConfigurationSerialization.getClassByAlias("ItemMeta"));
- // Colorize everything
- if(meta.hasDisplayName()){
- meta.setDisplayName(ChatColor.translateAlternateColorCodes('?', ChatColor.translateAlternateColorCodes('&', meta.getDisplayName())));
- }
- if(meta.hasLore()){
- List<String> lore = meta.getLore();
- for(int i = 0; i < lore.size(); i++){
- String line = lore.get(i);
- line = ChatColor.translateAlternateColorCodes('?', line);
- line = ChatColor.translateAlternateColorCodes('&', line);
- lore.set(i, line);
- }
- meta.setLore(lore);
- }
- if(meta instanceof BookMeta){
- BookMeta book = (BookMeta) meta;
- if(book.hasAuthor()){
- book.setAuthor(ChatColor.translateAlternateColorCodes('?', ChatColor.translateAlternateColorCodes('&', book.getAuthor())));
- }
- if(book.hasTitle()){
- book.setTitle(ChatColor.translateAlternateColorCodes('?', ChatColor.translateAlternateColorCodes('&', book.getTitle())));
- }
- if(book.hasPages()){
- List<String> pages = book.getPages();
- for(int i = 0; i < pages.size(); i++){
- String line = pages.get(i);
- line = ChatColor.translateAlternateColorCodes('?', line);
- line = ChatColor.translateAlternateColorCodes('&', line);
- pages.set(i, line);
- }
- book.setPages(pages);
- }
- }
- // Assign the item meta
- item.setItemMeta(meta);
- }
- }
- @Override
- public String getValue(){
- Map<String, Object> serial = item.serialize();
- if(serial.get("meta") != null){
- ItemMeta itemmeta = item.getItemMeta();
- Map<String, Object> meta = itemmeta.serialize();
- if(meta.containsKey("color")){
- Map<String, Object> meta2 = new HashMap<String, Object>();
- for(String key : meta.keySet()){
- meta2.put(key, meta.get(key));
- }
- meta2.put("color", ((Color) meta.get("color")).serialize());
- meta = meta2;
- }
- serial.put("meta", meta);
- }
- Gson gson = new Gson();
- return getType().getIdent().getIdent() + "|" + gson.toJson(serial);
- }
- @Override
- public boolean giveTo(XMailEntity entity){
- return true;
- }
- @Override
- public String getSingularName(){
- return "item";
- }
- @Override
- public String getPluralName(){
- return "items";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment