如何在Spigot插件的build.gradle中添加Essentials依赖调用其经济系统?
在Gradle中添加Essentials依赖并对接其经济系统的解决方案
1. 配置build.gradle添加依赖
Essentials不在Maven中央仓库,需要先添加官方仓库,再引入编译时依赖(避免打包进插件jar引发冲突):
第一步:添加Essentials仓库
在repositories块中加入Essentials的Maven仓库:
repositories { mavenCentral() // Essentials官方发布仓库 maven { url = uri("https://repo.essentialsx.net/releases/") } }
第二步:引入Essentials依赖
在dependencies块中添加编译时依赖,版本号请根据你服务器的Minecraft版本对应选择(比如1.20.x对应EssentialsX 2.20.x):
dependencies { // 你的Spigot API依赖(替换为你使用的版本) compileOnly 'org.spigotmc:spigot-api:1.20.4-R0.1-SNAPSHOT' // EssentialsX依赖 compileOnly 'net.essentialsx:EssentialsX:2.20.1' }
2. 对接Essentials经济系统的代码示例
通过Bukkit服务管理器获取Essentials的经济实例,实现余额查询、增减操作:
import net.ess3.api.Economy; import net.ess3.api.IEssentials; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.plugin.RegisteredServiceProvider; public class EconomyManager { private Economy economy; // 初始化经济服务 public boolean initEconomy() { RegisteredServiceProvider<IEssentials> essService = Bukkit.getServicesManager().getRegistration(IEssentials.class); if (essService != null) { economy = essService.getProvider().getEconomy(); return economy != null; } return false; } // 获取玩家余额 public double getPlayerBalance(Player player) { return economy != null ? economy.getMoney(player.getUniqueId()) : 0; } // 给玩家增加资金 public void addMoneyToPlayer(Player player, double amount) { if (economy != null) { economy.addMoney(player.getUniqueId(), amount); } } // 扣除玩家资金(返回是否成功) public boolean takeMoneyFromPlayer(Player player, double amount) { if (economy == null || getPlayerBalance(player) < amount) { return false; } economy.subtractMoney(player.getUniqueId(), amount); return true; } }
3. 关键配置补充
在你的plugin.yml中添加依赖声明,确保插件在Essentials加载完成后再启动:
name: YourPluginName main: com.yourpackage.YourMainClass version: 1.0 depend: [Essentials]
注意事项
- 确保服务器已安装EssentialsX(推荐使用,原版Essentials维护停滞)
- 严格匹配版本:Spigot版本与EssentialsX版本需对应,避免兼容性问题
- 始终使用
compileOnly引入依赖,不要将Essentials打包进插件jar,防止与服务器端的Essentials产生冲突
内容的提问来源于stack exchange,提问作者Evan Perez
相关产品推荐
相关产品推荐

