Skip to content

Commit

Permalink
Add tempflight allowed areas to config. (#84)
Browse files Browse the repository at this point in the history
Closes #81

Used because using permission nodes would likely require a whole new set
of nodes.
  • Loading branch information
LlmDl authored Jun 24, 2024
1 parent fa2692c commit b97725d
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.gmail.llmdlio</groupId>
<artifactId>TownyFlight</artifactId>
<version>1.11.0</version>
<version>1.12.0</version>
<name>TownyFlight</name>
<description>A flight plugin for Towny servers.</description>
<properties>
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/com/gmail/llmdlio/townyflight/TownyFlightAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/com/gmail/llmdlio/townyflight/config/Settings.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -16,6 +16,7 @@ public class Settings {
public static Boolean showPermissionInMessage;
public static Boolean siegeWarFound;
public static int flightDisableTimer;
public static List<String> allowedTempFlightAreas;
private static Map<String, String> lang = new HashMap<String,String>();

public static boolean loadSettings(TownyFlightConfig _config) {
Expand All @@ -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() {
Expand Down Expand Up @@ -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<String> allowedTempFlightAreas() {
return config.getStrArr(ConfigNodes.OPTIONS_TEMPFLIGHT_ALLOWED_AREAS);
}

public static boolean isAllowedTempFlightArea(String area) {
return allowedTempFlightAreas.contains(area);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> getStrArr(ConfigNodes node) {
return Arrays.stream(getString(node).split(",")).collect(Collectors.toList());
}
}

0 comments on commit b97725d

Please sign in to comment.