java—如何构造公共模块,以便子模块能够从API中获得正确类型的对象?

lp0sw83n  于 2021-07-07  发布在  Java
关注(0)|答案(0)|浏览(348)

嘿,我有 common 包含以下代码和两个附加模块的模块 bungee 以及 bukkit . 我需要如何构造代码以便使用 common 每个模块中的部分代码,但不必强制转换 ObjectChildCommandCommandSender ,有没有办法真正得到正确的答案 CommandSender 那里而不是 Object ?
需要记住的一些事情: common 模块没有依赖项。 bungee 模块使用bungeecordapi和 common 作为依赖项的模块。 bukkit 模块使用bukkitapi和 common 作为依赖项的模块。
bukkit和bungeecordapi都有自己的特性 CommandSender 但是他们不一样。
不能修改来自API的类。
这些课程是 common 模块。

public interface ChildCommand {

    void run(Object sender, String[] args);

    String getName();

}

public abstract class AbstractCommandService<S> {

    protected final Set<ChildCommand> childCommands;

    protected AbstractCommandService() {
        childCommands = new HashMap<>();
    }

    public boolean execute(Object sender, String commandName, String[] args) {
        for (ChildCommand childCommand : childCommands) {
            if (childCommand.getName().equalsIgnoreCase(commandName)) {
                childCommand.run(sender, args);
                return true;
            }
        }
        return false;
    }

    public void addCommand(ChildCommand command) {
        childCommands.add(command);
    }

}

这些课程是 bungee 模块。

public class BungeeReload implements ChildCommand {

    private final String NAME = "reload";

    public BungeeReload() {}

    @Override
    public String getName() {
        return NAME;
    }

    /*how do I structure my code so here I would get `net.md_5.bungee.api.CommandSender` 
    instead of `Object` for the sender parameter.*/
    @Override 
    public void run(Object sender, String[] args) {
        ((CommandSender)sender).sendMessage("Reload command has been ran on bungee.");
    }

}

public class BungeeCommandService extends AbstractCommandService<CommandSender> {}

public class MainBungeeCommand extends Command {

    private BungeeCommandService commandService;    

    public MainBungeeCommand() {
        super("cmd-name", "cmd-permission", "cmd-alias");
        commandService = new BungeeCommandService();

        commandService.addCommand(new BungeeReload());
    }

    @Override
    public void execute(CommandSender sender, String[] args) {
        if (args.length > 0) {
            String commandName = args[0];

            boolean successfullyExecuted = commandService.execute(sender, commandName, args);
            if (!successfullyExecuted) sender.sendMessage("Unknown command!"));
        } 
    }

}

这些课程是 bukkit 模块。

public class BukkitReload implements ChildCommand {

    private final String NAME = "reload";

    public BukkitReload () {}

    @Override
    public String getName() {
        return NAME;
    }

    /*how do I structure my code so here I would get `org.bukkit.command.CommandSender` 
    instead of `Object` for the sender parameter.*/
    @Override 
    public void run(Object sender, String[] args) {
        ((CommandSender)sender).sendMessage("Reload command has been ran on bukkit.");
    }

}

public class BukkitCommandService extends AbstractCommandService<CommandSender> {}

public class MainBukkitCommand implements CommandExecutor {

    private BukkitCommandService commandService;    

    public MainBukkitCommand() {
        commandService = new BukkitCommandService();
        commandService.addCommand(new BukkitReload());
    }

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (args.length > 0) {
            String commandName = args[0];

            boolean successfullyExecuted = commandService.execute(sender, commandName, args);
            if (!successfullyExecuted) sender.sendMessage("Unknown command!"));
            return true;
        }
        return false; 
    }

}

这里是API中的类。
bungeecordapi中的commandsender类。
来自bungeecordapi的命令类。
bukkitapi的commandsender类。
bukkitapi中的commandexecutor类。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题