|
1 | 1 | package mekanism.common.integration.computer; |
2 | 2 |
|
| 3 | +import com.mojang.brigadier.exceptions.CommandSyntaxException; |
3 | 4 | import java.util.ArrayList; |
4 | 5 | import java.util.Collection; |
5 | 6 | import java.util.Collections; |
|
39 | 40 | import net.minecraft.core.BlockPos; |
40 | 41 | import net.minecraft.core.GlobalPos; |
41 | 42 | import net.minecraft.core.Vec3i; |
42 | | -import net.minecraft.core.component.DataComponentMap; |
| 43 | +import net.minecraft.core.component.DataComponentPatch; |
43 | 44 | import net.minecraft.core.registries.BuiltInRegistries; |
| 45 | +import net.minecraft.nbt.NbtOps; |
| 46 | +import net.minecraft.nbt.NbtUtils; |
44 | 47 | import net.minecraft.resources.ResourceLocation; |
45 | 48 | import net.minecraft.world.item.Item; |
46 | 49 | import net.minecraft.world.item.ItemStack; |
@@ -181,18 +184,20 @@ public ItemStack getItemStack(int param) throws ComputerException { |
181 | 184 | try { |
182 | 185 | Item item = getItemFromResourceLocation(ResourceLocation.tryParse((String) map.get("name"))); |
183 | 186 | int count = SpecialConverters.getIntFromRaw(map.get("count")); |
184 | | - ItemStack stack = new ItemStack(item, count); |
185 | | - //TODO - 1.20.5: Add support for Components |
186 | 187 | String components = (String) map.get("components"); |
187 | | - /*if (components != null) { |
188 | | - stack.setTag(NbtUtils.snbtToStructure(components)); |
189 | | - }*/ |
190 | | - return stack; |
| 188 | + if (components != null) { |
| 189 | + try { |
| 190 | + DataComponentPatch dataComponents = DataComponentPatch.CODEC.decode(NbtOps.INSTANCE, NbtUtils.snbtToStructure(components)) |
| 191 | + .getOrThrow(ComputerException::new).getFirst(); |
| 192 | + return new ItemStack(item.builtInRegistryHolder(), count, dataComponents); |
| 193 | + } catch (CommandSyntaxException ex) { |
| 194 | + throw new ComputerException("Invalid SNBT: " + ex.getMessage()); |
| 195 | + } |
| 196 | + } |
| 197 | + return new ItemStack(item, count); |
191 | 198 | } catch (ClassCastException ex) { |
192 | 199 | throw new ComputerException("Invalid ItemStack at index " + param); |
193 | | - }/* catch (CommandSyntaxException e) { |
194 | | - throw new ComputerException("Invalid NBT or Attachment data"); |
195 | | - }*/ |
| 200 | + } |
196 | 201 | } |
197 | 202 |
|
198 | 203 | /** |
@@ -266,14 +271,14 @@ public Object convert(@Nullable FluidStack stack) { |
266 | 271 | if (stack == null) { |
267 | 272 | return null; |
268 | 273 | } |
269 | | - return SpecialConverters.wrapStack(RegistryUtils.getName(stack.getFluid()), "amount", stack.getAmount(), stack.getComponents()); |
| 274 | + return SpecialConverters.wrapStack(RegistryUtils.getName(stack.getFluid()), "amount", stack.getAmount(), stack.getComponentsPatch()); |
270 | 275 | } |
271 | 276 |
|
272 | 277 | public Object convert(@Nullable ItemStack stack) { |
273 | 278 | if (stack == null) { |
274 | 279 | return null; |
275 | 280 | } |
276 | | - return SpecialConverters.wrapStack(RegistryUtils.getName(stack.getItem()), "count", stack.getCount(), stack.getComponents()); |
| 281 | + return SpecialConverters.wrapStack(RegistryUtils.getName(stack.getItem()), "count", stack.getCount(), stack.getComponentsPatch()); |
277 | 282 | } |
278 | 283 |
|
279 | 284 | public Object convert(@Nullable BlockState state) { |
@@ -349,7 +354,7 @@ protected Map<String, Object> convertFilterCommon(IFilter<?> result) { |
349 | 354 | ItemStack stack = itemFilter.getItemStack(); |
350 | 355 | wrapped.put("item", convert(stack.getItem())); |
351 | 356 | if (!stack.isEmpty()) { |
352 | | - DataComponentMap components = stack.getComponents(); |
| 357 | + DataComponentPatch components = stack.getComponentsPatch(); |
353 | 358 | if (!components.isEmpty()) { |
354 | 359 | wrapped.put("itemComponents", SpecialConverters.wrapComponents(components)); |
355 | 360 | } |
@@ -507,15 +512,13 @@ private static Map<Class<?>, TableType> getBuiltInTables() { |
507 | 512 | TableType.builder(ItemStack.class, "A stack of Item(s)") |
508 | 513 | .addField("name", Item.class, "The Item's registered name") |
509 | 514 | .addField("count", int.class, "The count of items in the stack") |
510 | | - //TODO - 1.20.5: Update the description |
511 | | - .addField("components", String.class, "Any NBT of the item, in Command JSON format") |
| 515 | + .addField("components", String.class, "Any non default components of the item, in Command JSON format") |
512 | 516 | .build(types); |
513 | 517 |
|
514 | 518 | TableType.builder(FluidStack.class, "An amount of fluid") |
515 | 519 | .addField("name", ResourceLocation.class, "The Fluid's registered name, e.g. minecraft:water") |
516 | 520 | .addField("amount", int.class, "The amount in mB") |
517 | | - //TODO - 1.20.5: Update the description |
518 | | - .addField("components", String.class, "Any NBT of the fluid, in Command JSON format") |
| 521 | + .addField("components", String.class, "Any non default components of the fluid, in Command JSON format") |
519 | 522 | .build(types); |
520 | 523 |
|
521 | 524 | TableType.builder(ChemicalStack.class, "An amount of Gas/Fluid/Slurry/Pigment") |
|
0 commit comments