BotW-Minecraft-Server / EcoHelper

A helper to manage player's balance with LightEconomy
MIT License
0 stars 0 forks source link

LightEconomy 'EVENT_CANCELED' problem #3

Closed Sam-Chai closed 4 months ago

Sam-Chai commented 5 months ago

我在调用lighteco的API的时候,返回给我EVENT_CANCELED

目前不太确定是我的代码问题还是lighteco的问题,不过有意思的是lighteco以modImplementation模式作为依赖之后不会加载lighteco的CCA依赖,会报缺类的报错。我需要在ecohelper侧添加CCA作为额外的API支持。

// LightEconomy
    modImplementation "committee.nova.mods:lighteco-fabric:1.20.1-1.2.0"
---- Minecraft Crash Report ----
// You're mean.

Time: 2024-05-22 21:32:05
Description: Exception in server tick loop

java.lang.NoClassDefFoundError: dev/onyxstudios/cca/api/v3/entity/EntityComponentInitializer
    at committee.nova.mods.lighteco.api.FabricEco.getBalance(FabricEco.java:70)
    at committee.nova.mods.lighteco.api.FabricEco.getBalance(FabricEco.java:33)
    at link.botwmcs.ecohelper.util.ApiHandler.getBalance(ApiHandler.java:12)
    at link.botwmcs.ecohelper.command.EcoCommand.lambda$register$0(EcoCommand.java:21)
    at com.mojang.brigadier.CommandDispatcher.execute(CommandDispatcher.java:264)
    at net.minecraft.commands.Commands.performCommand(Commands.java:287)
    at net.minecraft.commands.Commands.performPrefixedCommand(Commands.java:280)
    at net.minecraft.server.dedicated.DedicatedServer.handleConsoleInputs(DedicatedServer.java:299)
    at net.minecraft.server.dedicated.DedicatedServer.tickChildren(DedicatedServer.java:284)
    at net.minecraft.server.MinecraftServer.tickServer(MinecraftServer.java:824)
    at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:671)
    at net.minecraft.server.MinecraftServer.method_29739(MinecraftServer.java:265)
    at java.base/java.lang.Thread.run(Thread.java:840)
Caused by: java.lang.ClassNotFoundException: dev.onyxstudios.cca.api.v3.entity.EntityComponentInitializer
    ... 13 more

以下是代码实现:

public class ApiHandler {
    public static double getBalance(Player player) {
        Optional<BigDecimal> balance = FabricEco.INSTANCE.getBalance(player);
        double checkBalance = balance.map(BigDecimal::doubleValue).orElse(0.0);
        return Security.safetyCheck(checkBalance);
    }

    public static EcoUtils.EcoActionResult addBalance(Player player, double value) {
        // First round the value to 2 decimal places
        BigDecimal afterValue = MathHelper.roundDouble(value);
        // Get the current balance
        double currentBalance = getBalance(player);
        // Add the value to the current balance
        double newBalance = Security.safetyCheck(currentBalance + afterValue.doubleValue());
        // Calculate the final value
        BigDecimal finalValue = BigDecimal.valueOf(newBalance - currentBalance);
        return FabricEco.INSTANCE.debt(player, finalValue);
    }

    public static EcoUtils.EcoActionResult removeBalance(Player player, double value) {
        BigDecimal afterValue = MathHelper.roundDouble(value);
        double currentBalance = getBalance(player);
        double newBalance = Security.safetyCheck(currentBalance - afterValue.doubleValue());
        BigDecimal finalValue = BigDecimal.valueOf(currentBalance - newBalance);
        return FabricEco.INSTANCE.credit(player, finalValue);
    }

    public static EcoUtils.EcoActionResult setBalance(Player player, double value) {
        BigDecimal afterValue = MathHelper.roundDouble(value);
        double currentBalance = getBalance(player);
        double newBalance = Security.safetyCheck(afterValue.doubleValue());
        BigDecimal finalValue = BigDecimal.valueOf(newBalance - currentBalance);
        return FabricEco.INSTANCE.debt(player, finalValue);
    }
}

检查结果是否合法的小方法:

public static double safetyCheck(double money) {
        double minBalance = ServerConfig.CONFIG.minimumBalance.get();
        double maxBalance = ServerConfig.CONFIG.maximumBalance.get();
        return Math.max(minBalance, Math.min(maxBalance, money));
    }

我翻阅lighteco的代码,发现EVENT_CANCELED只有在vary方法中发现BiPredicate<Player, BigDecimal> argChecker, BiFunction<Player, BigDecimal, BigDecimal> processor, BiPredicate<Player, BigDecimal> resultChecker这三者任意为false,则会取消事件。 另外,在玩家加入游戏之后,读取playerData中的内容,会发现这里都是空的:

cardinal_components: {
        "lighteco:balance": {
            accounts: []
        }
    }

所以不太确定是否是LightEco在1.20.1fabric下的playerData注册有问题还是EcoHelper的代码出错,目前暂未想出办法来解决......还请NovaCommittee的大佬们出谋划策

Sam-Chai commented 4 months ago

目前,通过手动发包的方式解决。 https://github.com/BotW-Minecraft-Server/EcoHelper/blob/modern/src/main/java/link/botwmcs/ecohelper/network/S2CHandler.java