All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.the_fireplace.grandeconomy.events.KillingEvents Maven / Gradle / Ivy

There is a newer version: 4.1.0+1.16.5
Show newest version
package dev.the_fireplace.grandeconomy.events;

import dev.the_fireplace.grandeconomy.GrandEconomy;
import dev.the_fireplace.grandeconomy.api.CurrencyAPI;
import dev.the_fireplace.grandeconomy.config.ModConfig;
import net.minecraft.class_1282;
import net.minecraft.class_3222;

public class KillingEvents {
    public static void onPlayerDeath(class_3222 dyingPlayer, class_1282 source) {
        if (!dyingPlayer.field_6002.method_8608()) {
            double flatMoneyTransferAmount = ModConfig.getData().getPvpMoneyTransferFlat();
            double percentMoneyTransferAmount = ModConfig.getData().getPvpMoneyTransferPercent();
            if (doubleEquals(flatMoneyTransferAmount, 0) && doubleEquals(percentMoneyTransferAmount, 0))
                return;

            if (!(source.method_5529() instanceof class_3222))
                return;
            class_3222 killer = (class_3222) source.method_5529();

            CurrencyAPI currencyAPI = CurrencyAPI.getInstance();
            double dyingPlayerBalance = currencyAPI.getBalance(dyingPlayer.method_5667(), true);
            if (dyingPlayerBalance < 0.01) {
                return;
            }
            double amountTaken = constrain(
                (dyingPlayerBalance * percentMoneyTransferAmount) / 100 + flatMoneyTransferAmount,
                0,
                dyingPlayerBalance
            );
            currencyAPI.takeFromBalance(dyingPlayer.method_5667(), amountTaken, true);
            GrandEconomy.getFeedbackSender().basic(dyingPlayer, "grandeconomy.killed_balance", currencyAPI.getFormattedBalance(dyingPlayer.method_5667(), true));

            currencyAPI.addToBalance(killer.method_5667(), amountTaken, true);
            GrandEconomy.getFeedbackSender().basic(killer, "grandeconomy.killer_balance", currencyAPI.getFormattedBalance(killer.method_5667(), true));
        }
    }

    private static boolean doubleEquals(double d1, double d2) {
        return Math.abs(d1 - d2) < 0.0001;
    }

    private static double constrain(double input, double min, double max) {
        return Math.max(Math.min(input, max), min);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy