Skip to content

Commit

Permalink
Fix save format for effect in area effect block (#591)
Browse files Browse the repository at this point in the history
The registry int ids are not necessarily consistent between play
sessions or save files, which makes it a bad idea to use it for
serialization (with the potential exception of network serialization).

The area effect block was doing this with its effect, which is bad for
both save files and structure templates.
  • Loading branch information
kirderf1 authored Apr 6, 2024
1 parent 58e0347 commit 9c62bf8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
- Minestuck fences will now connect with vanilla fences appropriately
- All walls will now connect with each other properly
- Fixed name typos for some chess castle blocks
- Area effect blocks now save their effect as a string id instead of an int id

### Removed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.LivingEntity;
Expand All @@ -19,6 +21,7 @@
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
import net.minecraftforge.registries.ForgeRegistries;

import javax.annotation.Nonnull;
import java.util.Objects;
Expand Down Expand Up @@ -148,7 +151,11 @@ public void load(CompoundTag compound)
{
super.load(compound);

MobEffect effectRead = MobEffect.byId(compound.getInt("effect"));
MobEffect effectRead;
if(compound.contains("effect", Tag.TAG_STRING))
effectRead = ForgeRegistries.MOB_EFFECTS.getValue(new ResourceLocation(compound.getString("effect")));
else // backwards-compatibility with Minestuck 1.20.1-1.11.2.1 and earlier
effectRead = MobEffect.byId(compound.getInt("effect"));
if(effectRead != null)
effect = effectRead;

Expand All @@ -170,7 +177,7 @@ public void saveAdditional(CompoundTag compound)
{
super.saveAdditional(compound);

compound.putInt("effect", MobEffect.getId(getEffect()));
compound.putString("effect", String.valueOf(ForgeRegistries.MOB_EFFECTS.getKey(this.getEffect())));
compound.putInt("effectAmplifier", effectAmplifier);

compound.putInt("minAreaOffsetX", minAreaOffset.getX());
Expand Down

0 comments on commit 9c62bf8

Please sign in to comment.