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

The following examples show how to use org.spongepowered.api.service.economy.transaction.ResultType#ACCOUNT_NO_FUNDS . 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: 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 2
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 3
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 4
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 5
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 6
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();
}