-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement list subcommand(close #12)
- Loading branch information
Showing
7 changed files
with
128 additions
and
1 deletion.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/main/java/com/github/kory33/signvote/command/subcommand/ListCommandExecutor.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,45 @@ | ||
package com.github.kory33.signvote.command.subcommand; | ||
|
||
import java.util.ArrayList; | ||
import java.util.stream.Stream; | ||
|
||
import org.bukkit.command.Command; | ||
import org.bukkit.command.CommandSender; | ||
|
||
import com.github.kory33.signvote.configurable.JSONConfiguration; | ||
import com.github.kory33.signvote.constants.MessageConfigurationNodes; | ||
import com.github.kory33.signvote.constants.PermissionNodes; | ||
import com.github.kory33.signvote.core.SignVote; | ||
import com.github.kory33.signvote.manager.VoteSessionManager; | ||
import com.github.kory33.signvote.session.VoteSession; | ||
import com.github.kory33.signvote.ui.ChatInterface; | ||
import com.github.kory33.signvote.ui.ListSessionInterface; | ||
|
||
public class ListCommandExecutor extends SubCommandExecutor { | ||
private final JSONConfiguration messageConfig; | ||
private final VoteSessionManager voteSessionManager; | ||
|
||
public ListCommandExecutor(SignVote plugin) { | ||
this.messageConfig = plugin.getMessagesConfiguration(); | ||
this.voteSessionManager = plugin.getVoteSessionManager(); | ||
} | ||
|
||
@Override | ||
protected String getHelpString() { | ||
return messageConfig.getString(MessageConfigurationNodes.LIST_COMMAND_HELP); | ||
} | ||
|
||
@Override | ||
public boolean onCommand(CommandSender sender, Command command, ArrayList<String> args) { | ||
if (!sender.hasPermission(PermissionNodes.LIST_SESSION)) { | ||
sender.sendMessage(messageConfig.getString(MessageConfigurationNodes.MISSING_PERMS)); | ||
return true; | ||
} | ||
|
||
Stream<VoteSession> sessions = this.voteSessionManager.getVoteSessionStream(); | ||
ChatInterface listInterface = new ListSessionInterface(sessions, this.messageConfig); | ||
listInterface.send(sender); | ||
|
||
return true; | ||
} | ||
} |
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
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
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
57 changes: 57 additions & 0 deletions
57
src/main/java/com/github/kory33/signvote/ui/ListSessionInterface.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,57 @@ | ||
package com.github.kory33.signvote.ui; | ||
|
||
import java.util.ArrayList; | ||
import java.util.stream.Stream; | ||
|
||
import com.github.kory33.signvote.configurable.JSONConfiguration; | ||
import com.github.kory33.signvote.constants.MessageConfigurationNodes; | ||
import com.github.kory33.signvote.session.VoteSession; | ||
import com.github.ucchyocean.messaging.tellraw.MessageComponent; | ||
import com.github.ucchyocean.messaging.tellraw.MessageParts; | ||
|
||
public class ListSessionInterface extends ChatInterface { | ||
private final Stream<VoteSession> sessionStream; | ||
private final JSONConfiguration messageConfig; | ||
|
||
public ListSessionInterface(Stream<VoteSession> sessionStream, JSONConfiguration messageConfig) { | ||
this.sessionStream = sessionStream; | ||
this.messageConfig = messageConfig; | ||
} | ||
|
||
private MessageParts getConfigMessagePart(String configurationNode) { | ||
return new MessageParts(this.messageConfig.getString(configurationNode)); | ||
} | ||
|
||
private String getSessionInfoLine(VoteSession session) { | ||
String prefix = messageConfig.getString(MessageConfigurationNodes.LIST_UI_LINE_PREFIX); | ||
String sessionName = session.getName(); | ||
String sessionState = ""; | ||
if (session.isOpen()) { | ||
sessionState = messageConfig.getString(MessageConfigurationNodes.LIST_UI_SESSION_OPEN); | ||
} else { | ||
sessionState = messageConfig.getString(MessageConfigurationNodes.LIST_UI_SESSION_CLOSED); | ||
} | ||
|
||
return prefix + sessionName + sessionState + "\n"; | ||
} | ||
|
||
@Override | ||
protected MessageComponent constructInterfaceMessages() { | ||
MessageParts header = this.getConfigMessagePart(MessageConfigurationNodes.UI_HEADER); | ||
MessageParts footer = this.getConfigMessagePart(MessageConfigurationNodes.UI_FOOTER); | ||
|
||
MessageParts heading = this.getConfigMessagePart(MessageConfigurationNodes.LIST_UI_HEADING); | ||
|
||
ArrayList<MessageParts> messageList = new ArrayList<>(); | ||
messageList.add(header); | ||
messageList.add(heading); | ||
|
||
this.sessionStream | ||
.map(session -> new MessageParts(this.getSessionInfoLine(session))) | ||
.forEach(messageList::add); | ||
|
||
messageList.add(footer); | ||
|
||
return new MessageComponent(messageList); | ||
} | ||
} |
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
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