private Optional<Player> validPlayer(Cause cause) { Optional<EntityDamageSource> optionalSrc = cause.first(EntityDamageSource.class); if (optionalSrc.isPresent()) { Entity src = optionalSrc.get().getSource(); if (src instanceof Player) { return Optional.of((Player) src); } else if (src instanceof Projectile) { Projectile projectile = (Projectile) src; Optional<UUID> optionalUUID = projectile.getCreator(); if (!optionalUUID.isPresent()) { return Optional.empty(); } Optional<Player> optionalPlayer = Sponge.getServer().getPlayer(optionalUUID.get()); if (!optionalPlayer.isPresent()) { return Optional.empty(); } return Optional.of(optionalPlayer.get()); } } return Optional.empty(); }
@Listener(order = Order.PRE) public void onDestructEntityEventDeathPlayer( DestructEntityEvent.Death event, @First EntityDamageSource damageSrc) { if (!(event.getTargetEntity() instanceof Player)) { return; } Player player = (Player) event.getTargetEntity(); WorldSettings settings = WorldSettings.get(player.getWorld()); DeathReason reason = DeathReason.GENERIC; Entity src = damageSrc.getSource(); if (src instanceof Player) { reason = DeathReason.PLAYER; } else if (src instanceof Projectile) { Projectile projectile = (Projectile) src; Optional<UUID> optionalUUID = projectile.getCreator(); if (!optionalUUID.isPresent()) { return; } Optional<Player> optionalPlayer = Sponge.getServer().getPlayer(optionalUUID.get()); if (optionalPlayer.isPresent()) { reason = DeathReason.PLAYER; } else { reason = DeathReason.PROJECTILE; } } else { DamageType cause = damageSrc.getType(); reason = DeathReason.valueOf(cause.getName().toUpperCase()); } Optional<EconomyService> optionalEconomy = Sponge.getServiceManager().provide(EconomyService.class); if (!optionalEconomy.isPresent()) { Main.instance().getLog().error("Economy plugin not found"); return; } EconomyService economy = optionalEconomy.get(); BigDecimal balance = economy .getOrCreateAccount(player.getUniqueId()) .get() .getBalance(WorldSettings.get(player.getWorld()).getCurrency()); PlayerWallet wallet = settings.getPlayerWallet(); BigDecimal amount = wallet.getAmount(reason, balance); WalletDropEvent walletDropEvent = new WalletDropEvent(amount, player); if (walletDropEvent.getAmount().compareTo(BigDecimal.ZERO) != 0 && (!Sponge.getEventManager().post(walletDropEvent))) { WalletDrop.depositOrWithdraw( player, walletDropEvent.getAmount().multiply(BigDecimal.valueOf(-1))); if (settings.isDropsEnabled()) { for (MoneyStack moneyStack : walletDropEvent.getMoneyStacks()) { moneyStack.drop(walletDropEvent.getLocation()); } } WalletDrop.sendDeathMessage( WorldSettings.get(player.getWorld()), player, walletDropEvent.getAmount()); } }