为什么Cmds.java中的getPlugin方法不起作用?

时间:2018-08-31 20:55:45

标签: java bukkit

我想从 EventsClass.class 更改为 logged 的变量,该变量是 放置在 Cmds.class 中。

但是我对这一行有疑问:

private EventsClass plugin = EventsClass.getPlugin(EventsClass.class);

编译器无法解析getPlugin。

代码

Main.java:

package com.github.noisersup.serverrust.LoginMe;

import org.bukkit.plugin.java.JavaPlugin;

public class Main extends JavaPlugin {

    @Override
    public void onEnable() {
        loadConfig();
        getServer().getPluginManager().registerEvents(new EventsClass(),this);
    }

    public void loadConfig(){
        getConfig().options().copyDefaults(true);
        this.saveConfig();
    }
}

EventsClass.java:

    package com.github.noisersup.serverrust.LoginMe;

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerMoveEvent;

    import java.util.UUID;

    public class EventsClass implements Listener {

    public boolean logged;

    @EventHandler
    public void onJoin(PlayerJoinEvent e){

        Player p = e.getPlayer();
        UUID uuid = p.getUniqueId();

        logged = false;

        if("player."+uuid+".password"!=null){

            //login
            p.sendMessage("§2Welcome again "+p.getName()+"! Please login with command: §6§n/login <password>");


        }else{
            //register
            p.sendMessage("§2Welcome "+p.getName()+"! Please register with command: + §6§n/register <password> <repeat_password>");
        }
    }

    @EventHandler
    public void onMove(PlayerMoveEvent e){
        if(!logged){
            e.setCancelled(true);
        }

    }
}

Cmds.java:

package com.github.noisersup.serverrust.LoginMe;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;


import java.util.UUID;

public class Cmds implements CommandExecutor {


    String cmd1 = "login";
    String cmd2 = "register";
    String cmd3 = "changepassword";

    private EventsClass plugin = EventsClass.getPlugin(EventsClass.class);


    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String s, String[] args) {

        if(sender instanceof  Player) {
            Player p = (Player) sender;
            UUID uuid = p.getUniqueId();

            if (cmd.getName().equalsIgnoreCase(cmd1)) {
                if ("player." + uuid + ".password" != null){
                    if(!args[0].isEmpty()) {
                        if (args[0].equals("player." + uuid + ".password")) {
                            p.sendMessage("§2Logged!");
                            logged = true;
                        }else{
                            p.sendMessage("§4Wrong password!");
                        }
                    }else{
                        p.sendMessage("§4Enter password!");
                    }
                }else{
                    p.sendMessage("§4You must be registered before login.");
                }
            }
        }

        return false;
    }
}

2 个答案:

答案 0 :(得分:0)

编译器无法解析方法getPlugin(...),因为EventsClass没有这样的方法。它只有方法onJoin(...)onMove(...)

答案 1 :(得分:0)

此代码将清楚地指示编译错误:

private EventsClass plugin = EventsClass.getPlugin(EventsClass.class);
                                         ^^^^^^^^^

因为在getPlugin(Class<?> class)类中没有定义EventsClass

EventsClass仅定义onJoin(PlayerJoinEvent e)onMove(PlayerMoveEvent e)方法。

另一方面,这里的Main类使用以下方法扩展JavaPlugin

public static <T extends JavaPlugin> T getPlugin(Class<T> clazz)

这就是为什么可能进行Main.getPlugin(...)通话的原因。