Skip to content

Commit 0d730a1

Browse files
committed
Add support to the Computer wrappers for data components
1 parent dfe836a commit 0d730a1

File tree

7 files changed

+41
-36
lines changed

7 files changed

+41
-36
lines changed

src/datagen/generated/mekanism/.cache/2ca94f3a6e22cb9eec299788405fc6e4ad158c09

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/datagen/generated/mekanism/data/mekanism/computer_help/jekyll.md

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/mekanism/common/block/BlockMekanism.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ protected boolean canBeReplaced(@NotNull BlockState state, @NotNull Fluid fluid)
8585
@NotNull
8686
@Override
8787
public ItemStack getCloneItemStack(@NotNull BlockState state, @NotNull HitResult target, @NotNull LevelReader world, @NotNull BlockPos pos, @NotNull Player player) {
88-
ItemStack stack = new ItemStack(this);
88+
ItemStack stack = super.getCloneItemStack(state, target, world, pos, player);
8989
//TODO - 1.20.5: Figure this out, we want to make sure we copy components
9090
/*TileEntityUpdateable tile = WorldUtils.getTileEntity(TileEntityUpdateable.class, world, pos);
9191
if (tile != null) {

src/main/java/mekanism/common/content/qio/QIOCraftingWindow.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,7 @@ private boolean testEquivalentItem(Level world, @NotNull QIOFrequency frequency,
865865
if (!stack.isEmpty()) {
866866
//Note: This should never happen as we pre-validate attempting to insert, but in case it does, log it
867867
Mekanism.logger.error("Failed to insert item ({} with components: {}) into crafting window: {}.", removed.getItem(),
868-
removed.getComponents(), windowIndex);
868+
removed.getComponentsPatch(), windowIndex);
869869
}
870870
//TODO - 1.18: Debate potentially briefly highlighting the slot to make it more evident to the player
871871
// that something about the slot changed.

src/main/java/mekanism/common/integration/computer/BaseComputerHelper.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package mekanism.common.integration.computer;
22

3+
import com.mojang.brigadier.exceptions.CommandSyntaxException;
34
import java.util.ArrayList;
45
import java.util.Collection;
56
import java.util.Collections;
@@ -39,8 +40,10 @@
3940
import net.minecraft.core.BlockPos;
4041
import net.minecraft.core.GlobalPos;
4142
import net.minecraft.core.Vec3i;
42-
import net.minecraft.core.component.DataComponentMap;
43+
import net.minecraft.core.component.DataComponentPatch;
4344
import net.minecraft.core.registries.BuiltInRegistries;
45+
import net.minecraft.nbt.NbtOps;
46+
import net.minecraft.nbt.NbtUtils;
4447
import net.minecraft.resources.ResourceLocation;
4548
import net.minecraft.world.item.Item;
4649
import net.minecraft.world.item.ItemStack;
@@ -181,18 +184,20 @@ public ItemStack getItemStack(int param) throws ComputerException {
181184
try {
182185
Item item = getItemFromResourceLocation(ResourceLocation.tryParse((String) map.get("name")));
183186
int count = SpecialConverters.getIntFromRaw(map.get("count"));
184-
ItemStack stack = new ItemStack(item, count);
185-
//TODO - 1.20.5: Add support for Components
186187
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);
191198
} catch (ClassCastException ex) {
192199
throw new ComputerException("Invalid ItemStack at index " + param);
193-
}/* catch (CommandSyntaxException e) {
194-
throw new ComputerException("Invalid NBT or Attachment data");
195-
}*/
200+
}
196201
}
197202

198203
/**
@@ -266,14 +271,14 @@ public Object convert(@Nullable FluidStack stack) {
266271
if (stack == null) {
267272
return null;
268273
}
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());
270275
}
271276

272277
public Object convert(@Nullable ItemStack stack) {
273278
if (stack == null) {
274279
return null;
275280
}
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());
277282
}
278283

279284
public Object convert(@Nullable BlockState state) {
@@ -349,7 +354,7 @@ protected Map<String, Object> convertFilterCommon(IFilter<?> result) {
349354
ItemStack stack = itemFilter.getItemStack();
350355
wrapped.put("item", convert(stack.getItem()));
351356
if (!stack.isEmpty()) {
352-
DataComponentMap components = stack.getComponents();
357+
DataComponentPatch components = stack.getComponentsPatch();
353358
if (!components.isEmpty()) {
354359
wrapped.put("itemComponents", SpecialConverters.wrapComponents(components));
355360
}
@@ -507,15 +512,13 @@ private static Map<Class<?>, TableType> getBuiltInTables() {
507512
TableType.builder(ItemStack.class, "A stack of Item(s)")
508513
.addField("name", Item.class, "The Item's registered name")
509514
.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")
512516
.build(types);
513517

514518
TableType.builder(FluidStack.class, "An amount of fluid")
515519
.addField("name", ResourceLocation.class, "The Fluid's registered name, e.g. minecraft:water")
516520
.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")
519522
.build(types);
520523

521524
TableType.builder(ChemicalStack.class, "An amount of Gas/Fluid/Slurry/Pigment")

src/main/java/mekanism/common/integration/computer/SpecialConverters.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package mekanism.common.integration.computer;
22

3+
import com.mojang.brigadier.exceptions.CommandSyntaxException;
34
import java.util.HashMap;
45
import java.util.Locale;
56
import java.util.Map;
@@ -19,8 +20,11 @@
1920
import mekanism.common.content.transporter.SorterItemStackFilter;
2021
import mekanism.common.tile.machine.TileEntityOredictionificator;
2122
import mekanism.common.util.text.InputValidator;
22-
import net.minecraft.core.component.DataComponentMap;
23+
import net.minecraft.core.component.DataComponentPatch;
2324
import net.minecraft.core.registries.BuiltInRegistries;
25+
import net.minecraft.nbt.CompoundTag;
26+
import net.minecraft.nbt.NbtOps;
27+
import net.minecraft.nbt.NbtUtils;
2428
import net.minecraft.resources.ResourceLocation;
2529
import net.minecraft.world.item.Item;
2630
import net.minecraft.world.item.ItemStack;
@@ -50,16 +54,16 @@ private static ItemStack tryCreateFilterItem(@Nullable String rawName, @Nullable
5054
if (item == Items.AIR) {
5155
return ItemStack.EMPTY;
5256
}
53-
ItemStack stack = new ItemStack(item);
54-
//TODO - 1.20.5: Add support for components
55-
/*if (rawComponents != null) {
57+
if (rawComponents != null) {
5658
try {
57-
stack.setTag(NbtUtils.snbtToStructure(rawComponents));
59+
DataComponentPatch dataComponents = DataComponentPatch.CODEC.decode(NbtOps.INSTANCE, NbtUtils.snbtToStructure(rawComponents))
60+
.getOrThrow(ComputerException::new).getFirst();
61+
return new ItemStack(item.builtInRegistryHolder(), 1, dataComponents);
5862
} catch (CommandSyntaxException ex) {
5963
throw new ComputerException("Invalid SNBT: " + ex.getMessage());
6064
}
61-
}*/
62-
return stack;
65+
}
66+
return new ItemStack(item);
6367
}
6468

6569
private static Item tryCreateItem(@Nullable Object rawName) {
@@ -212,7 +216,7 @@ private static void decodeItemStackFilter(@NotNull Map<?, ?> map, IItemStackFilt
212216
itemFilter.setItemStack(stack);
213217
}
214218

215-
static Map<String, Object> wrapStack(ResourceLocation name, String sizeKey, int amount, @NotNull DataComponentMap components) {
219+
static Map<String, Object> wrapStack(ResourceLocation name, String sizeKey, int amount, @NotNull DataComponentPatch components) {
216220
int elements = 2;
217221
boolean hasComponents = !components.isEmpty() && amount > 0;
218222
if (hasComponents) {
@@ -227,9 +231,7 @@ static Map<String, Object> wrapStack(ResourceLocation name, String sizeKey, int
227231
return wrapped;
228232
}
229233

230-
static String wrapComponents(@NotNull DataComponentMap components) {
231-
//TODO - 1.20.5: Add support for components
232-
//return NbtUtils.structureToSnbt(components);
233-
return "";
234+
static String wrapComponents(@NotNull DataComponentPatch components) {
235+
return NbtUtils.structureToSnbt((CompoundTag) DataComponentPatch.CODEC.encodeStart(NbtOps.INSTANCE, components).getOrThrow());
234236
}
235237
}

src/main/java/mekanism/common/inventory/slot/BasicInventorySlot.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ private void setStack(ItemStack stack, boolean validateStack) {
138138
} else {
139139
//Throws a RuntimeException as IItemHandlerModifiable specifies is allowed when something unexpected happens
140140
// As setStack is more meant to be used as an internal method
141-
throw new RuntimeException("Invalid stack for slot: " + stack + " " + stack.getComponents());
141+
throw new RuntimeException("Invalid stack for slot: " + stack + " " + stack.getComponentsPatch());
142142
}
143143
onContentsChanged();
144144
}

0 commit comments

Comments
 (0)