Skip to content

Instantly share code, notes, and snippets.

@keepoff07
Created January 11, 2015 21:25
Show Gist options
  • Save keepoff07/2d4dd830f4a692766dca to your computer and use it in GitHub Desktop.
Save keepoff07/2d4dd830f4a692766dca to your computer and use it in GitHub Desktop.
[Bukkit] JSONItem: convert to ItemStack from JsonText
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.bukkit.Color;
import org.bukkit.FireworkEffect;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.FireworkEffectMeta;
import org.bukkit.inventory.meta.FireworkMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.inventory.meta.Repairable;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class JSONItem {
/**
* JSONからItemStackへ変換します。<br>
* BukkitAPIで出来る簡単な範囲でのみやっているので、一部のデータタグは適用できません。<br>
* また、org.json.JSONObject等を使用させていただいでいます。<br>
* 以下のURLより入手してください。<br>
* http://www.json.org/java/<br>
* 同じくMaterialName.javaを使用しています。<br>
* 以下のURLより入手してください。<br>
* https://gist.github.com/keepoff07/586e5d790a2bd59a2647<br>
* ・現在の未対応状況
* 1.8からのNBT全般
* AttributeModifiers
* Map
*
* @param json
* @return ItemStack
*/
public static ItemStack convert(String json){
if(json.startsWith("{") && json.endsWith("}")){
Pattern p = Pattern.compile(":minecraft:([A-z]+),");
Matcher m = p.matcher(json);
while(m.find()){
String s1 = m.group(1);
json = json.replace(":minecraft:"+s1+",", ":"+s1+",");
}
try{
JSONObject mainJSONObject = new JSONObject(json);
Material id = getItemMaterial(mainJSONObject, "id");
if(id == null) return null;
int amount = getInt(mainJSONObject, "Count");
short damage = (short)getInt(mainJSONObject, "Damage");
ItemStack item = new ItemStack(id, amount, damage);
if(mainJSONObject.has("tag")){
Object sb = mainJSONObject.get("tag");
if(sb instanceof JSONObject){
item = getItemMeta((JSONObject)sb, item);
}
}
return item;
}catch(JSONException e){
return null;
}
} else return null;
}
private static String getString(JSONObject o, String key){
try{
if(o.has(key)){
Object b = o.get(key);
if(b instanceof String) return (String)b;
else if(b instanceof Integer) return String.valueOf((Integer)b);
else if(b instanceof Boolean) return String.valueOf((Boolean)b);
else return null;
}
return null;
}catch(JSONException e){
return null;
}
}
private static List<String> getStringList(JSONObject o, String key){
try{
if(o.has(key)){
Object b = o.get(key);
if(b instanceof JSONArray){
JSONArray array = (JSONArray)b;
List<String> list = new ArrayList<String>();
for(int i = 0; i < array.length(); i++){
Object sb = array.get(i);
String s = null;
if(sb instanceof String) s = (String)sb;
else if(sb instanceof Integer) s = String.valueOf((Integer)sb);
else if(sb instanceof Boolean) s = String.valueOf((Boolean)sb);
else continue;
if(s != null) list.add(s);
}
if(!list.isEmpty()) return list;
}
}
return null;
}catch(JSONException e){
return null;
}
}
private static int getInt(JSONObject o, String key){
try{
if(o.has(key)){
Object b = o.get(key);
if(b instanceof Integer){
return (Integer)b;
} else if(b instanceof String){
String s = (String)b;
if(a(s)){
return Integer.parseInt(s);
}
Pattern p = Pattern.compile("([0-9])([A-z])");
Matcher m = p.matcher(s);
while(m.find()){
String s1 = m.group(1);
String s2 = m.group(2);
s = s.replace(s1+s2, s1);
}
if(a(s)){
return Integer.parseInt(s);
}
}
}
return 0;
}catch(JSONException e){
return 0;
}
}
private static boolean getBoolean(JSONObject o, String key){
int i = getInt(o, key);
if(i == 0) return false;
else return true;
}
@SuppressWarnings("deprecation")
private static Material getItemMaterial(JSONObject o, String key){
try{
if(o.has(key)){
String Sid = getString(o,key);
if(a(Sid)){
int Iid = Integer.parseInt(Sid);
return Material.getMaterial(Iid);
}
Pattern p = Pattern.compile("([0-9])([A-z])");
Matcher m = p.matcher(Sid);
while(m.find()){
String s1 = m.group(1);
String s2 = m.group(2);
Sid = Sid.replace(s1+s2, s1);
}
if(a(Sid)){
int Iid = Integer.parseInt(Sid);
return Material.getMaterial(Iid);
}
return MaterialName.getItemMaterial(Sid);
}
return null;
}catch(JSONException e){
return null;
}
}
private static Color getColor(JSONObject o, String key){
try{
if(o.has(key)){
String s = getString(o, key);
if(s != null && a(s)){
int n = Integer.parseInt(s);
int R = n/65536;
int G = (n-R*65536)/256;
int B = n-R*65536-G*256;
return Color.fromRGB(R, G, B);
}
}
return null;
}catch(JSONException e){
return null;
}
}
private static Color[] getColors(JSONObject o, String key){
try{
if(o.has(key)){
List<String> sl = getStringList(o, key);
List<Color> cl = new ArrayList<Color>();
if(sl != null){
for(String s : sl){
if(a(s)){
int n = Integer.parseInt(s);
int R = n/65536;
int G = (n-R*65536)/256;
int B = n-R*65536-G*256;
Color c = Color.fromRGB(R, G, B);
cl.add(c);
}
}
}
if(!cl.isEmpty()){
return (Color[])cl.toArray(new Color[0]);
}
}
return null;
}catch(JSONException e){
return null;
}
}
private static boolean a(String s){
try{
Integer.parseInt(s);
return true;
}catch(NumberFormatException e){
return false;
}
}
private static boolean b(Material m){
return (m.equals(Material.LEATHER_HELMET)|m.equals(Material.LEATHER_CHESTPLATE)|m.equals(Material.LEATHER_LEGGINGS)|m.equals(Material.LEATHER_BOOTS));
}
private static boolean c(Material m){
return m.equals(Material.POTION);
}
private static boolean d(Material m){
return m.equals(Material.WRITTEN_BOOK);
}
private static boolean e(Material m){
return m.equals(Material.SKULL_ITEM);
}
private static boolean fa(Material m){
return m.equals(Material.FIREWORK_CHARGE);
}
private static boolean fb(Material m){
return m.equals(Material.FIREWORK);
}
private static boolean g(Material m){
return m.equals(Material.ENCHANTED_BOOK);
}
private static ItemStack getItemMeta(JSONObject o, ItemStack item){
Material m = item.getType();
ItemMeta meta = item.getItemMeta();
meta = TagDisplay1(o, meta.clone());
meta = TagEnch(o, meta.clone());
meta = TagRepairCost(o, meta.clone());
if(b(m)){
item.setItemMeta(TagDisplay2(o, meta.clone()));
return item;
}
if(c(m)){
item.setItemMeta(TagPotion(o, meta.clone()));
return item;
}
if(d(m)){
item.setItemMeta(TagBook(o, meta.clone()));
return item;
}
if(e(m)){
item.setItemMeta(TagSkull(o, meta.clone()));
return item;
}
if(fa(m)){
item.setItemMeta(TagFireworkEffect(o, meta.clone()));
return item;
}
if(fb(m)){
item.setItemMeta(TagFirework(o, meta.clone()));
return item;
}
if(g(m)){
item.setItemMeta(TagEnchBook(o, meta.clone()));
return item;
}
item.setItemMeta(meta);
return item;
}
private static ItemMeta TagDisplay1(JSONObject o, ItemMeta meta){
try{
if(o.has("display")){
JSONObject b = o.getJSONObject("display");
String name = getString(b,"Name");
List<String> lore = getStringList(b,"Lore");
if(name != null){
meta.setDisplayName(name);
}
if(lore != null){
meta.setLore(lore);
}
return meta;
}
return meta;
}catch(JSONException e){
return meta;
}
}
private static LeatherArmorMeta TagDisplay2(JSONObject o, ItemMeta meta){
try{
if(o.has("display")){
JSONObject b = o.getJSONObject("display");
Color color = getColor(b, "color");
if(color != null){
LeatherArmorMeta lmeta = (LeatherArmorMeta)meta;
lmeta.setColor(color);
return lmeta;
}
}
return (LeatherArmorMeta)meta;
}catch(JSONException e){
return (LeatherArmorMeta)meta;
}
}
@SuppressWarnings("deprecation")
private static ItemMeta TagEnch(JSONObject o, ItemMeta meta){
try{
if(o.has("ench")){
JSONArray array = o.getJSONArray("ench");
for(int i = 0; i < array.length(); i++){
Object sb = array.get(i);
if(sb instanceof JSONObject){
JSONObject so = (JSONObject)sb;
int id = getInt(so, "id");
int lvl = getInt(so, "lvl");
Enchantment en = Enchantment.getById(id);
if(en != null){
meta.addEnchant(en, lvl, true);
}
}
}
return meta;
}
return meta;
}catch(JSONException e){
return meta;
}
}
private static ItemMeta TagRepairCost(JSONObject o, ItemMeta meta){
try{
if(o.has("RepairCost")){
Repairable Rmeta = (Repairable)meta;
int count = getInt(o, "RepairCost");
Rmeta.setRepairCost(count);
return (ItemMeta)Rmeta;
}
return meta;
}catch(JSONException e){
return meta;
}
}
@SuppressWarnings("deprecation")
private static PotionMeta TagPotion(JSONObject o, ItemMeta meta){
try{
if(o.has("CustomPotionEffects")){
JSONArray array = o.getJSONArray("CustomPotionEffects");
PotionMeta Pmeta = (PotionMeta)meta;
for(int i = 0; i < array.length(); i++){
Object sb = array.get(i);
if(sb instanceof JSONObject){
JSONObject so = (JSONObject)sb;
int id = getInt(so, "Id");
int lvl = getInt(so, "Amplifier");
int time = getInt(so, "Duration");
boolean ambient = getBoolean(so, "Ambient");
Pmeta.addCustomEffect(new PotionEffect(PotionEffectType.getById(id), time, lvl, ambient), false);
}
}
}
return (PotionMeta)meta;
}catch(JSONException e){
return (PotionMeta)meta;
}
}
private static BookMeta TagBook(JSONObject o, ItemMeta meta){
try{
BookMeta Bmeta = (BookMeta)meta;
if(o.has("title")){
String title = getString(o,"title");
if(title != null){
Bmeta.setTitle(title);
}
}
if(o.has("author")){
String author = getString(o,"author");
if(author != null){
Bmeta.setAuthor(author);
}
}
if(o.has("pages")){
List<String> page = getStringList(o, "pages");
if(page != null){
Bmeta.setPages(page);
}
}
return Bmeta;
}catch(JSONException e){
return (BookMeta)meta;
}
}
private static SkullMeta TagSkull(JSONObject o, ItemMeta meta){
try{
SkullMeta Smeta = (SkullMeta)meta;
if(o.has("SkullOwner")){
String SkullOwner = getString(o,"SkullOwner");
if(SkullOwner != null){
Smeta.setOwner(SkullOwner);
}
}
return Smeta;
}catch(JSONException e){
return (SkullMeta)meta;
}
}
private static FireworkMeta TagFirework(JSONObject o, ItemMeta meta){
try{
FireworkMeta Fmeta = (FireworkMeta)meta;
if(o.has("Fireworks")){
JSONObject so = o.getJSONObject("Fireworks");
if(so.has("Flight")){
int fly = getInt(so, "Flight");
if(fly >= 0 & fly <= 128){
Fmeta.setPower(fly);
}
}
if(so.has("Explosions")){
FireworkEffect[] fs = getFireworkEffects(so);
if(fs != null){
Fmeta.addEffects(fs);
}
}
}
return Fmeta;
}catch(JSONException e){
return (FireworkMeta)meta;
}
}
private static FireworkEffectMeta TagFireworkEffect(JSONObject o, ItemMeta meta){
try{
FireworkEffectMeta Fmeta = (FireworkEffectMeta)meta;
if(o.has("Explosion")){
FireworkEffect f = getFireworkEffect(o);
if(f != null){
Fmeta.setEffect(f);
}
}
return Fmeta;
}catch(JSONException e){
return (FireworkEffectMeta)meta;
}
}
private static FireworkEffect[] getFireworkEffects(JSONObject o){
try{
if(o.has("Explosions")){
JSONArray array = o.getJSONArray("Explosions");
List<FireworkEffect> list = new ArrayList<FireworkEffect>();
for(int i = 0; i < array.length(); i++){
Object sb = array.get(i);
if(sb instanceof JSONObject){
FireworkEffect.Builder build = FireworkEffect.builder();
JSONObject so = (JSONObject)sb;
if(so.has("Flicker")){
build.flicker(getBoolean(so, "Flicker"));
}else{
build.flicker(false);
}
if(so.has("Trail")){
build.trail(getBoolean(so, "Trail"));
}else{
build.trail(false);
}
if(so.has("Type")){
int t = getInt(so, "Type");
if(t == 1) build.with(FireworkEffect.Type.BALL_LARGE);
else if(t == 2) build.with(FireworkEffect.Type.STAR);
else if(t == 3) build.with(FireworkEffect.Type.CREEPER);
else if(t == 4) build.with(FireworkEffect.Type.BURST);
else build.with(FireworkEffect.Type.BALL);
}else{
build.with(FireworkEffect.Type.BALL);
}
if(so.has("Colors")){
Color[] color = getColors(so, "Colors");
if(color != null){
build.withColor(color);
}else{
build.withColor(new Color[]{Color.BLACK});
}
}else{
build.withColor(new Color[]{Color.BLACK});
}
if(so.has("FadeColors")){
Color[] color = getColors(so, "FadeColors");
if(color != null){
build.withFade(color);
}
}
list.add(build.build());
}
}
if(!list.isEmpty()){
return (FireworkEffect[])list.toArray(new FireworkEffect[0]);
}
}
return null;
}catch(JSONException e){
return null;
}
}
private static FireworkEffect getFireworkEffect(JSONObject o){
try{
if(o.has("Explosion")){
JSONObject so = o.getJSONObject("Explosion");
FireworkEffect.Builder build = FireworkEffect.builder();
if(so.has("Flicker")){
build.flicker(getBoolean(so, "Flicker"));
}else{
build.flicker(false);
}
if(so.has("Trail")){
build.trail(getBoolean(so, "Trail"));
}else{
build.trail(false);
}
if(so.has("Type")){
int t = getInt(so, "Type");
if(t == 1) build.with(FireworkEffect.Type.BALL_LARGE);
else if(t == 2) build.with(FireworkEffect.Type.STAR);
else if(t == 3) build.with(FireworkEffect.Type.CREEPER);
else if(t == 4) build.with(FireworkEffect.Type.BURST);
else build.with(FireworkEffect.Type.BALL);
}else{
build.with(FireworkEffect.Type.BALL);
}
if(so.has("Colors")){
Color[] color = getColors(so, "Colors");
if(color != null){
build.withColor(color);
}else{
build.withColor(new Color[]{Color.BLACK});
}
}else{
build.withColor(new Color[]{Color.BLACK});
}
if(so.has("FadeColors")){
Color[] color = getColors(so, "FadeColors");
if(color != null){
build.withFade(color);
}
}
return build.build();
}
return null;
}catch(JSONException e){
return null;
}
}
@SuppressWarnings("deprecation")
private static EnchantmentStorageMeta TagEnchBook(JSONObject o, ItemMeta meta){
try{
EnchantmentStorageMeta Emeta = (EnchantmentStorageMeta)meta;
if(o.has("StoredEnchantments")){
JSONArray array = o.getJSONArray("StoredEnchantments");
for(int i = 0; i < array.length(); i++){
Object sb = array.get(i);
if(sb instanceof JSONObject){
JSONObject so = (JSONObject)sb;
int id = getInt(so, "id");
int lvl = getInt(so, "lvl");
Enchantment en = Enchantment.getById(id);
if(en != null){
Emeta.addStoredEnchant(en, lvl, true);
}
}
}
return Emeta;
}
return Emeta;
}catch(JSONException e){
return (EnchantmentStorageMeta)meta;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment