-
Notifications
You must be signed in to change notification settings - Fork 6
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
1 parent
8777eaf
commit f3b4bd9
Showing
7 changed files
with
118 additions
and
27 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
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,37 @@ | ||
package io.pocketvote.cmd; | ||
|
||
import cn.nukkit.command.Command; | ||
import cn.nukkit.command.CommandSender; | ||
import cn.nukkit.command.PluginIdentifiableCommand; | ||
import cn.nukkit.plugin.Plugin; | ||
import cn.nukkit.utils.TextFormat; | ||
import io.pocketvote.PocketVote; | ||
import io.pocketvote.task.TopVoterTask; | ||
|
||
public class VoteCommand extends Command implements PluginIdentifiableCommand { | ||
|
||
private PocketVote plugin; | ||
|
||
public VoteCommand(PocketVote plugin) { | ||
super("vote", "PocketVote vote command", "/vote [top]", new String[]{"v"}); | ||
this.plugin = plugin; | ||
} | ||
|
||
@Override | ||
public boolean execute(CommandSender sender, String s, String[] args) { | ||
if(!sender.hasPermission("pocketvote.vote")) { | ||
sender.sendMessage(TextFormat.RED + "You do not have permission to use /vote."); | ||
return true; | ||
} | ||
if(args.length > 0 && args[0].equalsIgnoreCase("TOP")) { | ||
plugin.getServer().getScheduler().scheduleAsyncTask(plugin, new TopVoterTask(plugin, sender.getName())); | ||
return true; | ||
} | ||
return false; // TODO: Add vote link here when "top" is not specified. | ||
} | ||
|
||
@Override | ||
public Plugin getPlugin() { | ||
return plugin; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package io.pocketvote.task; | ||
|
||
import cn.nukkit.Server; | ||
import cn.nukkit.command.CommandSender; | ||
import cn.nukkit.command.ConsoleCommandSender; | ||
import cn.nukkit.utils.TextFormat; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import io.pocketvote.PocketVote; | ||
import io.pocketvote.data.TaskResult; | ||
|
||
public class TopVoterTask extends ApiRequest { | ||
|
||
private final String player; | ||
|
||
public TopVoterTask(PocketVote plugin, String player) { | ||
super(plugin.isDev() ? "http://127.0.0.1:9000/v2/top/10" : "https://api.pocketvote.io/v2/top/10", "GET", "TOP", null); | ||
this.player = player; | ||
|
||
plugin.getLogger().debug("Getting top voters."); | ||
} | ||
|
||
@Override | ||
public void onCompletion(Server server) { | ||
CommandSender player = this.player.equalsIgnoreCase("CONSOLE") ? new ConsoleCommandSender() : server.getPlayer(this.player); | ||
if(player == null) return; | ||
|
||
if(!(super.getResult() instanceof TaskResult) || !hasResult() || !(getResult() instanceof TaskResult)) { | ||
player.sendMessage(TextFormat.RED + "Failed to retrieve top voters. Try again later."); | ||
server.getLogger().error("[PocketVote] Result of " + getClass().getCanonicalName() + " was not an instance of TaskResult."); | ||
return; | ||
} | ||
|
||
TaskResult result = (TaskResult) getResult(); | ||
|
||
if(result.hasError()) { | ||
player.sendMessage(TextFormat.RED + "An error occurred while contacting the PocketVote servers, please try again later."); | ||
server.getLogger().error("[PocketVote] TopVoterTask: " + result.getMessage()); | ||
return; | ||
} | ||
|
||
if(!result.hasPayload() || !result.getRawPayload().isArray()) { | ||
server.getLogger().debug("[PocketVote] TopVoterTask: No payload or payload was not an array."); | ||
return; | ||
} | ||
|
||
player.sendMessage(TextFormat.AQUA + "### Current top 10 voters ###"); | ||
int rank = 1; | ||
boolean color = true; | ||
|
||
for(final JsonNode vote : result.getRawPayload()) { | ||
player.sendMessage("" + (color ? TextFormat.WHITE : TextFormat.GRAY) + rank + ". " + vote.get("player").asText() + " (" + vote.get("votes").asInt() + ")"); | ||
rank++; | ||
color = !color; | ||
} | ||
|
||
if(result.getRawPayload().size() == 0) player.sendMessage(TextFormat.GRAY + "No voters found, start voting!"); | ||
} | ||
|
||
} |
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