Skip to content

Commit

Permalink
implement list subcommand(close #12)
Browse files Browse the repository at this point in the history
  • Loading branch information
kory33 committed Apr 22, 2017
1 parent 8b853b5 commit 0a682f4
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 1 deletion.
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,10 @@ public class MessageConfigurationNodes {
public static final String UNVOTE_UI = UI_ROOT + ".unvote";
public static final String UNVOTE_UI_HEADING = UNVOTE_UI + ".heading";
public static final String UNVOTE_UI_COMFIRM = UNVOTE_UI + ".comfirm";

public static final String LIST_UI = UI_ROOT + ".list";
public static final String LIST_UI_HEADING = LIST_UI + ".heading";
public static final String LIST_UI_LINE_PREFIX = LIST_UI + ".lineprefix";
public static final String LIST_UI_SESSION_OPEN = LIST_UI + ".state.open";
public static final String LIST_UI_SESSION_CLOSED = LIST_UI + ".state.closed";
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ public class PermissionNodes {

public static final String SAVE = BASE_NODE + ".save";

public static final String LIST_SESSION = BASE_NODE + ".list";
public static final String LIST_SESSION = BASE_NODE + ".listsession";
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public Result filter(LogEvent event) {
}

private Result filter(final String text) {
if (text == null) {
return super.onMismatch;
}

final Matcher matcher = matchPattern.matcher(text);
if (matcher.find()) {
return super.onMatch;
Expand Down
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);
}
}
12 changes: 12 additions & 0 deletions src/main/resources/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
"summary": "delete the specified vote session.",
"deleted": "§aSession §6{0} §ahas been successfully §cDELETED"
},
"list": {
"help": "/signvote list - See the list of existing sessions",
"summary": "See the list of existing sessions."
},
"deletevp": {
"help": "deletevp - delete the specified votepoint",
"summary": "delete the specified votepoint",
Expand Down Expand Up @@ -96,6 +100,14 @@
"unvote": {
"heading": "§aYou have already voted to §6{0} §awith a score of §6{1}.\n§eClick the button below to CANCEL this vote.",
"comfirm": "§cComfirm the vote to be cancelled.\n"
},
"list": {
"heading": "§bList of existing sesions:\n",
"lineprefix": "§6",
"state": {
"open": " §a(open)",
"closed": " §e(closed)"
}
}
}
}
3 changes: 3 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ permissions:
signvote.modifysession:
description: allows a player to modify the existing session
default: op
signvote.listsession:
description: allows a player to obtain the session list
default: op
signvote.opensession:
description: allows a player to re-open a closed session
default: op
Expand Down

0 comments on commit 0a682f4

Please sign in to comment.