0

我正在尝试制作一个混合模式验证(我自己的版本),但是有一个问题,在与星号链接的那一行,我得到了[令牌上的语法错误“(”,;预期],[令牌上的语法错误“ )", ; 预期],[void 是变量 onPlayerJoin 的无效类型],java 是否仍然可以运行?

    public class MixedPlugin extends JavaPlugin implements Listener{
    @Override
    public void onEnable(){
        getLogger().info("Mixed Mode: ON");
        getCommand("mm").setExecutor(new MixedCommand());

        @EventHandler
        **public void onPlayerJoin(PlayerJoinEvent event) {**
            Player player = event.getPlayer();
            String name = player.getName();
            boolean isGood = getURL("http://minecraft.net/haspaid.jsp?user=deadmau5" + name).equals("true");
            if (isGood) {
              player.sendMessage("Welcome back to server!");
            }
            else {
              List<String> ex = getConfig().getStringList("allow");
              if (((MemorySection)ex).getStringList("allow").contains(player.getName())) {
                player.sendMessage("Welcome back to server!");
                player.sendMessage("You are a few person that are using cracked minecraft and allowed to join this premium server!");
              } else {
                player.kickPlayer("Failed to login: User not premium");
              }
            }
        }
    }
    public String getURL(String url){
         Logger log = Logger.getLogger("Minecraft");
        String inputLine = "";
        try{
          URL mcheck = new URL(url);
          URLConnection mcheckc = mcheck.openConnection();
          mcheckc.setReadTimeout(1500);
          BufferedReader in = new BufferedReader(new InputStreamReader(mcheckc.getInputStream()));
          inputLine = in.readLine();
          in.close();
          return inputLine;
        } catch(Exception e){
          log.warning("Error retrieving "+url+": "+e.getMessage());
        }
        return "ERROR";
      }
    @Override
    public void onDisable(){
        getLogger().info("You have disable Mixed Mode");
    }
}
4

2 回答 2

4

这不是有效的代码。Java 不允许像您当前所做的那样嵌套函数(也就是不要放在onPlayerJoin方法onEnable中。

所以把方法放在onPlayerJoinnotonEnable里面。

例子:

@Override
public void onEnable(){
    getLogger().info("Mixed Mode: ON");
    getCommand("mm").setExecutor(new MixedCommand());
}

@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
    Player player = event.getPlayer();
    String name = player.getName();
    boolean isGood = getURL("http://minecraft.net/haspaid.jsp?user=deadmau5" + name).equals("true");
    if (isGood) {
      player.sendMessage("Welcome back to server!");
    }
    else {
      List<String> ex = getConfig().getStringList("allow");
      if (((MemorySection)ex).getStringList("allow").contains(player.getName())) {
        player.sendMessage("Welcome back to server!");
        player.sendMessage("You are a few person that are using cracked minecraft and allowed to join this premium server!");
      } else {
        player.kickPlayer("Failed to login: User not premium");
      }
    }
}
于 2013-06-03T14:16:36.683 回答
0

此代码无权工作,因为您将 onPlayerJoin 方法放在 onEnable() 方法中:使用:

    public void onEnable(){
        getLogger().info("Mixed Mode: ON");
        getCommand("mm").setExecutor(new MixedCommand());        
    }
    @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            Player player = event.getPlayer();
            String name = player.getName();
            boolean isGood = getURL("http://minecraft.net/haspaid.jsp?user=deadmau5" + name).equals("true");
            if (isGood) {
              player.sendMessage("Welcome back to server!");
            }
            else {
              List<String> ex = getConfig().getStringList("allow");
              if (((MemorySection)ex).getStringList("allow").contains(player.getName())) {
                player.sendMessage("Welcome back to server!");
                player.sendMessage("You are a few person that are using cracked minecraft and allowed to join this premium server!");
              } else {
                player.kickPlayer("Failed to login: User not premium");
              }
            }
        }```
I hope I helped :D
于 2022-02-15T16:48:02.283 回答