I’ve always liked the HiveMC Server and it has a voting system whereas players use /vote (map number, from 1 to 6). I want to create a plugin like that, but I don’t know how to create command arguments. Below is a part of my code.
public class VoteCommand implements CommandCallable {
private final Server server;
private final Optional<String> desc = Optional.of("Vote for a specific map");
private final Optional<String> help = Optional.of("Vote for a specific map");
public VoteCommand(Server server) {
this.server = server;
}
public boolean call(CommandSource source, String arguments, List<String> parents) throws CommandException {
return true;
}
public boolean testPermission(CommandSource source) {
return source.hasPermission("groups.user");
}
public Optional<String> getShortDescription() {
return desc;
}
public Optional<String> getHelp() {
return help;
}
public String getUsage() {
return "/<vote|lobby:vote|v> <map>";
}
public List<String> getSuggestions(CommandSource source, String arguments) throws CommandException {
return Collections.emptyList();
}
Could you provide me a snippet? In Bukkit they use if (args[0].equalsIgnoreCase(“example”){ //do something }. How would this work in Sponge? arguments is not a String array!
I have another problem, I followed what you said, but the voter might of insert a letter as an argument instead of a number. Therefore int MapNum = Interger.parseInt(args[1]); will result in a internal error!
package com.blockenton.plugins.base.voter.command;
import com.google.common.base.Optional;
import org.spongepowered.api.Server;
import org.spongepowered.api.text.format.TextColors;
import org.spongepowered.api.text.message.Messages;
import org.spongepowered.api.util.command.CommandCallable;
import org.spongepowered.api.util.command.CommandException;
import org.spongepowered.api.util.command.CommandSource;
import java.util.Collections;
import java.util.List;
public class VoteCommand implements CommandCallable {
private final Server server;
private final Optional desc = Optional.of("Vote for a specific map");
private final Optional help = Optional.of("Vote for a specific map");
public VoteCommand(Server server) {
this.server = server;
}
public boolean call(CommandSource source, String arguments, List parents) throws CommandException {
String[] args = arguments.split(" ");
int MapNumber = Integer.parseInt(args[1]);
return true;
}
public boolean testPermission(CommandSource source) {
return source.hasPermission("groups.user");
}
public Optional getShortDescription() {
return desc;
}
public Optional getHelp() {
return help;
}
public String getUsage() {
return "/ ";
}
public List getSuggestions(CommandSource source, String arguments) throws CommandException {
return Collections.emptyList();
}
}
That’s for you to figure out. I’m not going to go through every step otherwise I may as well write the plugin!
This is not an affective way to learn programming.
Hint: if there is an exception, catch it, then handle accordingly
int mapNumber;
try {
mapNumber = Integer.parseInt(arg[1]);
} catch (NumberFormatException e) {
source.sendMessage("Map argument must be an integer");
return true;
}
// Now we have mapNumber:
doSomething(mapNumber); // Example
You should probably listen to @DotDash and learn Java before jumping into plugin creation.
But to answer your question – every time someone votes, store their UUID, Player instance, name, or some form of identification in a list.
If someone uses the vote command then compare their identifier to an object stored in the list, if their identifier is in the list, disallow them from voting.
Collection, List, ArrayList, Map, HashMap, Set, Queue, etc. – whatever you wish. Each has their own best use. Look at all the types of collections at this link: Java Collections. Then decide from that which would be the best for your situation.