Skip to content

Commit

Permalink
Added new game types (#544)
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed Nov 21, 2017
1 parent 48bc751 commit 7fa408e
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/main/java/net/dv8tion/jda/core/JDABuilder.java
Expand Up @@ -375,7 +375,7 @@ public JDABuilder setIdle(boolean idle)
* <br>This value can be changed at any time in the {@link net.dv8tion.jda.core.managers.Presence Presence} from a JDA instance.
*
* <p><b>Hint:</b> You can create a {@link net.dv8tion.jda.core.entities.Game Game} object using
* {@link net.dv8tion.jda.core.entities.Game#of(String)} or {@link net.dv8tion.jda.core.entities.Game#of(String, String)}.
* {@link net.dv8tion.jda.core.entities.Game#playing(String)} or {@link net.dv8tion.jda.core.entities.Game#streaming(String, String)}.
*
* @param game
* An instance of {@link net.dv8tion.jda.core.entities.Game Game} (null allowed)
Expand Down
179 changes: 163 additions & 16 deletions src/main/java/net/dv8tion/jda/core/entities/Game.java
Expand Up @@ -112,19 +112,42 @@ public String toString()
/**
* Creates a new Game instance with the specified name.
* <br>In order to appear as "streaming" in the official client you must
* provide a valid (see documentation of method) streaming URL in {@link #of(String, String) Game.of(String, String)}.
* provide a valid (see documentation of method) streaming URL in {@link #streaming(String, String) Game.streaming(String, String)}.
*
* @param name
* The not-null name of the newly created game
*
* @throws IllegalArgumentException
* if the specified name is null or empty
* if the specified name is null, empty or blank
*
* @return A valid Game instance with the provided name with {@link GameType#DEFAULT}
*/
public static Game playing(String name)
{
Checks.notBlank(name, "Name");
return new Game(name, null, GameType.DEFAULT);
}

/**
* Creates a new Game instance with the specified name.
* <br>In order to appear as "streaming" in the official client you must
* provide a valid (see documentation of method) streaming URL in {@link #streaming(String, String) Game.of(String, String)}.
*
* @param name
* The not-null name of the newly created game
*
* @throws IllegalArgumentException
* if the specified name is null, empty or blank
*
* @return A valid Game instance with the provided name with {@link GameType#DEFAULT}
*
* @deprecated
* Use {@link #playing(String)} instead
*/
@Deprecated
public static Game of(String name)
{
return of(name, null);
return playing(name);
}

/**
Expand All @@ -138,13 +161,13 @@ public static Game of(String name)
* The streaming url to use, required to display as "streaming"
*
* @throws IllegalArgumentException
* if the specified name is null or empty
* If the specified name is null or empty
*
* @return A valid Game instance with the provided name and url
*
* @see #isValidStreamingUrl(String)
*/
public static Game of(String name, String url)
public static Game streaming(String name, String url)
{
Checks.notEmpty(name, "Provided game name");
GameType type;
Expand All @@ -155,6 +178,123 @@ public static Game of(String name, String url)
return new Game(name, url, type);
}

/**
* Creates a new Game instance with the specified name and url.
* <br>The specified URL must be valid according to discord standards in order to display as "streaming" in the official client.
* A valid streaming URL must be derived from {@code https://twitch.tv/} and can be verified using {@link #isValidStreamingUrl(String)}. (see documentation)
*
* @param name
* The not-null name of the newly created game
* @param url
* The streaming url to use, required to display as "streaming"
*
* @throws IllegalArgumentException
* If the specified name is null or empty
*
* @return A valid Game instance with the provided name and url
*
* @see #isValidStreamingUrl(String)
*
* @deprecated
* Use {@link #streaming(String, String)} instead!
*/
@Deprecated
public static Game of(String name, String url)
{
return streaming(name, url);
}

/**
* Creates a new Game instance with the specified name.
* <br>This will display as {@code Listening name} in the official client
*
* @param name
* The not-null name of the newly created game
*
* @throws IllegalArgumentException
* if the specified name is null, empty or blank
*
* @return A valid Game instance with the provided name with {@link GameType#LISTENING}
*/
public static Game listening(String name)
{
Checks.notBlank(name, "Name");
return new Game(name, null, GameType.LISTENING);
}

/**
* Creates a new Game instance with the specified name.
* <br>This will display as {@code Watching name} in the official client
*
* @param name
* The not-null name of the newly created game
*
* @throws IllegalArgumentException
* if the specified name is null, empty or blank
*
* @return A valid Game instance with the provided name with {@link GameType#WATCHING}
*/
public static Game watching(String name)
{
Checks.notBlank(name, "Name");
return new Game(name, null, GameType.WATCHING);
}

/**
* Creates a new Game instance with the specified name and url.
*
* @param type
* The {@link net.dv8tion.jda.core.entities.Game.GameType GameType} to use
* @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 and url
*/
public static Game of(GameType type, String name)
{
return of(type, name, null);
}

/**
* Creates a new Game instance with the specified name and url.
* <br>The provided url would only be used for {@link net.dv8tion.jda.core.entities.Game.GameType#STREAMING GameType.STREAMING}
* and should be a twitch url.
*
* @param type
* The {@link net.dv8tion.jda.core.entities.Game.GameType GameType} to use
* @param name
* The not-null name of the newly created game
* @param url
* The streaming url to use, required to display as "streaming".
*
* @throws IllegalArgumentException
* If the specified name is null or empty
*
* @return A valid Game instance with the provided name and url
*
* @see #isValidStreamingUrl(String)
*/
public static Game of(GameType type, String name, String url)
{
Checks.notNull(type, "Type");
switch (type)
{
case DEFAULT:
return playing(name);
case STREAMING:
return streaming(name, url);
case LISTENING:
return listening(name);
case WATCHING:
return watching(name);
default:
throw new IllegalArgumentException("GameType " + type + " is not supported!");
}
}

/**
* Checks if a given String is a valid Twitch url (ie, one that will display "Streaming" on the Discord client).
*
Expand Down Expand Up @@ -183,14 +323,15 @@ public enum GameType
*/
STREAMING(1),
/**
* 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.
*
* @deprecated This might be removed when discord introduces more activity types. Use {@link #STREAMING} instead.
* Used to indicate that the {@link net.dv8tion.jda.core.entities.Game Game} should display
* as {@code Listening...} in the official client.
*/
LISTENING(2),
/**
* Used to indicate that the {@link net.dv8tion.jda.core.entities.Game Game} should display
* as {@code Watching...} in the official client.
*/
@Deprecated
TWITCH(1);
WATCHING(3);

private final int key;

Expand Down Expand Up @@ -220,12 +361,18 @@ public int getKey()
*/
public static GameType fromKey(int key)
{
for (GameType level : GameType.values())
switch (key)
{
if (level.getKey() == key)
return level;
case 0:
default:
return DEFAULT;
case 1:
return STREAMING;
case 2:
return LISTENING;
case 3:
return WATCHING;
}
return DEFAULT;
}
}
}
10 changes: 5 additions & 5 deletions src/main/java/net/dv8tion/jda/core/managers/Presence.java
Expand Up @@ -78,18 +78,18 @@ public interface Presence

/**
* Sets the {@link net.dv8tion.jda.core.entities.Game Game} for this session.
* <br>A Game can be retrieved via {@link net.dv8tion.jda.core.entities.Game#of(String)}.
* <br>A Game can be retrieved via {@link net.dv8tion.jda.core.entities.Game#playing(String)}.
* For streams you provide a valid streaming url as second parameter
*
* <p>Examples:
* <br>{@code presence.setGame(Game.of("Thrones"));}
* <br>{@code presence.setGame(Game.of("Thrones", "https://twitch.tv/EasterEggs"));}
* <br>{@code presence.setGame(Game.playing("Thrones"));}
* <br>{@code presence.setGame(Game.streaming("Thrones", "https://twitch.tv/EasterEggs"));}
*
* @param game
* A {@link net.dv8tion.jda.core.entities.Game Game} instance or null to reset
*
* @see net.dv8tion.jda.core.entities.Game#of(String)
* @see net.dv8tion.jda.core.entities.Game#of(String, String)
* @see net.dv8tion.jda.core.entities.Game#playing(String)
* @see net.dv8tion.jda.core.entities.Game#streaming(String, String)
*/
void setGame(Game game);

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/dv8tion/jda/core/utils/WidgetUtil.java
Expand Up @@ -551,7 +551,7 @@ private Member(JSONObject json, Widget widget)
this.status = OnlineStatus.fromKey(json.getString("status"));
this.game = json.isNull("game") ? null :
json.getJSONObject("game").isNull("name") || json.getJSONObject("game").getString("name").isEmpty() ? null :
Game.of(json.getJSONObject("game").getString("name"));
Game.playing(json.getJSONObject("game").getString("name"));
}

private void setVoiceState(VoiceState voiceState)
Expand Down

0 comments on commit 7fa408e

Please sign in to comment.