|
| 1 | +package com.elmakers.mine.bukkit.utility.platform.v1_21_7; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collection; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Optional; |
| 7 | + |
| 8 | +import org.bukkit.Bukkit; |
| 9 | +import org.bukkit.Material; |
| 10 | +import org.bukkit.craftbukkit.v1_21_R5.CraftWorld; |
| 11 | +import org.bukkit.craftbukkit.v1_21_R5.inventory.CraftItemStack; |
| 12 | +import org.bukkit.inventory.ItemStack; |
| 13 | + |
| 14 | +import com.elmakers.mine.bukkit.utility.ReflectionUtils; |
| 15 | +import com.elmakers.mine.bukkit.utility.platform.Platform; |
| 16 | +import com.elmakers.mine.bukkit.utility.platform.base_v1_21_4.ItemUtilsBase_v1_21_4; |
| 17 | + |
| 18 | +import net.minecraft.core.component.DataComponents; |
| 19 | +import net.minecraft.nbt.CompoundTag; |
| 20 | +import net.minecraft.nbt.ListTag; |
| 21 | +import net.minecraft.nbt.StringTag; |
| 22 | +import net.minecraft.nbt.Tag; |
| 23 | +import net.minecraft.util.ProblemReporter; |
| 24 | +import net.minecraft.world.ItemStackWithSlot; |
| 25 | +import net.minecraft.world.item.component.CustomData; |
| 26 | +import net.minecraft.world.level.storage.TagValueInput; |
| 27 | +import net.minecraft.world.level.storage.ValueInput; |
| 28 | + |
| 29 | +public class ItemUtils extends ItemUtilsBase_v1_21_4 { |
| 30 | + public ItemUtils(Platform platform) { |
| 31 | + super(platform); |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public Object getHandle(org.bukkit.inventory.ItemStack stack) { |
| 36 | + if (stack == null || !(stack instanceof CraftItemStack)) { |
| 37 | + return null; |
| 38 | + } |
| 39 | + return ReflectionUtils.getHandle(platform.getLogger(), stack, CraftItemStack.class); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public CompoundTag getTag(Object mcItemStack) { |
| 44 | + if (mcItemStack == null || !(mcItemStack instanceof net.minecraft.world.item.ItemStack)) return null; |
| 45 | + net.minecraft.world.item.ItemStack itemStack = (net.minecraft.world.item.ItemStack)mcItemStack; |
| 46 | + CustomData customData = itemStack.get(DataComponents.CUSTOM_DATA); |
| 47 | + return customData == null ? null : customData.getUnsafe(); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public CompoundTag getTag(ItemStack itemStack) { |
| 52 | + CompoundTag tag = null; |
| 53 | + try { |
| 54 | + Object mcItemStack = getHandle(itemStack); |
| 55 | + if (mcItemStack == null) { |
| 56 | + if (itemStack.hasItemMeta()) { |
| 57 | + itemStack = makeReal(itemStack); |
| 58 | + mcItemStack = getHandle(itemStack); |
| 59 | + } |
| 60 | + } |
| 61 | + if (mcItemStack == null) return null; |
| 62 | + net.minecraft.world.item.ItemStack stack = (net.minecraft.world.item.ItemStack)mcItemStack; |
| 63 | + tag = getTag(stack); |
| 64 | + } catch (Throwable ex) { |
| 65 | + ex.printStackTrace(); |
| 66 | + } |
| 67 | + return tag; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public CompoundTag getOrCreateTag(Object mcItemStack) { |
| 72 | + if (mcItemStack == null || !(mcItemStack instanceof net.minecraft.world.item.ItemStack)) return null; |
| 73 | + net.minecraft.world.item.ItemStack itemStack = (net.minecraft.world.item.ItemStack)mcItemStack; |
| 74 | + CustomData customData = itemStack.get(DataComponents.CUSTOM_DATA); |
| 75 | + CompoundTag tag = null; |
| 76 | + if (customData == null) { |
| 77 | + tag = new CompoundTag(); |
| 78 | + // This makes a copy |
| 79 | + customData = CustomData.of(tag); |
| 80 | + tag = customData.getUnsafe(); |
| 81 | + ((net.minecraft.world.item.ItemStack)mcItemStack).set(DataComponents.CUSTOM_DATA, customData); |
| 82 | + } else { |
| 83 | + tag = customData.getUnsafe(); |
| 84 | + } |
| 85 | + return tag; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public CompoundTag getOrCreateTag(ItemStack itemStack) { |
| 90 | + CompoundTag tag = null; |
| 91 | + try { |
| 92 | + Object mcItemStack = getHandle(itemStack); |
| 93 | + if (mcItemStack == null) { |
| 94 | + if (itemStack.hasItemMeta()) { |
| 95 | + itemStack = makeReal(itemStack); |
| 96 | + mcItemStack = getHandle(itemStack); |
| 97 | + } |
| 98 | + } |
| 99 | + if (mcItemStack == null) return null; |
| 100 | + net.minecraft.world.item.ItemStack stack = (net.minecraft.world.item.ItemStack)mcItemStack; |
| 101 | + tag = getOrCreateTag(stack); |
| 102 | + } catch (Throwable ex) { |
| 103 | + ex.printStackTrace(); |
| 104 | + } |
| 105 | + return tag; |
| 106 | + } |
| 107 | + |
| 108 | + protected net.minecraft.world.item.ItemStack getNMSCopy(ItemStack stack) { |
| 109 | + net.minecraft.world.item.ItemStack nms = null; |
| 110 | + try { |
| 111 | + nms = CraftItemStack.asNMSCopy(stack); |
| 112 | + } catch (Throwable ex) { |
| 113 | + ex.printStackTrace(); |
| 114 | + } |
| 115 | + return nms; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public ItemStack getCopy(ItemStack stack) { |
| 120 | + if (stack == null) return null; |
| 121 | + net.minecraft.world.item.ItemStack craft = getNMSCopy(stack); |
| 122 | + return CraftItemStack.asCraftMirror(craft); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public boolean isEmpty(ItemStack itemStack) { |
| 127 | + if (itemStack == null || itemStack.getType() == Material.AIR) return true; |
| 128 | + Object handle = getHandle(itemStack); |
| 129 | + if (handle == null || !(handle instanceof net.minecraft.world.item.ItemStack)) return false; |
| 130 | + net.minecraft.world.item.ItemStack mcItem = (net.minecraft.world.item.ItemStack)handle; |
| 131 | + return mcItem.isEmpty(); |
| 132 | + } |
| 133 | + |
| 134 | + protected StringTag getTagString(String value) { |
| 135 | + return StringTag.valueOf(value); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public Object setStringList(Object nbtBase, String tag, Collection<String> values) { |
| 140 | + if (nbtBase == null || !(nbtBase instanceof CompoundTag)) return null; |
| 141 | + CompoundTag compoundTag = (CompoundTag)nbtBase; |
| 142 | + ListTag listTag = new ListTag(); |
| 143 | + |
| 144 | + for (String value : values) { |
| 145 | + StringTag nbtString = getTagString(value); |
| 146 | + listTag.add(nbtString); |
| 147 | + } |
| 148 | + |
| 149 | + compoundTag.put(tag, listTag); |
| 150 | + return listTag; |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public List<String> getStringList(Object nbtBase, String key) { |
| 155 | + List<String> list = new ArrayList<>(); |
| 156 | + if (nbtBase == null || !(nbtBase instanceof CompoundTag)) return list; |
| 157 | + CompoundTag compoundTag = (CompoundTag)nbtBase; |
| 158 | + Optional<ListTag> listTagOptional = compoundTag.getList(key); |
| 159 | + |
| 160 | + if (listTagOptional.isPresent()) { |
| 161 | + ListTag listTag = listTagOptional.get(); |
| 162 | + int size = listTag.size(); |
| 163 | + for (int i = 0; i < size; i++) { |
| 164 | + Tag entry = listTag.get(i); |
| 165 | + Optional<String> optionalString = entry.asString(); |
| 166 | + if (!optionalString.isPresent()) continue; |
| 167 | + list.add(optionalString.get()); |
| 168 | + } |
| 169 | + } |
| 170 | + return list; |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public ItemStack getItem(Object itemTag) { |
| 175 | + // This was only called by getItems, which no longer uses this method |
| 176 | + throw new RuntimeException("The getItem method is no longer supported"); |
| 177 | + |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public ItemStack[] getItems(Object rootTag, String tagName) { |
| 182 | + if (rootTag == null || !(rootTag instanceof CompoundTag)) return null; |
| 183 | + CompoundTag compoundTag = (CompoundTag)rootTag; |
| 184 | + |
| 185 | + try { |
| 186 | + CraftWorld world = (CraftWorld)Bukkit.getWorlds().get(0); |
| 187 | + ProblemReporter discard = ProblemReporter.DISCARDING; |
| 188 | + ValueInput valueInput = TagValueInput.create(discard, world.getHandle().registryAccess(), compoundTag); |
| 189 | + ValueInput.TypedInputList<ItemStackWithSlot> list = valueInput.listOrEmpty(tagName, ItemStackWithSlot.CODEC); |
| 190 | + List<ItemStack> itemList = new ArrayList<>(); |
| 191 | + for (ItemStackWithSlot stackWithSlot : list) { |
| 192 | + ItemStack item = CraftItemStack.asCraftMirror(stackWithSlot.stack()); |
| 193 | + itemList.add(item); |
| 194 | + } |
| 195 | + return itemList.toArray(new ItemStack[itemList.size()]); |
| 196 | + } catch (Exception ex) { |
| 197 | + ex.printStackTrace(); |
| 198 | + } |
| 199 | + return null; |
| 200 | + } |
| 201 | +} |
0 commit comments