How to create command arguments?

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.


package com.blockenton.plugins.base.voter.command;

import com.google.common.base.Optional;
import org.spongepowered.api.Server;
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<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();
}

}

String arguments

in the call method is what you are looking for.

1 Like

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!

String args[] = arguments.split(" ")
Simple, really

1 Like

Tell me, what does split(" ") do even?

Please use google for these questions

You need to know the basics of Java, String.split can be found here

3 Likes

http://docs.oracle.com/javase/tutorial/
http://www.java2s.com/Tutorial/Java/CatalogJava.htm
http://www.learnjavaonline.org/

1 Like

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();
    }
}

I posted a new question, could you answer it for me?
PS Like my avatar?

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

2 Likes

In fact, I already tried that! But if I use try and catch, I cannot seem to acess MapNumber outside the try/catch blocks.

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(" ");
        try {
            int MapNumber = Integer.parseInt(args[1]);
        } catch(Exception e) {
        }

        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();
    }
}
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

Problem: solved

2 Likes

Yep. Solved. THX!

package com.blockenton.plugins.base.voter.command;

import com.blockenton.plugins.base.voter.message.ErrorMessage;

import com.google.common.base.Optional;

import org.slf4j.Logger;
import org.spongepowered.api.Server;
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 Logger logger;

private Logger getLogger() {
    return logger;
}

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 {
    String[] args = arguments.split(" ");
    try {
        int MapNumber = Integer.parseInt(args[1]);
        
        if (MapNumber == 1) {
            
        }
        
        if (MapNumber == 2) {
            
        }
        
        if(MapNumber == 3) {
            
        }
        
        if(MapNumber == 4) {
            
        }
        
        if(MapNumber == 5) {
            
        }
        
        if(MapNumber == 6) {
            
        }
        else if(MapNumber < 1 & MapNumber > 6) {
            source.sendMessage(ErrorMessage.InvalidOptionError());
        }
    } catch (Exception e) {
        source.sendMessage(ErrorMessage.InvalidOptionError());
    }
    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();
}

}

1 Like

Tip: Learn Java first.

5 Likes

Ok, so I want each player to only vote once. How is that possible?

package com.blockenton.plugins.base.voter.command;

import com.blockenton.plugins.base.voter.message.ErrorMessage;

import com.google.common.base.Optional;

import org.slf4j.Logger;
import org.spongepowered.api.Server;
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 Logger logger;

    private Logger getLogger() {
        return logger;
    }

    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(" ");
        try {
            int MapNumber = Integer.parseInt(args[1]);

            if (MapNumber == 1) {
                
            }

            if (MapNumber == 2) {

            }

            if(MapNumber == 3) {

            }

            if(MapNumber == 4) {

            }

            if(MapNumber == 5) {

            }

            if(MapNumber == 6) {

            }
            else if(MapNumber < 1 & MapNumber > 6) {
                source.sendMessage(ErrorMessage.InvalidOptionError());
            }
        } catch (Exception e) {
            source.sendMessage(ErrorMessage.InvalidOptionError());
        }
        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();
    }
}

This’ll solve your problem easily.

2 Likes

So I need to create multiple instances of a class, so that each instance could store a player’s information?

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.

1 Like

So, do you mean the Collections Api?

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.