Untitled

 avatar
unknown
java
2 years ago
6.0 kB
7
Indexable
public class PlayerData {

    private static HashMap<UUID, PlayerData> cachedPlayerData = new HashMap<>();
    private boolean hasPickedUp;
    private boolean hasArmorStand;
    private Material currentPickedUp;
    private ArmorStand followingArmorStand;
    private Player player;

    /**
     * @param player The Player to base the Profile on
     * @param currentPickedUp The Material, that the player currently picked up
     * @return The Player Data Object
     */
    public PlayerData(Player player, Material currentPickedUp) {
        hasPickedUp = false;
        hasArmorStand = false;
        this.player = player;
        this.currentPickedUp = currentPickedUp;
        cachedPlayerData.put(player.getUniqueId(), this);
    }

    /**
     * @param uuid The Player UUID to base the Profile on
     * @param currentPickedUp The Material, that the player currently picked up
     * @return The Player Data Object
     */
    public PlayerData(UUID uuid, Material currentPickedUp) {
        hasPickedUp = false;
        hasArmorStand = false;
        this.player = Bukkit.getPlayer(uuid);
        this.currentPickedUp = currentPickedUp;
        cachedPlayerData.put(player.getUniqueId(), this);
    }

    /**
     * @param uuid The UUID to the Profile to check for
     * @return Returns if the Profile Exists
     */
    public static boolean exists(UUID uuid) {
        return cachedPlayerData.containsKey(uuid);
    }

    /**
     * @param player The player to the Profile to check for
     * @return Returns if the Profile Exists
     */
    public static boolean exist(Player player) {
        return cachedPlayerData.containsKey(player.getUniqueId());
    }

    /**
     * @param uuid The UUID to the Profile to get
     * @return If the Profile exists, will return it
     */
    public static PlayerData getPlayerData(UUID uuid) {
        return cachedPlayerData.get(uuid);
    }

    /**
     * @param player The UUID to the Profile to get
     * @return If the Profile exists, will return it
     */
    public static PlayerData getPlayerData(Player player) {
        return cachedPlayerData.get(player.getUniqueId());
    }

    /**
     * @return Returns the raw list of cached PlayerData
     */
    public static HashMap<UUID, PlayerData> getSavedPlayerData() {
        return cachedPlayerData;
    }


    /**
     * @param hasPickedUp The new Value of hasPickedUP
     */
    public void setHasPickedUp(boolean hasPickedUp) {
        this.hasPickedUp = hasPickedUp;
    }

    /**
     * @param hasArmorStand The new Value of hasArmorStand
     */
    public void setHasFollowingArmorStand(boolean hasArmorStand) {
        this.hasArmorStand = hasArmorStand;
    }

    /**
     * @return If the player has a following Armorstand
     */
    public boolean hasFollowingArmorStand() {
        return hasArmorStand;
    }

    /**
     * @return Returns the armorstand that is following the player of this Profile
     */
    public ArmorStand getFollowingArmorStand() {
        return followingArmorStand;
    }

    /**
     * @param armorStand Sets the given armorstand to follow the Player
     */
    public void setFollowingArmorStand(ArmorStand armorStand) {
        hasArmorStand = true;
        followingArmorStand = armorStand;
    }

    /**
     * @return Returns the material that the Player has picked up
     */
    public Material getPickedUp() {
        return currentPickedUp;
    }

    /**
     * @return Returns the Player from the Profile
     */
    public Player getPlayer() {
        return player;
    }

    /**
     * Removes the Armorstand and removes the Slowness from the player
     * @return the PlayerData that was modified
     */
    public PlayerData removeArmorStand() {
        followingArmorStand.remove();
        player.removePotionEffect(PotionEffectType.SLOW);
        hasArmorStand = false;
        currentPickedUp = Material.AIR;
        return this;
    }

    /**
     * Creates an Armorstand to follow the Player, binds it to Player Profile
     * @return the PlayerData that was modified
     */
    public PlayerData createFollowingArmorStand() {
        followingArmorStand = CarryUtils.summonArmorStand(player.getLocation(), currentPickedUp);
        hasArmorStand = true;
        return this;
    }

    /**
     * Handles the placement of a block that was picked up;
     * @return the PlayerData that was modified
     */
    public PlayerData handlePlacement(Block block){
        Block block1 = block.getLocation().add(0, 1, 0).getBlock();
        if (block1.getType() == Material.AIR) {
            block1.setType(getPickedUp());
            getPlayer().sendMessage(ChatColor.RED + "Set block one above");
            return this;
        }
        Block block2 = block.getLocation().add(1, 0, 0).getBlock();
        if (block2.getType() == Material.AIR) {
            block2.setType(getPickedUp());
            getPlayer().sendMessage(ChatColor.RED + "Set block x 1");
            return this;
        }
        Block block3 = block.getLocation().add(0, 0, 1).getBlock();
        if (block3.getType() == Material.AIR) {
            block3.setType(getPickedUp());
            getPlayer().sendMessage(ChatColor.RED + "Set block z 1");
            return this;
        }
        getPlayer().sendMessage(ChatColor.RED + "Couldn't Place the " + getPickedUp().name() + " you were holding!");
        return this;
    }

    /**
     * Handles the pickup of a block, binds it to player profile
     * @return the PlayerData that was modified
     */
    public PlayerData handlePickup(Block block){
        if (hasFollowingArmorStand()) {
            removeBlock(block);
            return this;
        }
        createFollowingArmorStand();
        removeBlock(block);
        return this;
    }
}
Editor is loading...