Fixed altar detection for the alicorn amulet and make it more efficient by saving the centers of active altars

This commit is contained in:
Sollace 2023-09-02 21:23:54 +01:00
parent 543adefd6d
commit cadad49aab
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB
3 changed files with 35 additions and 14 deletions

View file

@ -15,7 +15,7 @@ import com.minelittlepony.unicopia.entity.player.*;
import com.minelittlepony.unicopia.particle.FollowingParticleEffect;
import com.minelittlepony.unicopia.particle.ParticleUtils;
import com.minelittlepony.unicopia.particle.UParticles;
import com.minelittlepony.unicopia.server.world.Altar;
import com.minelittlepony.unicopia.server.world.UnicopiaWorldProperties;
import com.minelittlepony.unicopia.trinkets.TrinketsDelegate;
import com.minelittlepony.unicopia.util.VecHelper;
@ -41,6 +41,7 @@ import net.minecraft.particle.ParticleEffect;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.predicate.entity.EntityPredicates;
import net.minecraft.registry.tag.DamageTypeTags;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundCategory;
import net.minecraft.text.Text;
import net.minecraft.util.*;
@ -192,7 +193,8 @@ public class AlicornAmuletItem extends AmuletItem implements ItemTracker.Trackab
if (entity instanceof PlayerEntity) {
if (entity.isOnFire() && world.getBlockState(entity.getBlockPos().up()).isOf(Blocks.SOUL_FIRE)) {
if (Altar.of(entity.getBlockPos().up()).isValid(world)) {
if (UnicopiaWorldProperties.forWorld((ServerWorld)world).isActiveAltar(entity.getBlockPos())
|| UnicopiaWorldProperties.forWorld((ServerWorld)world).isActiveAltar(entity.getBlockPos().up())) {
if (living.asEntity().getHealth() < 2) {
entity.setFireTicks(0);
world.removeBlock(entity.getBlockPos().up(), false);

View file

@ -1,14 +1,10 @@
package com.minelittlepony.unicopia.server.world;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Stream;
import org.jetbrains.annotations.Nullable;
import com.minelittlepony.unicopia.entity.mob.FloatingArtefactEntity;
@ -26,6 +22,7 @@ import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.predicate.entity.EntityPredicates;
import net.minecraft.registry.tag.BlockTags;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.BlockRotation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
@ -54,10 +51,6 @@ public record Altar(BlockPos origin, Set<BlockPos> pillars) {
private static final BlockPos PILLAR_A = new BlockPos(INNER_RADIUS, 0, PILLAR_OFFSET_FROM_CENTER);
private static final BlockPos PILLAR_B = new BlockPos(INNER_RADIUS, 0, -PILLAR_OFFSET_FROM_CENTER);
private static final List<BlockPos> PILLAR_OFFSETS = Arrays.stream(BlockRotation.values()).flatMap(rotation -> {
return Stream.of(PILLAR_A.rotate(rotation), PILLAR_B.rotate(rotation));
}).toList();
public static Optional<Altar> locateAltar(World world, BlockPos startingPoint) {
BlockPos.Mutable mutable = startingPoint.mutableCopy();
@ -103,10 +96,6 @@ public record Altar(BlockPos origin, Set<BlockPos> pillars) {
return Optional.empty();
}
public static Altar of(BlockPos center) {
return new Altar(center, new HashSet<>(PILLAR_OFFSETS.stream().map(p -> p.add(center).up()).toList()));
}
private static boolean checkSlab(World world, BlockPos pos) {
return !PosHelper.any(pos, p -> !isObsidian(world, p), HORIZONTALS);
}
@ -154,9 +143,15 @@ public record Altar(BlockPos origin, Set<BlockPos> pillars) {
removeExisting(null, world, pillar);
world.spawnEntity(artefact);
});
if (!world.isClient) {
UnicopiaWorldProperties.forWorld((ServerWorld)world).addAltar(origin());
}
}
public void tearDown(@Nullable Entity except, World world) {
if (!world.isClient) {
UnicopiaWorldProperties.forWorld((ServerWorld)world).removeAltar(origin());
}
pillars.forEach(pillar -> removeExisting(except, world, pillar));
}

View file

@ -1,11 +1,17 @@
package com.minelittlepony.unicopia.server.world;
import java.util.HashSet;
import java.util.Set;
import com.minelittlepony.unicopia.Race;
import com.minelittlepony.unicopia.network.Channel;
import com.minelittlepony.unicopia.network.MsgSkyAngle;
import com.minelittlepony.unicopia.util.NbtSerialisable;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.PersistentState;
@ -16,6 +22,8 @@ public class UnicopiaWorldProperties extends PersistentState {
private Race defaultRace = Race.UNSET;
private float tangentalSkyAngle;
private final Set<BlockPos> activeAltarPositions = new HashSet<>();
public UnicopiaWorldProperties(ServerWorld world) {
this.world = world;
}
@ -24,6 +32,7 @@ public class UnicopiaWorldProperties extends PersistentState {
this(world);
defaultRace = Race.fromName(tag.getString("defaultRace"), Race.HUMAN);
tangentalSkyAngle = tag.getFloat("tangentalSkyAngle");
activeAltarPositions.addAll(NbtSerialisable.BLOCK_POS.readAll(tag.getList("activeAltars", NbtElement.COMPOUND_TYPE)).toList());
}
public Race getDefaultRace() {
@ -46,10 +55,25 @@ public class UnicopiaWorldProperties extends PersistentState {
Channel.SERVER_SKY_ANGLE.sendToAllPlayers(new MsgSkyAngle(tangentalSkyAngle), world);
}
public void removeAltar(BlockPos center) {
activeAltarPositions.remove(center);
markDirty();
}
public void addAltar(BlockPos center) {
activeAltarPositions.add(center);
markDirty();
}
public boolean isActiveAltar(BlockPos center) {
return activeAltarPositions.contains(center);
}
@Override
public NbtCompound writeNbt(NbtCompound tag) {
tag.putString("defaultRace", Race.REGISTRY.getId(defaultRace).toString());
tag.putFloat("tangentalSkyAngle", tangentalSkyAngle);
tag.put("activeAltars", NbtSerialisable.BLOCK_POS.writeAll(activeAltarPositions));
return tag;
}