因此,我最近开始学习Java和BukkitAPI。我创建了一个配置文件,并创建了一个用于播放器事件的类,但是后来我无法在主类中注册事件。我收到一个错误“对于类型OnStartup,未定义方法PlayerJoin(OnStartup)”,唯一的解决方法是制作一个方法。这是我的代码:

OnStartup(主类):

package ml.zonia.plugin;

import java.io.File;
import java.util.logging.Logger;
import org.bukkit.event.Listener;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

import ml.zonia.plugin.commands.Potion;
import ml.zonia.plugin.event.PlayerJoin;

public class OnStartup extends JavaPlugin implements Listener {

public void onEnable() {
    registerEvents();
    registerConfig();
        PluginDescriptionFile pdfFile = getDescription();
        Logger logger = getLogger();

        getServer().getPluginManager().registerEvents(this, this);

        getCommand("zonia").setExecutor(new Potion());

        logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " has been enabled.");
    }

public void registerEvents() {
    PluginManager pm = getServer().getPluginManager();
//here is the error on PlayerJoin:The method PlayerJoin(OnStartup) is undefined for the type OnStartup
    pm.registerEvents(PlayerJoin(this), this);
}

private void registerConfig() {
    getConfig().options().copyDefaults(true);
    saveConfig();

}

public void onDisable() {
    PluginDescriptionFile pdfFile = getDescription();
    Logger logger = getLogger();
    logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " has been disabled.");
    saveConfig();
}


}

药水类别(以防万一):

package ml.zonia.plugin.commands;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;

public class Potion implements CommandExecutor, Listener {

public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

    if (label.equalsIgnoreCase("zonia"))
        ;
    if (!(sender instanceof Player)) {
        sender.sendMessage("You must be in-game to execute this command.");
        return false;

    }

    Player player = (Player) sender;
    player.sendMessage(ChatColor.DARK_AQUA + "ZoniaCore, made by Patrick S.");
    return true;

}


}

玩家加入:

package ml.zonia.plugin.event;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

import ml.zonia.plugin.OnStartup;

public class PlayerJoin implements Listener {

private OnStartup plugin;

public PlayerJoin(OnStartup pl) {
    plugin = pl;
}
@EventHandler
public void onJoin(PlayerJoinEvent pje) {
    int PlayerSpeed;
    PlayerSpeed = plugin.getConfig().getInt("PlayerSpeed");
    if (!pje.getPlayer().hasPermission("zonia.effects.remove"))
        ;
    pje.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE, PlayerSpeed));
}

@EventHandler
public void onPlayerMove(PlayerMoveEvent pme) {
    double SpawnX, SpawnY, SpawnZ;
    SpawnX = plugin.getConfig().getDouble("SpawnX");
    SpawnY = plugin.getConfig().getDouble("SpawnY");
    SpawnZ = plugin.getConfig().getDouble("SpawnZ");
    if ((int) pme.getPlayer().getLocation().getY() == 20) {
        pme.getPlayer().teleport(new Location(Bukkit.getWorld("world"), SpawnX, SpawnY, SpawnZ));
    }
}


}

在Eclipse中,配置yml如下所示:

################################
#                              #
#     ZoniaCore-Hub Config     #
#         Version  1.0         #
#                              #
################################

#Sets the player's speed.
PlayerSpeed: 17

#sets X, Y, and Z of spawn.
SpawnX: -67.5
SpawnY: 156
SpawnZ: 4.5

#sets how much the player has to fall
#to be teleported back to spawn.
TeleportY: 50


但是当它在插件下的plugins文件夹中生成时,它会生成这样的配置,我似乎无法对其进行任何更改。

#
#                              #
#     ZoniaCore-Hub Config     #
#         Version  1.0         #
#                              #
PlayerSpeed: 6
SpawnX: -67.5
SpawnY: 156
SpawnZ: 4.5
TeleportY: 50

最佳答案

您可能是说:

pm.registerEvents(new PlayerJoin(this), this);


我没有看到PlayerJoin函数,只有构造函数。顺便说一句,除非函数是构造函数,否则以大写字母开头的函数名称将是一个不好的做法。

07-24 09:20