Move some methods out of the Equine<?> interface that don't need to be there

This commit is contained in:
Sollace 2022-12-25 16:01:12 +01:00
parent 33dc4cbb34
commit e9070b87b0
16 changed files with 57 additions and 130 deletions

View file

@ -9,8 +9,7 @@ import org.jetbrains.annotations.Nullable;
import com.minelittlepony.unicopia.*; import com.minelittlepony.unicopia.*;
import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType; import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType;
import com.minelittlepony.unicopia.block.data.ModificationType; import com.minelittlepony.unicopia.block.data.ModificationType;
import com.minelittlepony.unicopia.entity.Physics; import com.minelittlepony.unicopia.entity.*;
import com.minelittlepony.unicopia.entity.PonyContainer;
import com.minelittlepony.unicopia.particle.ParticleSource; import com.minelittlepony.unicopia.particle.ParticleSource;
import com.minelittlepony.unicopia.util.SoundEmitter; import com.minelittlepony.unicopia.util.SoundEmitter;
import com.minelittlepony.unicopia.util.VecHelper; import com.minelittlepony.unicopia.util.VecHelper;
@ -120,9 +119,6 @@ public interface Caster<E extends Entity> extends
return Optional.of((Caster<?>)entity); return Optional.of((Caster<?>)entity);
} }
return PonyContainer.of(entity) return Equine.<Entity, Caster<?>>of(entity, c -> c instanceof Caster<?>);
.map(PonyContainer::get)
.filter(c -> c instanceof Caster<?>)
.map(c -> (Caster<?>)c);
} }
} }

View file

@ -142,6 +142,11 @@ public class Creature extends Living<LivingEntity> implements WeaklyOwned.Mutabl
builder.add(UEntityAttributes.ENTITY_GRAVTY_MODIFIER); builder.add(UEntityAttributes.ENTITY_GRAVTY_MODIFIER);
} }
@Override
public boolean beforeUpdate() {
return false;
}
@Override @Override
public void tick() { public void tick() {
super.tick(); super.tick();

View file

@ -1,78 +1,45 @@
package com.minelittlepony.unicopia.entity; package com.minelittlepony.unicopia.entity;
import java.util.Optional; import java.util.Optional;
import java.util.function.Predicate;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import com.minelittlepony.unicopia.EntityConvertable; import com.minelittlepony.unicopia.EntityConvertable;
import com.minelittlepony.unicopia.Race; import com.minelittlepony.unicopia.Race;
import com.minelittlepony.unicopia.ability.magic.Caster;
import com.minelittlepony.unicopia.projectile.ProjectileImpactListener; import com.minelittlepony.unicopia.projectile.ProjectileImpactListener;
import com.minelittlepony.unicopia.util.NbtSerialisable; import com.minelittlepony.unicopia.util.NbtSerialisable;
import com.minelittlepony.unicopia.util.Tickable; import com.minelittlepony.unicopia.util.Tickable;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.projectile.ProjectileEntity;
public interface Equine<T extends Entity> extends NbtSerialisable, Tickable, ProjectileImpactListener, EntityConvertable<T> { public interface Equine<T extends Entity> extends NbtSerialisable, Tickable, ProjectileImpactListener, EntityConvertable<T> {
Race getSpecies();
Physics getPhysics(); Physics getPhysics();
Race getSpecies();
void setSpecies(Race race); void setSpecies(Race race);
/**
* Gets the last magical entity to attack us.
*/
@Nullable
Caster<?> getAttacker();
/**
* Returns true if this player is fully invisible.
* Held items and other effects will be hidden as well.
*/
default boolean isInvisible() {
return false;
}
/**
* Sets whether this player should be invisible.
*/
default void setInvisible(boolean invisible) {
}
/** /**
* Called at the beginning of an update cycle. * Called at the beginning of an update cycle.
*/ */
default boolean beforeUpdate() { boolean beforeUpdate();
return false;
@SuppressWarnings("unchecked")
static <E extends Entity, T extends Equine<? extends E>> Optional<T> of(@Nullable E entity) {
return entity instanceof Container ? Optional.of(((Container<T>)entity).get()) : Optional.empty();
} }
/** @SuppressWarnings("unchecked")
* Event triggered when this entity is hit by a projectile. static <E extends Entity, T> Optional<T> of(@Nullable E entity, Predicate<Object> typeCheck) {
*/ return entity instanceof Container
@Override ? (Optional<T>)Optional.of((Object)((Container<?>)entity).get()).filter(typeCheck)
default boolean onProjectileImpact(ProjectileEntity projectile) { : Optional.empty();
return false;
} }
/** interface Container<T extends Equine<?>> {
* Called when this entity jumps Equine<?> create();
*/
default void onJump() {
} T get();
/**
* Called when an entity is harmed.
*/
default Optional<Boolean> onDamage(DamageSource source, float amount) {
return Optional.empty();
}
static <E extends Entity, T extends Equine<E>> Optional<T> of(@Nullable E entity) {
return PonyContainer.<E, T>of(entity).map(PonyContainer::get);
} }
} }

View file

@ -1,6 +1,6 @@
package com.minelittlepony.unicopia.entity; package com.minelittlepony.unicopia.entity;
public interface IItemEntity extends PonyContainer<ItemImpl> { public interface IItemEntity extends Equine.Container<ItemImpl> {
int getAge(); int getAge();

View file

@ -2,10 +2,7 @@ package com.minelittlepony.unicopia.entity;
import java.util.List; import java.util.List;
import org.jetbrains.annotations.Nullable;
import com.minelittlepony.unicopia.*; import com.minelittlepony.unicopia.*;
import com.minelittlepony.unicopia.ability.magic.Caster;
import com.minelittlepony.unicopia.item.enchantment.UEnchantments; import com.minelittlepony.unicopia.item.enchantment.UEnchantments;
import com.minelittlepony.unicopia.util.VecHelper; import com.minelittlepony.unicopia.util.VecHelper;
@ -15,6 +12,7 @@ import net.minecraft.entity.data.DataTracker;
import net.minecraft.entity.data.TrackedData; import net.minecraft.entity.data.TrackedData;
import net.minecraft.entity.data.TrackedDataHandlerRegistry; import net.minecraft.entity.data.TrackedDataHandlerRegistry;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.ProjectileEntity;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtCompound;
@ -42,10 +40,9 @@ public class ItemImpl implements Equine<ItemEntity> {
owner.getDataTracker().startTracking(ITEM_RACE, Race.REGISTRY.getId(Race.HUMAN).toString()); owner.getDataTracker().startTracking(ITEM_RACE, Race.REGISTRY.getId(Race.HUMAN).toString());
} }
@Nullable
@Override @Override
public Caster<?> getAttacker() { public boolean onProjectileImpact(ProjectileEntity projectile) {
return null; return false;
} }
@Override @Override
@ -132,12 +129,10 @@ public class ItemImpl implements Equine<ItemEntity> {
return physics; return physics;
} }
@Override
public Race getSpecies() { public Race getSpecies() {
return Race.fromName(entity.getDataTracker().get(ITEM_RACE), Race.HUMAN); return Race.fromName(entity.getDataTracker().get(ITEM_RACE), Race.HUMAN);
} }
@Override
public void setSpecies(Race race) { public void setSpecies(Race race) {
entity.getDataTracker().set(ITEM_RACE, Race.REGISTRY.getId(race).toString()); entity.getDataTracker().set(ITEM_RACE, Race.REGISTRY.getId(race).toString());
} }

View file

@ -53,6 +53,8 @@ public abstract class Living<T extends LivingEntity> implements Equine<T>, Caste
@Nullable @Nullable
private Runnable landEvent; private Runnable landEvent;
private boolean invisible = false;
@Nullable @Nullable
private Caster<?> attacker; private Caster<?> attacker;
@ -69,6 +71,14 @@ public abstract class Living<T extends LivingEntity> implements Equine<T>, Caste
entity.getDataTracker().startTracking(effect, new NbtCompound()); entity.getDataTracker().startTracking(effect, new NbtCompound());
} }
public boolean isInvisible() {
return invisible && SpellPredicate.IS_DISGUISE.isOn(this);
}
public void setInvisible(boolean invisible) {
this.invisible = invisible;
}
public void waitForFall(Runnable action) { public void waitForFall(Runnable action) {
if (entity.isOnGround()) { if (entity.isOnGround()) {
action.run(); action.run();
@ -166,7 +176,6 @@ public abstract class Living<T extends LivingEntity> implements Equine<T>, Caste
} }
} }
@Override
public void onJump() { public void onJump() {
if (getPhysics().isGravityNegative()) { if (getPhysics().isGravityNegative()) {
entity.setVelocity(entity.getVelocity().multiply(1, -1, 1)); entity.setVelocity(entity.getVelocity().multiply(1, -1, 1));
@ -174,12 +183,10 @@ public abstract class Living<T extends LivingEntity> implements Equine<T>, Caste
} }
@Nullable @Nullable
@Override
public final Caster<?> getAttacker() { public final Caster<?> getAttacker() {
return attacker; return attacker;
} }
@Override
public Optional<Boolean> onDamage(DamageSource source, float amount) { public Optional<Boolean> onDamage(DamageSource source, float amount) {
if (source == DamageSource.LIGHTNING_BOLT && (invinsibilityTicks > 0 || tryCaptureLightning())) { if (source == DamageSource.LIGHTNING_BOLT && (invinsibilityTicks > 0 || tryCaptureLightning())) {
@ -280,7 +287,7 @@ public abstract class Living<T extends LivingEntity> implements Equine<T>, Caste
} }
public static Optional<Living<?>> getOrEmpty(Entity entity) { public static Optional<Living<?>> getOrEmpty(Entity entity) {
return PonyContainer.of(entity).map(PonyContainer::get).map(a -> a instanceof Living ? (Living<?>)a : null); return Equine.of(entity, a -> a instanceof Living);
} }
public static Living<?> living(Entity entity) { public static Living<?> living(Entity entity) {

View file

@ -1,22 +0,0 @@
package com.minelittlepony.unicopia.entity;
import java.util.Optional;
import org.jetbrains.annotations.Nullable;
import net.minecraft.entity.Entity;
public interface PonyContainer<T extends Equine<?>> {
Equine<?> create();
T get();
@SuppressWarnings("unchecked")
static <E extends Entity, T extends Equine<?>> Optional<PonyContainer<T>> of(@Nullable Entity entity) {
if (entity instanceof PonyContainer) {
return Optional.of(((PonyContainer<T>)entity));
}
return Optional.empty();
}
}

View file

@ -1,12 +1,9 @@
package com.minelittlepony.unicopia.entity.duck; package com.minelittlepony.unicopia.entity.duck;
import com.minelittlepony.unicopia.entity.Equine;
import com.minelittlepony.unicopia.entity.PonyContainer;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.util.Hand; import net.minecraft.util.Hand;
public interface LivingEntityDuck extends PonyContainer<Equine<?>> { public interface LivingEntityDuck {
void updateItemUsage(Hand hand, ItemStack stack, int time); void updateItemUsage(Hand hand, ItemStack stack, int time);
boolean isJumping(); boolean isJumping();

View file

@ -102,8 +102,6 @@ public class Pony extends Living<PlayerEntity> implements Copyable<Pony>, Update
@Nullable @Nullable
private Race clientPreferredRace; private Race clientPreferredRace;
private boolean invisible = false;
private int ticksInSun; private int ticksInSun;
private boolean hasShades; private boolean hasShades;
private int ticksSunImmunity = INITIAL_SUN_IMMUNITY; private int ticksSunImmunity = INITIAL_SUN_IMMUNITY;
@ -225,20 +223,10 @@ public class Pony extends Living<PlayerEntity> implements Copyable<Pony>, Update
return corruption; return corruption;
} }
@Override
public boolean isInvisible() {
return invisible && SpellPredicate.IS_DISGUISE.isOn(this);
}
public boolean isSpeciesPersisted() { public boolean isSpeciesPersisted() {
return speciesPersisted; return speciesPersisted;
} }
@Override
public void setInvisible(boolean invisible) {
this.invisible = invisible;
}
public boolean isSunImmune() { public boolean isSunImmune() {
return ticksSunImmunity > 0; return ticksSunImmunity > 0;
} }
@ -673,7 +661,7 @@ public class Pony extends Living<PlayerEntity> implements Copyable<Pony>, Update
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Nullable @Nullable
public static Pony of(@Nullable PlayerEntity player) { public static Pony of(@Nullable PlayerEntity player) {
return player == null ? null : ((PonyContainer<Pony>)player).get(); return player == null ? null : ((Container<Pony>)player).get();
} }
public static Stream<Pony> stream(Stream<Entity> entities) { public static Stream<Pony> stream(Stream<Entity> entities) {
@ -681,9 +669,7 @@ public class Pony extends Living<PlayerEntity> implements Copyable<Pony>, Update
} }
public static Optional<Pony> of(Entity entity) { public static Optional<Pony> of(Entity entity) {
return entity instanceof PlayerEntity return Equine.<Entity, Pony>of(entity, a -> a instanceof Pony);
? PonyContainer.of(entity).map(a -> (Pony)a.get())
: Optional.empty();
} }
public static boolean equal(GameProfile one, GameProfile two) { public static boolean equal(GameProfile one, GameProfile two) {

View file

@ -129,7 +129,7 @@ public class CrystalHeartItem extends Item implements FloatingArtefactEntity.Art
} }
}); });
VecHelper.findInRange(entity, entity.world, entity.getPos(), 20, i -> { VecHelper.findInRange(entity, entity.world, entity.getPos(), 20, i -> {
return i instanceof ItemEntity ie && isFillable(ie.getStack()) && PonyContainer.of(i).filter(p -> p.get().getSpecies() == Race.CHANGELING).isPresent(); return i instanceof ItemEntity ie && isFillable(ie.getStack()) && Equine.of(i).filter(p -> p.getSpecies() == Race.CHANGELING).isPresent();
}).forEach(i -> containers.add((ItemEntity)i)); }).forEach(i -> containers.add((ItemEntity)i));
int demand = outputs.size() + containers.stream().mapToInt(i -> i.getStack().getCount()).sum(); int demand = outputs.size() + containers.stream().mapToInt(i -> i.getStack().getCount()).sum();

View file

@ -7,7 +7,7 @@ import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import com.minelittlepony.unicopia.entity.Equine; import com.minelittlepony.unicopia.entity.Living;
import com.minelittlepony.unicopia.entity.player.Pony; import com.minelittlepony.unicopia.entity.player.Pony;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
@ -24,7 +24,7 @@ abstract class MixinDamageSource {
if (self.isFromFalling()) { if (self.isFromFalling()) {
info.setReturnValue(new DamageSource(self.name + ".pegasus").getDeathMessage(entity)); info.setReturnValue(new DamageSource(self.name + ".pegasus").getDeathMessage(entity));
} else { } else {
Equine.of(entity).map(Equine::getAttacker).ifPresent(attacker -> { Living.getOrEmpty(entity).map(Living::getAttacker).ifPresent(attacker -> {
Entity prime = entity.getPrimeAdversary(); Entity prime = entity.getPrimeAdversary();
if (prime != null && !attacker.isOwnedBy(prime)) { if (prime != null && !attacker.isOwnedBy(prime)) {
info.setReturnValue(Text.translatable("death.attack.generic.and_also", info.getReturnValue(), attacker.asEntity().getDisplayName())); info.setReturnValue(Text.translatable("death.attack.generic.and_also", info.getReturnValue(), attacker.asEntity().getDisplayName()));

View file

@ -6,8 +6,7 @@ import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import com.minelittlepony.unicopia.entity.IItemEntity; import com.minelittlepony.unicopia.entity.*;
import com.minelittlepony.unicopia.entity.ItemImpl;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.ItemEntity; import net.minecraft.entity.ItemEntity;
@ -21,14 +20,14 @@ abstract class MixinItemEntity extends Entity implements IItemEntity {
private MixinItemEntity() { super(null, null); } private MixinItemEntity() { super(null, null); }
@Override @Override
public ItemImpl create() { public Equine<?> create() {
return new ItemImpl((ItemEntity)(Object)this); return new ItemImpl((ItemEntity)(Object)this);
} }
@Override @Override
public ItemImpl get() { public ItemImpl get() {
if (caster == null) { if (caster == null) {
caster = create(); caster = (ItemImpl)create();
} }
return caster; return caster;
} }

View file

@ -29,7 +29,7 @@ import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
@Mixin(LivingEntity.class) @Mixin(LivingEntity.class)
abstract class MixinLivingEntity extends Entity implements LivingEntityDuck { abstract class MixinLivingEntity extends Entity implements LivingEntityDuck, Equine.Container<Living<?>> {
@Shadow @Shadow
protected ItemStack activeItemStack; protected ItemStack activeItemStack;
@Shadow @Shadow
@ -51,11 +51,11 @@ abstract class MixinLivingEntity extends Entity implements LivingEntityDuck {
} }
@Override @Override
public Equine<?> get() { public Living<?> get() {
if (caster == null) { if (caster == null) {
caster = create(); caster = create();
} }
return caster; return (Living<?>)caster;
} }
@Override @Override

View file

@ -17,7 +17,7 @@ import net.minecraft.entity.mob.MobEntity;
import net.minecraft.world.World; import net.minecraft.world.World;
@Mixin(MobEntity.class) @Mixin(MobEntity.class)
abstract class MixinMobEntity extends LivingEntity implements PonyContainer<Equine<?>> { abstract class MixinMobEntity extends LivingEntity implements Equine.Container<Creature> {
private MixinMobEntity() { super(null, null); } private MixinMobEntity() { super(null, null); }
@Shadow @Shadow
@ -27,14 +27,12 @@ abstract class MixinMobEntity extends LivingEntity implements PonyContainer<Equi
@Inject(method = "<init>(Lnet/minecraft/entity/EntityType;Lnet/minecraft/world/World;)V", at = @At("RETURN")) @Inject(method = "<init>(Lnet/minecraft/entity/EntityType;Lnet/minecraft/world/World;)V", at = @At("RETURN"))
private void init(EntityType<? extends MobEntity> entityType, World world, CallbackInfo info) { private void init(EntityType<? extends MobEntity> entityType, World world, CallbackInfo info) {
((Creature)get()).initAi(goalSelector, targetSelector); get().initAi(goalSelector, targetSelector);
} }
@Inject(method = "tickNewAi", at = @At("HEAD")) @Inject(method = "tickNewAi", at = @At("HEAD"))
public void beforeTickAi(CallbackInfo into) { public void beforeTickAi(CallbackInfo into) {
Equine<?> eq = Equine.of(this).orElse(null); if (get().getPhysics().isGravityNegative()) {
if (eq instanceof Living<?> && eq.getPhysics().isGravityNegative()) {
((RotatedView)world).pushRotation((int)getY()); ((RotatedView)world).pushRotation((int)getY());
} }
} }

View file

@ -7,7 +7,6 @@ import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyVariable; import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import com.minelittlepony.unicopia.entity.PonyContainer;
import com.minelittlepony.unicopia.entity.duck.PlayerEntityDuck; import com.minelittlepony.unicopia.entity.duck.PlayerEntityDuck;
import com.minelittlepony.unicopia.entity.Equine; import com.minelittlepony.unicopia.entity.Equine;
import com.minelittlepony.unicopia.entity.player.Pony; import com.minelittlepony.unicopia.entity.player.Pony;
@ -27,7 +26,7 @@ import net.minecraft.util.Unit;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
@Mixin(PlayerEntity.class) @Mixin(PlayerEntity.class)
abstract class MixinPlayerEntity extends LivingEntity implements PonyContainer<Pony>, PlayerEntityDuck { abstract class MixinPlayerEntity extends LivingEntity implements Equine.Container<Pony>, PlayerEntityDuck {
private MixinPlayerEntity() { super(null, null); } private MixinPlayerEntity() { super(null, null); }
@Override @Override
@Invoker("updateCapeAngles") @Invoker("updateCapeAngles")

View file

@ -6,7 +6,7 @@ import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import com.minelittlepony.unicopia.entity.PonyContainer; import com.minelittlepony.unicopia.entity.Equine;
import com.minelittlepony.unicopia.entity.duck.ServerPlayerEntityDuck; import com.minelittlepony.unicopia.entity.duck.ServerPlayerEntityDuck;
import com.minelittlepony.unicopia.entity.player.Pony; import com.minelittlepony.unicopia.entity.player.Pony;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
@ -14,7 +14,7 @@ import net.minecraft.screen.ScreenHandlerListener;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
@Mixin(ServerPlayerEntity.class) @Mixin(ServerPlayerEntity.class)
abstract class MixinServerPlayerEntity extends PlayerEntity implements ScreenHandlerListener, PonyContainer<Pony>, ServerPlayerEntityDuck { abstract class MixinServerPlayerEntity extends PlayerEntity implements ScreenHandlerListener, Equine.Container<Pony>, ServerPlayerEntityDuck {
MixinServerPlayerEntity() {super(null, null, 0, null);} MixinServerPlayerEntity() {super(null, null, 0, null);}
@Override @Override
@ -24,7 +24,7 @@ abstract class MixinServerPlayerEntity extends PlayerEntity implements ScreenHan
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Inject(method = "copyFrom(Lnet/minecraft/server/network/ServerPlayerEntity;Z)V", at = @At("HEAD")) @Inject(method = "copyFrom(Lnet/minecraft/server/network/ServerPlayerEntity;Z)V", at = @At("HEAD"))
private void onCopyFrom(ServerPlayerEntity oldPlayer, boolean alive, CallbackInfo info) { private void onCopyFrom(ServerPlayerEntity oldPlayer, boolean alive, CallbackInfo info) {
get().copyFrom(((PonyContainer<Pony>)oldPlayer).get()); get().copyFrom(((Equine.Container<Pony>)oldPlayer).get());
} }
} }