119 lines
3.2 KiB
Java
119 lines
3.2 KiB
Java
package tc.oc.pgm.match;
|
|
|
|
import java.util.Objects;
|
|
import java.util.Optional;
|
|
import java.util.UUID;
|
|
import javax.annotation.Nullable;
|
|
|
|
import org.bukkit.EntityLocation;
|
|
import org.bukkit.entity.Player;
|
|
import tc.oc.api.docs.PlayerId;
|
|
import tc.oc.commons.bukkit.chat.NameStyle;
|
|
import tc.oc.commons.bukkit.chat.PlayerComponent;
|
|
import tc.oc.commons.bukkit.nick.Identity;
|
|
import tc.oc.commons.core.chat.Audience;
|
|
import tc.oc.commons.core.chat.ForwardingAudience;
|
|
import tc.oc.commons.core.chat.NullAudience;
|
|
import tc.oc.commons.core.util.Utils;
|
|
import tc.oc.pgm.filters.query.IPlayerQuery;
|
|
|
|
import static com.google.common.base.Preconditions.checkNotNull;
|
|
|
|
/**
|
|
* Represents a "snapshot" view of a {@link MatchPlayer}.
|
|
*/
|
|
public class MatchPlayerState extends MatchEntityState implements IPlayerQuery, ForwardingAudience {
|
|
protected final Identity identity;
|
|
protected final Party party;
|
|
|
|
public MatchPlayerState(Match match, Identity identity, UUID uuid, Party party, EntityLocation location) {
|
|
super(match, Player.class, uuid, location, null);
|
|
checkNotNull(identity, "player");
|
|
checkNotNull(party, "party");
|
|
|
|
this.identity = identity;
|
|
this.party = party;
|
|
}
|
|
|
|
public Identity getIdentity() {
|
|
return identity;
|
|
}
|
|
|
|
@Override
|
|
public PlayerComponent getStyledName(NameStyle style) {
|
|
return new PlayerComponent(getIdentity(), style);
|
|
}
|
|
|
|
@Override
|
|
public PlayerId getPlayerId() {
|
|
return this.identity.getPlayerId();
|
|
}
|
|
|
|
@Override
|
|
public Party getParty() {
|
|
return this.party;
|
|
}
|
|
|
|
/**
|
|
* Return the {@link MatchPlayer} referenced by this state, or null if the
|
|
* player has switched parties or disconnected.
|
|
*/
|
|
public @Nullable MatchPlayer getMatchPlayer() {
|
|
return this.party.getPlayer(this.getPlayerId());
|
|
}
|
|
|
|
@Override
|
|
public Optional<MatchPlayer> onlinePlayer() {
|
|
return party.player(getPlayerId());
|
|
}
|
|
|
|
@Override
|
|
public MatchPlayerState playerState() {
|
|
return this;
|
|
}
|
|
|
|
@Override
|
|
public Optional<ParticipantState> participantState() {
|
|
return Optional.empty(); // Subclass overrides this
|
|
}
|
|
|
|
public boolean isPlayer(MatchPlayer player) {
|
|
return this.getPlayerId().equals(player.getPlayerId());
|
|
}
|
|
|
|
public boolean isPlayer(MatchPlayerState player) {
|
|
return this.getPlayerId().equals(player.getPlayerId());
|
|
}
|
|
|
|
public boolean canInteract() {
|
|
return getParty().isParticipating();
|
|
}
|
|
|
|
@Override
|
|
public Optional<Audience> audience() {
|
|
return Optional.ofNullable(getMatchPlayer());
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return getClass().getSimpleName() +
|
|
"{match=" + getMatch() +
|
|
" party=" + getParty() +
|
|
" player=" + getPlayerId() +
|
|
"}";
|
|
}
|
|
|
|
@Override
|
|
final public boolean equals(Object obj) {
|
|
return Utils.equals(MatchPlayerState.class, this, obj, that ->
|
|
this.getParty().equals(that.getParty()) &&
|
|
this.getPlayerId().equals(that.getPlayerId())
|
|
);
|
|
}
|
|
|
|
@Override
|
|
public int hashCode() {
|
|
return Objects.hash(getParty(), getPlayerId());
|
|
}
|
|
}
|