Fixed bulb duplicating arms when reloading a world

This commit is contained in:
Sollace 2024-01-18 16:24:02 +00:00
parent 0c26827790
commit dde64153dd
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB

View file

@ -10,11 +10,13 @@ import java.util.stream.Collectors;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import com.minelittlepony.unicopia.USounds; import com.minelittlepony.unicopia.USounds;
import com.minelittlepony.unicopia.entity.EntityReference;
import com.minelittlepony.unicopia.entity.player.Pony; import com.minelittlepony.unicopia.entity.player.Pony;
import com.minelittlepony.unicopia.util.NbtSerialisable; import com.minelittlepony.unicopia.util.NbtSerialisable;
import com.minelittlepony.unicopia.util.VecHelper; import com.minelittlepony.unicopia.util.VecHelper;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityDimensions; import net.minecraft.entity.EntityDimensions;
import net.minecraft.entity.EntityPose; import net.minecraft.entity.EntityPose;
import net.minecraft.entity.EntityType; import net.minecraft.entity.EntityType;
@ -34,7 +36,6 @@ import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtList; import net.minecraft.nbt.NbtList;
import net.minecraft.particle.ParticleTypes; import net.minecraft.particle.ParticleTypes;
import net.minecraft.predicate.entity.EntityPredicates; import net.minecraft.predicate.entity.EntityPredicates;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundEvent; import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents; import net.minecraft.sound.SoundEvents;
import net.minecraft.util.ActionResult; import net.minecraft.util.ActionResult;
@ -58,7 +59,7 @@ public class IgnominiousBulbEntity extends MobEntity {
); );
@Nullable @Nullable
private Map<BlockPos, TentacleEntity> tentacles; private Map<BlockPos, EntityReference<TentacleEntity>> tentacles;
private int prevAge; private int prevAge;
private int happyTicks; private int happyTicks;
@ -146,7 +147,9 @@ public class IgnominiousBulbEntity extends MobEntity {
Vec3d change = new Vec3d(x, y, z).subtract(getPos()); Vec3d change = new Vec3d(x, y, z).subtract(getPos());
super.setPosition(x, y, z); super.setPosition(x, y, z);
getTentacles().values().forEach(tentacle -> { getTentacles().values().forEach(tentacle -> {
tentacle.setPosition(tentacle.getPos().add(change)); tentacle.ifPresent(getWorld(), t -> {
t.setPosition(t.getPos().add(change));
});
}); });
} }
@ -170,9 +173,11 @@ public class IgnominiousBulbEntity extends MobEntity {
}); });
} }
for (TentacleEntity tentacle : tentacles.values()) { for (EntityReference<TentacleEntity> tentacle : tentacles.values()) {
if (getWorld().random.nextInt(isAngry() ? 12 : 1200) == 0) { if (getWorld().random.nextInt(isAngry() ? 12 : 1200) == 0) {
tentacle.addActiveTicks(120); tentacle.ifPresent(getWorld(), t -> {
t.addActiveTicks(120);
});
} }
} }
LivingEntity target = getAttacker(); LivingEntity target = getAttacker();
@ -191,6 +196,7 @@ public class IgnominiousBulbEntity extends MobEntity {
tentacles.values() tentacles.values()
.stream() .stream()
.flatMap(tentacle -> tentacle.getOrEmpty(getWorld()).stream())
.sorted(Comparator.comparing(a -> a.distanceTo(target))) .sorted(Comparator.comparing(a -> a.distanceTo(target)))
.limit(2) .limit(2)
.forEach(tentacle -> { .forEach(tentacle -> {
@ -224,27 +230,29 @@ public class IgnominiousBulbEntity extends MobEntity {
} }
} }
private Map<BlockPos, TentacleEntity> getTentacles() { private Map<BlockPos, EntityReference<TentacleEntity>> getTentacles() {
if (tentacles == null) { if (tentacles == null) {
tentacles = getWorld().getEntitiesByClass(TentacleEntity.class, this.getBoundingBox().expand(5, 0, 5), EntityPredicates.VALID_ENTITY) tentacles = getWorld().getEntitiesByClass(TentacleEntity.class, getBoundingBox().expand(5, 0, 5), EntityPredicates.VALID_ENTITY)
.stream() .stream()
.collect(Collectors.toMap(TentacleEntity::getBlockPos, tentacle -> { .collect(Collectors.toMap(TentacleEntity::getBlockPos, tentacle -> {
tentacle.setBulb(this); tentacle.setBulb(this);
return tentacle; return new EntityReference<>(tentacle);
})); }));
} }
return tentacles; return tentacles;
} }
private TentacleEntity updateTentacle(BlockPos pos, @Nullable TentacleEntity tentacle) { private EntityReference<TentacleEntity> updateTentacle(BlockPos pos, @Nullable EntityReference<TentacleEntity> tentacle) {
if (tentacle == null || tentacle.isRemoved()) { if (tentacle == null || tentacle.getOrEmpty(getWorld()).filter(Entity::isAlive).isEmpty()) {
tentacle = new TentacleEntity(getWorld(), pos); var created = new TentacleEntity(getWorld(), pos);
tentacle.updatePositionAndAngles(pos.getX() + 0.5, pos.getY(), pos.getZ() + 0.5, getWorld().random.nextFloat() * 360, 0); created.updatePositionAndAngles(pos.getX() + 0.5, pos.getY(), pos.getZ() + 0.5, getWorld().random.nextFloat() * 360, 0);
tentacle.setBulb(this); created.setBulb(this);
if (getWorld().isTopSolid(pos.down(), this)) { if (getWorld().isTopSolid(pos.down(), this)) {
getWorld().spawnEntity(tentacle); getWorld().spawnEntity(created);
} }
return new EntityReference<>(created);
} }
return tentacle; return tentacle;
} }
@ -277,7 +285,9 @@ public class IgnominiousBulbEntity extends MobEntity {
@Override @Override
public void remove(RemovalReason reason) { public void remove(RemovalReason reason) {
super.remove(reason); super.remove(reason);
getTentacles().values().forEach(tentacle -> tentacle.remove(reason)); getTentacles().values().forEach(tentacle -> {
tentacle.ifPresent(getWorld(), t -> t.remove(reason));
});
} }
@Override @Override
@ -319,7 +329,7 @@ public class IgnominiousBulbEntity extends MobEntity {
getTentacles().forEach((pos, tentacle) -> { getTentacles().forEach((pos, tentacle) -> {
var compound = new NbtCompound(); var compound = new NbtCompound();
compound.put("pos", NbtSerialisable.BLOCK_POS.write(pos)); compound.put("pos", NbtSerialisable.BLOCK_POS.write(pos));
compound.putUuid("uuid", tentacle.getUuid()); compound.put("target", tentacle.toNBT());
tentacles.add(compound); tentacles.add(compound);
}); });
nbt.put("tentacles", tentacles); nbt.put("tentacles", tentacles);
@ -332,13 +342,10 @@ public class IgnominiousBulbEntity extends MobEntity {
setAge(nbt.getInt("age")); setAge(nbt.getInt("age"));
if (!getWorld().isClient) { if (!getWorld().isClient) {
if (nbt.contains("tentacles", NbtElement.LIST_TYPE)) { if (nbt.contains("tentacles", NbtElement.LIST_TYPE)) {
var tentacles = new HashMap<BlockPos, TentacleEntity>(); var tentacles = new HashMap<BlockPos, EntityReference<TentacleEntity>>();
nbt.getList("tentacles", NbtElement.COMPOUND_TYPE).forEach(tag -> { nbt.getList("tentacles", NbtElement.COMPOUND_TYPE).forEach(tag -> {
var compound = (NbtCompound)tag; var compound = (NbtCompound)tag;
if (((ServerWorld)getWorld()).getEntity(compound.getUuid("uuid")) instanceof TentacleEntity tentacle) { tentacles.put(NbtSerialisable.BLOCK_POS.read(compound.getCompound("pos")), new EntityReference<>(compound.getCompound("target")));
tentacle.setBulb(this);
tentacles.put(NbtSerialisable.BLOCK_POS.read(compound.getCompound("pos")), tentacle);
}
}); });
this.tentacles = tentacles; this.tentacles = tentacles;
} }