Skip to content

Commit

Permalink
Town command blacklisting (#4849)
Browse files Browse the repository at this point in the history
* Trying out eclipse

* Implement perm node for bypassing this

* tweaks and adds limited commands

* adds perm node to plugin.yml

* try to get this to work

* fix maven crying a river because nre was not caught

* why are u crying about the javadocs

* add fixes made by llmdl

* adds checks that were missing

* Change getTownLimitedCommands() to getPlayerOwnedPlotLimdCommands

* remove unused import

* add tests for owntown and townowned nodes

* fix commands being blocked even if they shouldn't

* limited cmds actually work now lol

* requested changes (#1)

* requested changes (#2)

* fix bypass not checking for res township (req. changes)

* remove repeated check left from older commits (req changes)

* mvn exception not caught

* Rebase off master and make a couple polishing changes.

* Cleanup Commit & Requested Changes.

- Requests from Warriorrrr are made.
- Moves town-blocked commands above the personally-owned commands test
to reduce the required returns later on.
- Switches to the PermissionSource hasOwnTown and hasTownOwned
permission tests.

Co-authored-by: Llm Dl <LlmDlio@gmail.com>
  • Loading branch information
ggvbo and LlmDl authored Apr 26, 2021
1 parent 1166182 commit 8b3b72a
Show file tree
Hide file tree
Showing 19 changed files with 184 additions and 1 deletion.
3 changes: 3 additions & 0 deletions resources/chinese.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1374,3 +1374,6 @@ msg_ta_allies_enemies_updated: '&bAllies/Enemies updated for %s.'
towny_map_detailed_information: '&2Click to see detailed plot information.'
msg_err_nation_capital_too_many_residents: '&cThe current capital city has too many residents to be replaced by %s.'
msg_deleting_nation_will_result_in_losing_residents: '&cWarning: Deleting your nation will mean your town is over the population limit of %d, resulting in the removal of %d residents.'

msg_command_blocked_inside_towns: '&cYou cannot use that command inside towns.'
msg_command_limited: 'You can only use that command inside your own plots.'
3 changes: 3 additions & 0 deletions resources/english.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1372,3 +1372,6 @@ msg_ta_allies_enemies_updated: '&bAllies/Enemies updated for %s.'
towny_map_detailed_information: '&2Click to see detailed plot information.'
msg_err_nation_capital_too_many_residents: '&cThe current capital city has too many residents to be replaced by %s.'
msg_deleting_nation_will_result_in_losing_residents: '&cWarning: Deleting your nation will mean your town is over the population limit of %d, resulting in the removal of %d residents.'

msg_command_blocked_inside_towns: '&cYou cannot use that command inside towns.'
msg_command_limited: 'You can only use that command inside your own plots.'
5 changes: 4 additions & 1 deletion resources/es-419.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1391,4 +1391,7 @@ msg_ta_allies_enemies_updated: '&bSe actualizaron los aliados/enemigos de %s.'
# Añadido en la versión 0.102
towny_map_detailed_information: '&2Clic para ver información detallada del terreno.'
msg_err_nation_capital_too_many_residents: '&cThe current capital city has too many residents to be replaced by %s.'
msg_deleting_nation_will_result_in_losing_residents: '&cWarning: Deleting your nation will mean your town is over the population limit of %d, resulting in the removal of %d residents.'
msg_deleting_nation_will_result_in_losing_residents: '&cWarning: Deleting your nation will mean your town is over the population limit of %d, resulting in the removal of %d residents.'

msg_command_blocked_inside_towns: '&cYou cannot use that command inside towns.'
msg_command_limited: 'You can only use that command inside your own plots.'
3 changes: 3 additions & 0 deletions resources/french.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1374,3 +1374,6 @@ msg_ta_allies_enemies_updated: "&bAlliés / Ennemis mis à jour pour %s."
towny_map_detailed_information: '&2Click to see detailed plot information.'
msg_err_nation_capital_too_many_residents: '&cThe current capital city has too many residents to be replaced by %s.'
msg_deleting_nation_will_result_in_losing_residents: '&cWarning: Deleting your nation will mean your town is over the population limit of %d, resulting in the removal of %d residents.'

msg_command_blocked_inside_towns: '&cYou cannot use that command inside towns.'
msg_command_limited: 'You can only use that command inside your own plots.'
3 changes: 3 additions & 0 deletions resources/german.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1376,3 +1376,6 @@ msg_ta_allies_enemies_updated: '&bAllies/Enemies updated for %s.'
towny_map_detailed_information: '&2Click to see detailed plot information.'
msg_err_nation_capital_too_many_residents: '&cThe current capital city has too many residents to be replaced by %s.'
msg_deleting_nation_will_result_in_losing_residents: '&cWarning: Deleting your nation will mean your town is over the population limit of %d, resulting in the removal of %d residents.'

msg_command_blocked_inside_towns: '&cYou cannot use that command inside towns.'
msg_command_limited: 'You can only use that command inside your own plots.'
3 changes: 3 additions & 0 deletions resources/italian.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1375,3 +1375,6 @@ msg_ta_allies_enemies_updated: '&bAllies/Enemies updated for %s.'
towny_map_detailed_information: '&2Click to see detailed plot information.'
msg_err_nation_capital_too_many_residents: '&cThe current capital city has too many residents to be replaced by %s.'
msg_deleting_nation_will_result_in_losing_residents: '&cWarning: Deleting your nation will mean your town is over the population limit of %d, resulting in the removal of %d residents.'

msg_command_blocked_inside_towns: '&cYou cannot use that command inside towns.'
msg_command_limited: 'You can only use that command inside your own plots.'
3 changes: 3 additions & 0 deletions resources/korean.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1286,3 +1286,6 @@ msg_ta_allies_enemies_updated: '&bAllies/Enemies updated for %s.'
towny_map_detailed_information: '&2Click to see detailed plot information.'
msg_err_nation_capital_too_many_residents: '&cThe current capital city has too many residents to be replaced by %s.'
msg_deleting_nation_will_result_in_losing_residents: '&cWarning: Deleting your nation will mean your town is over the population limit of %d, resulting in the removal of %d residents.'

msg_command_blocked_inside_towns: '&cYou cannot use that command inside towns.'
msg_command_limited: 'You can only use that command inside your own plots.'
3 changes: 3 additions & 0 deletions resources/norwegian.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1373,3 +1373,6 @@ msg_ta_allies_enemies_updated: '&bAllies/Enemies updated for %s.'
towny_map_detailed_information: '&2Click to see detailed plot information.'
msg_err_nation_capital_too_many_residents: '&cThe current capital city has too many residents to be replaced by %s.'
msg_deleting_nation_will_result_in_losing_residents: '&cWarning: Deleting your nation will mean your town is over the population limit of %d, resulting in the removal of %d residents.'

msg_command_blocked_inside_towns: '&cYou cannot use that command inside towns.'
msg_command_limited: 'You can only use that command inside your own plots.'
1 change: 1 addition & 0 deletions resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ permissions:
towny.admin.nation_zone: true
towny.admin.outlaw.teleport_bypass: true
towny.admin.spawn: true
towny.admin.town_commands.blacklist_bypass: true

towny.outlaw.jailer:
description: Node given in townyperms.yml to mayors, assistants and sheriffs by default. Required to jail outlaws that are killed in a town.
Expand Down
3 changes: 3 additions & 0 deletions resources/polish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1372,3 +1372,6 @@ msg_ta_allies_enemies_updated: '&bAllies/Enemies updated for %s.'
towny_map_detailed_information: '&2Click to see detailed plot information.'
msg_err_nation_capital_too_many_residents: '&cThe current capital city has too many residents to be replaced by %s.'
msg_deleting_nation_will_result_in_losing_residents: '&cWarning: Deleting your nation will mean your town is over the population limit of %d, resulting in the removal of %d residents.'

msg_command_blocked_inside_towns: '&cYou cannot use that command inside towns.'
msg_command_limited: 'You can only use that command inside your own plots.'
3 changes: 3 additions & 0 deletions resources/pt-br.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1377,3 +1377,6 @@ msg_ta_allies_enemies_updated: '&bAllies/Enemies updated for %s.'
towny_map_detailed_information: '&2Click to see detailed plot information.'
msg_err_nation_capital_too_many_residents: '&cThe current capital city has too many residents to be replaced by %s.'
msg_deleting_nation_will_result_in_losing_residents: '&cWarning: Deleting your nation will mean your town is over the population limit of %d, resulting in the removal of %d residents.'

msg_command_blocked_inside_towns: '&cYou cannot use that command inside towns.'
msg_command_limited: 'You can only use that command inside your own plots.'
3 changes: 3 additions & 0 deletions resources/russian.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1373,3 +1373,6 @@ msg_ta_allies_enemies_updated: '&bAllies/Enemies updated for %s.'
towny_map_detailed_information: '&2Click to see detailed plot information.'
msg_err_nation_capital_too_many_residents: '&cThe current capital city has too many residents to be replaced by %s.'
msg_deleting_nation_will_result_in_losing_residents: '&cWarning: Deleting your nation will mean your town is over the population limit of %d, resulting in the removal of %d residents.'

msg_command_blocked_inside_towns: '&cYou cannot use that command inside towns.'
msg_command_limited: 'You can only use that command inside your own plots.'
3 changes: 3 additions & 0 deletions resources/se-SV.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1371,3 +1371,6 @@ msg_ta_allies_enemies_updated: '&bAllies/Enemies updated for %s.'
towny_map_detailed_information: '&2Click to see detailed plot information.'
msg_err_nation_capital_too_many_residents: '&cThe current capital city has too many residents to be replaced by %s.'
msg_deleting_nation_will_result_in_losing_residents: '&cWarning: Deleting your nation will mean your town is over the population limit of %d, resulting in the removal of %d residents.'

msg_command_blocked_inside_towns: '&cYou cannot use that command inside towns.'
msg_command_limited: 'You can only use that command inside your own plots.'
3 changes: 3 additions & 0 deletions resources/spanish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1374,3 +1374,6 @@ msg_ta_allies_enemies_updated: '&bAllies/Enemies updated for %s.'
towny_map_detailed_information: '&2Click to see detailed plot information.'
msg_err_nation_capital_too_many_residents: '&cThe current capital city has too many residents to be replaced by %s.'
msg_deleting_nation_will_result_in_losing_residents: '&cWarning: Deleting your nation will mean your town is over the population limit of %d, resulting in the removal of %d residents.'

msg_command_blocked_inside_towns: '&cYou cannot use that command inside towns.'
msg_command_limited: 'You can only use that command inside your own plots.'
3 changes: 3 additions & 0 deletions resources/zh-TW.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1372,3 +1372,6 @@ msg_ta_allies_enemies_updated: '&bAllies/Enemies updated for %s.'
towny_map_detailed_information: '&2Click to see detailed plot information.'
msg_err_nation_capital_too_many_residents: '&cThe current capital city has too many residents to be replaced by %s.'
msg_deleting_nation_will_result_in_losing_residents: '&cWarning: Deleting your nation will mean your town is over the population limit of %d, resulting in the removal of %d residents.'

msg_command_blocked_inside_towns: '&cYou cannot use that command inside towns.'
msg_command_limited: 'You can only use that command inside your own plots.'
34 changes: 34 additions & 0 deletions src/com/palmergames/bukkit/config/ConfigNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,40 @@ public enum ConfigNodes {
"# For example, to give a `visemayor` preference over an `assistant`, change this parameter to `visemayor,assistant`."
),

GTOWN_SETTINGS_COMMAND_BLACKLISTING(
"global_town_settings.town_command_blacklisting",
"",
"",
"# Allows blocking commands inside towns and limiting them to plots owned by the players only.",
"# Useful for limiting sethome/home commands to plots owned by the players themselves and not someone else.",
"# Admins and players with the towny.admin.town_commands.blacklist_bypass permission node will not be hindered."
),
GTOWN_SETTINGS_ENABLE_COMMAND_BLACKLISTING(
"global_town_settings.town_command_blacklisting.enabled",
"false",
"",
"# Allows blocking commands inside towns through the town_blacklisted_commands setting.",
"# This boolean allows you to disable this feature altogether if you don't need it"
),

GTOWN_TOWN_BLACKLISTED_COMMANDS(
"global_town_settings.town_command_blacklisting.town_blacklisted_commands",
"somecommandhere,othercommandhere",
"",
"# Comma separated list of commands which cannot be run inside of any town."
),

GTOWN_TOWN_LIMITED_COMMANDS(
"global_town_settings.town_command_blacklisting.player_owned_plot_limited_commands",
"sethome,home",
"",
"# This allows the usage of blacklisted commands only in plots personally-owned by the player.",
"# Players with the towny.claimed.townowned.* permission node will be able to run these commands",
"# inside of town-owned land. This would include mayors, assistants and possibly a builder rank.",
"# Players with the towny.claimed.owntown.* permission node (given to mayors/assistants usually,)",
"# will also not be limited by this command blacklist."
),

GNATION_SETTINGS(
"global_nation_settings",
"",
Expand Down
12 changes: 12 additions & 0 deletions src/com/palmergames/bukkit/towny/TownySettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -3048,5 +3048,17 @@ public static boolean doesFrostWalkerRequireBuildPerms() {
public static boolean isNotificationsAppearingOnBossbar() {
return getBoolean(ConfigNodes.NOTIFICATION_NOTIFICATIONS_APPEAR_ON_BOSSBAR);
}

public static boolean allowTownCommandBlacklisting() {
return getBoolean(ConfigNodes.GTOWN_SETTINGS_ENABLE_COMMAND_BLACKLISTING);
}

public static List<String> getTownBlacklistedCommands() {
return getStrArr(ConfigNodes.GTOWN_TOWN_BLACKLISTED_COMMANDS);
}

public static List<String> getPlayerOwnedPlotLimitedCommands() {
return getStrArr(ConfigNodes.GTOWN_TOWN_LIMITED_COMMANDS);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,98 @@ public void onJailedPlayerUsesCommand(PlayerCommandPreprocessEvent event) {
}
}

/**
* Allows restricting commands while being in a town.
* Also allows limiting commands to personally-owned plots only.
* Works almost the same way as jail command blacklisting, except has more stuff
* @param event PlayerCommandPreprocessEvent
*/
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onPlayerUsesCommandInsideTown(PlayerCommandPreprocessEvent event) {
if (plugin.isError()
|| !TownySettings.allowTownCommandBlacklisting()
|| !TownyAPI.getInstance().isTownyWorld(event.getPlayer().getWorld()))
return;

Player player = event.getPlayer();
Resident res = TownyUniverse.getInstance().getResident(player.getUniqueId());
if (res == null)
return;

String command = event.getMessage().substring(1).split(" ")[0];

/*
* Commands are sometimes blocked from being run inside any town.
*/
if (TownySettings.getTownBlacklistedCommands().contains(command) && !TownyAPI.getInstance().isWilderness(player.getLocation())) {
// Let admins run commands.
if (TownyUniverse.getInstance().getPermissionSource().has(player, PermissionNodes.TOWNY_ADMIN_TOWN_COMMAND_BLACKLIST_BYPASS.getNode()))
return;

TownyMessaging.sendErrorMsg(player, Translation.of("msg_command_blocked_inside_towns"));
event.setCancelled(true);
return;
}

/*
* Commands are sometimes limited to only plots that players personally own.
*/
if (TownySettings.getPlayerOwnedPlotLimitedCommands().contains(command)) {
// Let admins run commands.
if (TownyUniverse.getInstance().getPermissionSource().has(player, PermissionNodes.TOWNY_ADMIN_TOWN_COMMAND_BLACKLIST_BYPASS.getNode()))
return;

// Stop the command being run because this is in the wilderness.
if (TownyAPI.getInstance().isWilderness(player.getLocation())) {
TownyMessaging.sendErrorMsg(player, Translation.of("msg_command_limited"));
event.setCancelled(true);
return;
}

TownBlock tb = TownyAPI.getInstance().getTownBlock(player.getLocation());
Town town = TownyAPI.getInstance().getTown(player.getLocation());
if (tb == null || town == null)
return;

// If the player is in their own town and has towny.claimed.owntown.build.dirt
// (or more likely towny.claimed.owntown.*) then allow them to use the command.
// It is likely a mayor/assistant.
if (town.hasResident(res) && TownyUniverse.getInstance().getPermissionSource().hasOwnTownOverride(player, Material.DIRT, ActionType.BUILD))
return;

if (tb.hasResident()) {
// This is a personally-owned plot, let's make sure the player is the plot owner.

Resident owner = null;
try {
owner = tb.getResident();
} catch(NotRegisteredException nre) { return; }

// The owner and player are not the same, cancel the command.
if (!owner.getName().equals(player.getName())) {
TownyMessaging.sendErrorMsg(player, Translation.of("msg_command_limited"));
event.setCancelled(true);
}
// Player owns this plot personally, we won't limit their command-usage.
} else {
// This is not a personally-owned plot, but let's only block this command if the player isn't special to the town.

// If the player is in their own town and has towny.claimed.townowned.build.dirt
// (or more likely towny.claimed.townowned.*) then allow them to use the command.
// It is likely a assistant or town-ranked player.
if (town.hasResident(res) && TownyUniverse.getInstance().getPermissionSource().hasTownOwnedOverride(player, Material.DIRT, ActionType.BUILD)) {
return;

// Not a special person, and not in a personally-owned plot, cancel this command.
} else {
TownyMessaging.sendErrorMsg(player, Translation.of("msg_command_limited"));
event.setCancelled(true);
}
}
}

}

/*
* Handles AdminTool use on Blocks
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public enum PermissionNodes {
TOWNY_ADMIN("towny.admin"),
TOWNY_ADMIN_NATION_ZONE("towny.admin.nation_zone"),
TOWNY_ADMIN_OUTLAW_TELEPORT_BYPASS("towny.admin.outlaw.teleport_bypass"),
TOWNY_ADMIN_TOWN_COMMAND_BLACKLIST_BYPASS("towny.admin.town_commands.blacklist_bypass"),

/*
* Nation command permissions
*/
Expand Down

0 comments on commit 8b3b72a

Please sign in to comment.