Files
MagicGem-Reforged/src/main/java/cn/infstar/magicgem/MagicGemPlugin.java
T
Coldsmile_7 467d8f6913 Fix gem migration, enchantment switching and feedback
Preserve enchantment levels across mode switches, restore original gem messages and sounds, and report money and point transactions. Tighten legacy item matching and migration, fix delayed skill execution and GUI handling, and document parity boundaries. Keep version 1.0.1; validate with 93 passing tests.
2026-10-24 01:30:47 +08:00

170 lines
8.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package cn.infstar.magicgem;
import cn.infstar.magicgem.api.event.MagicReloadEvent;
import cn.infstar.magicgem.command.MagicGemCommand;
import cn.infstar.magicgem.condition.ConditionEngine;
import cn.infstar.magicgem.config.PluginSettings;
import cn.infstar.magicgem.config.LanguageManager;
import cn.infstar.magicgem.dsl.DirectiveParser;
import cn.infstar.magicgem.expression.Expression;
import cn.infstar.magicgem.expression.DynamicExpression;
import cn.infstar.magicgem.gem.GemRegistry;
import cn.infstar.magicgem.gem.GemService;
import cn.infstar.magicgem.gui.GemBrowser;
import cn.infstar.magicgem.gui.GemGuiManager;
import cn.infstar.magicgem.integration.EconomyBridge;
import cn.infstar.magicgem.integration.MythicBridge;
import cn.infstar.magicgem.integration.PlaceholderBridge;
import cn.infstar.magicgem.integration.PlayerPointsBridge;
import cn.infstar.magicgem.integration.ProtectionBridge;
import cn.infstar.magicgem.item.ItemCodec;
import cn.infstar.magicgem.item.ItemMatcher;
import cn.infstar.magicgem.listener.GemUseListener;
import cn.infstar.magicgem.migration.LegacyDirectoryImporter;
import cn.infstar.magicgem.reward.RewardEngine;
import cn.infstar.magicgem.skill.SkillRegistry;
import cn.infstar.magicgem.skill.SkillEngine;
import cn.infstar.magicgem.util.MessageDispatcher;
import de.tr7zw.changeme.nbtapi.NBT;
import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.util.Objects;
public class MagicGemPlugin extends JavaPlugin {
private volatile PluginSettings settings;
private GemRegistry gemRegistry;
private SkillRegistry skillRegistry;
private GemGuiManager guiManager;
private LanguageManager languages;
private EconomyBridge economy;
private PlayerPointsBridge points;
private PlaceholderBridge placeholders;
private MythicBridge mythic;
@Override
public void onLoad() {
NBT.preloadApi();
}
@Override
public void onEnable() {
new LegacyDirectoryImporter(this).importIfPresent();
saveDefaultConfig();
for (String resource : BUNDLED_RESOURCES) saveBundledResource(resource);
languages = new LanguageManager(this);
languages.reload();
DirectiveParser directiveParser = new DirectiveParser();
Expression expression = new Expression();
placeholders = new PlaceholderBridge(this);
DynamicExpression dynamicExpression = new DynamicExpression(expression, placeholders);
economy = new EconomyBridge(this);
points = new PlayerPointsBridge(this);
mythic = new MythicBridge(this);
ProtectionBridge protection = new ProtectionBridge(this);
gemRegistry = new GemRegistry(this, directiveParser);
skillRegistry = new SkillRegistry(this, directiveParser);
ItemCodec itemCodec = new ItemCodec(this, gemRegistry);
ItemMatcher itemMatcher = new ItemMatcher(itemCodec);
MessageDispatcher messages = new MessageDispatcher(this, itemCodec, directiveParser, placeholders, languages);
ConditionEngine conditionEngine = new ConditionEngine(dynamicExpression, itemMatcher, languages);
RewardEngine rewardEngine = new RewardEngine(this, itemCodec, itemMatcher, gemRegistry, skillRegistry, directiveParser,
dynamicExpression, placeholders, economy, points, mythic, languages, messages);
GemService gemService = new GemService(gemRegistry, itemCodec, itemMatcher, rewardEngine,
dynamicExpression, conditionEngine, languages);
SkillEngine skillEngine = new SkillEngine(this, skillRegistry, itemCodec, directiveParser,
dynamicExpression, conditionEngine, rewardEngine, mythic, protection, messages);
mythic.enableExtensions(gemRegistry, itemCodec, protection, skillEngine::mythicKill);
rewardEngine.skillInvoker(skillEngine::invoke);
GemBrowser gemBrowser = new GemBrowser(gemRegistry, itemCodec, languages);
guiManager = new GemGuiManager(this, gemRegistry, gemService, itemCodec, itemMatcher, messages, languages);
reloadAll();
MagicGemCommand command = new MagicGemCommand(gemRegistry, gemService, itemCodec, gemBrowser,
guiManager, skillEngine, messages, languages, this::reloadScope);
PluginCommand pluginCommand = Objects.requireNonNull(getCommand("mgem"));
pluginCommand.setExecutor(command);
pluginCommand.setTabCompleter(command);
getServer().getPluginManager().registerEvents(new GemUseListener(gemService, itemCodec, messages, () -> settings), this);
getServer().getPluginManager().registerEvents(gemBrowser, this);
getServer().getPluginManager().registerEvents(guiManager, this);
getServer().getPluginManager().registerEvents(skillEngine, this);
logIntegrationStatus();
getLogger().info("MagicGem Reforged 已启用,命令 /mgem,加载 " + gemRegistry.all().size() + " 颗宝石。");
}
private void logIntegrationStatus() {
logOptionalPlugin("PlaceholderAPI", "接口=" + (placeholders.available() ? "可用" : "不可用"));
logOptionalPlugin("Vault", "经济接口=" + (economy.available() ? "可用" : "不可用"));
logOptionalPlugin("PlayerPoints", "接口=" + (points.available() ? "可用" : "不可用"));
logOptionalPlugin("MythicMobs", "适配器=" + mythic.status());
logOptionalPlugin("MMOItems", null);
logOptionalPlugin("MythicArtifacts", null);
logOptionalPlugin("MythicCrucible", null);
logOptionalPlugin("Residence", null);
logOptionalPlugin("PvPManager", null);
logOptionalPlugin("EcoEnchants", null);
logOptionalPlugin("GoldenEnchants", null);
}
private void logOptionalPlugin(String name, String adapterStatus) {
Plugin dependency = getServer().getPluginManager().getPlugin(name);
if (dependency == null) {
getLogger().info("可选插件 " + name + ":未安装");
return;
}
String version = dependency.getDescription().getVersion();
if (!dependency.isEnabled()) {
getLogger().warning("可选插件 " + name + ":已安装但未启用(版本 " + version + ")");
return;
}
String suffix = adapterStatus == null ? "" : "," + adapterStatus;
getLogger().info("可选插件 " + name + ":已启用(版本 " + version + suffix + ")");
}
public void reloadAll() {
reloadScope("all");
}
private void reloadScope(String scope) {
boolean all = scope.equalsIgnoreCase("all");
boolean gems = all || scope.equalsIgnoreCase("gem");
boolean skills = all || scope.equalsIgnoreCase("skill");
if (all) {
reloadConfig();
if (languages != null) languages.reload();
settings = PluginSettings.load(getConfig());
onSettingsReloaded(settings);
if (economy != null) economy.refresh();
if (points != null) points.refresh();
if (placeholders != null) placeholders.refresh();
if (mythic != null) mythic.refresh();
}
if (gems && gemRegistry != null) gemRegistry.reload();
if (skills && skillRegistry != null) {
if (settings.skillEngine()) skillRegistry.reload();
else skillRegistry.clear();
}
if ((all || gems) && guiManager != null) guiManager.reload();
if (isEnabled()) getServer().getPluginManager().callEvent(new MagicReloadEvent());
}
protected void onSettingsReloaded(PluginSettings settings) { }
private void saveBundledResource(String path) {
File file = new File(getDataFolder(), path);
if (!file.exists()) saveResource(path, false);
}
private static final String[] BUNDLED_RESOURCES = {
"gems/ExampleGem.yml", "gems/AttributeGem.yml", "gems/ButtonGem.yml", "gems/EmbedGem.yml",
"gems/EnchantGem.yml", "gems/FunctionGem.yml", "gems/LoreGem.yml", "gems/MythicGem.yml",
"gems/PlayerGem.yml", "gems/PotionGem.yml",
"skills/ExampleSkills.yml", "skills/DefaultBuffs.yml", "skills/DefaultMMSkills.yml",
"skills/DefaultSkills.yml", "gui/Default.yml", "gui/gui.yml", "gui/rune.yml",
"languages/en_US.yml", "languages/zh_CN.yml"
};
}