Skip to content

Commit

Permalink
Command Addon API for /town buy (#7177)
Browse files Browse the repository at this point in the history
* Add TOWN_BUY to CommandType enum

* Fix a small typo in thrown exception

* Add HelpMenu for /town buy

* Add wildcard buy permission and bonus buy permission

* Declare the permissions in plugin.yml

* Implement the changes to TownCommand to allow for /t buy commands

* Remove title and command from /t buy bonus page

* Change /town buy permission checks

* Add legacy node

Similar to how we handled the list permission changing.

---------

Co-authored-by: LlmDl <LlmDlio@gmail.com>
  • Loading branch information
lexiccn and LlmDl authored Jan 9, 2024
1 parent 1bc2d6a commit 7510c70
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public enum CommandType {
NATION_SET,
NATION_TOGGLE,
TOWN,
TOWN_BUY,
TOWN_SET,
TOWN_TOGGLE,
PLOT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,14 @@ protected MenuBuilder load() {
}
},

TOWN_BUY {
@Override
protected MenuBuilder load() {
return new MenuBuilder("town buy")
.add("bonus [n]", Translatable.of("town_buy_help"));
}
},

RESIDENT_HELP {
@Override
protected MenuBuilder load() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ public List<String> onTabComplete(CommandSender sender, Command command, String
return townInviteTabComplete(sender, args, player, town);
case "buy":
if (args.length == 2)
return NameUtil.filterByStart(Collections.singletonList("bonus"), args[1]);
return NameUtil.filterByStart(TownyCommandAddonAPI.getTabCompletes(CommandType.TOWN_BUY, Collections.singletonList("bonus")), args[1]);
break;
case "toggle":
return switch (args.length) {
Expand Down Expand Up @@ -2440,32 +2440,52 @@ public static void townBuy(CommandSender sender, String[] split, @Nullable Town
if (!TownyEconomyHandler.isActive())
throw new TownyException(Translatable.of("msg_err_no_economy"));

if (split.length == 0 || split[0].equalsIgnoreCase("?") || split[0].equalsIgnoreCase("help")) {
HelpMenu.TOWN_BUY.send(sender);
return;
}

if (town == null && sender instanceof Player player) {
checkPermOrThrow(player, PermissionNodes.TOWNY_COMMAND_TOWN_BUY.getNode());
catchRuinedTown(player);
town = getTownFromPlayerOrThrow(player);
}

switch(split[0].toLowerCase(Locale.ROOT)) {
case "bonus" -> townBuyBonus(town, split, sender);
default -> {
if (TownyCommandAddonAPI.hasCommand(CommandType.TOWN_BUY, split[0])) {
TownyCommandAddonAPI.getAddonCommand(CommandType.TOWN_BUY, split[0]).execute(sender, "town", split);
}
}
}
}

/**
* Town tries to buy bonus blocks or checks the cost and increase.
*
* @param town - Towm object.
* @param split - List of command arguments.
* @param sender - Player.
* @throws TownyException - Exception.
*/
public static void townBuyBonus(Town town, String[] split, CommandSender sender) throws TownyException {
checkPermOrThrow(sender, PermissionNodes.TOWNY_COMMAND_TOWN_BUY_BONUS.getNode());

if (!TownySettings.isSellingBonusBlocks(town) && !TownySettings.isBonusBlocksPerTownLevel())
throw new TownyException("Config.yml has bonus blocks diabled at max_purchased_blocks: '0' ");
throw new TownyException("Config.yml has bonus blocks disabled at max_purchased_blocks: '0' ");
else if (TownySettings.isBonusBlocksPerTownLevel() && TownySettings.getMaxBonusBlocks(town) == 0)
throw new TownyException("Config.yml has bonus blocks disabled at town_level section: townBlockBonusBuyAmount: 0");

if (split.length == 0 || !split[0].equalsIgnoreCase("bonus")) {
TownyMessaging.sendMessage(sender, ChatTools.formatTitle("/town buy"));

if (split.length < 2) {
String line = Colors.Yellow + "[Purchased Bonus] " + Colors.Green + "Cost: " + Colors.LightGreen + "%s" + Colors.Gray + " | " + Colors.Green + "Max: " + Colors.LightGreen + "%d";
TownyMessaging.sendMessage(sender, String.format(line, prettyMoney(town.getBonusBlockCost()), TownySettings.getMaxPurchasedBlocks(town)));
if (TownySettings.getPurchasedBonusBlocksIncreaseValue() != 1.0)
TownyMessaging.sendMessage(sender, Colors.Green + "Cost Increase per TownBlock: " + Colors.LightGreen + "+" + new DecimalFormat("##.##%").format(TownySettings.getPurchasedBonusBlocksIncreaseValue()-1));
TownyMessaging.sendMessage(sender, ChatTools.formatCommand("", "/town buy", "bonus [n]", ""));
return;
}

// They have used `/t buy bonus`, check that they have specified an amount to purchase.
if (split.length == 2)
townBuyBonusTownBlocks(town, MathUtil.getIntOrThrow(split[1].trim()), sender);
else
throw new TownyException(Translatable.of("msg_must_specify_amnt", "/town buy bonus"));

townBuyBonusTownBlocks(town, MathUtil.getIntOrThrow(split[1].trim()), sender);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ public enum PermissionNodes {
TOWNY_COMMAND_TOWN_PLOTS("towny.command.town.plots"),
TOWNY_COMMAND_TOWN_PURGE("towny.command.town.purge"),
TOWNY_COMMAND_TOWN_MERGE("towny.command.town.merge"),
TOWNY_COMMAND_TOWN_BUY("towny.command.town.buy"),
TOWNY_COMMAND_TOWN_BUY("towny.command.town.buy.*"),
TOWNY_COMMAND_TOWN_BUY_BONUS("towny.command.town.buy.bonus"),
TOWNY_COMMAND_TOWN_JAIL("towny.command.town.jail"),
TOWNY_COMMAND_TOWN_JAIL_LIST("towny.command.town.jail.list"),
TOWNY_COMMAND_TOWN_UNJAIL("towny.command.town.unjail"),
Expand Down
2 changes: 2 additions & 0 deletions Towny/src/main/resources/lang/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ town_towntrust_help_0: "Adds a town as Trusted in the town."
town_towntrust_help_1: "Removes a town as Trusted in the town."
town_towntrust_help_2: "Lists the Trusted towns of a town."

town_buy_help: "Purchases bonus townblocks to increase the claim limit of the town."

mayor_help_3: 'Claim area not attached to town'
mayor_help_4: 'Claim around you to a radius of X'
mayor_help_5: 'Claim to the maximum radius'
Expand Down
13 changes: 12 additions & 1 deletion Towny/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ permissions:
default: false
children:
towny.command.town.bankhistory: true
towny.command.town.buy: true
towny.command.town.buy.*: true
towny.command.town.claim.*: true
towny.command.town.delete: true
towny.command.town.deposit: true
Expand Down Expand Up @@ -517,6 +517,17 @@ permissions:
towny.command.town.deposit.othertown:
description: User can deposit money into another town bank using /t deposit amount townname.
default: false
towny.command.town.buy:
description: Users are able to use the /t buy bonus command, for legacy townyperms file support. Replacing this node with towny.command.town.buy.bonus is recommended.
default: false
children:
towny.command.town.buy.bonus: true

towny.command.town.buy.*:
description: User can access all commands related to buying for the town.
default: false
children:
towny.command.town.buy.bonus: true

# Plot command permissions
towny.command.plot.*:
Expand Down

0 comments on commit 7510c70

Please sign in to comment.