Skip to content

Commit

Permalink
First pass on Presence controller/holder
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Spieß committed Nov 1, 2016
1 parent 9af8e2c commit 9c0d5dc
Show file tree
Hide file tree
Showing 11 changed files with 496 additions and 11 deletions.
7 changes: 3 additions & 4 deletions src/main/java/net/dv8tion/jda/client/JDAClient.java
Expand Up @@ -16,10 +16,7 @@

package net.dv8tion.jda.client;

import net.dv8tion.jda.client.entities.Friend;
import net.dv8tion.jda.client.entities.Group;
import net.dv8tion.jda.client.entities.Relationship;
import net.dv8tion.jda.client.entities.RelationshipType;
import net.dv8tion.jda.client.entities.*;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.entities.User;
Expand Down Expand Up @@ -48,4 +45,6 @@ public interface JDAClient
Friend getFriend(User user);
Friend getFriend(Member member);
Friend getFriendById(String id);

UserSettings getSettings();
}
48 changes: 48 additions & 0 deletions src/main/java/net/dv8tion/jda/client/entities/UserSettings.java
@@ -0,0 +1,48 @@
/*
* 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.client.entities;

import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.OnlineStatus;
import net.dv8tion.jda.core.entities.Guild;

import java.util.List;
import java.util.Locale;

public interface UserSettings
{

JDA getJDA();

OnlineStatus getStatus();
Locale getLocale();
//getTheme() : ?

List<Guild> getGuildPositions();
List<Guild> getRestrictedGuilds();

boolean isAllowEmailFriendRequest();
boolean isConvertEmoticons();
boolean isDetectPlatformAccounts();
boolean isDeveloperMode();
boolean isEnableTTS();
boolean isShowCurrentGame();
boolean isRenderEmbeds();
boolean isMessageDisplayCompact();
boolean isInlineEmbedMedia();
boolean isInlineAttachmentMedia();
}
Expand Up @@ -18,14 +18,11 @@

import net.dv8tion.jda.client.JDAClient;
import net.dv8tion.jda.client.entities.*;
import net.dv8tion.jda.core.AccountType;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.entities.User;
import net.dv8tion.jda.core.entities.impl.JDAImpl;
import org.apache.http.HttpHost;

import javax.management.relation.Relation;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -38,10 +35,12 @@ public class JDAClientImpl implements JDAClient
protected final HashMap<String, Group> groups = new HashMap<>();
protected final HashMap<String, Relationship> relationships = new HashMap<>();
protected final HashMap<String, CallUser> callUsers = new HashMap<>();
protected UserSettingsImpl userSettings;

public JDAClientImpl(JDAImpl api)
{
this.api = api;
this.userSettings = new UserSettingsImpl(api);
}

@Override
Expand Down Expand Up @@ -172,6 +171,12 @@ public Friend getFriendById(String id)
return (Friend) getRelationshipById(id, RelationshipType.FRIEND);
}

@Override
public UserSettings getSettings()
{
return userSettings;
}

public HashMap<String, Group> getGroupMap()
{
return groups;
Expand Down
@@ -0,0 +1,157 @@
/*
* 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.client.entities.impl;

import net.dv8tion.jda.client.entities.UserSettings;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.OnlineStatus;
import net.dv8tion.jda.core.entities.Guild;

import java.util.List;
import java.util.Locale;

public class UserSettingsImpl implements UserSettings
{

private final JDA api;

private OnlineStatus status = OnlineStatus.UNKNOWN;

public UserSettingsImpl(JDA api)
{
this.api = api;
}

@Override
public JDA getJDA()
{
return api;
}


@Override
public OnlineStatus getStatus()
{
return status;
}

@Override
public Locale getLocale()
{
return null;
}

@Override
public List<Guild> getGuildPositions()
{
return null;
}

@Override
public List<Guild> getRestrictedGuilds()
{
return null;
}

@Override
public boolean isAllowEmailFriendRequest()
{
return false;
}

@Override
public boolean isConvertEmoticons()
{
return false;
}

@Override
public boolean isDetectPlatformAccounts()
{
return false;
}

@Override
public boolean isDeveloperMode()
{
return false;
}

@Override
public boolean isEnableTTS()
{
return false;
}

@Override
public boolean isShowCurrentGame()
{
return false;
}

@Override
public boolean isRenderEmbeds()
{
return false;
}

@Override
public boolean isMessageDisplayCompact()
{
return false;
}

@Override
public boolean isInlineEmbedMedia()
{
return false;
}

@Override
public boolean isInlineAttachmentMedia()
{
return false;
}

/* -- Setters -- */

public UserSettingsImpl setStatus(OnlineStatus status)
{
this.status = status;
return this;
}

/* -- Object overrides -- */

@Override
public int hashCode()
{
return getJDA().getSelfInfo().getId().hashCode();
}

@Override
public boolean equals(Object obj)
{
return obj instanceof UserSettingsImpl && getJDA().equals(((UserSettingsImpl) obj).getJDA());
}

@Override
public String toString()
{
return "UserSettings(" + getJDA().getSelfInfo() + ")";
}
}
3 changes: 3 additions & 0 deletions src/main/java/net/dv8tion/jda/core/JDA.java
Expand Up @@ -20,6 +20,7 @@
import net.dv8tion.jda.client.JDAClient;
import net.dv8tion.jda.core.entities.*;
import net.dv8tion.jda.core.hooks.IEventManager;
import net.dv8tion.jda.core.managers.Presence;
import net.dv8tion.jda.core.requests.ratelimit.IBucket;
import org.apache.http.HttpHost;

Expand Down Expand Up @@ -460,4 +461,6 @@ public int getShardTotal()
JDABot asBot();

ShardInfo getShardInfo();

Presence getPresence();
}
12 changes: 12 additions & 0 deletions src/main/java/net/dv8tion/jda/core/OnlineStatus.java
Expand Up @@ -34,6 +34,18 @@ public enum OnlineStatus
this.key = key;
}

/**
* The valid API key for this OnlineStatus
*
* @return
* String representation of the valid API key for this OnlineStatus
* @see <a href="https://discordapp.com/developers/docs/topics/gateway#presence-update">PRESENCE_UPDATE</a>
*/
public String getKey()
{
return key;
}

/**
* Will get the {@link net.dv8tion.jda.core.OnlineStatus OnlineStatus} from the provided key.<br>
* If the provided key does no match a presence, this will return {@link net.dv8tion.jda.core.OnlineStatus#UNKNOWN UNKONWN}
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/net/dv8tion/jda/core/entities/Game.java
Expand Up @@ -15,6 +15,9 @@
*/
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.
*/
Expand Down Expand Up @@ -45,6 +48,44 @@ public interface 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).
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/net/dv8tion/jda/core/entities/impl/JDAImpl.java
Expand Up @@ -28,6 +28,7 @@
import net.dv8tion.jda.core.exceptions.RateLimitedException;
import net.dv8tion.jda.core.hooks.IEventManager;
import net.dv8tion.jda.core.hooks.InterfacedEventManager;
import net.dv8tion.jda.core.managers.Presence;
import net.dv8tion.jda.core.requests.*;
import net.dv8tion.jda.core.requests.ratelimit.IBucket;
import net.dv8tion.jda.core.utils.SimpleLog;
Expand Down Expand Up @@ -63,6 +64,7 @@ public class JDAImpl implements JDA
protected Status status = Status.INITIALIZING;
protected SelfInfo selfInfo;
protected ShardInfo shardInfo;
protected Presence presence;
protected String token = null;
protected boolean audioEnabled;
protected boolean useShutdownHook;
Expand Down Expand Up @@ -488,6 +490,14 @@ public ShardInfo getShardInfo()
return shardInfo;
}

@Override
public Presence getPresence()
{
if (presence == null)
presence = new Presence(this);
return presence;
}

@Override
public void installAuxiliaryCable(int port) throws UnsupportedOperationException
{
Expand Down

0 comments on commit 9c0d5dc

Please sign in to comment.