2022-01-01 21:08:50 +01:00
|
|
|
package com.minelittlepony.unicopia;
|
|
|
|
|
|
|
|
import java.util.Optional;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
|
|
|
|
import com.minelittlepony.unicopia.entity.EntityReference;
|
|
|
|
|
|
|
|
import net.minecraft.entity.Entity;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 <E> The type of object that owns us.
|
|
|
|
*/
|
|
|
|
public interface WeaklyOwned<E extends Entity> extends Owned<E> {
|
|
|
|
|
|
|
|
EntityReference<E> getMasterReference();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updated the owner of this object to be the same as another.
|
|
|
|
*
|
|
|
|
* @param sibling
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
default void setMaster(WeaklyOwned<? extends E> sibling) {
|
|
|
|
if (sibling instanceof WeaklyOwned) {
|
|
|
|
getMasterReference().copyFrom(((WeaklyOwned<E>)sibling).getMasterReference());
|
|
|
|
} else {
|
|
|
|
setMaster(sibling.getMaster());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-14 09:11:00 +01:00
|
|
|
default World getWorld() {
|
|
|
|
return ((Entity)this).world;
|
|
|
|
}
|
|
|
|
|
2022-01-01 21:08:50 +01:00
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
default E getMaster() {
|
|
|
|
return getMasterReference().get(getWorld());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
default void setMaster(E master) {
|
|
|
|
getMasterReference().set(master);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
default Optional<UUID> getMasterId() {
|
|
|
|
return getMasterReference().getId();
|
|
|
|
}
|
|
|
|
}
|