Search in sources :

Example 1 with ClientboundMembershipSuccessPacket

use of com.almuradev.almura.feature.membership.network.ClientboundMembershipSuccessPacket in project Almura by AlmuraDev.

the class MembershipHandler method handleMembershipPurchase.

public void handleMembershipPurchase(Player player, int newMembershipLevel) {
    // Typically only thing that fails here is if someone is running a hacked client.  This method double checks that.
    if (!player.hasPermission("almura.membership.purchase")) {
        serverNotificationManager.sendWindowMessage(player, Text.of("Permission Denied"), Text.of("Missing: almura.membership.purchase"));
        return;
    }
    final EconomyService econService = Sponge.getServiceManager().provide(EconomyService.class).orElse(null);
    final LuckPerms permService = Sponge.getServiceManager().provide(LuckPerms.class).orElse(null);
    if (econService != null && permService != null) {
        final Account account = econService.getOrCreateAccount(player.getUniqueId()).orElse(null);
        if (account != null) {
            BigDecimal balance;
            final Currency currency = econService.getDefaultCurrency();
            balance = account.getBalance(currency);
            double fee = 0;
            if (newMembershipLevel == 1) {
                fee = 2500000;
                if (!(balance.doubleValue() >= fee)) {
                    serverNotificationManager.sendWindowMessage(player, Text.of("Insufficient Funds"), Text.of("Insufficient Funds to purchase Citizen Membership."));
                    return;
                }
            }
            if (newMembershipLevel == 2) {
                fee = 5000000;
                if (!(balance.doubleValue() >= fee)) {
                    serverNotificationManager.sendWindowMessage(player, Text.of("Insufficient Funds"), Text.of("Insufficient Funds to purchase Explorer Membership."));
                    return;
                }
            }
            if (newMembershipLevel == 3) {
                fee = 10000000;
                if (!(balance.doubleValue() >= fee)) {
                    serverNotificationManager.sendWindowMessage(player, Text.of("Insufficient Funds"), Text.of("Insufficient Funds to purchase Pioneer Membership."));
                    return;
                }
            }
            String currentGroup = permService.getUserManager().getUser(player.getUniqueId()).getPrimaryGroup();
            String newMembership = "";
            if (newMembershipLevel == 1)
                newMembership = "Citizen";
            if (newMembershipLevel == 2)
                newMembership = "Explorer";
            if (newMembershipLevel == 3)
                newMembership = "Pioneer";
            BigDecimal deduct = new BigDecimal(fee);
            final String command = "lp user " + player.getName() + " promote members";
            if (newMembershipLevel == 1 && currentGroup.equalsIgnoreCase("survivor")) {
                this.commandManager.process(Sponge.getServer().getConsole(), command);
                account.withdraw(currency, deduct, Sponge.getCauseStackManager().getCurrentCause());
                this.network.sendTo(player, new ClientboundMembershipSuccessPacket(newMembership));
                return;
            }
            if (newMembershipLevel == 2 && (currentGroup.equalsIgnoreCase("survivor") || currentGroup.equalsIgnoreCase("citizen"))) {
                if (currentGroup.equalsIgnoreCase("survivor")) {
                    this.commandManager.process(Sponge.getServer().getConsole(), command);
                }
                this.commandManager.process(Sponge.getServer().getConsole(), command);
                account.withdraw(currency, deduct, Sponge.getCauseStackManager().getCurrentCause());
                this.network.sendTo(player, new ClientboundMembershipSuccessPacket(newMembership));
                return;
            }
            if (newMembershipLevel == 3 && (currentGroup.equalsIgnoreCase("survivor") || currentGroup.equalsIgnoreCase("citizen") || currentGroup.equalsIgnoreCase("explorer"))) {
                if (currentGroup.equalsIgnoreCase("survivor")) {
                    this.commandManager.process(Sponge.getServer().getConsole(), command);
                    this.commandManager.process(Sponge.getServer().getConsole(), command);
                }
                if (currentGroup.equalsIgnoreCase("citizen")) {
                    this.commandManager.process(Sponge.getServer().getConsole(), command);
                }
                this.commandManager.process(Sponge.getServer().getConsole(), command);
                account.withdraw(currency, deduct, Sponge.getCauseStackManager().getCurrentCause());
                this.network.sendTo(player, new ClientboundMembershipSuccessPacket(newMembership));
                return;
            }
            // This should be impossible, leaving this debug line.
            System.out.println("Current Group: " + currentGroup + " Failed to upgrade, no path found");
        }
    } else {
        serverNotificationManager.sendWindowMessage(player, Text.of("Error"), Text.of("Economy or Permission Service offline!"));
    }
}
Also used : Account(org.spongepowered.api.service.economy.account.Account) LuckPerms(net.luckperms.api.LuckPerms) EconomyService(org.spongepowered.api.service.economy.EconomyService) Currency(org.spongepowered.api.service.economy.Currency) ClientboundMembershipSuccessPacket(com.almuradev.almura.feature.membership.network.ClientboundMembershipSuccessPacket) BigDecimal(java.math.BigDecimal)

Aggregations

ClientboundMembershipSuccessPacket (com.almuradev.almura.feature.membership.network.ClientboundMembershipSuccessPacket)1 BigDecimal (java.math.BigDecimal)1 LuckPerms (net.luckperms.api.LuckPerms)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