summaryrefslogtreewikicommitdiff
path: root/src/main/java/net/tylermurphy/hideAndSeek/game/util/PlayerUtil.java
blob: 59c7b7358b49ca6afa57f609372177d19b7aff4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package net.tylermurphy.hideAndSeek.game.util;

import com.cryptomorin.xseries.messages.Titles;
import net.md_5.bungee.api.ChatColor;
import net.tylermurphy.hideAndSeek.Main;
import net.tylermurphy.hideAndSeek.configuration.Items;
import net.tylermurphy.hideAndSeek.game.Board;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

import static net.tylermurphy.hideAndSeek.configuration.Config.*;
import static net.tylermurphy.hideAndSeek.configuration.Config.lobbyPosition;
import static net.tylermurphy.hideAndSeek.configuration.Localization.message;

public class PlayerUtil {

    public static void loadHider(Player player, String gameWorld){
        player.teleport(new Location(Bukkit.getWorld(gameWorld), spawnPosition.getX(),spawnPosition.getY(),spawnPosition.getZ()));
        loadPlayer(player);
        player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED,1000000,5,false,false));
        Titles.sendTitle(player, 10, 70, 20, ChatColor.WHITE + "" + message("HIDER_TEAM_NAME"), ChatColor.WHITE + message("HIDERS_SUBTITLE").toString());
    }

    public static void loadSeeker(Player player, String gameWorld){
        player.teleport(new Location(Bukkit.getWorld(gameWorld), spawnPosition.getX(),spawnPosition.getY(),spawnPosition.getZ()));
        loadPlayer(player);
        player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS,1000000,127,false,false));
        player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW,1000000,127,false,false));
        player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP,1000000,128,false,false));
        Titles.sendTitle(player, 10, 70, 20, ChatColor.WHITE + "" + message("SEEKER_TEAM_NAME"), ChatColor.WHITE + message("SEEKERS_SUBTITLE").toString());
    }

    public static void loadSpectator(Player player, String gameWorld){
        player.teleport(new Location(Bukkit.getWorld(gameWorld), spawnPosition.getX(),spawnPosition.getY(),spawnPosition.getZ()));
        loadPlayer(player);
        player.setAllowFlight(true);
        player.setFlying(true);
        player.setFallDistance(0.0F);
        player.getInventory().setItem(flightToggleItemPosition, flightToggleItem);
        player.getInventory().setItem(teleportItemPosition, teleportItem);
        Main.getInstance().getBoard().getPlayers().forEach(otherPlayer -> {
            otherPlayer.hidePlayer(player);
        });
        Titles.sendTitle(player, 10, 70, 20, ChatColor.GRAY + "" + ChatColor.BOLD + "SPECTATING", ChatColor.WHITE + message("SPECTATOR_SUBTITLE").toString());
    }

    public static void resetPlayer(Player player, Board board){
        loadPlayer(player);
        if (board.isSeeker(player)) {
            if (pvpEnabled)
                for(ItemStack item : Items.SEEKER_ITEMS)
                    player.getInventory().addItem(item);
            for(PotionEffect effect : Items.SEEKER_EFFECTS)
                player.addPotionEffect(effect);
        } else if (board.isHider(player)) {
            if (pvpEnabled)
                for(ItemStack item : Items.HIDER_ITEMS)
                    player.getInventory().addItem(item);
            for(PotionEffect effect : Items.HIDER_EFFECTS)
                player.addPotionEffect(effect);
            if (glowEnabled) {
                player.getInventory().addItem(glowPowerupItem);
            }
        }
    }

    public static void unloadPlayer(Player player){
        player.setGameMode(GameMode.ADVENTURE);
        player.getInventory().clear();
        for(PotionEffect effect : player.getActivePotionEffects()) {
            player.removePotionEffect(effect.getType());
        }
        if (Version.atLeast("1.9")) {
            AttributeInstance attribute = player.getAttribute(Attribute.GENERIC_MAX_HEALTH);
            if (attribute != null) player.setHealth(attribute.getValue());
            for(Player temp : Main.getInstance().getBoard().getPlayers()) {
                Packet.setGlow(player, temp, false);
            }
        } else {
            player.setHealth(player.getMaxHealth());
        }
        Main.getInstance().getBoard().getPlayers().forEach(temp -> {
            player.showPlayer(temp);
            temp.showPlayer(player);
        });
        player.setAllowFlight(false);
        player.setFlying(false);
        player.setFallDistance(0.0F);
    }

    public static void joinPlayer(Player player){
        player.teleport(new Location(Bukkit.getWorld(lobbyWorld), lobbyPosition.getX(),lobbyPosition.getY(),lobbyPosition.getZ()));
        loadPlayer(player);
        if (lobbyStartItem != null && (!lobbyItemStartAdmin || player.hasPermission("hideandseek.start")))
            player.getInventory().setItem(lobbyItemStartPosition, lobbyStartItem);
        if (lobbyLeaveItem != null)
            player.getInventory().setItem(lobbyItemLeavePosition, lobbyLeaveItem);
    }

    private static void loadPlayer(Player player){
        player.setGameMode(GameMode.ADVENTURE);
        player.getInventory().clear();
        for(PotionEffect effect : player.getActivePotionEffects()) {
            player.removePotionEffect(effect.getType());
        }
        player.setFoodLevel(20);
        if (Version.atLeast("1.9")) {
            AttributeInstance attribute = player.getAttribute(Attribute.GENERIC_MAX_HEALTH);
            if (attribute != null) player.setHealth(attribute.getValue());
        } else {
            player.setHealth(player.getMaxHealth());
        }
    }

}