-
-
Notifications
You must be signed in to change notification settings - Fork 724
/
Game.java
160 lines (147 loc) · 4.8 KB
/
Game.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Copyright 2015-2017 Austin Keener & Michael Ritter & Florian Spieß
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dv8tion.jda.core.entities;
import net.dv8tion.jda.core.entities.impl.GameImpl;
import org.apache.http.util.Args;
/**
* Represents a Discord {@link net.dv8tion.jda.core.entities.Game Game}.
* <br>This should contain all information provided from Discord about a Game.
*
* @since 2.1
* @author John A. Grosh
*/
public interface Game
{
/**
* The displayed name of the {@link net.dv8tion.jda.core.entities.Game Game}. If no name has been set, this returns null.
*
* @return Possibly-null String containing the Game's name.
*/
String getName();
/**
* The URL of the {@link net.dv8tion.jda.core.entities.Game Game} if the game is actually a Stream.
* <br>This will return null for regular games.
*
* @return Possibly-null String containing the Game's URL.
*/
String getUrl();
/**
* The type of {@link net.dv8tion.jda.core.entities.Game Game}.
*
* @return Never-null {@link net.dv8tion.jda.core.entities.Game.GameType GameType} representing the type of Game
*/
GameType getType();
/**
* Creates a new Game instance with the specified name.
*
* @param name
* The not-null name of the newly created game
*
* @throws IllegalArgumentException
* if the specified name is null or empty
*
* @return A valid Game instance with the provided name with {@link GameType#DEFAULT}
*/
static Game of(String name)
{
return of(name, null);
}
/**
* Creates a new Game instance with the specified name and url.
*
* @param name
* The not-null name of the newly created game
* @param url
* The streaming url to use, invalid for {@link GameType#DEFAULT GameType#DEFAULT}
*
* @throws IllegalArgumentException
* if the specified name is null or empty
*
* @return A valid Game instance with the provided name and url
*
* @see #isValidStreamingUrl(String)
*/
static Game of(String name, String url)
{
Args.notEmpty(name, "Provided game name");
GameType type;
if (isValidStreamingUrl(url))
type = GameType.TWITCH;
else
type = GameType.DEFAULT;
return new GameImpl(name, url, type);
}
/**
* Checks if a given String is a valid Twitch url (ie, one that will display "Streaming" on the Discord client).
*
* @param url
* The url to check.
*
* @return True if the provided url is valid for triggering Discord's streaming status
*/
static boolean isValidStreamingUrl(String url)
{
return url != null && url.matches("^https?:\\/\\/(www\\.)?twitch\\.tv\\/.+");
}
/**
* The type game being played, differentiating between a game and stream types.
*/
enum GameType
{
/**
* The GameType used to represent a normal {@link net.dv8tion.jda.core.entities.Game Game} status.
*/
DEFAULT(0),
/**
* Used to indicate that the {@link net.dv8tion.jda.core.entities.Game Game} is a stream, specifically for
* <a href="https://www.twitch.tv">https://www.twitch.tv</a>.
* <br>This type is displayed as "Streaming" in the discord client.
*/
TWITCH(1);
private final int key;
GameType(int key)
{
this.key = key;
}
/**
* The Discord defined id key for this GameType.
*
* @return the id key.
*/
public int getKey()
{
return key;
}
/**
* Gets the GameType related to the provided key.
* <br>If an unknown key is provided, this returns {@link #DEFAULT}
*
* @param key
* The Discord key referencing a GameType.
*
* @return The GameType that has the key provided, or {@link #DEFAULT} for unknown key.
*/
public static GameType fromKey(int key)
{
for (GameType level : GameType.values())
{
if(level.getKey() == key)
return level;
}
return DEFAULT;
}
}
}