Search in sources :

Example 1 with ClientboundPlayerDiedPacket

use of com.almuradev.almura.feature.death.network.ClientboundPlayerDiedPacket in project Almura by AlmuraDev.

the class DeathHandler method onPlayerDeath.

@Listener(order = Order.LAST)
public void onPlayerDeath(final DestructEntityEvent.Death event, @Root final DamageSource damageSource, @Getter("getTargetEntity") final Player player) {
    this.cacheItemTypes();
    final EconomyService service = Sponge.getServiceManager().provide(EconomyService.class).orElse(null);
    if (service != null) {
        final Server server = Sponge.getServer();
        final double deathTax = RANGE.random(RANDOM);
        final Account account = service.getOrCreateAccount(player.getUniqueId()).orElse(null);
        BigDecimal balance;
        if (account != null && this.areCoinsLoaded()) {
            final Currency currency = service.getDefaultCurrency();
            double deathTaxAmount = 0;
            double droppedAmount = 0;
            boolean displayDrops = false;
            final DecimalFormat dFormat = new DecimalFormat("###,###,###,###.##");
            balance = account.getBalance(currency);
            if (balance.doubleValue() > 0 && !player.hasPermission(Almura.ID + ".death.exempt")) {
                double dropAmount = balance.doubleValue() - (balance.doubleValue() * (deathTax / 100));
                if (dropAmount > balance.doubleValue()) {
                    dropAmount = balance.doubleValue();
                }
                BigDecimal deduct = new BigDecimal(dropAmount);
                account.withdraw(currency, deduct, Sponge.getCauseStackManager().getCurrentCause());
                deathTaxAmount = this.dropAmountReturnChange(player, dropAmount);
                droppedAmount = (dropAmount - deathTaxAmount);
                displayDrops = true;
            }
            final double finalDroppedAmount = droppedAmount;
            final double finalDeathTaxAmount = deathTaxAmount;
            final boolean finalDisplayDrops = displayDrops;
            if (event.getMessage().toPlain().isEmpty()) {
                // Note:  noticed that sometimes, for what ever reason the message is empty... Mojang bug?
                UchatUtil.relayMessageToDiscord(":skull:", Text.of(player.getName() + " has died, dropped: $" + dFormat.format(finalDroppedAmount) + " and lost: $" + dFormat.format(finalDeathTaxAmount) + " to death taxes.").toPlain(), true);
            } else {
                UchatUtil.relayMessageToDiscord(":skull:", Text.of(event.getMessage().toPlain() + ", dropped: $" + dFormat.format(finalDroppedAmount) + " and lost: $" + dFormat.format(finalDeathTaxAmount) + " to death taxes.").toPlain(), true);
            }
            server.getOnlinePlayers().forEach(onlinePlayer -> {
                if (onlinePlayer.getUniqueId().equals(player.getUniqueId())) {
                    this.network.sendTo(player, new ClientboundPlayerDiedPacket(finalDroppedAmount, finalDeathTaxAmount, finalDisplayDrops, player.hasPermission(Almura.ID + ".death.revive")));
                } else {
                    serverNotificationManager.sendPopupNotification(onlinePlayer, Text.of(player.getName() + " has died!"), Text.of("Dropped: " + TextFormatting.GOLD + "$" + dFormat.format(finalDroppedAmount) + TextFormatting.RESET + " and " + "lost: " + TextFormatting.RED + "$" + dFormat.format(finalDeathTaxAmount) + TextFormatting.RESET + " to death taxes."), 5);
                }
            });
            return;
        }
    }
    // Service or Account was null, fallback.
    this.network.sendTo(player, new ClientboundPlayerDiedPacket(0.00, 0.00, false, player.hasPermission(Almura.ID + "death.revive")));
}
Also used : Account(org.spongepowered.api.service.economy.account.Account) EconomyService(org.spongepowered.api.service.economy.EconomyService) Server(org.spongepowered.api.Server) Currency(org.spongepowered.api.service.economy.Currency) DecimalFormat(java.text.DecimalFormat) ClientboundPlayerDiedPacket(com.almuradev.almura.feature.death.network.ClientboundPlayerDiedPacket) BigDecimal(java.math.BigDecimal) Listener(org.spongepowered.api.event.Listener)

Aggregations

ClientboundPlayerDiedPacket (com.almuradev.almura.feature.death.network.ClientboundPlayerDiedPacket)1 BigDecimal (java.math.BigDecimal)1 DecimalFormat (java.text.DecimalFormat)1 Server (org.spongepowered.api.Server)1 Listener (org.spongepowered.api.event.Listener)1 Currency (org.spongepowered.api.service.economy.Currency)1 EconomyService (org.spongepowered.api.service.economy.EconomyService)1 Account (org.spongepowered.api.service.economy.account.Account)1