Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.function.Consumer;
  4.  
  5. import javax.annotation.Nullable;
  6.  
  7. import org.bukkit.Material;
  8. import org.bukkit.craftbukkit.v1_13_R2.inventory.CraftItemStack;
  9. import org.bukkit.entity.Player;
  10. import org.bukkit.inventory.ItemStack;
  11.  
  12. import com.google.gson.Gson;
  13. import com.google.gson.JsonSyntaxException;
  14. import com.google.gson.reflect.TypeToken;
  15. import com.mojang.brigadier.exceptions.CommandSyntaxException;
  16.  
  17. import net.minecraft.server.v1_13_R2.MojangsonParser;
  18. import net.minecraft.server.v1_13_R2.NBTTagCompound;
  19.  
  20. public class ItemUtility {
  21.  
  22. public static Map<String, Object> itemToMap(ItemStack item) {
  23. Map<String, Object> map = new HashMap<String, Object>();
  24.  
  25. if (item == null) item = new ItemStack(Material.AIR);
  26.  
  27. Material type = item.getType();
  28. int count = item.getAmount();
  29. NBTTagCompound tag = CraftItemStack.asNMSCopy(item).getTag();
  30.  
  31. map.put("type", type);
  32. map.put("count", count);
  33. if (tag != null) map.put("tag", tag.asString());
  34.  
  35. return map;
  36. }
  37.  
  38. public static String itemToString(ItemStack item) {
  39. return new Gson().toJson(itemToMap(item));
  40. }
  41.  
  42. public static ItemStack itemFromMap(Map<String, Object> map) throws CommandSyntaxException {
  43. Material type = Material.valueOf((String) map.get("type"));
  44. int count = ((Double) map.get("count")).intValue();
  45. String tagRaw = (String) map.get("tag");
  46. NBTTagCompound tag = tagRaw != null? MojangsonParser.parse(tagRaw): null;
  47.  
  48. ItemStack item = new ItemStack(type, count);
  49. if (tag != null) {
  50. net.minecraft.server.v1_13_R2.ItemStack nmsStack = CraftItemStack.asNMSCopy(item);
  51. nmsStack.setTag(tag);
  52. item.setItemMeta(CraftItemStack.asBukkitCopy(nmsStack).getItemMeta());
  53. }
  54.  
  55. return item;
  56. }
  57.  
  58. @SuppressWarnings("unchecked")
  59. public static ItemStack itemFromString(String str) throws JsonSyntaxException, CommandSyntaxException {
  60. return itemFromMap((Map<String, Object>) new Gson().fromJson(str, new TypeToken<Map<String, Object>>() {}.getType()));
  61. }
  62.  
  63. public static void editTag(ItemStack item, Consumer<NBTTagCompound> consumer) {
  64. net.minecraft.server.v1_13_R2.ItemStack nmsStack = CraftItemStack.asNMSCopy(item);
  65. consumer.accept(nmsStack.getOrCreateTag());
  66. item.setItemMeta(CraftItemStack.asBukkitCopy(nmsStack).getItemMeta());
  67. }
  68.  
  69. public static NBTTagCompound getTag(ItemStack item) {
  70. return CraftItemStack.asNMSCopy(item).getTag();
  71. }
  72.  
  73. public static String toCommand(ItemStack item, @Nullable Player player) {
  74. return "/give " + (player == null? "@p" : player.getName()) + " minecraft:" + item.getType().toString().toLowerCase()
  75. + tagToString(item) + ' ' + item.getAmount();
  76. }
  77.  
  78. public static String tagToString(ItemStack item) {
  79. return CraftItemStack.asNMSCopy(item).getOrCreateTag().asString();
  80. }
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement