Esempio n. 1
0
 @Override
 public Optional<Text> getValue(@Nullable Object player) {
   if (player == null) return Optional.empty();
   if (player instanceof Player) {
     Player p = (Player) player;
     if (Sponge.getServiceManager().provide(EconomyService.class).isPresent()) {
       EconomyService es = Sponge.getServiceManager().provide(EconomyService.class).get();
       if (es.getOrCreateAccount(p.getUniqueId()).isPresent()) {
         BigDecimal balance =
             es.getOrCreateAccount(p.getUniqueId()).get().getBalance(es.getDefaultCurrency());
         return Optional.of(Text.of(balance.toString()));
       }
     }
     return Optional.empty();
   }
   return Optional.empty();
 }
Esempio n. 2
0
  @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());
    }
  }