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;
|
2023-08-06 19:26:17 +02:00
|
|
|
import com.minelittlepony.unicopia.entity.EntityReference.EntityValues;
|
2022-01-01 21:08:50 +01:00
|
|
|
|
|
|
|
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 <E> The type of object that owns us.
|
|
|
|
*/
|
2022-12-19 18:13:15 +01:00
|
|
|
public interface WeaklyOwned<E extends Entity> extends Owned<E>, WorldConvertable {
|
2022-01-01 21:08:50 +01:00
|
|
|
EntityReference<E> getMasterReference();
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
default E getMaster() {
|
2022-12-19 18:13:15 +01:00
|
|
|
return getMasterReference().get(asWorld());
|
2022-01-01 21:08:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
default Optional<UUID> getMasterId() {
|
2023-08-06 19:26:17 +02:00
|
|
|
return getMasterReference().getTarget().map(EntityValues::uuid);
|
2022-01-01 21:08:50 +01:00
|
|
|
}
|
2022-12-19 20:45:02 +01:00
|
|
|
|
|
|
|
interface Mutable<E extends Entity> extends WeaklyOwned<E>, Owned.Mutable<E> {
|
|
|
|
@Override
|
|
|
|
EntityReference<E> getMasterReference();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updated the owner of this object to be the same as another.
|
|
|
|
*
|
|
|
|
* @param sibling
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
default void setMaster(Owned<? extends E> sibling) {
|
|
|
|
if (sibling instanceof WeaklyOwned) {
|
|
|
|
getMasterReference().copyFrom(((WeaklyOwned<E>)sibling).getMasterReference());
|
|
|
|
} else {
|
|
|
|
setMaster(sibling.getMaster());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
default void setMaster(E master) {
|
|
|
|
getMasterReference().set(master);
|
|
|
|
}
|
|
|
|
}
|
2022-01-01 21:08:50 +01:00
|
|
|
}
|