From bc0b8ef0b31cac53d0bbc63c124dc72289ff3e77 Mon Sep 17 00:00:00 2001 From: Austin Keener Date: Sat, 8 Oct 2016 17:21:55 -0400 Subject: [PATCH] Implemented support for RELATIONSHIP_ADD and RELATIONSHIP_REMOVE. Created FriendAdded, FriendRemoved, UserBlocked, UserUnblocked, FriendRequestSent, (FR)Canceled, (FR)Received, and (FR)Ignored events. Reordered the events checked in ListenerAdapter to make Messages checked first for faster events for Messages. --- .../events/relationship/FriendAddedEvent.java | 34 ++++++ .../relationship/FriendRemovedEvent.java | 34 ++++++ .../FriendRequestCanceledEvent.java | 34 ++++++ .../FriendRequestIgnoredEvent.java | 34 ++++++ .../FriendRequestReceivedEvent.java | 34 ++++++ .../relationship/FriendRequestSentEvent.java | 34 ++++++ .../GenericRelationshipAddEvent.java | 28 +++++ .../GenericRelationshipEvent.java | 49 ++++++++ .../GenericRelationshipRemoveEvent.java | 28 +++++ .../events/relationship/UserBlockedEvent.java | 34 ++++++ .../relationship/UserUnblockedEvent.java | 34 ++++++ .../client/handle/RelationshipAddHandler.java | 75 ++++++++++++ .../handle/RelationshipRemoveHandler.java | 94 +++++++++++++++ .../dv8tion/jda/core/handle/EventCache.java | 2 +- .../jda/core/hooks/ListenerAdapter.java | 113 ++++++++++++------ .../jda/core/requests/WebSocketClient.java | 4 + 16 files changed, 627 insertions(+), 38 deletions(-) create mode 100644 src/main/java/net/dv8tion/jda/client/events/relationship/FriendAddedEvent.java create mode 100644 src/main/java/net/dv8tion/jda/client/events/relationship/FriendRemovedEvent.java create mode 100644 src/main/java/net/dv8tion/jda/client/events/relationship/FriendRequestCanceledEvent.java create mode 100644 src/main/java/net/dv8tion/jda/client/events/relationship/FriendRequestIgnoredEvent.java create mode 100644 src/main/java/net/dv8tion/jda/client/events/relationship/FriendRequestReceivedEvent.java create mode 100644 src/main/java/net/dv8tion/jda/client/events/relationship/FriendRequestSentEvent.java create mode 100644 src/main/java/net/dv8tion/jda/client/events/relationship/GenericRelationshipAddEvent.java create mode 100644 src/main/java/net/dv8tion/jda/client/events/relationship/GenericRelationshipEvent.java create mode 100644 src/main/java/net/dv8tion/jda/client/events/relationship/GenericRelationshipRemoveEvent.java create mode 100644 src/main/java/net/dv8tion/jda/client/events/relationship/UserBlockedEvent.java create mode 100644 src/main/java/net/dv8tion/jda/client/events/relationship/UserUnblockedEvent.java create mode 100644 src/main/java/net/dv8tion/jda/client/handle/RelationshipAddHandler.java create mode 100644 src/main/java/net/dv8tion/jda/client/handle/RelationshipRemoveHandler.java diff --git a/src/main/java/net/dv8tion/jda/client/events/relationship/FriendAddedEvent.java b/src/main/java/net/dv8tion/jda/client/events/relationship/FriendAddedEvent.java new file mode 100644 index 0000000000..9066b7948d --- /dev/null +++ b/src/main/java/net/dv8tion/jda/client/events/relationship/FriendAddedEvent.java @@ -0,0 +1,34 @@ +/* + * 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.events.relationship; + +import net.dv8tion.jda.client.entities.Friend; +import net.dv8tion.jda.client.entities.Relationship; +import net.dv8tion.jda.core.JDA; + +public class FriendAddedEvent extends GenericRelationshipAddEvent +{ + public FriendAddedEvent(JDA api, long responseNumber, Relationship relationship) + { + super(api, responseNumber, relationship); + } + + public Friend getFriend() + { + return (Friend) relationship; + } +} diff --git a/src/main/java/net/dv8tion/jda/client/events/relationship/FriendRemovedEvent.java b/src/main/java/net/dv8tion/jda/client/events/relationship/FriendRemovedEvent.java new file mode 100644 index 0000000000..a6f88d712e --- /dev/null +++ b/src/main/java/net/dv8tion/jda/client/events/relationship/FriendRemovedEvent.java @@ -0,0 +1,34 @@ +/* + * 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.events.relationship; + +import net.dv8tion.jda.client.entities.Friend; +import net.dv8tion.jda.client.entities.Relationship; +import net.dv8tion.jda.core.JDA; + +public class FriendRemovedEvent extends GenericRelationshipRemoveEvent +{ + public FriendRemovedEvent(JDA api, long responseNumber, Relationship relationship) + { + super(api, responseNumber, relationship); + } + + public Friend getFriend() + { + return (Friend) relationship; + } +} diff --git a/src/main/java/net/dv8tion/jda/client/events/relationship/FriendRequestCanceledEvent.java b/src/main/java/net/dv8tion/jda/client/events/relationship/FriendRequestCanceledEvent.java new file mode 100644 index 0000000000..8d2efb54c5 --- /dev/null +++ b/src/main/java/net/dv8tion/jda/client/events/relationship/FriendRequestCanceledEvent.java @@ -0,0 +1,34 @@ +/* + * 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.events.relationship; + +import net.dv8tion.jda.client.entities.OutgoingFriendRequest; +import net.dv8tion.jda.client.entities.Relationship; +import net.dv8tion.jda.core.JDA; + +public class FriendRequestCanceledEvent extends GenericRelationshipRemoveEvent +{ + public FriendRequestCanceledEvent(JDA api, long responseNumber, Relationship relationship) + { + super(api, responseNumber, relationship); + } + + public OutgoingFriendRequest getFriendRequest() + { + return (OutgoingFriendRequest) relationship; + } +} diff --git a/src/main/java/net/dv8tion/jda/client/events/relationship/FriendRequestIgnoredEvent.java b/src/main/java/net/dv8tion/jda/client/events/relationship/FriendRequestIgnoredEvent.java new file mode 100644 index 0000000000..e4ab527180 --- /dev/null +++ b/src/main/java/net/dv8tion/jda/client/events/relationship/FriendRequestIgnoredEvent.java @@ -0,0 +1,34 @@ +/* + * 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.events.relationship; + +import net.dv8tion.jda.client.entities.IncomingFriendRequest; +import net.dv8tion.jda.client.entities.Relationship; +import net.dv8tion.jda.core.JDA; + +public class FriendRequestIgnoredEvent extends GenericRelationshipRemoveEvent +{ + public FriendRequestIgnoredEvent(JDA api, long responseNumber, Relationship relationship) + { + super(api, responseNumber, relationship); + } + + public IncomingFriendRequest getFriendRequest() + { + return (IncomingFriendRequest) relationship; + } +} diff --git a/src/main/java/net/dv8tion/jda/client/events/relationship/FriendRequestReceivedEvent.java b/src/main/java/net/dv8tion/jda/client/events/relationship/FriendRequestReceivedEvent.java new file mode 100644 index 0000000000..c161772e0c --- /dev/null +++ b/src/main/java/net/dv8tion/jda/client/events/relationship/FriendRequestReceivedEvent.java @@ -0,0 +1,34 @@ +/* + * 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.events.relationship; + +import net.dv8tion.jda.client.entities.IncomingFriendRequest; +import net.dv8tion.jda.client.entities.Relationship; +import net.dv8tion.jda.core.JDA; + +public class FriendRequestReceivedEvent extends GenericRelationshipAddEvent +{ + public FriendRequestReceivedEvent(JDA api, long responseNumber, Relationship relationship) + { + super(api, responseNumber, relationship); + } + + public IncomingFriendRequest getFriendRequest() + { + return (IncomingFriendRequest) relationship; + } +} diff --git a/src/main/java/net/dv8tion/jda/client/events/relationship/FriendRequestSentEvent.java b/src/main/java/net/dv8tion/jda/client/events/relationship/FriendRequestSentEvent.java new file mode 100644 index 0000000000..4ed021112d --- /dev/null +++ b/src/main/java/net/dv8tion/jda/client/events/relationship/FriendRequestSentEvent.java @@ -0,0 +1,34 @@ +/* + * 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.events.relationship; + +import net.dv8tion.jda.client.entities.OutgoingFriendRequest; +import net.dv8tion.jda.client.entities.Relationship; +import net.dv8tion.jda.core.JDA; + +public class FriendRequestSentEvent extends GenericRelationshipAddEvent +{ + public FriendRequestSentEvent(JDA api, long responseNumber, Relationship relationship) + { + super(api, responseNumber, relationship); + } + + public OutgoingFriendRequest getFriendRequest() + { + return (OutgoingFriendRequest) relationship; + } +} diff --git a/src/main/java/net/dv8tion/jda/client/events/relationship/GenericRelationshipAddEvent.java b/src/main/java/net/dv8tion/jda/client/events/relationship/GenericRelationshipAddEvent.java new file mode 100644 index 0000000000..c4537fae43 --- /dev/null +++ b/src/main/java/net/dv8tion/jda/client/events/relationship/GenericRelationshipAddEvent.java @@ -0,0 +1,28 @@ +/* + * 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.events.relationship; + +import net.dv8tion.jda.client.entities.Relationship; +import net.dv8tion.jda.core.JDA; + +public abstract class GenericRelationshipAddEvent extends GenericRelationshipEvent +{ + public GenericRelationshipAddEvent(JDA api, long responseNumber, Relationship relationship) + { + super(api, responseNumber, relationship); + } +} diff --git a/src/main/java/net/dv8tion/jda/client/events/relationship/GenericRelationshipEvent.java b/src/main/java/net/dv8tion/jda/client/events/relationship/GenericRelationshipEvent.java new file mode 100644 index 0000000000..feaec5c4db --- /dev/null +++ b/src/main/java/net/dv8tion/jda/client/events/relationship/GenericRelationshipEvent.java @@ -0,0 +1,49 @@ +/* + * 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.events.relationship; + +import net.dv8tion.jda.client.entities.Relationship; +import net.dv8tion.jda.client.entities.RelationshipType; +import net.dv8tion.jda.core.JDA; +import net.dv8tion.jda.core.entities.User; +import net.dv8tion.jda.core.events.Event; + +public abstract class GenericRelationshipEvent extends Event +{ + protected final Relationship relationship; + + public GenericRelationshipEvent(JDA api, long responseNumber, Relationship relationship) + { + super(api, responseNumber); + this.relationship = relationship; + } + + public Relationship getRelationship() + { + return relationship; + } + + public RelationshipType getRelationshipType() + { + return relationship.getType(); + } + + public User getUser() + { + return relationship.getUser(); + } +} diff --git a/src/main/java/net/dv8tion/jda/client/events/relationship/GenericRelationshipRemoveEvent.java b/src/main/java/net/dv8tion/jda/client/events/relationship/GenericRelationshipRemoveEvent.java new file mode 100644 index 0000000000..f41786f994 --- /dev/null +++ b/src/main/java/net/dv8tion/jda/client/events/relationship/GenericRelationshipRemoveEvent.java @@ -0,0 +1,28 @@ +/* + * 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.events.relationship; + +import net.dv8tion.jda.client.entities.Relationship; +import net.dv8tion.jda.core.JDA; + +public abstract class GenericRelationshipRemoveEvent extends GenericRelationshipEvent +{ + public GenericRelationshipRemoveEvent(JDA api, long responseNumber, Relationship relationship) + { + super(api, responseNumber, relationship); + } +} diff --git a/src/main/java/net/dv8tion/jda/client/events/relationship/UserBlockedEvent.java b/src/main/java/net/dv8tion/jda/client/events/relationship/UserBlockedEvent.java new file mode 100644 index 0000000000..58dab5a0d1 --- /dev/null +++ b/src/main/java/net/dv8tion/jda/client/events/relationship/UserBlockedEvent.java @@ -0,0 +1,34 @@ +/* + * 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.events.relationship; + +import net.dv8tion.jda.client.entities.BlockedUser; +import net.dv8tion.jda.client.entities.Relationship; +import net.dv8tion.jda.core.JDA; + +public class UserBlockedEvent extends GenericRelationshipAddEvent +{ + public UserBlockedEvent(JDA api, long responseNumber, Relationship relationship) + { + super(api, responseNumber, relationship); + } + + public BlockedUser getBlockedUser() + { + return (BlockedUser) getBlockedUser(); + } +} diff --git a/src/main/java/net/dv8tion/jda/client/events/relationship/UserUnblockedEvent.java b/src/main/java/net/dv8tion/jda/client/events/relationship/UserUnblockedEvent.java new file mode 100644 index 0000000000..67cb053a4b --- /dev/null +++ b/src/main/java/net/dv8tion/jda/client/events/relationship/UserUnblockedEvent.java @@ -0,0 +1,34 @@ +/* + * 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.events.relationship; + +import net.dv8tion.jda.client.entities.BlockedUser; +import net.dv8tion.jda.client.entities.Relationship; +import net.dv8tion.jda.core.JDA; + +public class UserUnblockedEvent extends GenericRelationshipRemoveEvent +{ + public UserUnblockedEvent(JDA api, long responseNumber, Relationship relationship) + { + super(api, responseNumber, relationship); + } + + public BlockedUser getPreviouslyBlockedUser() + { + return (BlockedUser) relationship; + } +} diff --git a/src/main/java/net/dv8tion/jda/client/handle/RelationshipAddHandler.java b/src/main/java/net/dv8tion/jda/client/handle/RelationshipAddHandler.java new file mode 100644 index 0000000000..147d5044d4 --- /dev/null +++ b/src/main/java/net/dv8tion/jda/client/handle/RelationshipAddHandler.java @@ -0,0 +1,75 @@ +/* + * 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.handle; + +import net.dv8tion.jda.client.entities.Relationship; +import net.dv8tion.jda.client.events.relationship.FriendAddedEvent; +import net.dv8tion.jda.client.events.relationship.FriendRequestReceivedEvent; +import net.dv8tion.jda.client.events.relationship.FriendRequestSentEvent; +import net.dv8tion.jda.client.events.relationship.UserBlockedEvent; +import net.dv8tion.jda.core.entities.EntityBuilder; +import net.dv8tion.jda.core.entities.impl.JDAImpl; +import net.dv8tion.jda.core.handle.EventCache; +import net.dv8tion.jda.core.handle.SocketHandler; +import net.dv8tion.jda.core.requests.WebSocketClient; +import org.json.JSONObject; + +public class RelationshipAddHandler extends SocketHandler +{ + public RelationshipAddHandler(JDAImpl api) + { + super(api); + } + + @Override + protected String handleInternally(JSONObject content) + { + Relationship relationship = EntityBuilder.get(api).createRelationship(content); + switch (relationship.getType()) + { + case FRIEND: + api.getEventManager().handle( + new FriendAddedEvent( + api, responseNumber, + relationship)); + break; + case BLOCKED: + api.getEventManager().handle( + new UserBlockedEvent( + api, responseNumber, + relationship)); + break; + case INCOMING_FRIEND_REQUEST: + api.getEventManager().handle( + new FriendRequestReceivedEvent( + api, responseNumber, + relationship)); + break; + case OUTGOING_FRIEND_REQUEST: + api.getEventManager().handle( + new FriendRequestSentEvent( + api, responseNumber, + relationship)); + break; + default: + WebSocketClient.LOG.warn("Received a RELATIONSHIP_ADD with an unknown type! JSON: " + content); + return null; + } + EventCache.get(api).playbackCache(EventCache.Type.RELATIONSHIP, relationship.getUser().getId()); + return null; + } +} diff --git a/src/main/java/net/dv8tion/jda/client/handle/RelationshipRemoveHandler.java b/src/main/java/net/dv8tion/jda/client/handle/RelationshipRemoveHandler.java new file mode 100644 index 0000000000..8dfde3abb4 --- /dev/null +++ b/src/main/java/net/dv8tion/jda/client/handle/RelationshipRemoveHandler.java @@ -0,0 +1,94 @@ +/* + * 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.handle; + +import net.dv8tion.jda.client.entities.Relationship; +import net.dv8tion.jda.client.entities.RelationshipType; +import net.dv8tion.jda.client.entities.impl.JDAClientImpl; +import net.dv8tion.jda.client.events.relationship.FriendRemovedEvent; +import net.dv8tion.jda.client.events.relationship.FriendRequestCanceledEvent; +import net.dv8tion.jda.client.events.relationship.FriendRequestIgnoredEvent; +import net.dv8tion.jda.client.events.relationship.UserUnblockedEvent; +import net.dv8tion.jda.core.entities.impl.JDAImpl; +import net.dv8tion.jda.core.handle.EventCache; +import net.dv8tion.jda.core.handle.SocketHandler; +import net.dv8tion.jda.core.requests.WebSocketClient; +import org.json.JSONObject; + +public class RelationshipRemoveHandler extends SocketHandler +{ + public RelationshipRemoveHandler(JDAImpl api) + { + super(api); + } + + @Override + protected String handleInternally(JSONObject content) + { + String userId = content.getString("id"); + RelationshipType type = RelationshipType.fromKey(content.getInt("type")); + + //Technically this could be used to detect when another user has unblocked us, + // but it seems like functionality that may change so I'm not supporting it. + if (type == RelationshipType.NO_RELATIONSHIP) + return null; + + Relationship relationship = api.asClient().getRelationshipById(userId, type); + if (relationship == null) + { + EventCache.get(api).cache(EventCache.Type.RELATIONSHIP, userId, () -> + { + handle(responseNumber, allContent); + }); + EventCache.LOG.debug("Received a RELATIONSHIP_REMOVE for a relationship that was not yet cached! JSON: " + content); + return null; + } + ((JDAClientImpl) api.asClient()).getRelationshipMap().remove(userId); + + switch (type) + { + case FRIEND: + api.getEventManager().handle( + new FriendRemovedEvent( + api, responseNumber, + relationship)); + break; + case BLOCKED: + api.getEventManager().handle( + new UserUnblockedEvent( + api, responseNumber, + relationship)); + break; + case INCOMING_FRIEND_REQUEST: + api.getEventManager().handle( + new FriendRequestIgnoredEvent( + api, responseNumber, + relationship)); + break; + case OUTGOING_FRIEND_REQUEST: + api.getEventManager().handle( + new FriendRequestCanceledEvent( + api, responseNumber, + relationship)); + break; + default: + WebSocketClient.LOG.warn("Received a RELATIONSHIP_REMOVE with an unknown RelationshipType! JSON: " + content); + return null; + } + return null; + } +} diff --git a/src/main/java/net/dv8tion/jda/core/handle/EventCache.java b/src/main/java/net/dv8tion/jda/core/handle/EventCache.java index f0a1be0374..8ee7b3e1dc 100644 --- a/src/main/java/net/dv8tion/jda/core/handle/EventCache.java +++ b/src/main/java/net/dv8tion/jda/core/handle/EventCache.java @@ -103,6 +103,6 @@ public void clear() public enum Type { - USER, GUILD, CHANNEL, ROLE + USER, GUILD, CHANNEL, ROLE, RELATIONSHIP } } diff --git a/src/main/java/net/dv8tion/jda/core/hooks/ListenerAdapter.java b/src/main/java/net/dv8tion/jda/core/hooks/ListenerAdapter.java index 153d00e4b9..cee256b77b 100644 --- a/src/main/java/net/dv8tion/jda/core/hooks/ListenerAdapter.java +++ b/src/main/java/net/dv8tion/jda/core/hooks/ListenerAdapter.java @@ -15,13 +15,13 @@ */ package net.dv8tion.jda.core.hooks; -import net.dv8tion.jda.client.entities.Group; import net.dv8tion.jda.client.events.group.*; import net.dv8tion.jda.client.events.group.update.GenericGroupUpdateEvent; import net.dv8tion.jda.client.events.group.update.GroupUpdateIconEvent; import net.dv8tion.jda.client.events.group.update.GroupUpdateNameEvent; import net.dv8tion.jda.client.events.group.update.GroupUpdateOwnerEvent; import net.dv8tion.jda.client.events.message.group.*; +import net.dv8tion.jda.client.events.relationship.*; import net.dv8tion.jda.core.events.*; import net.dv8tion.jda.core.events.channel.priv.PrivateChannelCreateEvent; import net.dv8tion.jda.core.events.channel.priv.PrivateChannelDeleteEvent; @@ -58,7 +58,7 @@ *  @Override * public void onMessageReceived(MessageReceivedEvent event) * { - * System.out.printf("[%s]: %s\n", event.getAuthor().getUsername(), event.getMessage().getContent()); + * System.out.printf("[%s]: %s\n", event.getAuthor().getName(), event.getMessage().getContent()); * } * } * @see net.dv8tion.jda.core.hooks.EventListener @@ -115,6 +115,16 @@ public void onMessageEmbed(MessageEmbedEvent event) {} // public void onInviteReceived(InviteReceivedEvent event) {} + //Relationship Events (Client-Only) + public void onFriendAdded(FriendAddedEvent event) {} + public void onFriendRemoved(FriendRemovedEvent event) {} + public void onUserBlocked(UserBlockedEvent event) {} + public void onUserUnblocked(UserUnblockedEvent event) {} + public void onFriendRequestSent(FriendRequestSentEvent event) {} + public void onFriendRequestCanceled(FriendRequestCanceledEvent event) {} + public void onFriendRequestReceived(FriendRequestReceivedEvent event) {} + public void onFriendRequestIgnored(FriendRequestIgnoredEvent event) {} + //TextChannel Events public void onTextChannelDelete(TextChannelDeleteEvent event) {} public void onTextChannelUpdateName(TextChannelUpdateNameEvent event) {} @@ -205,12 +215,15 @@ public void onRoleUpdatePosition(RoleUpdatePositionEvent event) {} // public void onAudioRegionChange(AudioRegionChangeEvent event) {} //Generic Events - public void onGenericSelfUpdate(GenericSelfUpdateEvent event) {} - public void onGenericUser(GenericUserEvent event) {} public void onGenericMessage(GenericMessageEvent event) {} public void onGenericGuildMessage(GenericGuildMessageEvent event) {} public void onGenericPrivateMessage(GenericPrivateMessageEvent event) {} public void onGenericGroupMessage(GenericGroupMessageEvent event) {} + public void onGenericUser(GenericUserEvent event) {} + public void onGenericSelfUpdate(GenericSelfUpdateEvent event) {} + public void onGenericRelationship(GenericRelationshipEvent event) {} + public void onGenericRelationshipAdd(GenericRelationshipAddEvent event) {} + public void onGenericRelationshipRemove(GenericRelationshipRemoveEvent event) {} public void onGenericTextChannel(GenericTextChannelEvent event) {} public void onGenericTextChannelUpdate(GenericTextChannelUpdateEvent event) {} public void onGenericVoiceChannel(GenericVoiceChannelEvent event) {} @@ -220,7 +233,7 @@ public void onGenericGroupUpdate(GenericGroupUpdateEvent event) {} public void onGenericGuild(GenericGuildEvent event) {} public void onGenericGuildUpdate(GenericGuildUpdateEvent event) {} public void onGenericGuildMember(GenericGuildMemberEvent event) {} - public void onGenericRoleEvent(GenericRoleEvent event) {} + public void onGenericRole(GenericRoleEvent event) {} public void onGenericRoleUpdate(GenericRoleUpdateEvent event) {} // public void onGenericVoice(GenericVoiceEvent event) {} // public void onGenericAudio(GenericAudioEvent event) {} @@ -242,29 +255,6 @@ else if (event instanceof ShutdownEvent) else if (event instanceof StatusChangeEvent) onStatusChange((StatusChangeEvent) event); - //User Events - else if (event instanceof UserNameUpdateEvent) - onUserNameUpdate((UserNameUpdateEvent) event); - else if (event instanceof UserAvatarUpdateEvent) - onUserAvatarUpdate((UserAvatarUpdateEvent) event); - else if (event instanceof UserGameUpdateEvent) - onUserGameUpdate((UserGameUpdateEvent) event); - else if (event instanceof UserOnlineStatusUpdateEvent) - onUserOnlineStatusUpdate((UserOnlineStatusUpdateEvent) event); - else if (event instanceof UserTypingEvent) - onUserTyping((UserTypingEvent) event); - - else if (event instanceof SelfUpdateAvatarEvent) - onSelfUpdateAvatar((SelfUpdateAvatarEvent) event); - else if (event instanceof SelfUpdateEmailEvent) - onSelfUpdateEmail((SelfUpdateEmailEvent) event); - else if (event instanceof SelfUpdateMFAEvent) - onSelfUpdateMFA((SelfUpdateMFAEvent) event); - else if (event instanceof SelfUpdateNameEvent) - onSelfUpdateName((SelfUpdateNameEvent) event); - else if (event instanceof SelfUpdateVerifiedEvent) - onSelfUpdateVerified((SelfUpdateVerifiedEvent) event); - //Message Events //Guild (TextChannel) Message Events else if (event instanceof GuildMessageReceivedEvent) @@ -311,6 +301,48 @@ else if (event instanceof MessageEmbedEvent) // else if (event instanceof InviteReceivedEvent) // onInviteReceived(((InviteReceivedEvent) event)); + //User Events + else if (event instanceof UserNameUpdateEvent) + onUserNameUpdate((UserNameUpdateEvent) event); + else if (event instanceof UserAvatarUpdateEvent) + onUserAvatarUpdate((UserAvatarUpdateEvent) event); + else if (event instanceof UserGameUpdateEvent) + onUserGameUpdate((UserGameUpdateEvent) event); + else if (event instanceof UserOnlineStatusUpdateEvent) + onUserOnlineStatusUpdate((UserOnlineStatusUpdateEvent) event); + else if (event instanceof UserTypingEvent) + onUserTyping((UserTypingEvent) event); + + //Self Events + else if (event instanceof SelfUpdateAvatarEvent) + onSelfUpdateAvatar((SelfUpdateAvatarEvent) event); + else if (event instanceof SelfUpdateEmailEvent) + onSelfUpdateEmail((SelfUpdateEmailEvent) event); + else if (event instanceof SelfUpdateMFAEvent) + onSelfUpdateMFA((SelfUpdateMFAEvent) event); + else if (event instanceof SelfUpdateNameEvent) + onSelfUpdateName((SelfUpdateNameEvent) event); + else if (event instanceof SelfUpdateVerifiedEvent) + onSelfUpdateVerified((SelfUpdateVerifiedEvent) event); + + //Relationship Events + else if (event instanceof FriendAddedEvent) + onFriendAdded((FriendAddedEvent) event); + else if (event instanceof FriendRemovedEvent) + onFriendRemoved((FriendRemovedEvent) event); + else if (event instanceof UserBlockedEvent) + onUserBlocked((UserBlockedEvent) event); + else if (event instanceof UserUnblockedEvent) + onUserUnblocked((UserUnblockedEvent) event); + else if (event instanceof FriendRequestSentEvent) + onFriendRequestSent((FriendRequestSentEvent) event); + else if (event instanceof FriendRequestCanceledEvent) + onFriendRequestCanceled((FriendRequestCanceledEvent) event); + else if (event instanceof FriendRequestReceivedEvent) + onFriendRequestReceived((FriendRequestReceivedEvent) event); + else if (event instanceof FriendRequestIgnoredEvent) + onFriendRequestIgnored((FriendRequestIgnoredEvent) event); + //TextChannel Events else if (event instanceof TextChannelCreateEvent) onTextChannelCreate((TextChannelCreateEvent) event); @@ -469,12 +501,16 @@ else if (event instanceof RoleUpdatePositionEvent) //Generic Events //Start a new if statement so that these are no overridden by the above events. - if (event instanceof GenericPrivateMessageEvent) - onGenericPrivateMessage((GenericPrivateMessageEvent) event); - else if (event instanceof GenericGuildMessageEvent) + if (event instanceof GenericGuildMessageEvent) onGenericGuildMessage((GenericGuildMessageEvent) event); + else if (event instanceof GenericPrivateMessageEvent) + onGenericPrivateMessage((GenericPrivateMessageEvent) event); else if (event instanceof GenericGroupMessageEvent) onGenericGroupMessage((GenericGroupMessageEvent) event); + else if (event instanceof GenericRelationshipAddEvent) + onGenericRelationshipAdd((GenericRelationshipAddEvent) event); + else if (event instanceof GenericRelationshipRemoveEvent) + onGenericRelationshipRemove((GenericRelationshipRemoveEvent) event); else if (event instanceof GenericTextChannelUpdateEvent) onGenericTextChannelUpdate((GenericTextChannelUpdateEvent) event); else if (event instanceof GenericVoiceChannelUpdateEvent) @@ -493,21 +529,24 @@ else if (event instanceof GenericRoleUpdateEvent) // onGenericAudio((GenericAudioEvent) event); // //Generic events that have generic subclasses (the subclasses as above). - if (event instanceof GenericUserEvent) + + if (event instanceof GenericMessageEvent) + onGenericMessage((GenericMessageEvent) event); + else if (event instanceof GenericUserEvent) onGenericUser((GenericUserEvent) event); else if (event instanceof GenericSelfUpdateEvent) onGenericSelfUpdate((GenericSelfUpdateEvent) event); - else if (event instanceof GenericMessageEvent) - onGenericMessage((GenericMessageEvent) event); + else if (event instanceof GenericRelationshipEvent) + onGenericRelationship((GenericRelationshipEvent) event); else if (event instanceof GenericTextChannelEvent) onGenericTextChannel((GenericTextChannelEvent) event); else if (event instanceof GenericVoiceChannelEvent) onGenericVoiceChannel((GenericVoiceChannelEvent) event); - else if (event instanceof GenericGroupEvent) - onGenericGroup((GenericGroupEvent) event); else if (event instanceof GenericGuildEvent) onGenericGuild((GenericGuildEvent) event); + else if (event instanceof GenericGroupEvent) + onGenericGroup((GenericGroupEvent) event); else if (event instanceof GenericRoleEvent) - onGenericRoleEvent((GenericRoleEvent) event); + onGenericRole((GenericRoleEvent) event); } } diff --git a/src/main/java/net/dv8tion/jda/core/requests/WebSocketClient.java b/src/main/java/net/dv8tion/jda/core/requests/WebSocketClient.java index 1c35079676..912d0e6a26 100644 --- a/src/main/java/net/dv8tion/jda/core/requests/WebSocketClient.java +++ b/src/main/java/net/dv8tion/jda/core/requests/WebSocketClient.java @@ -19,6 +19,8 @@ import com.neovisionaries.ws.client.*; import net.dv8tion.jda.client.handle.ChannelRecipientAddHandler; import net.dv8tion.jda.client.handle.ChannelRecipientRemoveHandler; +import net.dv8tion.jda.client.handle.RelationshipAddHandler; +import net.dv8tion.jda.client.handle.RelationshipRemoveHandler; import net.dv8tion.jda.core.AccountType; import net.dv8tion.jda.core.JDA; import net.dv8tion.jda.core.entities.EntityBuilder; @@ -742,6 +744,8 @@ private void setupHandlers() { handlers.put("CHANNEL_RECIPIENT_ADD", new ChannelRecipientAddHandler(api)); handlers.put("CHANNEL_RECIPIENT_REMOVE", new ChannelRecipientRemoveHandler(api)); + handlers.put("RELATIONSHIP_ADD", new RelationshipAddHandler(api)); + handlers.put("RELATIONSHIP_REMOVE", new RelationshipRemoveHandler(api)); } } }