mirror of
https://github.com/Sollace/Unicopia.git
synced 2024-11-27 15:17:59 +01:00
Use an entity reference to remember the owner of a magic projectile, and store the level and corruption in the reference so we can remember it when the player is offline
This commit is contained in:
parent
f5293546c5
commit
54c0acb757
1 changed files with 24 additions and 7 deletions
|
@ -7,7 +7,7 @@ import java.util.function.Function;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import com.minelittlepony.unicopia.Affinity;
|
import com.minelittlepony.unicopia.Affinity;
|
||||||
import com.minelittlepony.unicopia.Owned;
|
import com.minelittlepony.unicopia.WeaklyOwned;
|
||||||
import com.minelittlepony.unicopia.ability.magic.Affine;
|
import com.minelittlepony.unicopia.ability.magic.Affine;
|
||||||
import com.minelittlepony.unicopia.ability.magic.Caster;
|
import com.minelittlepony.unicopia.ability.magic.Caster;
|
||||||
import com.minelittlepony.unicopia.ability.magic.Levelled;
|
import com.minelittlepony.unicopia.ability.magic.Levelled;
|
||||||
|
@ -53,7 +53,7 @@ import net.minecraft.world.World;
|
||||||
*
|
*
|
||||||
* Can also carry a spell if needed.
|
* Can also carry a spell if needed.
|
||||||
*/
|
*/
|
||||||
public class MagicProjectileEntity extends ThrownItemEntity implements Caster<MagicProjectileEntity>, Owned.Mutable<LivingEntity> {
|
public class MagicProjectileEntity extends ThrownItemEntity implements Caster<MagicProjectileEntity>, WeaklyOwned.Mutable<LivingEntity> {
|
||||||
private static final TrackedData<Float> DAMAGE = DataTracker.registerData(MagicProjectileEntity.class, TrackedDataHandlerRegistry.FLOAT);
|
private static final TrackedData<Float> DAMAGE = DataTracker.registerData(MagicProjectileEntity.class, TrackedDataHandlerRegistry.FLOAT);
|
||||||
private static final TrackedData<Float> GRAVITY = DataTracker.registerData(MagicProjectileEntity.class, TrackedDataHandlerRegistry.FLOAT);
|
private static final TrackedData<Float> GRAVITY = DataTracker.registerData(MagicProjectileEntity.class, TrackedDataHandlerRegistry.FLOAT);
|
||||||
private static final TrackedData<Boolean> HYDROPHOBIC = DataTracker.registerData(MagicProjectileEntity.class, TrackedDataHandlerRegistry.BOOLEAN);
|
private static final TrackedData<Boolean> HYDROPHOBIC = DataTracker.registerData(MagicProjectileEntity.class, TrackedDataHandlerRegistry.BOOLEAN);
|
||||||
|
@ -66,6 +66,7 @@ public class MagicProjectileEntity extends ThrownItemEntity implements Caster<Ma
|
||||||
private final EntityPhysics<MagicProjectileEntity> physics = new EntityPhysics<>(this, GRAVITY, false);
|
private final EntityPhysics<MagicProjectileEntity> physics = new EntityPhysics<>(this, GRAVITY, false);
|
||||||
|
|
||||||
private final EntityReference<Entity> homingTarget = new EntityReference<>();
|
private final EntityReference<Entity> homingTarget = new EntityReference<>();
|
||||||
|
private final EntityReference<LivingEntity> owner = new EntityReference<>();
|
||||||
|
|
||||||
public MagicProjectileEntity(EntityType<MagicProjectileEntity> type, World world) {
|
public MagicProjectileEntity(EntityType<MagicProjectileEntity> type, World world) {
|
||||||
super(type, world);
|
super(type, world);
|
||||||
|
@ -107,10 +108,24 @@ public class MagicProjectileEntity extends ThrownItemEntity implements Caster<Ma
|
||||||
setOwner(owner);
|
setOwner(owner);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setOwner(@Nullable Entity entity) {
|
||||||
|
super.setOwner(entity);
|
||||||
|
if (entity instanceof LivingEntity l) {
|
||||||
|
this.owner.set(l);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Nullable
|
@Nullable
|
||||||
public LivingEntity getMaster() {
|
public Entity getOwner() {
|
||||||
return (LivingEntity)getOwner();
|
return getMaster();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public EntityReference<LivingEntity> getMasterReference() {
|
||||||
|
return owner;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHomingTarget(@Nullable Entity target) {
|
public void setHomingTarget(@Nullable Entity target) {
|
||||||
|
@ -119,12 +134,12 @@ public class MagicProjectileEntity extends ThrownItemEntity implements Caster<Ma
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LevelStore getLevel() {
|
public LevelStore getLevel() {
|
||||||
return Caster.of(getMaster()).map(Caster::getLevel).orElse(Levelled.EMPTY);
|
return getMasterReference().getTarget().map(target -> target.level()).orElse(Levelled.EMPTY);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LevelStore getCorruption() {
|
public LevelStore getCorruption() {
|
||||||
return Caster.of(getMaster()).map(Caster::getCorruption).orElse(Levelled.EMPTY);
|
return getMasterReference().getTarget().map(target -> target.corruption()).orElse(Levelled.EMPTY);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -169,7 +184,7 @@ public class MagicProjectileEntity extends ThrownItemEntity implements Caster<Ma
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void tick() {
|
public void tick() {
|
||||||
if (!getWorld().isClient() && !homingTarget.isPresent(getWorld())) {
|
if (!getWorld().isClient() && homingTarget.getOrEmpty(asWorld()).isEmpty()) {
|
||||||
if (getVelocity().length() < 0.1 || age > 90) {
|
if (getVelocity().length() < 0.1 || age > 90) {
|
||||||
discard();
|
discard();
|
||||||
}
|
}
|
||||||
|
@ -238,6 +253,7 @@ public class MagicProjectileEntity extends ThrownItemEntity implements Caster<Ma
|
||||||
super.readCustomDataFromNbt(compound);
|
super.readCustomDataFromNbt(compound);
|
||||||
physics.fromNBT(compound);
|
physics.fromNBT(compound);
|
||||||
homingTarget.fromNBT(compound.getCompound("homingTarget"));
|
homingTarget.fromNBT(compound.getCompound("homingTarget"));
|
||||||
|
owner.fromNBT(compound.getCompound("owner"));
|
||||||
if (compound.contains("effect")) {
|
if (compound.contains("effect")) {
|
||||||
getSpellSlot().put(Spell.readNbt(compound.getCompound("effect")));
|
getSpellSlot().put(Spell.readNbt(compound.getCompound("effect")));
|
||||||
}
|
}
|
||||||
|
@ -248,6 +264,7 @@ public class MagicProjectileEntity extends ThrownItemEntity implements Caster<Ma
|
||||||
super.writeCustomDataToNbt(compound);
|
super.writeCustomDataToNbt(compound);
|
||||||
physics.toNBT(compound);
|
physics.toNBT(compound);
|
||||||
compound.put("homingTarget", homingTarget.toNBT());
|
compound.put("homingTarget", homingTarget.toNBT());
|
||||||
|
compound.put("owner", owner.toNBT());
|
||||||
getSpellSlot().get(true).ifPresent(effect -> {
|
getSpellSlot().get(true).ifPresent(effect -> {
|
||||||
compound.put("effect", Spell.writeNbt(effect));
|
compound.put("effect", Spell.writeNbt(effect));
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue