Skip to content

Commit 1d19819

Browse files
committed
Provide registry access to more places where we use codecs
1 parent 8939e04 commit 1d19819

File tree

7 files changed

+20
-22
lines changed

7 files changed

+20
-22
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ yamlops_version=1.2.0
3737

3838
#Mod dependencies
3939
cc_tweaked_version=1.111.0
40-
crafttweaker_version=19.0.7
40+
crafttweaker_version=19.0.8
4141
curios_version=8.0.0-beta.5+1.20.6
4242
emi_version=1.1.6
4343
jade_api_id=5288706

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

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

3-
import com.mojang.brigadier.exceptions.CommandSyntaxException;
43
import java.util.ArrayList;
54
import java.util.Collection;
65
import java.util.Collections;
@@ -44,8 +43,6 @@
4443
import net.minecraft.core.Vec3i;
4544
import net.minecraft.core.component.DataComponentPatch;
4645
import net.minecraft.core.registries.BuiltInRegistries;
47-
import net.minecraft.nbt.NbtOps;
48-
import net.minecraft.nbt.NbtUtils;
4946
import net.minecraft.resources.ResourceLocation;
5047
import net.minecraft.world.item.Item;
5148
import net.minecraft.world.item.ItemStack;
@@ -189,13 +186,8 @@ public ItemStack getItemStack(int param) throws ComputerException {
189186
int count = SpecialConverters.getIntFromRaw(map.get(SerializationConstants.COUNT));
190187
String components = (String) map.get(SerializationConstants.COMPONENTS);
191188
if (components != null) {
192-
try {
193-
DataComponentPatch dataComponents = DataComponentPatch.CODEC.decode(NbtOps.INSTANCE, NbtUtils.snbtToStructure(components))
194-
.getOrThrow(ComputerException::new).getFirst();
195-
return new ItemStack(item.builtInRegistryHolder(), count, dataComponents);
196-
} catch (CommandSyntaxException ex) {
197-
throw new ComputerException("Invalid SNBT: " + ex.getMessage());
198-
}
189+
DataComponentPatch dataComponents = SpecialConverters.unwrapComponents(components);;
190+
return new ItemStack(item.builtInRegistryHolder(), count, dataComponents);
199191
}
200192
return new ItemStack(item, count);
201193
} catch (ClassCastException ex) {

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,8 @@ private static ItemStack tryCreateFilterItem(@Nullable String rawName, @Nullable
5656
return ItemStack.EMPTY;
5757
}
5858
if (rawComponents != null) {
59-
try {
60-
DataComponentPatch dataComponents = DataComponentPatch.CODEC.decode(NbtOps.INSTANCE, NbtUtils.snbtToStructure(rawComponents))
61-
.getOrThrow(ComputerException::new).getFirst();
62-
return new ItemStack(item.builtInRegistryHolder(), 1, dataComponents);
63-
} catch (CommandSyntaxException ex) {
64-
throw new ComputerException("Invalid SNBT: " + ex.getMessage());
65-
}
59+
DataComponentPatch dataComponents = unwrapComponents(rawComponents);
60+
return new ItemStack(item.builtInRegistryHolder(), 1, dataComponents);
6661
}
6762
return new ItemStack(item);
6863
}
@@ -234,6 +229,17 @@ static Map<String, Object> wrapStack(ResourceLocation name, String sizeKey, int
234229
}
235230

236231
static String wrapComponents(@NotNull DataComponentPatch components) {
232+
//TODO - 1.20.5: Make this and unwrapComponents take a HolderLookup.Provider
237233
return NbtUtils.structureToSnbt((CompoundTag) DataComponentPatch.CODEC.encodeStart(NbtOps.INSTANCE, components).getOrThrow());
238234
}
235+
236+
static DataComponentPatch unwrapComponents(@NotNull String rawComponents) throws ComputerException {
237+
CompoundTag nbt;
238+
try {
239+
nbt = NbtUtils.snbtToStructure(rawComponents);
240+
} catch (CommandSyntaxException ex) {
241+
throw new ComputerException("Invalid SNBT: " + ex.getMessage());
242+
}
243+
return DataComponentPatch.CODEC.decode(NbtOps.INSTANCE, nbt).getOrThrow(ComputerException::new).getFirst();
244+
}
239245
}

src/main/java/mekanism/common/integration/crafttweaker/ingredient/CrTGasStackIngredient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public static GasStackIngredient from(Many<KnownTag<Gas>> gasTag) {
127127
@ZenCodeType.Method
128128
@ZenCodeType.Caster(implicit = true)
129129
public static IData asIData(GasStackIngredient _this) {
130-
return IngredientCreatorAccess.gasStack().codec().encodeStart(IDataOps.INSTANCE, _this).getOrThrow();
130+
return IngredientCreatorAccess.gasStack().codec().encodeStart(IDataOps.INSTANCE.withRegistryAccess(), _this).getOrThrow();
131131
}
132132

133133
/**

src/main/java/mekanism/common/integration/crafttweaker/ingredient/CrTInfusionStackIngredient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public static InfusionStackIngredient from(Many<KnownTag<InfuseType>> infuseType
127127
@ZenCodeType.Method
128128
@ZenCodeType.Caster(implicit = true)
129129
public static IData asIData(InfusionStackIngredient _this) {
130-
return IngredientCreatorAccess.infusionStack().codec().encodeStart(IDataOps.INSTANCE, _this).getOrThrow();
130+
return IngredientCreatorAccess.infusionStack().codec().encodeStart(IDataOps.INSTANCE.withRegistryAccess(), _this).getOrThrow();
131131
}
132132

133133
/**

src/main/java/mekanism/common/integration/crafttweaker/ingredient/CrTPigmentStackIngredient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public static PigmentStackIngredient from(Many<KnownTag<Pigment>> pigmentTag) {
127127
@ZenCodeType.Method
128128
@ZenCodeType.Caster(implicit = true)
129129
public static IData asIData(PigmentStackIngredient _this) {
130-
return IngredientCreatorAccess.pigmentStack().codec().encodeStart(IDataOps.INSTANCE, _this).getOrThrow();
130+
return IngredientCreatorAccess.pigmentStack().codec().encodeStart(IDataOps.INSTANCE.withRegistryAccess(), _this).getOrThrow();
131131
}
132132

133133
/**

src/main/java/mekanism/common/integration/crafttweaker/ingredient/CrTSlurryStackIngredient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public static SlurryStackIngredient from(Many<KnownTag<Slurry>> slurryTag) {
127127
@ZenCodeType.Method
128128
@ZenCodeType.Caster(implicit = true)
129129
public static IData asIData(SlurryStackIngredient _this) {
130-
return IngredientCreatorAccess.slurryStack().codec().encodeStart(IDataOps.INSTANCE, _this).getOrThrow();
130+
return IngredientCreatorAccess.slurryStack().codec().encodeStart(IDataOps.INSTANCE.withRegistryAccess(), _this).getOrThrow();
131131
}
132132

133133
/**

0 commit comments

Comments
 (0)