1.0.1:Fix error:[DirectoryProviderSource] Error loading plugin: name is not defined

This commit is contained in:
xiaobai
2026-02-13 20:56:03 +08:00
parent c7d09c3039
commit 8d8cdcb244
6 changed files with 304 additions and 357 deletions

View File

@@ -23,10 +23,9 @@ public class AdminCommands implements CommandExecutor {
}
if (command.getName().equalsIgnoreCase("pblreload")) {
plugin.reloadConfig();
plugin.getBlockManager().loadData();
plugin.getSkinManager().loadAllSkins();
sender.sendMessage("§a插件配置已重载");
// 调用插件的完整重载方法
plugin.reloadPluginConfig();
sender.sendMessage("§a插件配置已完全重载");
return true;
}
@@ -44,7 +43,12 @@ public class AdminCommands implements CommandExecutor {
targetId = target.getUniqueId();
} else {
// 尝试从离线玩家获取UUID
targetId = Bukkit.getOfflinePlayer(targetName).getUniqueId();
try {
targetId = Bukkit.getOfflinePlayer(targetName).getUniqueId();
} catch (Exception e) {
sender.sendMessage("§c找不到玩家: " + targetName);
return true;
}
}
plugin.getBlockManager().clearPlayerBlocks(targetId);
@@ -69,12 +73,21 @@ public class AdminCommands implements CommandExecutor {
}
}
plugin.getLifeSystem().revivePlayer(target);
sender.sendMessage("§a玩家 " + target.getName() + " 已复活!");
if (plugin.getLifeSystem() != null) {
plugin.getLifeSystem().revivePlayer(target);
sender.sendMessage("§a玩家 " + target.getName() + " 已复活!");
} else {
sender.sendMessage("§c复活失败生命系统未初始化");
}
return true;
}
if (command.getName().equalsIgnoreCase("pblstats")) {
if (plugin.getBlockManager() == null) {
sender.sendMessage("§c方块管理器未初始化");
return true;
}
int totalPlayers = plugin.getBlockManager().getPlayerBlocksCount();
int totalBlocks = plugin.getBlockManager().getTotalBlocksCount();
@@ -85,9 +98,11 @@ public class AdminCommands implements CommandExecutor {
for (Player player : Bukkit.getOnlinePlayers()) {
int blocks = plugin.getBlockManager().getRemainingBlocks(player.getUniqueId());
sender.sendMessage("§7- " + player.getName() + ": §e" + blocks + " §7/ §a5");
String status = blocks > 0 ? "§a存活" : "§c已淘汰";
sender.sendMessage("§7- " + player.getName() + ": §e" + blocks + " §7/ §a5 §7(" + status + "§7)");
}
sender.sendMessage("§a=================================");
return true;
}

View File

@@ -1,60 +1,209 @@
package com.playerblocklife;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
public class ConfigManager {
private final PlayerBlockLife plugin;
private FileConfiguration config;
private File configFile;
public ConfigManager(PlayerBlockLife plugin) {
this.plugin = plugin;
loadConfig();
this.configFile = new File(plugin.getDataFolder(), "config.yml");
}
/**
* 加载配置
*/
public void loadConfig() {
plugin.saveDefaultConfig();
config = plugin.getConfig();
// 确保配置文件夹存在
if (!plugin.getDataFolder().exists()) {
plugin.getDataFolder().mkdirs();
}
// 如果配置文件不存在从JAR中复制默认配置
if (!configFile.exists()) {
plugin.saveDefaultConfig();
plugin.logInfo("创建默认配置文件");
}
// 重新加载配置
reloadConfig();
}
/**
* 重新加载配置
*/
public void reloadConfig() {
plugin.reloadConfig();
config = plugin.getConfig();
// 重新从磁盘加载配置
config = YamlConfiguration.loadConfiguration(configFile);
// 加载默认配置作为后备
InputStream defaultConfigStream = plugin.getResource("config.yml");
if (defaultConfigStream != null) {
YamlConfiguration defaultConfig = YamlConfiguration.loadConfiguration(
new InputStreamReader(defaultConfigStream, StandardCharsets.UTF_8));
config.setDefaults(defaultConfig);
}
// 检查配置版本,如果需要则更新
checkConfigVersion();
plugin.logInfo("配置已加载");
}
/**
* 检查配置版本并更新
*/
private void checkConfigVersion() {
int currentVersion = config.getInt("config-version", 1);
int latestVersion = 1; // 最新配置版本
if (currentVersion < latestVersion) {
plugin.logWarning("检测到旧版配置文件,正在更新...");
updateConfig(currentVersion, latestVersion);
}
}
/**
* 更新配置文件
*/
private void updateConfig(int fromVersion, int toVersion) {
if (fromVersion == 1 && toVersion == 2) {
// 示例:添加新配置项
if (!config.contains("new-feature.enabled")) {
config.set("new-feature.enabled", true);
config.set("new-feature.duration", 60);
}
// 更新版本号
config.set("config-version", toVersion);
try {
config.save(configFile);
plugin.logInfo("配置文件已更新到版本 " + toVersion);
} catch (IOException e) {
plugin.logError("保存更新后的配置文件失败", e);
}
}
}
/**
* 保存配置
*/
public void saveConfig() {
try {
config.save(configFile);
} catch (IOException e) {
plugin.logError("保存配置文件失败", e);
}
}
/**
* 获取配置对象
*/
public FileConfiguration getConfig() {
if (config == null) {
reloadConfig();
}
return config;
}
// 以下为配置项的获取方法
public int getBlocksPerPlayer() {
return config.getInt("blocks.amount", 5);
return getConfig().getInt("blocks.amount", 5);
}
public int getSpreadRange() {
return config.getInt("blocks.spread", 5);
return getConfig().getInt("blocks.spread", 5);
}
public int getDepth() {
return config.getInt("blocks.depth", -1);
return getConfig().getInt("blocks.depth", -1);
}
public String getBlockMaterial() {
return getConfig().getString("blocks.material", "player_head");
}
public boolean isDieWhenBlocksGone() {
return config.getBoolean("game.die_when_blocks_gone", true);
return getConfig().getBoolean("game.die_when_blocks_gone", true);
}
public boolean isBecomeSpectator() {
return config.getBoolean("game.become_spectator", true);
return getConfig().getBoolean("game.become_spectator", true);
}
public boolean isHealthSystemEnabled() {
return config.getBoolean("game.health_system", true);
return getConfig().getBoolean("game.health_system", true);
}
public boolean isSkinSystemEnabled() {
return config.getBoolean("skin.enabled", true);
return getConfig().getBoolean("skin.enabled", true);
}
public String getSkinSource() {
return config.getString("skin.source", "player_profile");
return getConfig().getString("skin.source", "player_profile");
}
public int getCacheExpireDays() {
return getConfig().getInt("skin.cache.expire_days", 7);
}
public boolean isAutoSaveEnabled() {
return getConfig().getBoolean("storage.auto_save.enabled", true);
}
public int getAutoSaveInterval() {
return getConfig().getInt("storage.auto_save.interval", 300);
}
public String getStorageType() {
return getConfig().getString("storage.type", "yaml");
}
public boolean isBroadcastOnBlockBreak() {
return getConfig().getBoolean("game.broadcast.on_block_break", true);
}
public boolean isBroadcastOnPlayerDeath() {
return getConfig().getBoolean("game.broadcast.on_player_death", true);
}
public int getBroadcastRange() {
return getConfig().getInt("game.broadcast.range", 30);
}
public boolean isGiveExpReward() {
return getConfig().getBoolean("game.break_rewards.give_exp", true);
}
public int getExpRewardAmount() {
return getConfig().getInt("game.break_rewards.exp_amount", 5);
}
public boolean isProtectFromExplosions() {
return getConfig().getBoolean("protection.protect_from_explosions", true);
}
public boolean isProtectFromFire() {
return getConfig().getBoolean("protection.protect_from_fire", true);
}
public boolean isProtectFromPistons() {
return getConfig().getBoolean("protection.protect_from_pistons", true);
}
public String getMessage(String path, String defaultValue) {
String message = config.getString("messages." + path, defaultValue);
String message = getConfig().getString("messages." + path, defaultValue);
if (message != null) {
message = message.replace("&", "§");
}

View File

@@ -1,6 +1,8 @@
package com.playerblocklife;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.logging.Level;
public class PlayerBlockLife extends JavaPlugin {
@@ -10,36 +12,40 @@ public class PlayerBlockLife extends JavaPlugin {
private LifeSystem lifeSystem;
private ConfigManager configManager;
// 在 PlayerBlockLife.java 中添加:
@Override
public void reloadConfig() {
super.reloadConfig();
configManager.reloadConfig();
getLogger().info("配置已重新加载");
}
@Override
public void onEnable() {
instance = this;
// 第一步:保存默认配置
saveDefaultConfig();
// 第二步:初始化管理器(注意顺序!)
this.configManager = new ConfigManager(this);
this.skinManager = new SkinManager(this);
this.blockManager = new PlayerBlockManager(this, skinManager);
this.lifeSystem = new LifeSystem(this);
// 第三步:加载数据(必须在管理器初始化之后)
this.configManager.loadConfig();
// 第四步:注册事件监听器
getServer().getPluginManager().registerEvents(new BlockBreakListener(this), this);
getServer().getPluginManager().registerEvents(new PlayerJoinListener(this), this);
getServer().getPluginManager().registerEvents(new PlayerQuitListener(this), this);
// 第五步:注册命令
getCommand("setlifeblocks").setExecutor(new SetLifeBlocksCommand(this));
getCommand("checklifeblocks").setExecutor(new CheckLifeBlocksCommand(this));
getCommand("pblreload").setExecutor(new AdminCommands(this));
getCommand("pbldelete").setExecutor(new AdminCommands(this));
getCommand("pblrevive").setExecutor(new AdminCommands(this));
getCommand("pblstats").setExecutor(new AdminCommands(this));
// 第六步:加载其他数据
blockManager.loadData();
skinManager.loadAllSkins();
// 第七步:启动定时任务
startScheduler();
getLogger().info("§a========================================");
@@ -50,6 +56,7 @@ public class PlayerBlockLife extends JavaPlugin {
@Override
public void onDisable() {
// 保存数据
if (blockManager != null) {
blockManager.saveData();
}
@@ -59,15 +66,57 @@ public class PlayerBlockLife extends JavaPlugin {
getLogger().info("§cPlayerBlockLife 插件已禁用");
}
/**
* 重写 reloadConfig 方法,避免循环依赖
*/
@Override
public void reloadConfig() {
// 只调用父类的reloadConfig不调用configManager的方法
super.reloadConfig();
getLogger().info("基础配置文件已重新加载");
}
/**
* 插件的完整重载方法(用于命令)
*/
public void reloadPluginConfig() {
if (configManager != null) {
configManager.reloadConfig();
}
if (blockManager != null) {
blockManager.loadData();
}
if (skinManager != null) {
skinManager.loadAllSkins();
}
getLogger().info("插件配置已完全重载");
}
private void startScheduler() {
// 每5分钟自动保存数据
getServer().getScheduler().runTaskTimerAsynchronously(this, () -> {
blockManager.saveData();
skinManager.saveSkinData();
if (blockManager != null) {
blockManager.saveData();
}
if (skinManager != null) {
skinManager.saveSkinData();
}
getLogger().info("数据已自动保存");
}, 6000L, 6000L);
// 每10秒检查玩家生命方块
getServer().getScheduler().runTaskTimer(this, () -> {
lifeSystem.checkAllPlayers();
if (lifeSystem != null) {
lifeSystem.checkAllPlayers();
}
}, 200L, 200L);
// 每分钟清理一次过期缓存
getServer().getScheduler().runTaskTimerAsynchronously(this, () -> {
if (skinManager != null) {
skinManager.cleanupOldCache();
}
}, 1200L, 1200L);
}
public static PlayerBlockLife getInstance() {