-
-
Notifications
You must be signed in to change notification settings - Fork 739
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
319 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
src/main/java/net/dv8tion/jda/api/managers/SoundboardSoundManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/* | ||
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors | ||
* | ||
* 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.api.managers; | ||
|
||
import net.dv8tion.jda.api.entities.Guild; | ||
import net.dv8tion.jda.api.entities.SoundboardSound; | ||
import net.dv8tion.jda.api.entities.emoji.Emoji; | ||
|
||
import javax.annotation.CheckReturnValue; | ||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* Manager providing functionality to update one or more fields for a {@link SoundboardSound}. | ||
* | ||
* <p><b>Example</b> | ||
* <pre>{@code | ||
* manager.setVolume(0.33) | ||
* .setEmoji(null) | ||
* .queue(); | ||
* manager.reset(SoundboardSoundManager.VOLUME | SoundboardSoundManager.EMOJI) | ||
* .setVolume("1") | ||
* .setEmoji(Color.RED) | ||
* .queue(); | ||
* }</pre> | ||
* | ||
* @see SoundboardSound#getManager() | ||
*/ | ||
public interface SoundboardSoundManager extends Manager<SoundboardSoundManager> | ||
{ | ||
/** Used to reset the name field */ | ||
long NAME = 1; | ||
/** Used to reset the volume field */ | ||
long VOLUME = 1 << 1; | ||
/** Used to reset the emoji field */ | ||
long EMOJI = 1 << 2; | ||
|
||
/** | ||
* Resets the fields specified by the provided bit-flag pattern. | ||
* You can specify a combination by using a bitwise OR concat of the flag constants. | ||
* <br>Example: {@code manager.reset(SoundboardSoundManager.VOLUME | SoundboardSoundManager.EMOJI);} | ||
* | ||
* <p><b>Flag Constants:</b> | ||
* <ul> | ||
* <li>{@link #NAME}</li> | ||
* <li>{@link #VOLUME}</li> | ||
* <li>{@link #EMOJI}</li> | ||
* </ul> | ||
* | ||
* @param fields | ||
* Integer value containing the flags to reset. | ||
* | ||
* @return SoundboardSoundManager for chaining convenience | ||
*/ | ||
@Nonnull | ||
@Override | ||
@CheckReturnValue | ||
SoundboardSoundManager reset(long fields); | ||
|
||
/** | ||
* Resets the fields specified by the provided bit-flag patterns. | ||
* <br>Example: {@code manager.reset(SoundboardSoundManager.VOLUME | SoundboardSoundManager.EMOJI);} | ||
* | ||
* <p><b>Flag Constants:</b> | ||
* <ul> | ||
* <li>{@link #NAME}</li> | ||
* <li>{@link #VOLUME}</li> | ||
* <li>{@link #EMOJI}</li> | ||
* </ul> | ||
* | ||
* @param fields | ||
* Integer values containing the flags to reset. | ||
* | ||
* @return SoundboardSoundManager for chaining convenience | ||
*/ | ||
@Nonnull | ||
@Override | ||
@CheckReturnValue | ||
SoundboardSoundManager reset(long... fields); | ||
|
||
/** | ||
* The target {@link SoundboardSound} for this manager | ||
* | ||
* @return The target SoundboardSound | ||
*/ | ||
@Nonnull | ||
SoundboardSound getSoundboardSound(); | ||
|
||
/** | ||
* The {@link Guild} this Manager's {@link SoundboardSound} is in. | ||
* | ||
* @return The parent {@link Guild} | ||
*/ | ||
@Nonnull | ||
Guild getGuild(); | ||
|
||
/** | ||
* Sets the <b><u>name</u></b> of the selected {@link SoundboardSound}. | ||
* | ||
* <p>A role name <b>must not</b> be {@code null} nor less than 2 characters or more than 32 characters long! | ||
* | ||
* @param name | ||
* The new name for the selected {@link SoundboardSound} | ||
* | ||
* @throws IllegalArgumentException | ||
* If the provided name is {@code null} or not between 2-32 characters long | ||
* | ||
* @return SoundboardSoundManager for chaining convenience | ||
*/ | ||
@Nonnull | ||
@CheckReturnValue | ||
SoundboardSoundManager setName(@Nonnull String name); | ||
|
||
/** | ||
* Sets the <b><u>volume</u></b> of the selected {@link SoundboardSound}. | ||
* | ||
* @param volume | ||
* The new volume for the selected {@link SoundboardSound} | ||
* | ||
* @throws IllegalArgumentException | ||
* If the provided volume is not between 0-1 | ||
* | ||
* @return SoundboardSoundManager for chaining convenience | ||
*/ | ||
@Nonnull | ||
@CheckReturnValue | ||
SoundboardSoundManager setVolume(double volume); | ||
|
||
/** | ||
* Sets the <b><u>emoji</u></b> of the selected {@link SoundboardSound}. | ||
* | ||
* @param emoji | ||
* The new emoji for the selected {@link SoundboardSound} | ||
* | ||
* @return SoundboardSoundManager for chaining convenience | ||
*/ | ||
@Nonnull | ||
@CheckReturnValue | ||
SoundboardSoundManager setEmoji(@Nullable Emoji emoji); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
src/main/java/net/dv8tion/jda/internal/managers/SoundboardSoundManagerImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* | ||
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors | ||
* | ||
* 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.internal.managers; | ||
|
||
import net.dv8tion.jda.api.entities.Guild; | ||
import net.dv8tion.jda.api.entities.SoundboardSound; | ||
import net.dv8tion.jda.api.entities.emoji.CustomEmoji; | ||
import net.dv8tion.jda.api.entities.emoji.Emoji; | ||
import net.dv8tion.jda.api.managers.SoundboardSoundManager; | ||
import net.dv8tion.jda.api.requests.Route; | ||
import net.dv8tion.jda.api.utils.data.DataObject; | ||
import okhttp3.RequestBody; | ||
|
||
import javax.annotation.CheckReturnValue; | ||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
|
||
public class SoundboardSoundManagerImpl extends ManagerBase<SoundboardSoundManager> implements SoundboardSoundManager | ||
{ | ||
private SoundboardSound soundboardSound; | ||
private String name; | ||
private double volume; | ||
private Emoji emoji; | ||
|
||
public SoundboardSoundManagerImpl(SoundboardSound soundboardSound) | ||
{ | ||
super(soundboardSound.getJDA(), Route.SoundboardSounds.MODIFY_GUILD_SOUNDBOARD_SOUNDS.compile(soundboardSound.getGuild().getId(), soundboardSound.getId())); | ||
this.soundboardSound = soundboardSound; | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
@SuppressWarnings("DataFlowIssue") | ||
public Guild getGuild() | ||
{ | ||
return soundboardSound.getGuild(); | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public SoundboardSound getSoundboardSound() | ||
{ | ||
final SoundboardSound soundboardSound = getGuild().getSoundboardSoundById(this.soundboardSound.getId()); | ||
if (soundboardSound != null) | ||
this.soundboardSound = soundboardSound; | ||
return this.soundboardSound; | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
@CheckReturnValue | ||
public SoundboardSoundManagerImpl reset(long fields) | ||
{ | ||
super.reset(fields); | ||
if ((fields & NAME) == NAME) | ||
this.name = null; | ||
if ((fields & VOLUME) == VOLUME) | ||
this.volume = 1; | ||
if ((fields & EMOJI) == EMOJI) | ||
this.emoji = null; | ||
return this; | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
@CheckReturnValue | ||
public SoundboardSoundManagerImpl reset(long... fields) | ||
{ | ||
super.reset(fields); | ||
return this; | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
@CheckReturnValue | ||
public SoundboardSoundManagerImpl reset() | ||
{ | ||
super.reset(); | ||
this.name = null; | ||
this.volume = 1; | ||
this.emoji = null; | ||
return this; | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public SoundboardSoundManagerImpl setName(@Nonnull String name) | ||
{ | ||
this.name = name; | ||
set |= NAME; | ||
return this; | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public SoundboardSoundManagerImpl setVolume(double volume) | ||
{ | ||
this.volume = volume; | ||
set |= VOLUME; | ||
return this; | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public SoundboardSoundManagerImpl setEmoji(@Nullable Emoji emoji) | ||
{ | ||
this.emoji = emoji; | ||
set |= EMOJI; | ||
return this; | ||
} | ||
|
||
@Override | ||
protected RequestBody finalizeData() | ||
{ | ||
DataObject object = DataObject.empty().put("name", getSoundboardSound().getName()); | ||
if (shouldUpdate(NAME)) | ||
object.put("name", name); | ||
if (shouldUpdate(VOLUME)) | ||
object.put("volume", volume); | ||
if (shouldUpdate(EMOJI)) | ||
{ | ||
if (emoji instanceof CustomEmoji) | ||
object.put("emoji_id", ((CustomEmoji) emoji).getId()); | ||
else if (emoji != null) | ||
object.put("emoji_name", emoji.getName()); | ||
else | ||
{ | ||
object.put("emoji_id", null); | ||
object.put("emoji_name", null); | ||
} | ||
} | ||
reset(); | ||
return getRequestBody(object); | ||
} | ||
} |