11package mekanism .common .integration .computer ;
22
33import com .mojang .brigadier .exceptions .CommandSyntaxException ;
4+ import com .mojang .serialization .DynamicOps ;
45import java .util .HashMap ;
56import java .util .Locale ;
67import java .util .Map ;
2627import net .minecraft .nbt .CompoundTag ;
2728import net .minecraft .nbt .NbtOps ;
2829import net .minecraft .nbt .NbtUtils ;
30+ import net .minecraft .nbt .Tag ;
2931import net .minecraft .resources .ResourceLocation ;
32+ import net .minecraft .server .MinecraftServer ;
3033import net .minecraft .world .item .Item ;
3134import net .minecraft .world .item .ItemStack ;
3235import net .minecraft .world .item .Items ;
36+ import net .neoforged .neoforge .server .ServerLifecycleHooks ;
3337import org .jetbrains .annotations .NotNull ;
3438import org .jetbrains .annotations .Nullable ;
3539
@@ -229,8 +233,7 @@ static Map<String, Object> wrapStack(ResourceLocation name, String sizeKey, int
229233 }
230234
231235 static String wrapComponents (@ NotNull DataComponentPatch components ) {
232- //TODO - 1.20.5: Make this and unwrapComponents take a HolderLookup.Provider
233- return NbtUtils .structureToSnbt ((CompoundTag ) DataComponentPatch .CODEC .encodeStart (NbtOps .INSTANCE , components ).getOrThrow ());
236+ return NbtUtils .structureToSnbt ((CompoundTag ) DataComponentPatch .CODEC .encodeStart (getRegistryNbtOps (), components ).getOrThrow ());
234237 }
235238
236239 static DataComponentPatch unwrapComponents (@ NotNull String rawComponents ) throws ComputerException {
@@ -240,6 +243,15 @@ static DataComponentPatch unwrapComponents(@NotNull String rawComponents) throws
240243 } catch (CommandSyntaxException ex ) {
241244 throw new ComputerException ("Invalid SNBT: " + ex .getMessage ());
242245 }
243- return DataComponentPatch .CODEC .decode (NbtOps .INSTANCE , nbt ).getOrThrow (ComputerException ::new ).getFirst ();
246+ return DataComponentPatch .CODEC .decode (getRegistryNbtOps (), nbt ).getOrThrow (ComputerException ::new ).getFirst ();
247+ }
248+
249+ private static DynamicOps <Tag > getRegistryNbtOps () {
250+ //TODO: Can we pass the registry access in from the tiles rather than having to look it up from the current server?
251+ MinecraftServer server = ServerLifecycleHooks .getCurrentServer ();
252+ if (server != null ) {
253+ return server .registryAccess ().createSerializationContext (NbtOps .INSTANCE );
254+ }
255+ return NbtOps .INSTANCE ;
244256 }
245257}
0 commit comments