-
Notifications
You must be signed in to change notification settings - Fork 168
/
ProtocolSupportAPI.java
132 lines (113 loc) · 4.56 KB
/
ProtocolSupportAPI.java
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
123
124
125
126
127
128
129
130
131
132
package protocolsupport.api;
import java.math.BigInteger;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.commons.lang3.Validate;
import org.bukkit.entity.Player;
import protocolsupport.protocol.storage.ProtocolStorage;
import protocolsupport.zplatform.ServerPlatform;
public class ProtocolSupportAPI {
private ProtocolSupportAPI() {
}
private static final BigInteger apiversion = BigInteger.valueOf(15);
/**
* Returns ProtocolSupport API version <br>
* This number is incremented every time API changes (behavior change, method/field added/removed)
* @return API version
*/
public static @Nonnull BigInteger getAPIVersion() {
return apiversion;
}
/**
* Returns player {@link ProtocolVersion} <br>
* Returns {@link ProtocolVersion#UNKNOWN} if player is not online or is not a real player
* @param player player
* @return player protocol version or UNKNOWN
*/
public static @Nonnull ProtocolVersion getProtocolVersion(Player player) {
Connection connection = getConnection(player);
return connection != null ? connection.getVersion() : ProtocolVersion.UNKNOWN;
}
/**
* Returns {@link ProtocolVersion} of connection with specified address <br>
* Returns {@link ProtocolVersion#UNKNOWN} if there is no connection with specified address
* @param address address
* @return connection protocol version or UNKNOWN
*/
public static @Nonnull ProtocolVersion getProtocolVersion(SocketAddress address) {
Connection connection = getConnection(address);
return connection != null ? connection.getVersion() : ProtocolVersion.UNKNOWN;
}
/**
* Returns all currently active connections
* @return all currently active connections
*/
public static @Nonnull List<Connection> getConnections() {
return new ArrayList<>(ProtocolStorage.getConnections());
}
/**
* Returns player {@link Connection} <br>
* Returns null if player is not a real player
* @param player player
* @return player {@link Connection} or null
*/
public static @Nullable Connection getConnection(@Nonnull Player player) {
Validate.notNull(player, "Player can't be null");
Connection connection = ServerPlatform.get().getMiscUtils().getConnection(player);
return connection != null ? connection : getConnection(player.spigot().getRawAddress());
}
/**
* Returns connection with specified address <br>
* Returns null if there is no connection with specified address
* @param address address
* @return {@link Connection} with specified address
*/
public static @Nullable Connection getConnection(@Nonnull SocketAddress address) {
return ProtocolStorage.getConnection(address);
}
private static final Set<ProtocolVersion> enabledVersions = Collections.newSetFromMap(new ConcurrentHashMap<>());
static {
enabledVersions.addAll(Arrays.asList(ProtocolVersion.getAllSupported()));
}
/**
* Enables protocol version support if it was disabled previously <br>
* By default all supported versions are enabled
* @param version protocol version which support needs to be enabled
*/
public static void enableProtocolVersion(@Nonnull ProtocolVersion version) {
Validate.isTrue(version.isSupported(), "Can't enable support for version that is not supported at all");
enabledVersions.add(version);
}
/**
* Disables protocol version support, players with that version won't be able to login to server and ping will report server as unsupported version <br>
* Disabling all versions before 1.9 will allow multiple passengers on entity
* @param version protocol version which support needs to be disabled
*/
public static void disableProtocolVersion(@Nonnull ProtocolVersion version) {
Validate.isTrue(version.isSupported(), "Can't disable support for version that is not supported at all");
enabledVersions.remove(version);
}
/**
* Return true if protocol version support is enabled
* @param version protocol version for which support enabled should be checked
* @return true if protocol version support is enabled, false otherwise
*/
public static boolean isProtocolVersionEnabled(@Nonnull ProtocolVersion version) {
return enabledVersions.contains(version);
}
/**
* Returns all currently enabled protocol versions
* @return all currently enabled protocol versions
*/
public static @Nonnull Collection<ProtocolVersion> getEnabledProtocolVersions() {
return new ArrayList<>(enabledVersions);
}
}