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;
|
|
|
|
|
2024-01-26 16:57:36 +01:00
|
|
|
import com.minelittlepony.unicopia.item.FriendshipBraceletItem;
|
|
|
|
|
2022-01-01 21:08:50 +01:00
|
|
|
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();
|
|
|
|
|
2022-12-19 17:27:24 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2024-01-26 16:57:36 +01:00
|
|
|
Optional<UUID> getMasterId();
|
|
|
|
|
|
|
|
default boolean isOwnerOrFriend(Entity target) {
|
2024-02-06 16:26:33 +01:00
|
|
|
return isFriend(target) || isOwnerOrVehicle(target);
|
|
|
|
}
|
|
|
|
|
|
|
|
default boolean isFriend(Entity target) {
|
|
|
|
return FriendshipBraceletItem.isComrade(this, target);
|
2024-01-26 16:57:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
default boolean isOwnerOrVehicle(@Nullable Entity target) {
|
|
|
|
if (isOwnedBy(target)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Entity owner = getMaster();
|
|
|
|
return target != null && owner != null && owner.isConnectedThroughVehicle(target);
|
2022-12-19 17:27:24 +01:00
|
|
|
}
|
|
|
|
|
2022-01-01 21:08:50 +01:00
|
|
|
default boolean isOwnedBy(@Nullable Object owner) {
|
2024-01-26 16:57:36 +01:00
|
|
|
return owner instanceof Entity e && e.getUuid().equals(getMasterId().orElse(null));
|
2022-01-01 21:08:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
default boolean hasCommonOwner(Owned<?> sibling) {
|
2024-04-08 17:53:01 +02:00
|
|
|
return getMasterId().equals(sibling.getMasterId());
|
2022-01-01 21:08:50 +01:00
|
|
|
}
|
2022-12-19 20:45:02 +01:00
|
|
|
|
|
|
|
interface Mutable<E extends Entity> {
|
|
|
|
/**
|
|
|
|
* Updates the owner of this object.
|
|
|
|
*/
|
|
|
|
void setMaster(@Nullable E owner);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updated the owner of this object to be the same as another.
|
|
|
|
*
|
|
|
|
* @param sibling
|
|
|
|
*/
|
|
|
|
default void setMaster(Owned<? extends E> sibling) {
|
|
|
|
setMaster(sibling.getMaster());
|
|
|
|
}
|
|
|
|
}
|
2018-09-16 00:45:44 +02:00
|
|
|
}
|