From b97725dead5db8253cedbf859621bc0604f70ab5 Mon Sep 17 00:00:00 2001 From: LlmDl Date: Mon, 24 Jun 2024 17:51:37 -0500 Subject: [PATCH] Add tempflight allowed areas to config. (#84) Closes #81 Used because using permission nodes would likely require a whole new set of nodes. --- pom.xml | 2 +- .../llmdlio/townyflight/TownyFlightAPI.java | 43 +++++++++++++++++++ .../townyflight/config/ConfigNodes.java | 5 +++ .../llmdlio/townyflight/config/Settings.java | 15 ++++++- .../townyflight/config/TownyFlightConfig.java | 21 +++++++++ 5 files changed, 83 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 113acb8..4963acd 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.gmail.llmdlio TownyFlight - 1.11.0 + 1.12.0 TownyFlight A flight plugin for Towny servers. diff --git a/src/main/java/com/gmail/llmdlio/townyflight/TownyFlightAPI.java b/src/main/java/com/gmail/llmdlio/townyflight/TownyFlightAPI.java index 0e2d86f..1953f1f 100644 --- a/src/main/java/com/gmail/llmdlio/townyflight/TownyFlightAPI.java +++ b/src/main/java/com/gmail/llmdlio/townyflight/TownyFlightAPI.java @@ -59,6 +59,9 @@ public boolean canFly(Player player, boolean silent) { || getForceAllowFlight(player)) return true; + if (hasTempFlight(player) && tempFlightAllowsLocation(player)) + return true; + if (!hasTempFlight(player) && !Permission.has(player, "townyflight.command.tfly", silent)) return false; Resident resident = TownyUniverse.getInstance().getResident(player.getUniqueId()); @@ -76,6 +79,46 @@ public boolean canFly(Player player, boolean silent) { return true; } + /** + * Returns true when a player is at a suitable location, matching the allowed + * areas in config.yml. + * + * @param player Player to test. + * @return true when tempflight is allowed here. + */ + private boolean tempFlightAllowsLocation(Player player) { + Location location = player.getLocation(); + Resident resident = TownyAPI.getInstance().getResident(player); + if (resident == null) + return false; + + if (TownyAPI.getInstance().isWilderness(location)) + return Settings.isAllowedTempFlightArea("wilderness"); + + if (Settings.isAllowedTempFlightArea("alltowns")) + return true; + + Town town = TownyAPI.getInstance().getTown(location); + if (Settings.isAllowedTempFlightArea("owntown") && town.hasResident(resident)) + return true; + + if (Settings.isAllowedTempFlightArea("trustedtowns") && town.getTrustedResidents().contains(resident)) + return true; + + if (!town.hasNation() || !resident.hasTown()) + return false; + + Town residentTown = resident.getTownOrNull(); + if (Settings.isAllowedTempFlightArea("nationtowns") && CombatUtil.isSameNation(town, residentTown)) + return true; + + if (Settings.isAllowedTempFlightArea("alliedtowns") && CombatUtil.isAlly(town, residentTown)) + return true; + + return false; + } + + /** * Returns true if a player is allowed to fly at their current location. Checks * if they are in the wilderness, in their own town and if not, whether they diff --git a/src/main/java/com/gmail/llmdlio/townyflight/config/ConfigNodes.java b/src/main/java/com/gmail/llmdlio/townyflight/config/ConfigNodes.java index f5ca32b..437208e 100644 --- a/src/main/java/com/gmail/llmdlio/townyflight/config/ConfigNodes.java +++ b/src/main/java/com/gmail/llmdlio/townyflight/config/ConfigNodes.java @@ -141,6 +141,11 @@ public enum ConfigNodes { "false", "", "# If set to false, TownyFlight will not prevent combat of flying people."), + OPTIONS_TEMPFLIGHT_ALLOWED_AREAS( + "options.tempflight_allowed_areas", + "owntown,nationtowns", + "", + "# The list of areas which allow tempflight, allowed words: owntown, nationtowns, alliedtowns, alltowns, trustedtowns, wilderness"), OPTIONS_SHOW_PERMISSION( "options.show_Permission_After_No_Permission_Message", "true", diff --git a/src/main/java/com/gmail/llmdlio/townyflight/config/Settings.java b/src/main/java/com/gmail/llmdlio/townyflight/config/Settings.java index d50ee6c..559bdf7 100644 --- a/src/main/java/com/gmail/llmdlio/townyflight/config/Settings.java +++ b/src/main/java/com/gmail/llmdlio/townyflight/config/Settings.java @@ -1,8 +1,8 @@ package com.gmail.llmdlio.townyflight.config; import java.util.HashMap; +import java.util.List; import java.util.Map; - import org.bukkit.ChatColor; import org.bukkit.configuration.ConfigurationSection; @@ -16,6 +16,7 @@ public class Settings { public static Boolean showPermissionInMessage; public static Boolean siegeWarFound; public static int flightDisableTimer; + public static List allowedTempFlightAreas; private static Map lang = new HashMap(); public static boolean loadSettings(TownyFlightConfig _config) { @@ -32,6 +33,7 @@ private static void loadOptions() { disableCombatPrevention = Boolean.valueOf(getOption("disable_Combat_Prevention")); showPermissionInMessage = Boolean.valueOf(getOption("show_Permission_After_No_Permission_Message")); flightDisableTimer = Integer.valueOf(getOption("flight_Disable_Timer")); + allowedTempFlightAreas = allowedTempFlightAreas(); } public static void loadStrings() { @@ -75,11 +77,20 @@ private static String getOption(String string) { private static String getString(String string) { return colour(getConfig("language").getString(string)); } - + private static ConfigurationSection getConfig(String path) { return config.getConfig().getConfigurationSection(path); } + private static String colour(String string) { return ChatColor.translateAlternateColorCodes('&', string); } + + public static List allowedTempFlightAreas() { + return config.getStrArr(ConfigNodes.OPTIONS_TEMPFLIGHT_ALLOWED_AREAS); + } + + public static boolean isAllowedTempFlightArea(String area) { + return allowedTempFlightAreas.contains(area); + } } diff --git a/src/main/java/com/gmail/llmdlio/townyflight/config/TownyFlightConfig.java b/src/main/java/com/gmail/llmdlio/townyflight/config/TownyFlightConfig.java index d5262a3..43a0c8d 100644 --- a/src/main/java/com/gmail/llmdlio/townyflight/config/TownyFlightConfig.java +++ b/src/main/java/com/gmail/llmdlio/townyflight/config/TownyFlightConfig.java @@ -3,6 +3,9 @@ import java.io.File; import java.io.IOException; import java.nio.file.Path; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; import com.gmail.llmdlio.townyflight.TownyFlight; import com.palmergames.bukkit.config.CommentedConfiguration; @@ -79,4 +82,22 @@ private static void setNewProperty(String root, Object value) { value = ""; newConfig.set(root, value.toString()); } + + public static String getString(String root, String def) { + + String data = config.getString(root.toLowerCase(), def); + if (data == null) { + TownyFlight.getPlugin().getLogger().warning(root.toLowerCase() + " from config.yml"); + return ""; + } + return data; + } + + public static String getString(ConfigNodes node) { + return config.getString(node.getRoot().toLowerCase(), node.getDefault()); + } + + public List getStrArr(ConfigNodes node) { + return Arrays.stream(getString(node).split(",")).collect(Collectors.toList()); + } }