Я довольно новичок в Java и программирую плагины для Minecraft.Я хотел бы, чтобы слушатель чата проверил, что имя игрока указано в конфиге, а затем установил отображаемое имя на него, но если имя игрока отсутствует в конфиге, то я хотел бы просто установить имя игрока.
Это мой слушатель чата:
package me.purp.servercore.listeners;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import me.purp.servercore.Main;
import me.purp.servercore.utils.Utils;
public class ServerChat implements Listener
{
private Main plugin;
public ServerChat(Main plugin)
{
Bukkit.getPluginManager().registerEvents(this, plugin);
}
@EventHandler
public void Chat(AsyncPlayerChatEvent event)
{
String message = event.getMessage();
Player player = event.getPlayer();
if (!plugin.getConfig().contains(player.getName())) {
event.setFormat(Utils.color("&7" + player.getDisplayName() + " &7&l» &r" + message));
} else if (plugin.getConfig().contains(player.getName())) {
event.setFormat(Utils.color("&7" + plugin.getConfig().getString(player.getName()) + " &7&l» &r" + message));
}
}
}
Это мой псевдоним класса:
package me.purp.servercore.commands;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import me.purp.servercore.Main;
import me.purp.servercore.utils.Utils;
public class PlayerNick implements CommandExecutor
{
private Main plugin;
public PlayerNick(Main plugin)
{
this.plugin = plugin;
plugin.getCommand("nick").setExecutor(this);
}
public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
{
FileConfiguration config = plugin.getConfig();
if (!(sender instanceof Player))
{
sender.sendMessage(Utils.color(config.getString("PlayerEntityFalse")));
return true;
}
Player player = (Player) sender;
if (player.hasPermission("core.nick"))
{
if (args.length == 0)
{
player.sendMessage(Utils.color("&cYou cannot have a blank nickname!"));
return true;
}
String nick = "";
for (String arg : args)
{
nick += arg + " ";
}
player.sendMessage(Utils.color("&7Your nickname is now: " + nick));
config.set(player.getName(), nick);
plugin.saveConfig();
}
return false;
}
}
Это моя ошибка:
Could not pass event AsyncPlayerChatEvent to Core v0.2
org.bukkit.event.EventException: null
at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.12.2.jar:git-Spigot-2cf50f0-8166d17]
at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.12.2.jar:git-Spigot-2cf50f0-8166d17]
at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500) [spigot-1.12.2.jar:git-Spigot-2cf50f0-8166d17]
at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:482) [spigot-1.12.2.jar:git-Spigot-2cf50f0-8166d17]
at net.minecraft.server.v1_12_R1.PlayerConnection.chat(PlayerConnection.java:1319) [spigot-1.12.2.jar:git-Spigot-2cf50f0-8166d17]
at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1257) [spigot-1.12.2.jar:git-Spigot-2cf50f0-8166d17]
at net.minecraft.server.v1_12_R1.PacketPlayInChat$1.run(PacketPlayInChat.java:39) [spigot-1.12.2.jar:git-Spigot-2cf50f0-8166d17]
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:?]
at java.util.concurrent.FutureTask.run(Unknown Source) [?:?]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:?]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:?]
at java.lang.Thread.run(Unknown Source) [?:?]
Caused by: java.lang.NullPointerException
at me.purp.servercore.listeners.ServerChat.Chat(ServerChat.java:29) ~[?:?]
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:?]
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:?]
at java.lang.reflect.Method.invoke(Unknown Source) ~[?:?]
at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot-1.12.2.jar:git-Spigot-2cf50f0-8166d17]
... 11 more