turt2live

Untitled

Dec 17th, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.53 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.Map;
  15.  
  16. import org.bukkit.configuration.MemoryConfiguration;
  17. import org.bukkit.craftbukkit.libs.com.google.gson.Gson;
  18. import org.bukkit.craftbukkit.libs.com.google.gson.reflect.TypeToken;
  19. import org.bukkit.inventory.ItemStack;
  20.  
  21. import com.turt2live.xmail.api.AttachmentName;
  22. import com.turt2live.xmail.player.XMailEntity;
  23.  
  24. public class ItemAttachment extends Attachment implements AttachmentName {
  25.  
  26.     protected ItemStack item;
  27.  
  28.     public ItemAttachment(ItemStack item){
  29.         super(AttachmentType.ITEM2);
  30.         if(item != null){
  31.             this.item = item.clone();
  32.         }
  33.     }
  34.  
  35.     public ItemStack getItem(){
  36.         return item;
  37.     }
  38.  
  39.     public void setValue(String value){
  40.         Map<String, Object> keys = new HashMap<String, Object>();
  41.         Gson gson = new Gson();
  42.         System.out.println(value);
  43.         keys = gson.fromJson(value, new TypeToken<Map<String, Object>>(){}.getType());
  44.         // Repair gson derps
  45.         if(keys.get("amount") != null){
  46.             Double d = (Double) keys.get("amount");
  47.             Integer i = d.intValue();
  48.             keys.put("amount", i);
  49.         }
  50.         //@SuppressWarnings ("unchecked")
  51.         //Map<String, Object> meta = (Map<String, Object>) keys.get("meta");
  52.         MemoryConfiguration mem = new MemoryConfiguration();
  53.         mem.addDefault("theitem", keys);
  54.         this.item = mem.getItemStack("theitem");
  55.         //this.item = ItemStack.deserialize(keys);
  56.     }
  57.  
  58.     @Override
  59.     public String getValue(){
  60.         Map<String, Object> serial = item.serialize();
  61.         //      if(serial.get("meta") != null){
  62.         //          ItemMeta itemmeta = item.getItemMeta();
  63.         //          Map<String, Object> meta = itemmeta.serialize();
  64.         //          serial.put("meta", meta);
  65.         //      }
  66.         Gson gson = new Gson();
  67.         return getType().getIdent().getIdent() + "|" + gson.toJson(serial);
  68.     }
  69.  
  70.     @Override
  71.     public boolean giveTo(XMailEntity entity){
  72.         return true;
  73.     }
  74.  
  75.     @Override
  76.     public String getSingularName(){
  77.         return "item";
  78.     }
  79.  
  80.     @Override
  81.     public String getPluralName(){
  82.         return "items";
  83.     }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment