Java Code Examples for org.spongepowered.api.service.economy.transaction.ResultType#SUCCESS

The following examples show how to use org.spongepowered.api.service.economy.transaction.ResultType#SUCCESS . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TaxApplyTask.java    From GriefDefender with MIT License 4 votes vote down vote up
private void handleClaimTax(GDClaim claim, GDPlayerData playerData, boolean inTown) {
    final GDPermissionUser user = PermissionHolderCache.getInstance().getOrCreateUser(playerData.getUniqueId());
    double taxRate = GDPermissionManager.getInstance().getInternalOptionValue(TypeToken.of(Double.class), user, Options.TAX_RATE, claim);
    double taxOwed = claim.getEconomyData().getTaxBalance() + (claim.getClaimBlocks() * taxRate);
    try (final CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        Sponge.getCauseStackManager().pushCause(GriefDefenderPlugin.getInstance());
        GDTaxClaimEvent event = new GDTaxClaimEvent(claim, taxRate, taxOwed);
        GriefDefender.getEventManager().post(event);
        if (event.cancelled()) {
            return;
        }
        final double taxBalance = claim.getEconomyData().getTaxBalance();
        taxRate = event.getTaxRate();
        taxOwed = taxBalance + (claim.getClaimBlocks() * taxRate);
        final TransactionResult result = EconomyUtil.withdrawFunds(user.getUniqueId(), taxOwed);
        if (result.getResult() != ResultType.SUCCESS) {
            final Instant localNow = Instant.now();
            Instant taxPastDueDate = claim.getEconomyData().getTaxPastDueDate();
            if (taxPastDueDate == null) {
                 claim.getEconomyData().setTaxPastDueDate(Instant.now());
            } else {
                final int taxExpirationDays = GDPermissionManager.getInstance().getInternalOptionValue(TypeToken.of(Integer.class), user, Options.TAX_EXPIRATION, claim);
                if (taxExpirationDays > 0) {
                    claim.getInternalClaimData().setExpired(true);
                    if (taxExpirationDays == 0) {
                        claim.getInternalClaimData().setExpired(true);
                        claim.getData().save();
                    } else if (taxPastDueDate.plus(Duration.ofDays(taxExpirationDays)).isBefore(localNow)) {
                        claim.getInternalClaimData().setExpired(true);
                        claim.getData().save();
                    }
                }
            }
            final double totalTaxOwed = taxBalance + taxOwed;
            claim.getEconomyData().setTaxBalance(totalTaxOwed);
            claim.getEconomyData().addPaymentTransaction(new GDPaymentTransaction(TransactionType.TAX, TransactionResultType.FAIL, Instant.now(), taxOwed));
        } else {
            claim.getEconomyData().addPaymentTransaction(new GDPaymentTransaction(TransactionType.TAX, TransactionResultType.SUCCESS, Instant.now(), taxOwed));
            claim.getEconomyData().setTaxPastDueDate(null);
            claim.getEconomyData().setTaxBalance(0);
            claim.getInternalClaimData().setExpired(false);

            if (inTown) {
                final GDClaim town = claim.getTownClaim();
                town.getData()
                    .getEconomyData()
                    .addPaymentTransaction(new GDPaymentTransaction(TransactionType.TAX, TransactionResultType.SUCCESS, Instant.now(), taxOwed));
                if (town.getEconomyAccountId().isPresent()) {
                    EconomyUtil.depositFunds(town.getEconomyAccountId().get(), taxOwed);
                }
            }
            claim.getData().save();
        }
    }
}
 
Example 2
Source File: NationDepositExecutor.java    From Nations with MIT License 4 votes vote down vote up
public CommandResult execute(CommandSource src, CommandContext ctx) throws CommandException
{
	if (src instanceof Player)
	{
		if (!ctx.<Double>getOne("amount").isPresent())
		{
			src.sendMessage(Text.of(TextColors.YELLOW, "/n deposit <amount>\n/n withdraw <amount>"));
			return CommandResult.success();
		}
		
		Player player = (Player) src;
		Nation nation = DataHandler.getNationOfPlayer(player.getUniqueId());
		if (nation == null)
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NONATION));
			return CommandResult.success();
		}
		
		if (NationsPlugin.getEcoService() == null)
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NOECO));
			return CommandResult.success();
		}
		Optional<UniqueAccount> optAccount = NationsPlugin.getEcoService().getOrCreateAccount(player.getUniqueId());
		if (!optAccount.isPresent())
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_ECONOACCOUNT));
			return CommandResult.success();
		}
		Optional<Account> optNationAccount = NationsPlugin.getEcoService().getOrCreateAccount("nation-" + nation.getUUID().toString());
		if (!optNationAccount.isPresent())
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_ECONONATION));
			return CommandResult.success();
		}
		BigDecimal amount = BigDecimal.valueOf(ctx.<Double>getOne("amount").get());
		TransactionResult result = optAccount.get().transfer(optNationAccount.get(), NationsPlugin.getEcoService().getDefaultCurrency(), amount, NationsPlugin.getCause());
		if (result.getResult() == ResultType.ACCOUNT_NO_FUNDS)
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NOENOUGHMONEY));
			return CommandResult.success();
		}
		else if (result.getResult() != ResultType.SUCCESS)
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_ECOTRANSACTION));
			return CommandResult.success();
		}
		
		String[] s1 = LanguageHandler.SUCCESS_DEPOSIT.split("\\{AMOUNT\\}");
		Builder builder = Text.builder();
		if (s1[0].indexOf("{BALANCE}") >= 0)
		{
			String[] splited0 = s1[0].split("\\{BALANCE\\}");
			builder
			.append(Text.of(TextColors.GREEN, (splited0.length > 0) ? splited0[0] : ""))
			.append(Utils.formatPrice(TextColors.GREEN, optNationAccount.get().getBalance(NationsPlugin.getEcoService().getDefaultCurrency())))
			.append(Text.of(TextColors.GREEN, (splited0.length > 1) ? splited0[1] : ""));
		}
		else
		{
			builder.append(Text.of(TextColors.GREEN, s1[0]));
		}
		builder.append(Utils.formatPrice(TextColors.GREEN, amount));
		if (s1[1].indexOf("{BALANCE}") >= 0)
		{
			String[] splited1 = s1[1].split("\\{BALANCE\\}");
			builder
			.append(Text.of(TextColors.GREEN, (splited1.length > 0) ? splited1[0] : ""))
			.append(Utils.formatPrice(TextColors.GREEN, optNationAccount.get().getBalance(NationsPlugin.getEcoService().getDefaultCurrency())))
			.append(Text.of(TextColors.GREEN, (splited1.length > 1) ? splited1[1] : ""));
		}
		else
		{
			builder.append(Text.of(TextColors.GREEN, s1[1]));
		}
		src.sendMessage(builder.build());
	}
	else
	{
		src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NOPLAYER));
	}
	return CommandResult.success();
}
 
Example 3
Source File: NationCreateExecutor.java    From Nations with MIT License 4 votes vote down vote up
public CommandResult execute(CommandSource src, CommandContext ctx) throws CommandException
{
	if (!ctx.<String>getOne("name").isPresent())
	{
		src.sendMessage(Text.of(TextColors.YELLOW, "/n create <name>"));
		return CommandResult.success();
	}
	if (src instanceof Player)
	{
		Player player = (Player) src;
		if (!ConfigHandler.getNode("worlds").getNode(player.getWorld().getName()).getNode("enabled").getBoolean())
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_PLUGINDISABLEDINWORLD));
			return CommandResult.success();
		}
		if (DataHandler.getNationOfPlayer(player.getUniqueId()) != null)
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NEEDLEAVE));
			return CommandResult.success();
		}
		String nationName = ctx.<String>getOne("name").get();
		if (DataHandler.getNation(nationName) != null)
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NAMETAKEN));
			return CommandResult.success();
		}
		if (!nationName.matches("[\\p{Alnum}\\p{IsIdeographic}\\p{IsLetter}\"_\"]*"))
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NAMEALPHA));
			return CommandResult.success();
		}
		if (nationName.length() < ConfigHandler.getNode("others", "minNationNameLength").getInt() || nationName.length() > ConfigHandler.getNode("others", "maxNationNameLength").getInt())
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NAMELENGTH
					.replaceAll("\\{MIN\\}", ConfigHandler.getNode("others", "minNationNameLength").getString())
					.replaceAll("\\{MAX\\}", ConfigHandler.getNode("others", "maxNationNameLength").getString())));
			return CommandResult.success();
		}
		
		if (NationsPlugin.getEcoService() == null)
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NOECO));
			return CommandResult.success();
		}
		Optional<UniqueAccount> optAccount = NationsPlugin.getEcoService().getOrCreateAccount(player.getUniqueId());
		if (!optAccount.isPresent())
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_ECONOACCOUNT));
			return CommandResult.success();
		}
		BigDecimal price = BigDecimal.valueOf(ConfigHandler.getNode("prices", "nationCreationPrice").getDouble());
		TransactionResult result = optAccount.get().withdraw(NationsPlugin.getEcoService().getDefaultCurrency(), price, NationsPlugin.getCause());
		if (result.getResult() == ResultType.ACCOUNT_NO_FUNDS)
		{
			src.sendMessage(Text.builder()
					.append(Text.of(TextColors.RED, LanguageHandler.ERROR_NEEDMONEY.split("\\{AMOUNT\\}")[0]))
					.append(Utils.formatPrice(TextColors.RED, price))
					.append(Text.of(TextColors.RED, LanguageHandler.ERROR_NEEDMONEY.split("\\{AMOUNT\\}")[1])).build());
			return CommandResult.success();
		}
		else if (result.getResult() != ResultType.SUCCESS)
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_ECOTRANSACTION));
			return CommandResult.success();
		}
		
		Nation nation = new Nation(UUID.randomUUID(), nationName);
		nation.addCitizen(player.getUniqueId());
		nation.setPresident(player.getUniqueId());
		Optional<Account> optNationAccount = NationsPlugin.getEcoService().getOrCreateAccount("nation-" + nation.getUUID().toString());
		if (!optNationAccount.isPresent())
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_CREATEECONATION));
			NationsPlugin.getLogger().error("Could not create nation's account on the economy service !");
			return CommandResult.success();
		}
		optNationAccount.get().setBalance(NationsPlugin.getEcoService().getDefaultCurrency(), BigDecimal.ZERO, NationsPlugin.getCause());
		DataHandler.addNation(nation);
		MessageChannel.TO_ALL.send(Text.of(TextColors.AQUA, LanguageHandler.INFO_NEWNATIONANNOUNCE.replaceAll("\\{PLAYER\\}", player.getName()).replaceAll("\\{NATION\\}", nation.getName())));
		src.sendMessage(Text.of(TextColors.GREEN, LanguageHandler.INFO_NEWNATION.replaceAll("\\{NATION\\}", nation.getName())));
	}
	else
	{
		src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NOPLAYER));
	}
	return CommandResult.success();
}
 
Example 4
Source File: NationClaimOutpostExecutor.java    From Nations with MIT License 4 votes vote down vote up
public CommandResult execute(CommandSource src, CommandContext ctx) throws CommandException
{
	if (src instanceof Player)
	{
		Player player = (Player) src;
		if (!ConfigHandler.getNode("worlds").getNode(player.getWorld().getName()).getNode("enabled").getBoolean())
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_PLUGINDISABLEDINWORLD));
			return CommandResult.success();
		}
		Nation nation = DataHandler.getNationOfPlayer(player.getUniqueId());
		if (nation == null)
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NONATION));
			return CommandResult.success();
		}
		if (!nation.isStaff(player.getUniqueId()))
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_PERM_NATIONSTAFF));
			return CommandResult.success();
		}
		Location<World> loc = player.getLocation();
		if (!DataHandler.canClaim(loc, false, nation.getUUID()))
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_TOOCLOSE));
			return CommandResult.success();
		}
		
		if (NationsPlugin.getEcoService() == null)
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NOECO));
			return CommandResult.success();
		}
		Optional<Account> optAccount = NationsPlugin.getEcoService().getOrCreateAccount("nation-" + nation.getUUID());
		if (!optAccount.isPresent())
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_ECONONATION));
			return CommandResult.success();
		}
		BigDecimal price = BigDecimal.valueOf(ConfigHandler.getNode("prices", "outpostCreationPrice").getDouble());
		TransactionResult result = optAccount.get().withdraw(NationsPlugin.getEcoService().getDefaultCurrency(), price, NationsPlugin.getCause());
		if (result.getResult() == ResultType.ACCOUNT_NO_FUNDS)
		{
			src.sendMessage(Text.builder()
					.append(Text.of(TextColors.RED, LanguageHandler.ERROR_NEEDMONEYNATION.split("\\{AMOUNT\\}")[0]))
					.append(Utils.formatPrice(TextColors.RED, price))
					.append(Text.of(TextColors.RED, LanguageHandler.ERROR_NEEDMONEYNATION.split("\\{AMOUNT\\}")[1])).build());
			return CommandResult.success();
		}
		else if (result.getResult() != ResultType.SUCCESS)
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_ECOTRANSACTION));
			return CommandResult.success();
		}
		
		nation.getRegion().addRect(new Rect(loc.getExtent().getUniqueId(), loc.getBlockX(), loc.getBlockX(), loc.getBlockZ(), loc.getBlockZ()));
		DataHandler.addToWorldChunks(nation);
		DataHandler.saveNation(nation.getUUID());
		src.sendMessage(Text.of(TextColors.GREEN, LanguageHandler.SUCCESS_OUTPOST));
	}
	else
	{
		src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NOPLAYER));
	}
	return CommandResult.success();
}
 
Example 5
Source File: NationUnclaimExecutor.java    From Nations with MIT License 4 votes vote down vote up
public CommandResult execute(CommandSource src, CommandContext ctx) throws CommandException
{
	if (src instanceof Player)
	{
		Player player = (Player) src;
		Nation nation = DataHandler.getNationOfPlayer(player.getUniqueId());
		if (nation == null)
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NONATION));
			return CommandResult.success();
		}
		if (!nation.isStaff(player.getUniqueId()))
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_PERM_NATIONSTAFF));
			return CommandResult.success();
		}
		Point a = DataHandler.getFirstPoint(player.getUniqueId());
		Point b = DataHandler.getSecondPoint(player.getUniqueId());
		if (a == null || b == null)
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NEEDAXESELECT));
			return CommandResult.success();
		}
		if (!ConfigHandler.getNode("worlds").getNode(a.getWorld().getName()).getNode("enabled").getBoolean())
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_PLUGINDISABLEDINWORLD));
			return CommandResult.success();
		}
		Rect rect = new Rect(a, b);
		if (!nation.getRegion().intersects(rect))
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NEEDINTERSECT));
			return CommandResult.success();
		}
		for (Location<World> spawn : nation.getSpawns().values())
		{
			if (rect.isInside(new Vector2i(spawn.getBlockX(), spawn.getBlockZ())))
			{
				src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_AREACONTAINSPAWN));
				return CommandResult.success();
			}
		}
		for (Zone zone : nation.getZones().values())
		{
			if (zone.getRect().intersects(rect))
			{
				src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_SELECTIONCONTAINZONE));
				return CommandResult.success();
			}
		}
		Region claimed = nation.getRegion().copy();
		claimed.removeRect(rect);
		int toUnclaim = nation.getRegion().size() - claimed.size();
		
		BigDecimal refund = BigDecimal.valueOf(0);
		if (ConfigHandler.getNode("prices", "unclaimRefundPercentage").getInt() != 0)
		{
			if (NationsPlugin.getEcoService() == null)
			{
				src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NOECO));
				return CommandResult.success();
			}
			Optional<Account> optAccount = NationsPlugin.getEcoService().getOrCreateAccount("nation-" + nation.getUUID().toString());
			if (!optAccount.isPresent())
			{
				src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_ECONONATION));
				return CommandResult.success();
			}
			refund = BigDecimal.valueOf(toUnclaim * ConfigHandler.getNode("prices", "blockClaimPrice").getInt() * (ConfigHandler.getNode("prices", "unclaimRefundPercentage").getInt() / 100));
			TransactionResult result = optAccount.get().deposit(NationsPlugin.getEcoService().getDefaultCurrency(), refund, NationsPlugin.getCause());
			if (result.getResult() != ResultType.SUCCESS)
			{
				src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_ECOTRANSACTION));
				return CommandResult.success();
			}
		}
		
		
		nation.setRegion(claimed);
		DataHandler.addToWorldChunks(nation);
		DataHandler.saveNation(nation.getUUID());
		if (!refund.equals(BigDecimal.ZERO))
		{
			String str = LanguageHandler.INFO_UNCLAIMREFUND.replaceAll("\\{NUM\\}", Integer.toString(toUnclaim)).replaceAll("\\{PRECENT\\}", ConfigHandler.getNode("prices", "blockClaimPrice").getString());
			src.sendMessage(Text.builder()
					.append(Text.of(TextColors.AQUA, str.split("\\{AMOUNT\\}")[0]))
					.append(Utils.formatPrice(TextColors.AQUA, refund))
					.append(Text.of(TextColors.AQUA, str.split("\\{AMOUNT\\}")[1])).build());
		}
		else
		{
			src.sendMessage(Text.of(TextColors.AQUA, LanguageHandler.SUCCESS_UNCLAIM));
		}
	}
	else
	{
		src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NOPLAYER));
	}
	return CommandResult.success();
}
 
Example 6
Source File: NationBuyextraExecutor.java    From Nations with MIT License 4 votes vote down vote up
public CommandResult execute(CommandSource src, CommandContext ctx) throws CommandException
{
	if (src instanceof Player)
	{
		Player player = (Player) src;
		Nation nation = DataHandler.getNationOfPlayer(player.getUniqueId());
		if (nation == null)
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NONATION));
			return CommandResult.success();
		}
		if (!nation.isStaff(player.getUniqueId()))
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_PERM_NATIONPRES));
			return CommandResult.success();
		}
		if (!ctx.<String>getOne("amount").isPresent())
		{
			src.sendMessage(Text.of(TextColors.YELLOW, "/n buyextra <amount>"));
			return CommandResult.success();
		}
		int n = ctx.<Integer>getOne("amount").get();
		int maxToBuy = ConfigHandler.getNode("others", "maxExtra").getInt() - nation.getExtras();
		if (n > maxToBuy)
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NOMOREBLOCK.replaceAll("\\{NUM\\}", Integer.toString(maxToBuy))));
			return CommandResult.success();
		}

		if (NationsPlugin.getEcoService() == null)
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NOECO));
			return CommandResult.success();
		}
		Optional<Account> optAccount = NationsPlugin.getEcoService().getOrCreateAccount("nation-" + nation.getUUID().toString());
		if (!optAccount.isPresent())
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_ECONONATION));
			return CommandResult.success();
		}
		BigDecimal price = BigDecimal.valueOf(n * ConfigHandler.getNode("prices", "extraPrice").getDouble());
		TransactionResult result = optAccount.get().withdraw(NationsPlugin.getEcoService().getDefaultCurrency(), price, NationsPlugin.getCause());
		if (result.getResult() == ResultType.ACCOUNT_NO_FUNDS)
		{
			String[] splited = LanguageHandler.ERROR_NEEDMONEY.split("\\{AMOUNT\\}");
			src.sendMessage(Text.builder()
					.append(Text.of(TextColors.RED, (splited.length > 0) ? splited[0] : ""))
					.append(Utils.formatPrice(TextColors.RED, price))
					.append(Text.of(TextColors.RED, (splited.length > 1) ? splited[1] : "")).build());
			return CommandResult.success();
		}
		else if (result.getResult() != ResultType.SUCCESS)
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_ECOTRANSACTION));
			return CommandResult.success();
		}

		nation.addExtras(n);
		DataHandler.saveNation(nation.getUUID());
		String[] splited2 = LanguageHandler.SUCCESS_ADDBLOCKS.replaceAll("\\{NUM\\}", Integer.toString(n)).split("\\{AMOUNT\\}");
		src.sendMessage(Text.builder()
				.append(Text.of(TextColors.AQUA, (splited2.length > 0) ? splited2[0] : ""))
				.append(Utils.formatPrice(TextColors.AQUA, price))
				.append(Text.of(TextColors.AQUA, (splited2.length > 1) ? splited2[1] : "")).build());
	}
	else
	{
		src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NOPLAYER));
	}
	return CommandResult.success();
}
 
Example 7
Source File: NationWithdrawExecutor.java    From Nations with MIT License 4 votes vote down vote up
public CommandResult execute(CommandSource src, CommandContext ctx) throws CommandException
{
	if (src instanceof Player)
	{
		if (!ctx.<Double>getOne("amount").isPresent())
		{
			src.sendMessage(Text.of(TextColors.YELLOW, "/n deposit <amount>\n/n withdraw <amount>"));
			return CommandResult.success();
		}
		
		Player player = (Player) src;
		Nation nation = DataHandler.getNationOfPlayer(player.getUniqueId());
		if (nation == null)
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NONATION));
			return CommandResult.success();
		}
		if (!nation.isStaff(player.getUniqueId()))
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_PERM_NATIONSTAFF));
			return CommandResult.success();
		}
		
		if (NationsPlugin.getEcoService() == null)
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NOECO));
			return CommandResult.success();
		}
		Optional<UniqueAccount> optAccount = NationsPlugin.getEcoService().getOrCreateAccount(player.getUniqueId());
		if (!optAccount.isPresent())
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_ECONOACCOUNT));
			return CommandResult.success();
		}
		Optional<Account> optNationAccount = NationsPlugin.getEcoService().getOrCreateAccount("nation-" + nation.getUUID().toString());
		if (!optNationAccount.isPresent())
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_ECONONATION));
			return CommandResult.success();
		}
		BigDecimal amount = BigDecimal.valueOf(ctx.<Double>getOne("amount").get());
		TransactionResult result = optNationAccount.get().transfer(optAccount.get(), NationsPlugin.getEcoService().getDefaultCurrency(), amount, NationsPlugin.getCause());
		if (result.getResult() == ResultType.ACCOUNT_NO_FUNDS)
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NOENOUGHMONEYNATION));
			return CommandResult.success();
		}
		else if (result.getResult() != ResultType.SUCCESS)
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_ECOTRANSACTION));
			return CommandResult.success();
		}
		
		String[] s1 = LanguageHandler.SUCCESS_WITHDRAW.split("\\{AMOUNT\\}");
		Builder builder = Text.builder();
		if (s1[0].indexOf("\\{BALANCE\\}") >= 0)
		{
			builder
			.append(Text.of(TextColors.GREEN, s1[0].split("\\{BALANCE\\}")[0]))
			.append(Utils.formatPrice(TextColors.GREEN, optNationAccount.get().getBalance(NationsPlugin.getEcoService().getDefaultCurrency())))
			.append(Text.of(TextColors.GREEN, s1[0].split("\\{BALANCE\\}")[1]));
		}
		builder.append(Utils.formatPrice(TextColors.GREEN, amount));
		if (s1[1].indexOf("\\{BALANCE\\}") >= 0)
		{
			builder
			.append(Text.of(TextColors.GREEN, s1[1].split("\\{BALANCE\\}")[0]))
			.append(Utils.formatPrice(TextColors.GREEN, optNationAccount.get().getBalance(NationsPlugin.getEcoService().getDefaultCurrency())))
			.append(Text.of(TextColors.GREEN, s1[1].split("\\{BALANCE\\}")[1]));
		}
		src.sendMessage(builder.build());
	}
	else
	{
		src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NOPLAYER));
	}
	return CommandResult.success();
}
 
Example 8
Source File: NationClaimExecutor.java    From Nations with MIT License 4 votes vote down vote up
public CommandResult execute(CommandSource src, CommandContext ctx) throws CommandException
{
	if (src instanceof Player)
	{
		Player player = (Player) src;
		Nation nation = DataHandler.getNationOfPlayer(player.getUniqueId());
		if (nation == null)
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NONATION));
			return CommandResult.success();
		}
		if (!nation.isStaff(player.getUniqueId()))
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_PERM_NATIONSTAFF));
			return CommandResult.success();
		}
		Point a = DataHandler.getFirstPoint(player.getUniqueId());
		Point b = DataHandler.getSecondPoint(player.getUniqueId());
		if (a == null || b == null)
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NEEDAXESELECT));
			return CommandResult.success();
		}
		if (!ConfigHandler.getNode("worlds").getNode(a.getWorld().getName()).getNode("enabled").getBoolean())
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_PLUGINDISABLEDINWORLD));
			return CommandResult.success();
		}
		Rect rect = new Rect(a, b);
		if (nation.getRegion().size() > 0 && !nation.getRegion().isAdjacent(rect))
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NEEDADJACENT));
			return CommandResult.success();
		}
		if (!DataHandler.canClaim(rect, false, nation.getUUID()))
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_TOOCLOSE));
			return CommandResult.success();
		}
		Region claimed = nation.getRegion().copy();
		claimed.addRect(rect);
		
		if (claimed.size() > nation.maxBlockSize())
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NOENOUGHBLOCKS));
			return CommandResult.success();
		}
		
		if (NationsPlugin.getEcoService() == null)
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NOECO));
			return CommandResult.success();
		}
		Optional<Account> optAccount = NationsPlugin.getEcoService().getOrCreateAccount("nation-" + nation.getUUID().toString());
		if (!optAccount.isPresent())
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_ECONONATION));
			return CommandResult.success();
		}
		BigDecimal price = BigDecimal.valueOf((claimed.size() - nation.getRegion().size()) * ConfigHandler.getNode("prices", "blockClaimPrice").getDouble());
		TransactionResult result = optAccount.get().withdraw(NationsPlugin.getEcoService().getDefaultCurrency(), price, NationsPlugin.getCause());
		if (result.getResult() == ResultType.ACCOUNT_NO_FUNDS)
		{
			src.sendMessage(Text.builder()
					.append(Text.of(TextColors.RED, LanguageHandler.ERROR_NEEDMONEYNATION.split("\\{AMOUNT\\}")[0]))
					.append(Utils.formatPrice(TextColors.RED, price))
					.append(Text.of(TextColors.RED, LanguageHandler.ERROR_NEEDMONEYNATION.split("\\{AMOUNT\\}")[1])).build());
			return CommandResult.success();
		}
		else if (result.getResult() != ResultType.SUCCESS)
		{
			src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_ECOTRANSACTION));
			return CommandResult.success();
		}
		
		nation.setRegion(claimed);
		DataHandler.addToWorldChunks(nation);
		DataHandler.saveNation(nation.getUUID());
		src.sendMessage(Text.of(TextColors.AQUA, LanguageHandler.SUCCESS_CLAIM));
	}
	else
	{
		src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NOPLAYER));
	}
	return CommandResult.success();
}
 
Example 9
Source File: NationadminEcoExecutor.java    From Nations with MIT License 4 votes vote down vote up
public CommandResult execute(CommandSource src, CommandContext ctx) throws CommandException
{
	if (!ctx.<String>getOne("nation").isPresent() || !ctx.<String>getOne("give|take|set").isPresent() || !ctx.<String>getOne("amount").isPresent())
	{
		src.sendMessage(Text.of(TextColors.YELLOW, "/na eco <give|take|set> <nation> <amount>"));
		return CommandResult.success();
	}
	String nationName = ctx.<String>getOne("nation").get();
	BigDecimal amount = BigDecimal.valueOf(ctx.<Double>getOne("amount").get());
	String operation = ctx.<String>getOne("give|take|set").get();
	
	Nation nation = DataHandler.getNation(nationName);
	if (nation == null)
	{
		src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_BADNATIONNAME));
		return CommandResult.success();
	}
	if (NationsPlugin.getEcoService() == null)
	{
		src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_NOECO));
		return CommandResult.success();
	}
	Optional<Account> optAccount = NationsPlugin.getEcoService().getOrCreateAccount("nation-" + nation.getUUID().toString());
	if (!optAccount.isPresent())
	{
		src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_ECONOACCOUNT));
		return CommandResult.success();
	}
	TransactionResult result;
	if (operation.equalsIgnoreCase("give"))
	{
		result = optAccount.get().deposit(NationsPlugin.getEcoService().getDefaultCurrency(), amount, NationsPlugin.getCause());
	}
	else if (operation.equalsIgnoreCase("take"))
	{
		result = optAccount.get().withdraw(NationsPlugin.getEcoService().getDefaultCurrency(), amount, NationsPlugin.getCause());
	}
	else if (operation.equalsIgnoreCase("set"))
	{
		result = optAccount.get().setBalance(NationsPlugin.getEcoService().getDefaultCurrency(), amount, NationsPlugin.getCause());
	}
	else
	{
		src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_BADARG_GTS));
		return CommandResult.success();
	}
	if (result.getResult() != ResultType.SUCCESS)
	{
		src.sendMessage(Text.of(TextColors.RED, LanguageHandler.ERROR_ECOTRANSACTION));
		return CommandResult.success();
	}
	src.sendMessage(Text.of(TextColors.GREEN, LanguageHandler.SUCCESS_GENERAL));
	return CommandResult.success();
}
 
Example 10
Source File: TaxApplyTask.java    From GriefPrevention with MIT License 4 votes vote down vote up
private void handleClaimTax(GPClaim claim, GPPlayerData playerData, boolean inTown) {
    final Subject subject = playerData.getPlayerSubject();
    final Account claimAccount = claim.getEconomyAccount().orElse(null);
    double taxRate = GPOptionHandler.getClaimOptionDouble(subject, claim, GPOptions.Type.TAX_RATE, playerData);
    double taxOwed = claim.getEconomyData().getTaxBalance() + (claim.getClaimBlocks() * taxRate);
    try (final CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        Sponge.getCauseStackManager().pushCause(GriefPreventionPlugin.instance);
        GPTaxClaimEvent event = new GPTaxClaimEvent(claim, taxRate, taxOwed);
        Sponge.getEventManager().post(event);
        if (event.isCancelled()) {
            return;
        }
        final double taxBalance = claim.getEconomyData().getTaxBalance();
        taxRate = event.getTaxRate();
        taxOwed = taxBalance + (claim.getClaimBlocks() * taxRate);

        TransactionResult result = claimAccount.withdraw(this.economyService.getDefaultCurrency(), BigDecimal.valueOf(taxOwed), Sponge.getCauseStackManager().getCurrentCause());
        if (result.getResult() != ResultType.SUCCESS) {
            final Instant localNow = Instant.now();
            Instant taxPastDueDate = claim.getEconomyData().getTaxPastDueDate().orElse(null);
            if (taxPastDueDate == null) {
                claim.getEconomyData().setTaxPastDueDate(localNow);
            } else {
                final int taxExpirationDays = GPOptionHandler.getClaimOptionDouble(subject, claim, GPOptions.Type.TAX_EXPIRATION, playerData).intValue();
                if (taxExpirationDays <= 0) {
                    claim.getInternalClaimData().setExpired(true);
                    claim.getData().save();
                } else if (taxPastDueDate.plus(Duration.ofDays(taxExpirationDays)).isBefore(localNow)) {
                    claim.getInternalClaimData().setExpired(true);
                    claim.getData().save();
                }
            }
            final double totalTaxOwed = taxBalance + taxOwed;
            claim.getEconomyData().setTaxBalance(totalTaxOwed);
            claim.getEconomyData().addBankTransaction(new GPBankTransaction(BankTransactionType.TAX_FAIL, Instant.now(), taxOwed));
        } else {
            claim.getEconomyData().addBankTransaction(new GPBankTransaction(BankTransactionType.TAX_SUCCESS, Instant.now(), taxOwed));
            claim.getEconomyData().setTaxPastDueDate(null);
            claim.getEconomyData().setTaxBalance(0);
            claim.getInternalClaimData().setExpired(false);
            if (inTown) {
                final GPClaim town = claim.getTownClaim();
                town.getData()
                    .getEconomyData()
                    .addBankTransaction(new GPBankTransaction(BankTransactionType.TAX_SUCCESS, Instant.now(), taxOwed));
                town.getEconomyAccount()
                    .get()
                    .deposit(this.economyService.getDefaultCurrency(), BigDecimal.valueOf(taxOwed), Sponge.getCauseStackManager().getCurrentCause());
            }
            claim.getData().save();
        }
    }
}