Jump to content
Null

Ruse - New command system

Recommended Posts

Example on what you're adding:

@CommandInfo(
        command = {"test", "tester", "testing"},
        description = "Test command.",
        rights = {SUPPORT, MODERATOR, GLOBAL_MODERATOR, ADMINISTRATOR, OWNER, DEVELOPER, COMMUNITYMANAGER}
)
public class Test extends Command {
    @Override
    public void execute(Player player, String command) {
        player.sendMessage("Lol working");
    }
}

CommandInfo.java

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE) //on class level
public @interface CommandInfo {
    String[] command() default "";
    String description() default "";
    int donationAmountRequired() default 0;
    PlayerRights[] rights() default PlayerRights.PLAYER;
}

Command.java

public class Command {
    protected Logger logger = Logger.getLogger(super.getClass());
    public void execute(Player player, String command) {};
}

CommandHandler.java

@Log4j
public class CommandHandler implements PacketListener {
    public static List<Class> commands = new ArrayList<>();
    @SneakyThrows
    @Override
    public void handleMessage(Player player, Packet packet) {
        String command = Misc.readString(packet.getBuffer());
        if (command.contains("\r") || command.contains("\n")) {
            return;
        }
        Command c;
        try {
            for(Class clazz : commands) {
                if(clazz.isAnnotationPresent(CommandInfo.class)) {
                    Annotation annotation = clazz.getAnnotation(CommandInfo.class);
                    CommandInfo commandInfo = (CommandInfo) annotation;
                    String[] realCommand;
                    if(command.contains(" ") && command.contains("-")) {
                        int spaceIndex = command.indexOf(" ");
                        int dashIndex = command.indexOf("-");
                        if(spaceIndex < dashIndex) {
                            realCommand = command.split(" ");
                        } else {
                            realCommand = command.split("-");
                        }
                    } else if(command.contains(" ")) {
                        realCommand = command.split(" ");
                    } else if(command.contains("-")) {
                        realCommand = command.split("-");
                    } else {
                        realCommand = new String[]{command};
                    }
                    List<String> possibleCommands = Arrays.asList(commandInfo.command());
                    if(possibleCommands.contains(realCommand[0])) {
                        c = (Command) clazz.newInstance();
                        List<PlayerRights> rights = Arrays.asList(commandInfo.rights());
                        if(player.getRights().shouldDebug() || rights.contains(player.getRights())) {
                            if (player.getAmountDonated() >= ((CommandInfo) annotation).donationAmountRequired()) {
                                c.execute(player, command);
                                return;
                        } else {
                                player.sendMessage("You need a total donated value of $" + commandInfo.donationAmountRequired() + " to use this command.");
                            }
                        }
                    }
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
                player.getPacketSender().sendMessage("Error executing that command.");
        }
    }
}

PacketConstants.java

		PACKETS[103] = new CommandPacketListener();

How to load commands:

        new FastClasspathScanner().matchSubclassesOf(Command.class, clazz -> {
            try {
                if (!Modifier.isAbstract(clazz.getModifiers())) {
                    synchronized (GameServer.class) {
                        CommandHandler.commands.add(clazz);
                    }
                }
            }catch(Exception e) {
                logger.error("Error adding command " + clazz.getPackage() + "." + clazz.getName() + "\n " + e);
            }
        }).scan();

 

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites
You do not have the required permissions to read this content.

Share this post


Link to post
Share on other sites
You do not have the required permissions to read this content.

Share this post


Link to post
Share on other sites
You do not have the required permissions to read this content.

Share this post


Link to post
Share on other sites
You do not have the required permissions to read this content.

Share this post


Link to post
Share on other sites
You do not have the required permissions to read this content.

Share this post


Link to post
Share on other sites
You do not have the required permissions to read this content.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...