001package fr.aumgn.dac2.game;
002
003import java.util.UUID;
004
005import org.bukkit.Bukkit;
006import org.bukkit.ChatColor;
007import org.bukkit.Location;
008import org.bukkit.World;
009import org.bukkit.entity.Player;
010
011import fr.aumgn.bukkitutils.geom.Direction;
012import fr.aumgn.bukkitutils.geom.Directions;
013import fr.aumgn.bukkitutils.geom.Vector;
014import fr.aumgn.bukkitutils.playerref.PlayerRef;
015import fr.aumgn.dac2.config.Color;
016import fr.aumgn.dac2.game.start.PlayerStartData;
017import fr.aumgn.dac2.shape.column.ColumnPattern;
018import fr.aumgn.dac2.shape.column.UniformPattern;
019
020/**
021 * Stores common data associated to player and
022 * implements common behaviors.
023 */
024public class GamePlayer {
025
026    public final PlayerRef playerId;
027    private final Color color;
028    private final UUID worldId;
029    private final Vector pos;
030    private final Direction dir;
031
032    private int index;
033
034    public GamePlayer(PlayerRef playerId, PlayerStartData joinData) {
035        this.playerId = playerId;
036        this.color = joinData.getColor();
037        this.worldId = joinData.getWorldId();
038        this.pos = joinData.getPosition();
039        this.dir = joinData.getDirection();
040        this.index = -1;
041    }
042
043    public String getDisplayName() {
044        String name;
045        Player player = playerId.getPlayer();
046        if (player == null) {
047            name = ChatColor.ITALIC + playerId.getName();
048        } else {
049            name = player.getDisplayName();
050        }
051
052        return color.chat + name + ChatColor.RESET;
053    }
054
055    public Color getColor() {
056        return color;
057    }
058
059    public ColumnPattern getColumnPattern() {
060        return new UniformPattern(color);
061    }
062
063    public void sendMessage(String message) {
064        Player player = playerId.getPlayer();
065        if (player != null) {
066            player.sendMessage(message);
067        }
068    }
069
070    public boolean isOnline() {
071        return playerId.isOnline();
072    }
073
074    public void teleport(World world, Vector pos) {
075        Player player = playerId.getPlayer();
076        if (player == null) {
077            return;
078        }
079;
080        teleport(world, pos, Directions.fromPlayer(player));
081    }
082
083    public void teleport(World world, Vector pos, Direction dir) {
084        teleport(pos.toLocation(world, dir));
085    }
086
087    public void teleport(Location location) {
088        Player player = playerId.getPlayer();
089        if (player != null) {
090            player.setFallDistance(0f);
091            player.teleport(location);
092        }
093    }
094
095    public Runnable delayedTeleport(final Location location) {
096        return new Runnable() {
097            @Override
098            public void run() {
099                teleport(location);
100            }
101        };
102    }
103
104    public void tpToStart() {
105        teleport(pos.toLocation(Bukkit.getWorld(worldId), dir));
106    }
107
108    public Runnable delayedTpToStart() {
109        return delayedTeleport(pos.toLocation(Bukkit.getWorld(worldId), dir));
110    }
111
112    public int getIndex() {
113        return index;
114    }
115
116    void setIndex(int index) {
117        this.index = index;
118    }
119}