gpt4 book ai didi

java - ChatColor.translateAlternateColorCodes 错误

转载 作者:太空宇宙 更新时间:2023-11-04 06:32:54 25 4
gpt4 key购买 nike

自从我向插件添加自定义配置以来,我收到 ChatColor.translateAlternateColorCodes 错误。

这是错误:

Caused by: java.lang.NullPointerException
at org.bukkit.ChatColor.translateAlternateColorCodes(ChatColor.java:206)
~[spigot.jar:git-Spigot-1473]
at com.gmail.santiagoelheroe.LoginVip.<init>(LoginVip.java:44) ~[?:?]

该错误表明问题出在 LoginVip 类中的第 44 行。

YamlConfiguration configuracion = YamlConfiguration.loadConfiguration(configFile);
String textpermisos = configuracion.getString("Configuration.NoPermissionsMessage");

// Line 44
String permisos = (ChatColor.translateAlternateColorCodes('&', textpermisos));

String prefixtext = configuracion.getString("Configuration.Prefix");
String prefix = (ChatColor.translateAlternateColorCodes('&', prefixtext));

我必须修复此错误才能完成我的第一个插件。

配置类:

import java.io.File;
import java.util.logging.Level;
import org.bukkit.configuration.file.YamlConfiguration;

public class Config {

public static File configFile = new File("Plugins/LoginVip/config.yml");

public static void load() {
YamlConfiguration spawn = YamlConfiguration.loadConfiguration(configFile);
}
public static void saveConfig() {
YamlConfiguration configuracion = new YamlConfiguration();
configuracion.set("Configuration.NoPermissionsMessage", "&cYou don't have permissions to do that");
try {
configuracion.save(configFile);
} catch (Exception e) {
LoginVip.log.log(Level.WARNING, "[LV] Error creating Config.yml file");
}
}
}

启用:

@Override

public void onEnable() {
log.log(Level.INFO, "[LV] Plugin loaded");
if(!Config.configFile.exists()) {
Config.saveConfig();
}
if(!Config.spawnFile.exists()) {
Config.saveSpawn();
}
Config.load();
}

最佳答案

textpermisosnull。来自 MemoryConfiguration.getString(String) 的 Javadocs:

Gets the requested String by path.

If the String does not exist but a default value has been specified, this will return the default value. If the String does not exist and no default value was specified, this will return null.

这意味着您的配置文件不包含“Configuration.NoPermissionsMessage”的键值映射。它是 null,然后被传递到 ChatColor.translateAlternateColorCodes(char, String) 中。这是它的源代码,我的注释指出了崩溃日志中的哪一行 ChatColor.java:206 是:

/*
* Translates a string using an alternate color code character into a
* string that uses the internal ChatColor.COLOR_CODE color code
* character. The alternate color code character will only be replaced if
* it is immediately followed by 0-9, A-F, a-f, K-O, k-o, R or r.
*
* @param altColorChar The alternate color code character to replace. Ex: &
* @param textToTranslate Text containing the alternate color code character.
* @return Text containing the ChatColor.COLOR_CODE color code character.
*/
public static String translateAlternateColorCodes(char altColorChar, String textToTranslate) {
char[] b = textToTranslate.toCharArray(); // textToTranslate is null, it causes a NPE to be thrown.
for (int i = 0; i < b.length - 1; i++) {
if (b[i] == altColorChar && "0123456789AaBbCcDdEeFfKkLlMmNnOoRr".indexOf(b[i+1]) > -1) {
b[i] = ChatColor.COLOR_CHAR;
b[i+1] = Character.toLowerCase(b[i+1]);
}
}
return new String(b);
}

解决这个问题:

  • 添加默认映射,以便 getString() 不会返回 null,而是返回默认值。这是执行此操作的一种方法(请参阅应用为 HashMap 的文档):

    YamlConfiguration configuracion = YamlConfiguration.loadConfiguration(configFile);

    String defpermisos = "";
    String textpermisos = configuracion.getString("Configuration.NoPermissionsMessage", defpermisos);
    String permisos = ChatColor.translateAlternateColorCodes('&', textpermisos);

    String defprefix = "";
    String textprefix = configuracion.getString("Configuration.Prefix", defprefix);
    String prefix = ChatColor.translateAlternateColorCodes('&', textprefix);
  • 修改您的代码,使其仅在 != null 检查后翻译颜色代码。

    YamlConfiguration configuracion = YamlConfiguration.loadConfiguration(configFile);

    String textpermisos = configuracion.getString("Configuration.NoPermissionsMessage");
    String permisos = null;
    if (textpermisos != null)
    permisos = ChatColor.translateAlternateColorCodes('&', textpermisos);

    String prefixtext = configuracion.getString("Configuration.Prefix");
    String prefix = null;
    if (prefixtext != null)
    prefix = ChatColor.translateAlternateColorCodes('&', prefixtext);

关于java - ChatColor.translateAlternateColorCodes 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25855047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com