/
Game.java
130 lines (117 loc) · 3.72 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
/*
* Copyright 2015-2016 Austin Keener & Michael Ritter
*
* 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.commons.lang3.StringUtils;
/**
* Represents a Discord {@link net.dv8tion.jda.core.entities.Game Game}. This should contain all information provided from Discord about a Game.
*/
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}. 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}. This will return null for regular games.
*
* @return
* Possibly-null int 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
* @return
* A valid Game instance with the provided name with {@link GameType#DEFAULT}
* @throws IllegalArgumentException
* if the specified name is null or empty
*/
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}
* @return
* A valid Game instance with the provided name and url
* @throws IllegalArgumentException
* if the specified name is null or empty
* @see #isValidStreamingUrl(String)
*/
static Game of(String name, String url)
{
if (StringUtils.isEmpty(name))
throw new IllegalArgumentException("Game name may not be null or empty.");
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
* boolean
*/
static boolean isValidStreamingUrl(String url)
{
return url != null && url.matches("^https?:\\/\\/(www\\.)?twitch\\.tv\\/.+");
}
enum GameType
{
DEFAULT(0),
TWITCH(1);
private final int key;
GameType(int key)
{
this.key = key;
}
public int getKey()
{
return key;
}
public static GameType fromKey(int key)
{
for (GameType level : GameType.values())
{
if(level.getKey() == key)
return level;
}
return DEFAULT;
}
}
}