Passive mobs will now correctly flee from players that have been wearing the alicorn amulet at least 1 day

This commit is contained in:
Sollace 2022-12-09 21:19:45 +00:00
parent bc01a8a824
commit 65cf691242
6 changed files with 38 additions and 6 deletions

View file

@ -1,7 +1,7 @@
package com.minelittlepony.unicopia.client.render;
import com.minelittlepony.unicopia.Unicopia;
import com.minelittlepony.unicopia.item.UItems;
import com.minelittlepony.unicopia.entity.AmuletSelectors;
import net.minecraft.client.render.entity.feature.FeatureRendererContext;
import net.minecraft.client.render.entity.model.BipedEntityModel;
@ -18,7 +18,7 @@ public class IcarusWingsFeatureRenderer<E extends LivingEntity> extends WingsFea
@Override
protected boolean canRender(E entity) {
return !super.canRender(entity) && UItems.PEGASUS_AMULET.isApplicable(entity);
return !super.canRender(entity) && AmuletSelectors.PEGASUS_AMULET.test(entity);
}
@Override

View file

@ -8,4 +8,10 @@ import net.minecraft.entity.LivingEntity;
public interface AmuletSelectors {
Predicate<LivingEntity> ALICORN_AMULET = UItems.ALICORN_AMULET::isApplicable;
Predicate<LivingEntity> PEGASUS_AMULET = UItems.PEGASUS_AMULET::isApplicable;
Predicate<LivingEntity> ALICORN_AMULET_AFTER_1_DAYS = ALICORN_AMULET.and(ItemTracker.wearing(UItems.ALICORN_AMULET, ItemTracker.after(ItemTracker.DAYS)));
Predicate<LivingEntity> ALICORN_AMULET_AFTER_2_DAYS = ALICORN_AMULET.and(ItemTracker.wearing(UItems.ALICORN_AMULET, ItemTracker.after(2 * ItemTracker.DAYS)));
Predicate<LivingEntity> ALICORN_AMULET_AFTER_3_DAYS = ALICORN_AMULET.and(ItemTracker.wearing(UItems.ALICORN_AMULET, ItemTracker.after(3 * ItemTracker.DAYS)));
Predicate<LivingEntity> ALICORN_AMULET_AFTER_4_DAYS = ALICORN_AMULET.and(ItemTracker.wearing(UItems.ALICORN_AMULET, ItemTracker.after(3 * ItemTracker.DAYS)));
}

View file

@ -130,7 +130,7 @@ public class Creature extends Living<LivingEntity> implements WeaklyOwned<Living
goals.add(1, new FleeEntityGoal<>(mob, LivingEntity.class, 10, 1.5, 1.9, AmuletSelectors.ALICORN_AMULET));
}
if (entity instanceof PassiveEntity mob) {
goals.add(1, new FleeEntityGoal<>(mob, LivingEntity.class, 10, 1.1, 1.7, AmuletSelectors.ALICORN_AMULET));
goals.add(1, new FleeEntityGoal<>(mob, LivingEntity.class, 10, 1.1, 1.7, AmuletSelectors.ALICORN_AMULET_AFTER_1_DAYS));
}
}

View file

@ -1,10 +1,12 @@
package com.minelittlepony.unicopia.entity;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Stream;
import com.minelittlepony.unicopia.util.NbtSerialisable;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemConvertible;
import net.minecraft.item.ItemStack;
@ -20,6 +22,26 @@ public class ItemTracker implements NbtSerialisable {
private final Map<Trackable, Long> items = new HashMap<>();
public static Predicate<LivingEntity> wearing(Trackable charm, Predicate<Long> range) {
return e -> Living.getOrEmpty(e)
.map(Living::getArmour)
.map(a -> a.getTicks(charm))
.filter(range)
.isPresent();
}
public static Predicate<Long> between(long minTime, long maxTime) {
return before(maxTime).and(after(minTime));
}
public static Predicate<Long> before(long maxTime) {
return ticks -> ticks <= maxTime;
}
public static Predicate<Long> after(long maxTime) {
return ticks -> ticks <= maxTime;
}
public void update(Living<?> living, Stream<ItemStack> stacks) {
final Set<Trackable> found = new HashSet<>();
final Set<ItemStack> foundStacks = new HashSet<>();

View file

@ -273,8 +273,12 @@ public abstract class Living<T extends LivingEntity> implements Equine<T>, Caste
updateVelocity(entity);
}
public static Optional<Living<?>> getOrEmpty(LivingEntity entity) {
return PonyContainer.of(entity).map(a -> a instanceof Living ? (Living<?>)a.get() : null);
}
public static Living<?> living(Entity entity) {
return PonyContainer.of(entity).map(a -> a instanceof Living ? (Living<?>)a.get() : null).orElse(null);
return entity instanceof LivingEntity ? getOrEmpty((LivingEntity)entity).orElse(null) : null;
}
public static void updateVelocity(Entity entity) {

View file

@ -164,7 +164,7 @@ public class Pony extends Living<PlayerEntity> implements Transmittable, Copieab
@Override
public Race getSpecies() {
if (UItems.ALICORN_AMULET.isApplicable(entity)) {
if (AmuletSelectors.ALICORN_AMULET.test(entity)) {
return Race.ALICORN;
}
@ -504,7 +504,7 @@ public class Pony extends Living<PlayerEntity> implements Transmittable, Copieab
public Optional<Text> trySleep(BlockPos pos) {
if (UItems.ALICORN_AMULET.isApplicable(entity)) {
if (AmuletSelectors.ALICORN_AMULET.test(entity)) {
return Optional.of(Text.translatable("block.unicopia.bed.not_tired"));
}