ashtton / spigot-command-api

A lightweight yet extremely powerful spigot command api
25 stars 7 forks source link

Problem with argument processing #10

Closed 0PandaDEV closed 7 months ago

0PandaDEV commented 8 months ago

The documentation doesn't make that really clear, it only shows how to create and register one.

ashtton commented 8 months ago

If you check the impl folder under processors in the source code you can see examples for all of the default processors.

If you still have questions then just let me know what you're trying to make a processor for and I'll try my best to assist you.

0PandaDEV commented 8 months ago

I saw that folder and i even tried creating a new processor directly in the plugin not how its supposed to je made through the wrapper but i still don't get how to apply them on a command or am i wrong and they listen for the object usage in a argument like for example the GameMode processor

0PandaDEV commented 8 months ago

Never mind, just got it working

0PandaDEV commented 8 months ago

But now I found a new issue, I think this is an issue in the API itself.

When I execute the command /gm 1 I get the following prints. This does not make any sense, it seems that the return null in the check if the mode is null does not return for some reason.

image

package net.pandadev.databasetest.commands;

import net.pandadev.databasetest.commandapi.Command;
import net.pandadev.databasetest.commandapi.paramter.Param;
import org.bukkit.GameMode;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class TestCommand {

    @Command(names = {"gmc", "creative"}, permission = "nextron.gamemode.creative")
    public void creative(CommandSender sender, @Param(name = "target", required = false) Player target) {
        if (target == null) {
            if (sender instanceof Player) {
                setGamemode((Player) sender, GameMode.CREATIVE);
                return;
            }
        }

        assert target != null;
        setGamemode(target, GameMode.CREATIVE);
    }

    @Command(names = {"gms", "survival"}, permission = "nextron.gamemode.survival")
    public void survival(CommandSender sender, @Param(name = "target", required = false) Player target) {
        if (target == null) {
            if (sender instanceof Player) {
                setGamemode((Player) sender, GameMode.SURVIVAL);
                return;
            }
        }

        assert target != null;
        setGamemode(target, GameMode.SURVIVAL);
    }

    @Command(names = {"gmsp", "spectator"}, permission = "nextron.gamemode.spectator")
    public void spectator(CommandSender sender, @Param(name = "target", required = false) Player target) {
        if (target == null) {
            if (sender instanceof Player) {
                setGamemode((Player) sender, GameMode.SPECTATOR);
                return;
            }
        }

        assert target != null;
        setGamemode(target, GameMode.SPECTATOR);
    }

    @Command(names = {"gma", "adventure"}, permission = "nextron.gamemode.adventure")
    public void adventure(CommandSender sender, @Param(name = "target", required = false) Player target) {
        if (target == null) {
            if (sender instanceof Player) {
                setGamemode((Player) sender, GameMode.ADVENTURE);
                return;
            }
        }

        assert target != null;
        setGamemode(target, GameMode.ADVENTURE);
    }

    @Command(names = {"gamemode", "gm"}, permission = "nextron.gamemode", playerOnly = true)
    public void gamemodeCommand(Player player, @Param(name = "gamemode") GameMode gamemode, @Param(name = "target", required = false) Player target) {
        if (target == null) {
            setGamemode(player, gamemode);
        } else {
            if (!player.hasPermission("nextron.gamemode.other")) {
                player.sendMessage("§cYou do not have permission to change others' gamemode.");
                return;
            }
            setGamemode(target, gamemode);
            player.sendMessage("§aGamemode of " + target.getName() + " set to " + gamemode.toString().toLowerCase() + ".");
        }
    }

    private void setGamemode(Player player, GameMode gamemode) {
        if (player.getGameMode().equals(gamemode)) {
            player.sendMessage("You are already in " + gamemode.toString().toLowerCase() + " mode.");
            return;
        }
        player.setGameMode(gamemode);
        player.sendMessage("Your gamemode has been set to " + gamemode.toString().toLowerCase() + ".");
    }

}
package net.pandadev.databasetest.commandapi.paramter.impl;

import net.pandadev.databasetest.commandapi.paramter.Processor;
import org.bukkit.GameMode;
import org.bukkit.command.CommandSender;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class GamemodeProcessor extends Processor<GameMode> {

    private final Map<String, GameMode> aliases = new HashMap<>();

    public GamemodeProcessor() {
        aliases.put("creative", GameMode.CREATIVE);
        aliases.put("c", GameMode.CREATIVE);
        aliases.put("1", GameMode.CREATIVE);
        aliases.put("survival", GameMode.SURVIVAL);
        aliases.put("s", GameMode.SURVIVAL);
        aliases.put("0", GameMode.SURVIVAL);
        aliases.put("adventure", GameMode.ADVENTURE);
        aliases.put("a", GameMode.ADVENTURE);
        aliases.put("2", GameMode.ADVENTURE);
        aliases.put("spectator", GameMode.SPECTATOR);
        aliases.put("sp", GameMode.SPECTATOR);
        aliases.put("3", GameMode.SPECTATOR);
    }

    public GameMode process(CommandSender sender, String supplied) {
        if (supplied.equalsIgnoreCase("creative") || supplied.equalsIgnoreCase("c") || supplied.equals("1")) {
            return GameMode.CREATIVE;
        }

        if (supplied.equalsIgnoreCase("survival") || supplied.equalsIgnoreCase("s") || supplied.equals("0")) {
            return GameMode.SURVIVAL;
        }

        if (supplied.equalsIgnoreCase("adventure") || supplied.equalsIgnoreCase("a") || supplied.equals("2")) {
            return GameMode.ADVENTURE;
        }

        if (supplied.equalsIgnoreCase("spectator") || supplied.equalsIgnoreCase("sp") || supplied.equals("3")) {
            return GameMode.SPECTATOR;
        }

        sender.sendMessage("§cThe value you entered \"" + supplied + "\" is not a valid gamemode.");
        return null;
    }

    public List<String> tabComplete(CommandSender sender, String supplied) {
        Set<String> gameModeNames = aliases.keySet();
        return gameModeNames.stream()
                .filter(name -> name.startsWith(supplied.toLowerCase()))
                .collect(Collectors.toList());
    }
}
ashtton commented 8 months ago

@0PandaDEV

I am unable to reproduce this error using the exact code as provided. May I ask what version of Spigot you are using?

0PandaDEV commented 8 months ago

1.20.4

ashtton commented 8 months ago

Still unable to reproduce. Is there anyway you have conflicting argument processors or somehow accidentally smudged some of the code when pasting it into your project?

0PandaDEV commented 8 months ago

idk i tried multiple things but it always comes back to this error

ashtton commented 8 months ago

Is the project you're trying to do this to on github? If so it is and is open source could you push the code that is producing that error to a test branch and I will clone it and attempt to reproduce.

0PandaDEV commented 7 months ago

I fixed it in the version i edited to use in Nextron.