From 42f00123d9ff91dbf15eb47d70bec3868466ac6f Mon Sep 17 00:00:00 2001 From: Sara Freimer Date: Fri, 3 May 2024 14:44:52 -0500 Subject: [PATCH] Ensure when we are dealing with things that might care about registry stuff we use registry ops --- .../java/mekanism/common/attachments/BlockData.java | 5 ++--- src/main/java/mekanism/common/content/gear/Module.java | 10 +++++++--- .../mekanism/common/content/gear/ModuleContainer.java | 9 +++++---- src/main/java/mekanism/common/entity/EntityRobit.java | 5 ++--- .../common/lib/radiation/RadiationManager.java | 8 ++++++-- .../mekanism/common/lib/radiation/RadiationSource.java | 10 ++++++---- .../common/tile/TileEntityModificationStation.java | 4 ++-- 7 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/main/java/mekanism/common/attachments/BlockData.java b/src/main/java/mekanism/common/attachments/BlockData.java index adc248b0944..5046828a33c 100644 --- a/src/main/java/mekanism/common/attachments/BlockData.java +++ b/src/main/java/mekanism/common/attachments/BlockData.java @@ -17,7 +17,6 @@ import net.minecraft.core.HolderLookup; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.nbt.CompoundTag; -import net.minecraft.nbt.NbtOps; import net.minecraft.network.chat.Component; import net.minecraft.network.codec.ByteBufCodecs; import net.minecraft.network.codec.StreamCodec; @@ -46,9 +45,9 @@ public record BlockData(BlockState blockState, @Nullable CompoundTag blockEntity ).apply(instance, (state, tag) -> new BlockData(state, tag.orElse(null)))); //TODO - 1.20.5: Test this and see if there is a proper stream codec for block states public static final StreamCodec STREAM_CODEC = StreamCodec.composite( - ByteBufCodecs.TRUSTED_COMPOUND_TAG, data -> (CompoundTag) BlockState.CODEC.encodeStart(NbtOps.INSTANCE, data.blockState()).getOrThrow(), + ByteBufCodecs.fromCodecTrusted(BlockState.CODEC), BlockData::blockState, ByteBufCodecs.optional(ByteBufCodecs.TRUSTED_COMPOUND_TAG), data -> Optional.ofNullable(data.blockEntityTag()), - (state, tag) -> new BlockData(BlockState.CODEC.parse(NbtOps.INSTANCE, state).result().orElseThrow(), tag.orElse(null)) + (state, tag) -> new BlockData(state, tag.orElse(null)) ); public BlockData(HolderLookup.Provider provider, BlockState state, @Nullable BlockEntity blockEntity) { diff --git a/src/main/java/mekanism/common/content/gear/Module.java b/src/main/java/mekanism/common/content/gear/Module.java index ab130968639..1d06d236095 100644 --- a/src/main/java/mekanism/common/content/gear/Module.java +++ b/src/main/java/mekanism/common/content/gear/Module.java @@ -28,12 +28,15 @@ import mekanism.common.util.MekanismUtils; import mekanism.common.util.StorageUtils; import net.minecraft.MethodsReturnNonnullByDefault; +import net.minecraft.core.HolderLookup; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.NbtOps; +import net.minecraft.nbt.Tag; import net.minecraft.network.RegistryFriendlyByteBuf; import net.minecraft.network.chat.Component; import net.minecraft.network.codec.ByteBufCodecs; import net.minecraft.network.codec.StreamCodec; +import net.minecraft.resources.RegistryOps; import net.minecraft.util.ExtraCodecs; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.player.Player; @@ -213,11 +216,12 @@ public boolean isEnabled() { return enabled; } - Module withReplacedInstallCount(int installed) { + Module withReplacedInstallCount(HolderLookup.Provider provider, int installed) { + RegistryOps registryOps = provider.createSerializationContext(NbtOps.INSTANCE); //TODO - 1.20.5: Re-evaluate this - CompoundTag tag = (CompoundTag) Module.CODEC.encodeStart(NbtOps.INSTANCE, this).getOrThrow(); + CompoundTag tag = (CompoundTag) Module.CODEC.encodeStart(registryOps, this).getOrThrow(); tag.putInt(NBTConstants.AMOUNT, installed); - return (Module) Module.CODEC.decode(NbtOps.INSTANCE, tag).getOrThrow().getFirst(); + return (Module) Module.CODEC.decode(registryOps, tag).getOrThrow().getFirst(); } Module withReplacedConfig(ModuleConfig config) { diff --git a/src/main/java/mekanism/common/content/gear/ModuleContainer.java b/src/main/java/mekanism/common/content/gear/ModuleContainer.java index 6a74ae85959..a0b68edc4ed 100644 --- a/src/main/java/mekanism/common/content/gear/ModuleContainer.java +++ b/src/main/java/mekanism/common/content/gear/ModuleContainer.java @@ -23,6 +23,7 @@ import mekanism.common.lib.codec.SequencedCollectionCodec; import mekanism.common.lib.collection.EmptySequencedMap; import mekanism.common.registries.MekanismDataComponents; +import net.minecraft.core.HolderLookup; import net.minecraft.network.RegistryFriendlyByteBuf; import net.minecraft.network.chat.Component; import net.minecraft.network.codec.ByteBufCodecs; @@ -254,7 +255,7 @@ public boolean canInstall(ItemStack stack, IModuleDataProvider typeProvider) * * @return number installed */ - public > int addModule(ItemStack stack, IModuleDataProvider typeProvider, int toInstall) { + public > int addModule(HolderLookup.Provider provider, ItemStack stack, IModuleDataProvider typeProvider, int toInstall) { ModuleData type = typeProvider.getModuleData(); Module module = get(type); boolean wasFirst = module == null; @@ -268,7 +269,7 @@ public > int addModule(ItemStack stack, IMo //Nothing to actually install because we are already at the max stack size return 0; } - module = module.withReplacedInstallCount(module.getInstalledCount() + toInstall); + module = module.withReplacedInstallCount(provider, module.getInstalledCount() + toInstall); } //Add the module to the list of tracked and known modules if necessary or replace the existing value SequencedMap, Module> copiedModules = new LinkedHashMap<>(typedModules); @@ -285,7 +286,7 @@ public > int addModule(ItemStack stack, IMo return toInstall; } - public > void removeModule(ItemStack stack, IModuleDataProvider typeProvider, + public > void removeModule(HolderLookup.Provider provider, ItemStack stack, IModuleDataProvider typeProvider, @Range(from = 1, to = Integer.MAX_VALUE) int toRemove) { ModuleData type = typeProvider.getModuleData(); Module module = get(type); @@ -309,7 +310,7 @@ public > void removeModule(ItemStack stack, } } } else {//update the module with the new installed count - module = module.withReplacedInstallCount(installed); + module = module.withReplacedInstallCount(provider, installed); copiedModules.put(type, module); //Update the level of any corresponding enchantment adjustedEnchantments = updateEnchantment(module, null); diff --git a/src/main/java/mekanism/common/entity/EntityRobit.java b/src/main/java/mekanism/common/entity/EntityRobit.java index 8df5229164b..b06c59ab1eb 100644 --- a/src/main/java/mekanism/common/entity/EntityRobit.java +++ b/src/main/java/mekanism/common/entity/EntityRobit.java @@ -129,7 +129,6 @@ import net.neoforged.neoforge.common.CommonHooks; import net.neoforged.neoforge.common.NeoForge; import net.neoforged.neoforge.common.util.ITeleporter; -import net.neoforged.neoforge.items.ItemHandlerHelper; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -511,7 +510,7 @@ public void addAdditionalSaveData(@NotNull CompoundTag nbtTags) { nbtTags.putBoolean(NBTConstants.FOLLOW, getFollowing()); nbtTags.putBoolean(NBTConstants.PICKUP_DROPS, getDropPickup()); if (homeLocation != null) { - Optional result = GlobalPos.CODEC.encodeStart(NbtOps.INSTANCE, homeLocation).result(); + Optional result = GlobalPos.CODEC.encodeStart(provider.createSerializationContext(NbtOps.INSTANCE), homeLocation).result(); //noinspection OptionalIsPresent - Capturing lambda if (result.isPresent()) { nbtTags.put(NBTConstants.HOME_LOCATION, result.get()); @@ -531,7 +530,7 @@ public void readAdditionalSaveData(@NotNull CompoundTag nbtTags) { NBTUtils.setEnumIfPresent(nbtTags, NBTConstants.SECURITY_MODE, SecurityMode.BY_ID, this::setSecurityMode); setFollowing(nbtTags.getBoolean(NBTConstants.FOLLOW)); setDropPickup(nbtTags.getBoolean(NBTConstants.PICKUP_DROPS)); - NBTUtils.setCompoundIfPresent(nbtTags, NBTConstants.HOME_LOCATION, home -> homeLocation = GlobalPos.CODEC.parse(NbtOps.INSTANCE, home).result().orElse(null)); + NBTUtils.setCompoundIfPresent(nbtTags, NBTConstants.HOME_LOCATION, home -> homeLocation = GlobalPos.CODEC.parse(provider.createSerializationContext(NbtOps.INSTANCE), home).result().orElse(null)); ContainerType.ITEM.readFrom(provider, nbtTags, getInventorySlots(null)); ContainerType.ENERGY.readFrom(provider, nbtTags, getEnergyContainers(null)); progress = nbtTags.getInt(NBTConstants.PROGRESS); diff --git a/src/main/java/mekanism/common/lib/radiation/RadiationManager.java b/src/main/java/mekanism/common/lib/radiation/RadiationManager.java index 3dde72850b9..2f6a5ee6e46 100644 --- a/src/main/java/mekanism/common/lib/radiation/RadiationManager.java +++ b/src/main/java/mekanism/common/lib/radiation/RadiationManager.java @@ -47,7 +47,9 @@ import net.minecraft.core.RegistryAccess; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.ListTag; +import net.minecraft.nbt.NbtOps; import net.minecraft.nbt.Tag; +import net.minecraft.resources.RegistryOps; import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.MinecraftServer; @@ -676,8 +678,9 @@ public void load(@NotNull CompoundTag nbtTags, @NotNull HolderLookup.Provider pr if (nbtTags.contains(NBTConstants.RADIATION_LIST, Tag.TAG_LIST)) { ListTag list = nbtTags.getList(NBTConstants.RADIATION_LIST, Tag.TAG_COMPOUND); loadedSources = new HashList<>(); + RegistryOps registryOps = provider.createSerializationContext(NbtOps.INSTANCE); for (Tag nbt : list) { - RadiationSource.load((CompoundTag) nbt).ifPresent(loadedSources::add); + RadiationSource.load(registryOps, (CompoundTag) nbt).ifPresent(loadedSources::add); } } else { loadedSources = Collections.emptySet(); @@ -703,9 +706,10 @@ public void load(@NotNull CompoundTag nbtTags, @NotNull HolderLookup.Provider pr @Override public CompoundTag save(@NotNull CompoundTag nbtTags, @NotNull HolderLookup.Provider provider) { if (manager != null && !manager.radiationTable.isEmpty()) { + RegistryOps registryOps = provider.createSerializationContext(NbtOps.INSTANCE); ListTag list = new ListTag(); for (RadiationSource source : manager.radiationTable.values()) { - list.add(source.write()); + list.add(source.write(registryOps)); } nbtTags.put(NBTConstants.RADIATION_LIST, list); } diff --git a/src/main/java/mekanism/common/lib/radiation/RadiationSource.java b/src/main/java/mekanism/common/lib/radiation/RadiationSource.java index 2cae9a2434b..b789741790c 100644 --- a/src/main/java/mekanism/common/lib/radiation/RadiationSource.java +++ b/src/main/java/mekanism/common/lib/radiation/RadiationSource.java @@ -8,6 +8,8 @@ import net.minecraft.core.GlobalPos; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.NbtOps; +import net.minecraft.nbt.Tag; +import net.minecraft.resources.RegistryOps; import org.jetbrains.annotations.NotNull; public class RadiationSource implements IRadiationSource { @@ -43,8 +45,8 @@ public boolean decay() { return magnitude < RadiationManager.MIN_MAGNITUDE; } - public static Optional load(CompoundTag tag) { - Optional result = GlobalPos.CODEC.parse(NbtOps.INSTANCE, tag).result(); + public static Optional load(RegistryOps registryOps, CompoundTag tag) { + Optional result = GlobalPos.CODEC.parse(registryOps, tag).result(); //noinspection OptionalIsPresent - Capturing lambda if (result.isPresent()) { return Optional.of(new RadiationSource(result.get(), tag.getDouble(NBTConstants.RADIATION))); @@ -52,8 +54,8 @@ public static Optional load(CompoundTag tag) { return Optional.empty(); } - public CompoundTag write() { - CompoundTag tag = (CompoundTag) GlobalPos.CODEC.encodeStart(NbtOps.INSTANCE, pos).result() + public CompoundTag write(RegistryOps registryOps) { + CompoundTag tag = (CompoundTag) GlobalPos.CODEC.encodeStart(registryOps, pos).result() .orElseGet(CompoundTag::new); tag.putDouble(NBTConstants.RADIATION, magnitude); return tag; diff --git a/src/main/java/mekanism/common/tile/TileEntityModificationStation.java b/src/main/java/mekanism/common/tile/TileEntityModificationStation.java index adae9338f65..d10f6aebe2f 100644 --- a/src/main/java/mekanism/common/tile/TileEntityModificationStation.java +++ b/src/main/java/mekanism/common/tile/TileEntityModificationStation.java @@ -100,7 +100,7 @@ protected boolean onUpdateServer() { clientEnergyUsed = energyContainer.extract(energyContainer.getEnergyPerTick(), Action.EXECUTE, AutomationType.INTERNAL); if (operatingTicks == ticksRequired) { operatingTicks = 0; - int added = container.addModule(stack, data, moduleSlot.getCount()); + int added = container.addModule(level.registryAccess(), stack, data, moduleSlot.getCount()); if (added > 0) { containerSlot.setStack(stack); MekanismUtils.logMismatchedStackSize(moduleSlot.shrinkStack(added, Action.EXECUTE), added); @@ -129,7 +129,7 @@ public void removeModule(Player player, ModuleData type, boolean removeAll) { if (installed > 0) { int toRemove = removeAll ? installed : 1; if (player.getInventory().add(type.getItemProvider().getItemStack(toRemove))) { - container.removeModule(stack, type, toRemove); + container.removeModule(player.level().registryAccess(), stack, type, toRemove); containerSlot.setStack(stack); } }