package com.minelittlepony.unicopia; import java.util.Optional; import java.util.UUID; import org.jetbrains.annotations.Nullable; import com.minelittlepony.unicopia.entity.EntityReference; import com.minelittlepony.unicopia.entity.EntityReference.EntityValues; import net.minecraft.entity.Entity; /** * Interface for things that can be weakly owned (by an entity). * Ownership links for these kinds of owned instances are preserved even if the owner is not present to oversee it. * * @param The type of object that owns us. */ public interface WeaklyOwned extends Owned, WorldConvertable { EntityReference getMasterReference(); @Nullable @Override default E getMaster() { return getMasterReference().get(asWorld()); } @Override default Optional getMasterId() { return getMasterReference().getTarget().map(EntityValues::uuid); } interface Mutable extends WeaklyOwned, Owned.Mutable { @Override EntityReference getMasterReference(); /** * Updated the owner of this object to be the same as another. * * @param sibling */ @Override @SuppressWarnings("unchecked") default void setMaster(Owned sibling) { if (sibling instanceof WeaklyOwned w) { getMasterReference().copyFrom(w.getMasterReference()); } else { setMaster(sibling.getMaster()); } } @Override default void setMaster(E master) { getMasterReference().set(master); } } }