ItemStackUtils.java 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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.getOrCreateChildTag("display");
  13. ListNBT nbtList = new ListNBT();
  14. list.forEach(s -> nbtList.add(StringNBT.valueOf(ITextComponent.Serializer.toJson(new StringTextComponent(s.toString())))));
  15. com.put("Lore", nbtList);
  16. }
  17. public static void addLore(ItemStack stack, String s) {
  18. CompoundNBT com = stack.getOrCreateChildTag("display");
  19. ListNBT nbtList = com.getList("Lore", 8);
  20. nbtList.add(StringNBT.valueOf(ITextComponent.Serializer.toJson(new StringTextComponent(s))));
  21. com.put("Lore", nbtList);
  22. }
  23. public static List<Object> getLore(ItemStack stack) {
  24. CompoundNBT com = stack.getOrCreateChildTag("display");
  25. ListNBT nbtList = com.getList("Lore", 8);
  26. ArrayList<Object> list = new ArrayList<>();
  27. for(int i = 0; i < nbtList.size(); i++) {
  28. list.add(ITextComponent.Serializer.getComponentFromJson(nbtList.getString(i)).getString());
  29. }
  30. return list;
  31. }
  32. public static void addLore(ItemStack stack, String s, int i) {
  33. CompoundNBT com = stack.getOrCreateChildTag("display");
  34. ListNBT list = com.getList("Lore", 8);
  35. if(i >= list.size()) {
  36. list.add(StringNBT.valueOf(ITextComponent.Serializer.toJson(new StringTextComponent(s))));
  37. } else {
  38. list.set(i, StringNBT.valueOf(ITextComponent.Serializer.toJson(new StringTextComponent(s))));
  39. }
  40. com.put("Lore", list);
  41. }
  42. }