Skip to content

Commit

Permalink
Ensure when we are dealing with things that might care about registry…
Browse files Browse the repository at this point in the history
… stuff we use registry ops
  • Loading branch information
pupnewfster committed May 3, 2024
1 parent 8057e0b commit 42f0012
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 21 deletions.
5 changes: 2 additions & 3 deletions src/main/java/mekanism/common/attachments/BlockData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ByteBuf, BlockData> 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) {
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/mekanism/common/content/gear/Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -213,11 +216,12 @@ public boolean isEnabled() {
return enabled;
}

Module<MODULE> withReplacedInstallCount(int installed) {
Module<MODULE> withReplacedInstallCount(HolderLookup.Provider provider, int installed) {
RegistryOps<Tag> 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>) Module.CODEC.decode(NbtOps.INSTANCE, tag).getOrThrow().getFirst();
return (Module<MODULE>) Module.CODEC.decode(registryOps, tag).getOrThrow().getFirst();
}

Module<MODULE> withReplacedConfig(ModuleConfig<?> config) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -254,7 +255,7 @@ public boolean canInstall(ItemStack stack, IModuleDataProvider<?> typeProvider)
*
* @return number installed
*/
public <MODULE extends ICustomModule<MODULE>> int addModule(ItemStack stack, IModuleDataProvider<MODULE> typeProvider, int toInstall) {
public <MODULE extends ICustomModule<MODULE>> int addModule(HolderLookup.Provider provider, ItemStack stack, IModuleDataProvider<MODULE> typeProvider, int toInstall) {
ModuleData<MODULE> type = typeProvider.getModuleData();
Module<MODULE> module = get(type);
boolean wasFirst = module == null;
Expand All @@ -268,7 +269,7 @@ public <MODULE extends ICustomModule<MODULE>> 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<ModuleData<?>, Module<?>> copiedModules = new LinkedHashMap<>(typedModules);
Expand All @@ -285,7 +286,7 @@ public <MODULE extends ICustomModule<MODULE>> int addModule(ItemStack stack, IMo
return toInstall;
}

public <MODULE extends ICustomModule<MODULE>> void removeModule(ItemStack stack, IModuleDataProvider<MODULE> typeProvider,
public <MODULE extends ICustomModule<MODULE>> void removeModule(HolderLookup.Provider provider, ItemStack stack, IModuleDataProvider<MODULE> typeProvider,
@Range(from = 1, to = Integer.MAX_VALUE) int toRemove) {
ModuleData<MODULE> type = typeProvider.getModuleData();
Module<MODULE> module = get(type);
Expand All @@ -309,7 +310,7 @@ public <MODULE extends ICustomModule<MODULE>> 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);
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/mekanism/common/entity/EntityRobit.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Tag> result = GlobalPos.CODEC.encodeStart(NbtOps.INSTANCE, homeLocation).result();
Optional<Tag> result = GlobalPos.CODEC.encodeStart(provider.createSerializationContext(NbtOps.INSTANCE), homeLocation).result();
//noinspection OptionalIsPresent - Capturing lambda
if (result.isPresent()) {
nbtTags.put(NBTConstants.HOME_LOCATION, result.get());
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Tag> 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();
Expand All @@ -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<Tag> 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);
}
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/mekanism/common/lib/radiation/RadiationSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -43,17 +45,17 @@ public boolean decay() {
return magnitude < RadiationManager.MIN_MAGNITUDE;
}

public static Optional<RadiationSource> load(CompoundTag tag) {
Optional<GlobalPos> result = GlobalPos.CODEC.parse(NbtOps.INSTANCE, tag).result();
public static Optional<RadiationSource> load(RegistryOps<Tag> registryOps, CompoundTag tag) {
Optional<GlobalPos> 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)));
}
return Optional.empty();
}

public CompoundTag write() {
CompoundTag tag = (CompoundTag) GlobalPos.CODEC.encodeStart(NbtOps.INSTANCE, pos).result()
public CompoundTag write(RegistryOps<Tag> registryOps) {
CompoundTag tag = (CompoundTag) GlobalPos.CODEC.encodeStart(registryOps, pos).result()
.orElseGet(CompoundTag::new);
tag.putDouble(NBTConstants.RADIATION, magnitude);
return tag;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 42f0012

Please sign in to comment.