ItemStackUtils.java 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package me.km.utils;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import net.minecraft.item.ItemStack;
  5. import net.minecraft.nbt.CompoundNBT;
  6. import net.minecraft.nbt.ListNBT;
  7. import net.minecraft.nbt.StringNBT;
  8. import net.minecraft.util.text.ITextComponent;
  9. import net.minecraft.util.text.StringTextComponent;
  10. public class ItemStackUtils {
  11. public static void setLore(ItemStack stack, List<Object> list) {
  12. CompoundNBT com = stack.getOrCreateTagElement("display");
  13. ListNBT nbtList = new ListNBT();
  14. list.forEach(s -> nbtList.add(StringNBT
  15. .valueOf(ITextComponent.Serializer.toJson(new StringTextComponent(s.toString())))));
  16. com.put("Lore", nbtList);
  17. }
  18. public static void addLore(ItemStack stack, String s) {
  19. CompoundNBT com = stack.getOrCreateTagElement("display");
  20. ListNBT nbtList = com.getList("Lore", 8);
  21. nbtList.add(
  22. StringNBT.valueOf(ITextComponent.Serializer.toJson(new StringTextComponent(s))));
  23. com.put("Lore", nbtList);
  24. }
  25. public static List<Object> getLore(ItemStack stack) {
  26. CompoundNBT com = stack.getOrCreateTagElement("display");
  27. ListNBT nbtList = com.getList("Lore", 8);
  28. ArrayList<Object> list = new ArrayList<>();
  29. for(int i = 0; i < nbtList.size(); i++) {
  30. list.add(ITextComponent.Serializer.fromJson(nbtList.getString(i)).getString());
  31. }
  32. return list;
  33. }
  34. public static void addLore(ItemStack stack, String s, int i) {
  35. CompoundNBT com = stack.getOrCreateTagElement("display");
  36. ListNBT list = com.getList("Lore", 8);
  37. if(i >= list.size()) {
  38. list.add(StringNBT
  39. .valueOf(ITextComponent.Serializer.toJson(new StringTextComponent(s))));
  40. } else {
  41. list.set(i, StringNBT
  42. .valueOf(ITextComponent.Serializer.toJson(new StringTextComponent(s))));
  43. }
  44. com.put("Lore", list);
  45. }
  46. }