turt2live

Untitled

Dec 18th, 2012
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.10 KB | None | 0 0
  1. /*******************************************************************************
  2.  * Copyright (c) 2012 turt2live (Travis Ralston).
  3.  * All rights reserved. This program and the accompanying materials
  4.  * are made available under the terms of the GNU Lesser Public License v2.1
  5.  * which accompanies this distribution, and is available at
  6.  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  7.  *
  8.  * Contributors:
  9.  * turt2live (Travis Ralston) - initial API and implementation
  10.  ******************************************************************************/
  11. package com.turt2live.xmail.mail.attachment;
  12.  
  13. import java.util.HashMap;
  14. import java.util.List;
  15. import java.util.Map;
  16.  
  17. import org.bukkit.ChatColor;
  18. import org.bukkit.Color;
  19. import org.bukkit.configuration.serialization.ConfigurationSerialization;
  20. import org.bukkit.craftbukkit.libs.com.google.gson.Gson;
  21. import org.bukkit.craftbukkit.libs.com.google.gson.reflect.TypeToken;
  22. import org.bukkit.inventory.ItemStack;
  23. import org.bukkit.inventory.meta.BookMeta;
  24. import org.bukkit.inventory.meta.ItemMeta;
  25.  
  26. import com.turt2live.xmail.api.AttachmentName;
  27. import com.turt2live.xmail.player.XMailEntity;
  28.  
  29. public class ItemAttachment extends Attachment implements AttachmentName {
  30.  
  31.     protected ItemStack item;
  32.  
  33.     public ItemAttachment(ItemStack item){
  34.         super(AttachmentType.ITEM2);
  35.         if(item != null){
  36.             this.item = item.clone();
  37.         }
  38.     }
  39.  
  40.     public ItemStack getItem(){
  41.         return item;
  42.     }
  43.  
  44.     @SuppressWarnings ("unchecked")
  45.     public void setValue(String value){
  46.         Gson gson = new Gson();
  47.         Map<String, Object> keys = gson.fromJson(value, new TypeToken<Map<String, Object>>(){}.getType());
  48.  
  49.         // Repair Gson thinking int == double
  50.         if(keys.get("amount") != null){
  51.             Double d = (Double) keys.get("amount");
  52.             Integer i = d.intValue();
  53.             keys.put("amount", i);
  54.         }
  55.  
  56.         // Create item
  57.         this.item = ItemStack.deserialize(keys);
  58.  
  59.         // Handle item meta
  60.         if(keys.containsKey("meta")){
  61.             Map<String, Object> itemmeta = (Map<String, Object>) keys.get("meta");
  62.  
  63.             // Fix Gson thinking double == int
  64.             if(itemmeta.containsKey("repair-cost")){
  65.                 Double d = (Double) itemmeta.get("repair-cost");
  66.                 Integer i = d.intValue();
  67.                 itemmeta.put("repair-cost", i);
  68.             }
  69.  
  70.             // Fix Gson thinking double == int
  71.             if(itemmeta.containsKey("color")){
  72.                 Map<String, Object> raw = (Map<String, Object>) itemmeta.get("color");
  73.                 Map<String, Object> colorMap = new HashMap<String, Object>();
  74.                 // We have to transfer the map because under rare cases "RED" is actually "red" (for example)
  75.                 for(String key : raw.keySet()){
  76.                     Double d = (Double) raw.get(key);
  77.                     Integer b = d.intValue();
  78.                     colorMap.put(key.toUpperCase(), b);
  79.                 }
  80.                 Color color = Color.deserialize(colorMap);
  81.                 itemmeta.put("color", color);
  82.             }
  83.  
  84.             // Create the Item Meta
  85.             ItemMeta meta = (ItemMeta) ConfigurationSerialization.deserializeObject(itemmeta, ConfigurationSerialization.getClassByAlias("ItemMeta"));
  86.  
  87.             // Colorize everything
  88.             if(meta.hasDisplayName()){
  89.                 meta.setDisplayName(ChatColor.translateAlternateColorCodes('?', ChatColor.translateAlternateColorCodes('&', meta.getDisplayName())));
  90.             }
  91.             if(meta.hasLore()){
  92.                 List<String> lore = meta.getLore();
  93.                 for(int i = 0; i < lore.size(); i++){
  94.                     String line = lore.get(i);
  95.                     line = ChatColor.translateAlternateColorCodes('?', line);
  96.                     line = ChatColor.translateAlternateColorCodes('&', line);
  97.                     lore.set(i, line);
  98.                 }
  99.                 meta.setLore(lore);
  100.             }
  101.             if(meta instanceof BookMeta){
  102.                 BookMeta book = (BookMeta) meta;
  103.                 if(book.hasAuthor()){
  104.                     book.setAuthor(ChatColor.translateAlternateColorCodes('?', ChatColor.translateAlternateColorCodes('&', book.getAuthor())));
  105.                 }
  106.                 if(book.hasTitle()){
  107.                     book.setTitle(ChatColor.translateAlternateColorCodes('?', ChatColor.translateAlternateColorCodes('&', book.getTitle())));
  108.                 }
  109.                 if(book.hasPages()){
  110.                     List<String> pages = book.getPages();
  111.                     for(int i = 0; i < pages.size(); i++){
  112.                         String line = pages.get(i);
  113.                         line = ChatColor.translateAlternateColorCodes('?', line);
  114.                         line = ChatColor.translateAlternateColorCodes('&', line);
  115.                         pages.set(i, line);
  116.                     }
  117.                     book.setPages(pages);
  118.                 }
  119.             }
  120.  
  121.             // Assign the item meta
  122.             item.setItemMeta(meta);
  123.         }
  124.     }
  125.  
  126.     @Override
  127.     public String getValue(){
  128.         Map<String, Object> serial = item.serialize();
  129.         if(serial.get("meta") != null){
  130.             ItemMeta itemmeta = item.getItemMeta();
  131.             Map<String, Object> meta = itemmeta.serialize();
  132.             if(meta.containsKey("color")){
  133.                 Map<String, Object> meta2 = new HashMap<String, Object>();
  134.                 for(String key : meta.keySet()){
  135.                     meta2.put(key, meta.get(key));
  136.                 }
  137.                 meta2.put("color", ((Color) meta.get("color")).serialize());
  138.                 meta = meta2;
  139.             }
  140.             serial.put("meta", meta);
  141.         }
  142.         Gson gson = new Gson();
  143.         return getType().getIdent().getIdent() + "|" + gson.toJson(serial);
  144.     }
  145.  
  146.     @Override
  147.     public boolean giveTo(XMailEntity entity){
  148.         return true;
  149.     }
  150.  
  151.     @Override
  152.     public String getSingularName(){
  153.         return "item";
  154.     }
  155.  
  156.     @Override
  157.     public String getPluralName(){
  158.         return "items";
  159.     }
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment