-
Notifications
You must be signed in to change notification settings - Fork 557
Expand file tree
/
Copy pathSpectateLoginService.java
More file actions
104 lines (84 loc) · 2.75 KB
/
SpectateLoginService.java
File metadata and controls
104 lines (84 loc) · 2.75 KB
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
package fr.xephi.authme.service;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Player;
import javax.inject.Inject;
import java.util.HashMap;
import java.util.Map;
/**
* Sets the player gamemode to Spectator, puts the player in an invisible armorstand and fixes the direction of the view
*/
public class SpectateLoginService {
private Map<Player, ArmorStand> armorStands = new HashMap<>();
private Map<Player, GameMode> gameModeMap = new HashMap<>();
@Inject
private CommonService service;
/**
* Creates a stand for the player
*
* @param player the player
*/
public void createStand(Player player) {
if (player.isDead()) {
return;
}
Location location = player.getLocation();
ArmorStand stand = spawnStand(location);
armorStands.put(player, stand);
gameModeMap.put(player, player.getGameMode());
player.setGameMode(GameMode.SPECTATOR);
player.setSpectatorTarget(stand);
}
/**
* Updates spectator target for the player
*
* @param player the player
*/
public void updateTarget(Player player) {
ArmorStand stand = armorStands.get(player);
if (stand != null) {
player.setSpectatorTarget(stand);
}
}
/**
* Removes the player's stand and deletes effects
*
* @param player the player
*/
public void removeStand(Player player) {
ArmorStand stand = armorStands.get(player);
if (stand != null) {
stand.remove();
player.setSpectatorTarget(null);
player.setGameMode(gameModeMap.get(player));
gameModeMap.remove(player);
armorStands.remove(player);
}
}
/**
* Removes all armorstands and restores player gamemode
*/
public void removeArmorstands() {
for (Player player : armorStands.keySet()) {
removeStand(player);
}
gameModeMap.clear();
armorStands.clear();
}
public boolean hasStand(Player player) {
return armorStands.containsKey(player);
}
private ArmorStand spawnStand(Location loc) {
double pitch = service.getProperty(RestrictionSettings.HEAD_PITCH);
double yaw = service.getProperty(RestrictionSettings.HEAD_YAW);
Location location = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getBlockZ(),
(float) yaw, (float) pitch);
ArmorStand stand = location.getWorld().spawn(location, ArmorStand.class);
stand.setGravity(false);
stand.setAI(false);
stand.setInvisible(true);
return stand;
}
}