2020-06-26 11:44:47 +02:00
|
|
|
package com.minelittlepony.unicopia;
|
2018-09-16 00:45:44 +02:00
|
|
|
|
2022-01-01 21:08:50 +01:00
|
|
|
import java.util.Optional;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
|
|
|
|
import net.minecraft.entity.Entity;
|
|
|
|
|
2019-03-12 21:10:48 +01:00
|
|
|
/**
|
2022-01-01 21:08:50 +01:00
|
|
|
* Interface for things that can be owned by an entity.
|
|
|
|
* <p>
|
|
|
|
* Ownership is retained so long as the owner is still active. If the owner leaves or dies, the link is broken.
|
2019-03-12 21:10:48 +01:00
|
|
|
*
|
|
|
|
* @param <E> The type of object that owns us.
|
|
|
|
*/
|
2022-01-01 21:08:50 +01:00
|
|
|
public interface Owned<E extends Entity> {
|
|
|
|
/**
|
|
|
|
* Gets the owner that holds this object.
|
|
|
|
*/
|
|
|
|
@Nullable
|
|
|
|
E getMaster();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the unique entity id of the entity that holds this object.
|
|
|
|
* <p>
|
|
|
|
* Since {@link Owned#getMaster()} will only return if the owner is loaded, use this to perform checks
|
|
|
|
* in the owner's absence.
|
|
|
|
*/
|
|
|
|
default Optional<UUID> getMasterId() {
|
|
|
|
return Optional.of(getMaster()).map(Entity::getUuid);
|
|
|
|
}
|
2018-09-16 00:45:44 +02:00
|
|
|
|
2019-03-12 21:10:48 +01:00
|
|
|
/**
|
|
|
|
* Updates the owner of this object.
|
|
|
|
*/
|
2022-01-01 21:08:50 +01:00
|
|
|
void setMaster(@Nullable E owner);
|
2018-09-16 00:45:44 +02:00
|
|
|
|
2019-03-12 21:10:48 +01:00
|
|
|
/**
|
2022-01-01 21:08:50 +01:00
|
|
|
* Updated the owner of this object to be the same as another.
|
|
|
|
*
|
|
|
|
* @param sibling
|
2019-03-12 21:10:48 +01:00
|
|
|
*/
|
2022-01-01 21:08:50 +01:00
|
|
|
default void setMaster(Owned<? extends E> sibling) {
|
|
|
|
setMaster(sibling.getMaster());
|
|
|
|
}
|
|
|
|
|
|
|
|
default boolean isOwnedBy(@Nullable Object owner) {
|
|
|
|
return owner instanceof Entity e && getMasterId().isPresent() && e.getUuid().equals(getMasterId().get());
|
|
|
|
}
|
|
|
|
|
|
|
|
default boolean hasCommonOwner(Owned<?> sibling) {
|
|
|
|
return getMasterId().isPresent() && getMasterId().equals(sibling.getMasterId());
|
|
|
|
}
|
2018-09-16 00:45:44 +02:00
|
|
|
}
|