forked from kuroppoi/brainwine
-
Notifications
You must be signed in to change notification settings - Fork 2
add music command #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
boraini
wants to merge
1
commit into
extended-features
Choose a base branch
from
music-command
base: extended-features
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
gameserver/src/main/java/brainwine/gameserver/command/admin/MusicCommand.java
This file contains hidden or 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,105 @@ | ||
| package brainwine.gameserver.command.admin; | ||
|
|
||
| import static brainwine.gameserver.player.NotificationType.SYSTEM; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import org.apache.commons.lang3.math.NumberUtils; | ||
|
|
||
| import brainwine.gameserver.GameConfiguration; | ||
| import brainwine.gameserver.command.Command; | ||
| import brainwine.gameserver.command.CommandExecutor; | ||
| import brainwine.gameserver.command.CommandInfo; | ||
| import brainwine.gameserver.player.Player; | ||
| import brainwine.gameserver.server.messages.EffectMessage; | ||
| import brainwine.gameserver.util.MapHelper; | ||
|
|
||
| // To add more music add new emitters in config-particles.yml. The emitter name should be in form "music musicname" | ||
|
|
||
| @CommandInfo(name = "music", description = "Plays ambient sound in the world.", aliases = { "mus" }) | ||
| public class MusicCommand extends Command { | ||
|
|
||
| @Override | ||
| public void execute(CommandExecutor executor, String[] args) { | ||
| if(args.length < 1) { | ||
| executor.notify(String.format("Usage: %s", getUsage(executor)), SYSTEM); | ||
| return; | ||
| } | ||
|
|
||
| String musicName = args[0]; | ||
|
|
||
| Map<String, String> emitters = MapHelper.getMap(GameConfiguration.getBaseConfig(), "emitters"); | ||
|
|
||
| if (emitters == null) { | ||
| executor.notify("The music command is not configured for this server.", SYSTEM); | ||
| return; | ||
| } | ||
|
|
||
| if ("list".equals(musicName)) { | ||
| List<String> allMusic = emitters.keySet().stream() | ||
| .filter(k -> k.startsWith("music ")) | ||
| .map(k -> { | ||
| int start = k.indexOf(" "); | ||
| if (start != 1) { | ||
| return k.substring(start + 1); | ||
| } else { | ||
| return k; | ||
| } | ||
| }).toList(); | ||
|
|
||
| int pageSize = 8; | ||
| int pageCount = (int)Math.ceil(allMusic.size() / (double)pageSize); | ||
| int page = 1; | ||
|
|
||
| if (args.length > 1) { | ||
| page = Math.max(1, NumberUtils.toInt(args[1], page)); | ||
| } | ||
|
|
||
| executor.notify(String.format("========== Music List (Page %s of %s) ==========", page, pageCount), SYSTEM); | ||
|
|
||
| int start = Math.max(0, (page - 1) * pageSize); | ||
| int stop = Math.min(allMusic.size(), page * pageSize); | ||
|
|
||
| for (int i = start; i < stop; i++) { | ||
| executor.notify(String.format("- %s", allMusic.get(i)), SYSTEM); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (!(executor instanceof Player)) { | ||
| executor.notify("Cannot play music from the server command line yet.", SYSTEM); | ||
| return; | ||
| } | ||
|
|
||
| String effectName = String.format("music %s", musicName); | ||
|
|
||
| if (emitters.containsKey(effectName)) { | ||
| Player player = (Player) executor; | ||
|
|
||
| // TODO: sound moves with players | ||
| if (player.getZone() != null) for (Player other : player.getZone().getPlayers()) { | ||
| EffectMessage message = new EffectMessage(other.getX(), other.getY(), effectName, 10.0f); | ||
| other.sendMessage(message); | ||
| } | ||
|
|
||
| return; | ||
| } else { | ||
| executor.notify(String.format("Music %s is not known to the server.", musicName), SYSTEM); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String getUsage(CommandExecutor executor) { | ||
| return "/music list [page] or /music <name>"; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean canExecute(CommandExecutor executor) { | ||
| // we need the zone information for the player | ||
| return executor.isAdmin(); | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The boundary should be -1 not 1.