pull/6/head
parent
1b8203d533
commit
9ad62e89a9
5 changed files with 73 additions and 84 deletions
@ -0,0 +1,64 @@ |
||||
package xyz.soukup.ecoCraftCore.utilities; |
||||
|
||||
import org.bukkit.Bukkit; |
||||
import org.bukkit.inventory.Inventory; |
||||
import org.bukkit.inventory.ItemStack; |
||||
import org.bukkit.configuration.file.YamlConfiguration; |
||||
|
||||
|
||||
public class Converter { |
||||
|
||||
public static String toString(ItemStack item) { |
||||
if (item == null) return ""; |
||||
|
||||
YamlConfiguration config = new YamlConfiguration(); |
||||
config.set("item", item); |
||||
return config.saveToString(); |
||||
} |
||||
|
||||
public static ItemStack itemstackFromString(String data) { |
||||
if (data == null || data.isEmpty()) return null; |
||||
|
||||
YamlConfiguration config = new YamlConfiguration(); |
||||
try { |
||||
config.loadFromString(data); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
return null; |
||||
} |
||||
return config.getItemStack("item"); |
||||
} |
||||
|
||||
public static String toString(Inventory inventory) { |
||||
if (inventory == null) return ""; |
||||
|
||||
ItemStack[] items = inventory.getContents(); |
||||
YamlConfiguration config = new YamlConfiguration(); |
||||
config.set("items", items); |
||||
config.set("size", inventory.getSize()); |
||||
return config.saveToString(); |
||||
} |
||||
|
||||
public static Inventory inventoryFromString(String data) { |
||||
if (data == null || data.isEmpty()) return null; |
||||
|
||||
YamlConfiguration config = new YamlConfiguration(); |
||||
try { |
||||
config.loadFromString(data); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
return null; |
||||
} |
||||
|
||||
int size = config.getInt("size"); |
||||
Inventory inv = Bukkit.createInventory(null, size); |
||||
|
||||
ItemStack[] items = ((ItemStack[]) config.get("items")); |
||||
if (items != null) { |
||||
inv.setContents(items); |
||||
} |
||||
|
||||
return inv; |
||||
} |
||||
} |
||||
|
||||
@ -1,71 +0,0 @@ |
||||
package xyz.soukup.ecoCraftCore.utilities; |
||||
|
||||
import com.google.common.reflect.TypeToken; |
||||
import com.google.gson.Gson; |
||||
import org.bukkit.Bukkit; |
||||
import org.bukkit.inventory.Inventory; |
||||
import org.bukkit.inventory.ItemStack; |
||||
|
||||
import java.lang.reflect.Type; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
public class JSONConverter { |
||||
|
||||
public static String itemStackToJson(ItemStack item) { |
||||
try { |
||||
Map<String, Object> map = item.serialize(); |
||||
return new Gson().toJson(map); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
public static ItemStack itemStackFromJson(String json) { |
||||
try { |
||||
Type type = new TypeToken<Map<String, Object>>() {}.getType(); |
||||
Map<String, Object> map = new Gson().fromJson(json, type); |
||||
return ItemStack.deserialize(map); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
public static String inventoryToJson(Inventory inventory) { |
||||
try { |
||||
Map<String, Object> map = new HashMap<>(); |
||||
map.put("size", inventory.getSize()); |
||||
map.put("contents", inventory.getContents()); |
||||
return new Gson().toJson(map); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
public static Inventory inventoryFromJson(String json) { |
||||
try { |
||||
Type type = new TypeToken<Map<String, Object>>() {}.getType(); |
||||
Map<String, Object> map = new Gson().fromJson(json, type); |
||||
int size = ((Double) map.get("size")).intValue(); |
||||
|
||||
Object[] contentsArray = ((Object[]) ((Map) map).get("contents")); |
||||
ItemStack[] contents = new ItemStack[contentsArray.length]; |
||||
|
||||
for (int i = 0; i < contentsArray.length; i++) { |
||||
Map<String, Object> itemMap = (Map<String, Object>) contentsArray[i]; |
||||
contents[i] = ItemStack.deserialize(itemMap); |
||||
} |
||||
|
||||
Inventory inventory = Bukkit.createInventory(null, size); |
||||
inventory.setContents(contents); |
||||
|
||||
return inventory; |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
return null; |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue