Skip to content

Commit 42f0012

Browse files
committed
Ensure when we are dealing with things that might care about registry stuff we use registry ops
1 parent 8057e0b commit 42f0012

File tree

7 files changed

+30
-21
lines changed

7 files changed

+30
-21
lines changed

src/main/java/mekanism/common/attachments/BlockData.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import net.minecraft.core.HolderLookup;
1818
import net.minecraft.core.registries.BuiltInRegistries;
1919
import net.minecraft.nbt.CompoundTag;
20-
import net.minecraft.nbt.NbtOps;
2120
import net.minecraft.network.chat.Component;
2221
import net.minecraft.network.codec.ByteBufCodecs;
2322
import net.minecraft.network.codec.StreamCodec;
@@ -46,9 +45,9 @@ public record BlockData(BlockState blockState, @Nullable CompoundTag blockEntity
4645
).apply(instance, (state, tag) -> new BlockData(state, tag.orElse(null))));
4746
//TODO - 1.20.5: Test this and see if there is a proper stream codec for block states
4847
public static final StreamCodec<ByteBuf, BlockData> STREAM_CODEC = StreamCodec.composite(
49-
ByteBufCodecs.TRUSTED_COMPOUND_TAG, data -> (CompoundTag) BlockState.CODEC.encodeStart(NbtOps.INSTANCE, data.blockState()).getOrThrow(),
48+
ByteBufCodecs.fromCodecTrusted(BlockState.CODEC), BlockData::blockState,
5049
ByteBufCodecs.optional(ByteBufCodecs.TRUSTED_COMPOUND_TAG), data -> Optional.ofNullable(data.blockEntityTag()),
51-
(state, tag) -> new BlockData(BlockState.CODEC.parse(NbtOps.INSTANCE, state).result().orElseThrow(), tag.orElse(null))
50+
(state, tag) -> new BlockData(state, tag.orElse(null))
5251
);
5352

5453
public BlockData(HolderLookup.Provider provider, BlockState state, @Nullable BlockEntity blockEntity) {

src/main/java/mekanism/common/content/gear/Module.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,15 @@
2828
import mekanism.common.util.MekanismUtils;
2929
import mekanism.common.util.StorageUtils;
3030
import net.minecraft.MethodsReturnNonnullByDefault;
31+
import net.minecraft.core.HolderLookup;
3132
import net.minecraft.nbt.CompoundTag;
3233
import net.minecraft.nbt.NbtOps;
34+
import net.minecraft.nbt.Tag;
3335
import net.minecraft.network.RegistryFriendlyByteBuf;
3436
import net.minecraft.network.chat.Component;
3537
import net.minecraft.network.codec.ByteBufCodecs;
3638
import net.minecraft.network.codec.StreamCodec;
39+
import net.minecraft.resources.RegistryOps;
3740
import net.minecraft.util.ExtraCodecs;
3841
import net.minecraft.world.entity.LivingEntity;
3942
import net.minecraft.world.entity.player.Player;
@@ -213,11 +216,12 @@ public boolean isEnabled() {
213216
return enabled;
214217
}
215218

216-
Module<MODULE> withReplacedInstallCount(int installed) {
219+
Module<MODULE> withReplacedInstallCount(HolderLookup.Provider provider, int installed) {
220+
RegistryOps<Tag> registryOps = provider.createSerializationContext(NbtOps.INSTANCE);
217221
//TODO - 1.20.5: Re-evaluate this
218-
CompoundTag tag = (CompoundTag) Module.CODEC.encodeStart(NbtOps.INSTANCE, this).getOrThrow();
222+
CompoundTag tag = (CompoundTag) Module.CODEC.encodeStart(registryOps, this).getOrThrow();
219223
tag.putInt(NBTConstants.AMOUNT, installed);
220-
return (Module<MODULE>) Module.CODEC.decode(NbtOps.INSTANCE, tag).getOrThrow().getFirst();
224+
return (Module<MODULE>) Module.CODEC.decode(registryOps, tag).getOrThrow().getFirst();
221225
}
222226

223227
Module<MODULE> withReplacedConfig(ModuleConfig<?> config) {

src/main/java/mekanism/common/content/gear/ModuleContainer.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import mekanism.common.lib.codec.SequencedCollectionCodec;
2424
import mekanism.common.lib.collection.EmptySequencedMap;
2525
import mekanism.common.registries.MekanismDataComponents;
26+
import net.minecraft.core.HolderLookup;
2627
import net.minecraft.network.RegistryFriendlyByteBuf;
2728
import net.minecraft.network.chat.Component;
2829
import net.minecraft.network.codec.ByteBufCodecs;
@@ -254,7 +255,7 @@ public boolean canInstall(ItemStack stack, IModuleDataProvider<?> typeProvider)
254255
*
255256
* @return number installed
256257
*/
257-
public <MODULE extends ICustomModule<MODULE>> int addModule(ItemStack stack, IModuleDataProvider<MODULE> typeProvider, int toInstall) {
258+
public <MODULE extends ICustomModule<MODULE>> int addModule(HolderLookup.Provider provider, ItemStack stack, IModuleDataProvider<MODULE> typeProvider, int toInstall) {
258259
ModuleData<MODULE> type = typeProvider.getModuleData();
259260
Module<MODULE> module = get(type);
260261
boolean wasFirst = module == null;
@@ -268,7 +269,7 @@ public <MODULE extends ICustomModule<MODULE>> int addModule(ItemStack stack, IMo
268269
//Nothing to actually install because we are already at the max stack size
269270
return 0;
270271
}
271-
module = module.withReplacedInstallCount(module.getInstalledCount() + toInstall);
272+
module = module.withReplacedInstallCount(provider, module.getInstalledCount() + toInstall);
272273
}
273274
//Add the module to the list of tracked and known modules if necessary or replace the existing value
274275
SequencedMap<ModuleData<?>, Module<?>> copiedModules = new LinkedHashMap<>(typedModules);
@@ -285,7 +286,7 @@ public <MODULE extends ICustomModule<MODULE>> int addModule(ItemStack stack, IMo
285286
return toInstall;
286287
}
287288

288-
public <MODULE extends ICustomModule<MODULE>> void removeModule(ItemStack stack, IModuleDataProvider<MODULE> typeProvider,
289+
public <MODULE extends ICustomModule<MODULE>> void removeModule(HolderLookup.Provider provider, ItemStack stack, IModuleDataProvider<MODULE> typeProvider,
289290
@Range(from = 1, to = Integer.MAX_VALUE) int toRemove) {
290291
ModuleData<MODULE> type = typeProvider.getModuleData();
291292
Module<MODULE> module = get(type);
@@ -309,7 +310,7 @@ public <MODULE extends ICustomModule<MODULE>> void removeModule(ItemStack stack,
309310
}
310311
}
311312
} else {//update the module with the new installed count
312-
module = module.withReplacedInstallCount(installed);
313+
module = module.withReplacedInstallCount(provider, installed);
313314
copiedModules.put(type, module);
314315
//Update the level of any corresponding enchantment
315316
adjustedEnchantments = updateEnchantment(module, null);

src/main/java/mekanism/common/entity/EntityRobit.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@
129129
import net.neoforged.neoforge.common.CommonHooks;
130130
import net.neoforged.neoforge.common.NeoForge;
131131
import net.neoforged.neoforge.common.util.ITeleporter;
132-
import net.neoforged.neoforge.items.ItemHandlerHelper;
133132
import org.jetbrains.annotations.NotNull;
134133
import org.jetbrains.annotations.Nullable;
135134

@@ -511,7 +510,7 @@ public void addAdditionalSaveData(@NotNull CompoundTag nbtTags) {
511510
nbtTags.putBoolean(NBTConstants.FOLLOW, getFollowing());
512511
nbtTags.putBoolean(NBTConstants.PICKUP_DROPS, getDropPickup());
513512
if (homeLocation != null) {
514-
Optional<Tag> result = GlobalPos.CODEC.encodeStart(NbtOps.INSTANCE, homeLocation).result();
513+
Optional<Tag> result = GlobalPos.CODEC.encodeStart(provider.createSerializationContext(NbtOps.INSTANCE), homeLocation).result();
515514
//noinspection OptionalIsPresent - Capturing lambda
516515
if (result.isPresent()) {
517516
nbtTags.put(NBTConstants.HOME_LOCATION, result.get());
@@ -531,7 +530,7 @@ public void readAdditionalSaveData(@NotNull CompoundTag nbtTags) {
531530
NBTUtils.setEnumIfPresent(nbtTags, NBTConstants.SECURITY_MODE, SecurityMode.BY_ID, this::setSecurityMode);
532531
setFollowing(nbtTags.getBoolean(NBTConstants.FOLLOW));
533532
setDropPickup(nbtTags.getBoolean(NBTConstants.PICKUP_DROPS));
534-
NBTUtils.setCompoundIfPresent(nbtTags, NBTConstants.HOME_LOCATION, home -> homeLocation = GlobalPos.CODEC.parse(NbtOps.INSTANCE, home).result().orElse(null));
533+
NBTUtils.setCompoundIfPresent(nbtTags, NBTConstants.HOME_LOCATION, home -> homeLocation = GlobalPos.CODEC.parse(provider.createSerializationContext(NbtOps.INSTANCE), home).result().orElse(null));
535534
ContainerType.ITEM.readFrom(provider, nbtTags, getInventorySlots(null));
536535
ContainerType.ENERGY.readFrom(provider, nbtTags, getEnergyContainers(null));
537536
progress = nbtTags.getInt(NBTConstants.PROGRESS);

src/main/java/mekanism/common/lib/radiation/RadiationManager.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@
4747
import net.minecraft.core.RegistryAccess;
4848
import net.minecraft.nbt.CompoundTag;
4949
import net.minecraft.nbt.ListTag;
50+
import net.minecraft.nbt.NbtOps;
5051
import net.minecraft.nbt.Tag;
52+
import net.minecraft.resources.RegistryOps;
5153
import net.minecraft.resources.ResourceKey;
5254
import net.minecraft.resources.ResourceLocation;
5355
import net.minecraft.server.MinecraftServer;
@@ -676,8 +678,9 @@ public void load(@NotNull CompoundTag nbtTags, @NotNull HolderLookup.Provider pr
676678
if (nbtTags.contains(NBTConstants.RADIATION_LIST, Tag.TAG_LIST)) {
677679
ListTag list = nbtTags.getList(NBTConstants.RADIATION_LIST, Tag.TAG_COMPOUND);
678680
loadedSources = new HashList<>();
681+
RegistryOps<Tag> registryOps = provider.createSerializationContext(NbtOps.INSTANCE);
679682
for (Tag nbt : list) {
680-
RadiationSource.load((CompoundTag) nbt).ifPresent(loadedSources::add);
683+
RadiationSource.load(registryOps, (CompoundTag) nbt).ifPresent(loadedSources::add);
681684
}
682685
} else {
683686
loadedSources = Collections.emptySet();
@@ -703,9 +706,10 @@ public void load(@NotNull CompoundTag nbtTags, @NotNull HolderLookup.Provider pr
703706
@Override
704707
public CompoundTag save(@NotNull CompoundTag nbtTags, @NotNull HolderLookup.Provider provider) {
705708
if (manager != null && !manager.radiationTable.isEmpty()) {
709+
RegistryOps<Tag> registryOps = provider.createSerializationContext(NbtOps.INSTANCE);
706710
ListTag list = new ListTag();
707711
for (RadiationSource source : manager.radiationTable.values()) {
708-
list.add(source.write());
712+
list.add(source.write(registryOps));
709713
}
710714
nbtTags.put(NBTConstants.RADIATION_LIST, list);
711715
}

src/main/java/mekanism/common/lib/radiation/RadiationSource.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import net.minecraft.core.GlobalPos;
99
import net.minecraft.nbt.CompoundTag;
1010
import net.minecraft.nbt.NbtOps;
11+
import net.minecraft.nbt.Tag;
12+
import net.minecraft.resources.RegistryOps;
1113
import org.jetbrains.annotations.NotNull;
1214

1315
public class RadiationSource implements IRadiationSource {
@@ -43,17 +45,17 @@ public boolean decay() {
4345
return magnitude < RadiationManager.MIN_MAGNITUDE;
4446
}
4547

46-
public static Optional<RadiationSource> load(CompoundTag tag) {
47-
Optional<GlobalPos> result = GlobalPos.CODEC.parse(NbtOps.INSTANCE, tag).result();
48+
public static Optional<RadiationSource> load(RegistryOps<Tag> registryOps, CompoundTag tag) {
49+
Optional<GlobalPos> result = GlobalPos.CODEC.parse(registryOps, tag).result();
4850
//noinspection OptionalIsPresent - Capturing lambda
4951
if (result.isPresent()) {
5052
return Optional.of(new RadiationSource(result.get(), tag.getDouble(NBTConstants.RADIATION)));
5153
}
5254
return Optional.empty();
5355
}
5456

55-
public CompoundTag write() {
56-
CompoundTag tag = (CompoundTag) GlobalPos.CODEC.encodeStart(NbtOps.INSTANCE, pos).result()
57+
public CompoundTag write(RegistryOps<Tag> registryOps) {
58+
CompoundTag tag = (CompoundTag) GlobalPos.CODEC.encodeStart(registryOps, pos).result()
5759
.orElseGet(CompoundTag::new);
5860
tag.putDouble(NBTConstants.RADIATION, magnitude);
5961
return tag;

src/main/java/mekanism/common/tile/TileEntityModificationStation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ protected boolean onUpdateServer() {
100100
clientEnergyUsed = energyContainer.extract(energyContainer.getEnergyPerTick(), Action.EXECUTE, AutomationType.INTERNAL);
101101
if (operatingTicks == ticksRequired) {
102102
operatingTicks = 0;
103-
int added = container.addModule(stack, data, moduleSlot.getCount());
103+
int added = container.addModule(level.registryAccess(), stack, data, moduleSlot.getCount());
104104
if (added > 0) {
105105
containerSlot.setStack(stack);
106106
MekanismUtils.logMismatchedStackSize(moduleSlot.shrinkStack(added, Action.EXECUTE), added);
@@ -129,7 +129,7 @@ public void removeModule(Player player, ModuleData<?> type, boolean removeAll) {
129129
if (installed > 0) {
130130
int toRemove = removeAll ? installed : 1;
131131
if (player.getInventory().add(type.getItemProvider().getItemStack(toRemove))) {
132-
container.removeModule(stack, type, toRemove);
132+
container.removeModule(player.level().registryAccess(), stack, type, toRemove);
133133
containerSlot.setStack(stack);
134134
}
135135
}

0 commit comments

Comments
 (0)