From 7ed7ecea8b443652b2da3d4e23291848b081bde1 Mon Sep 17 00:00:00 2001 From: Timo McGrath <69724732+tlm9201@users.noreply.github.com> Date: Mon, 8 Apr 2024 14:57:06 -0400 Subject: [PATCH] Added issue #894 - config enable/disable outpost teleportation during battle sessions & siege (#902) * added outpost spawn prevention configurability when a nation has an active offensive siege during a battle session * fixed imports * removed redundant exception * fixed mismatched root/confignodes key * Update src/main/java/com/gmail/goosius/siegewar/listeners/SiegeWarTownEventListener.java added nullcheck Co-authored-by: LlmDl * prevented re-cancelling townspawnevent --------- Co-authored-by: LlmDl --- .../listeners/SiegeWarTownEventListener.java | 39 +++++++++++++++---- .../siegewar/settings/ConfigNodes.java | 5 +++ .../siegewar/settings/SiegeWarSettings.java | 4 ++ src/main/resources/lang/en-US.yml | 2 + 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/gmail/goosius/siegewar/listeners/SiegeWarTownEventListener.java b/src/main/java/com/gmail/goosius/siegewar/listeners/SiegeWarTownEventListener.java index 329170522..cffaf9ef3 100644 --- a/src/main/java/com/gmail/goosius/siegewar/listeners/SiegeWarTownEventListener.java +++ b/src/main/java/com/gmail/goosius/siegewar/listeners/SiegeWarTownEventListener.java @@ -3,6 +3,7 @@ import com.gmail.goosius.siegewar.SiegeController; import com.gmail.goosius.siegewar.SiegeWar; import com.gmail.goosius.siegewar.enums.SiegeWarPermissionNodes; +import com.gmail.goosius.siegewar.objects.BattleSession; import com.gmail.goosius.siegewar.utils.PermissionUtil; import com.gmail.goosius.siegewar.TownOccupationController; import com.gmail.goosius.siegewar.metadata.TownMetaDataController; @@ -24,10 +25,13 @@ import com.palmergames.bukkit.towny.event.town.TownRuinedEvent; import com.palmergames.bukkit.towny.event.town.TownPreSetHomeBlockEvent; import com.palmergames.bukkit.towny.event.town.toggle.TownToggleNeutralEvent; +import com.palmergames.bukkit.towny.object.Nation; +import com.palmergames.bukkit.towny.object.TownBlock; import com.palmergames.bukkit.towny.object.Town; import com.palmergames.bukkit.towny.object.Translatable; import com.palmergames.bukkit.towny.object.Translation; import com.palmergames.bukkit.towny.object.Translator; +import com.palmergames.bukkit.towny.object.WorldCoord; import com.palmergames.util.TimeMgmt; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -268,22 +272,41 @@ public void onTownRankGivenToPlayer(TownAddResidentRankEvent event) { /** * If this is a peaceful town, UN-CANCEL the town spawn event - * + * If the town's nation has an active offensive siege and a battle session is active, CANCEL town spawn event to outposts + * * Lowest priority so that more important features like the siegezone-tp-block can can take precedence * * @param event town spawn event */ @EventHandler(priority = EventPriority.LOWEST) public void on(TownSpawnEvent event) { - if(SiegeWarSettings.getWarSiegeEnabled() - && SiegeWarSettings.getWarCommonPeacefulTownsEnabled() + if(SiegeWarSettings.getWarSiegeEnabled()) { + Town toTown = event.getToTown(); + + if (SiegeWarSettings.getWarCommonPeacefulTownsEnabled() && SiegeWarSettings.isPeacefulTownPublicSpawnEnabled()) { - if (event.isCancelled() - && SiegeWarTownPeacefulnessUtil.isTownPeaceful(event.getToTown()) - && event.getToTown().isPublic()) { - event.setCancelled(false); //UN-Cancel the event + if (event.isCancelled() + && SiegeWarTownPeacefulnessUtil.isTownPeaceful(toTown) + && toTown.isPublic()) { + event.setCancelled(false); //UN-Cancel the event + return; //Avoid re-cancelling event + } + } + + Nation nation = toTown.getNationOrNull(); + TownBlock tb = WorldCoord.parseWorldCoord(event.getTo()).getTownBlockOrNull(); + if (nation == null || tb == null) + return; + //If enabled in config, prevent teleportation to outposts during offensive siege while battle session is active. + if (!SiegeController.getActiveOffensiveSieges(nation).isEmpty() + && tb.isOutpost() + && SiegeWarSettings.getWarSiegeOutpostTeleportationDisabled() && BattleSession.getBattleSession().isActive()) { + Translator translator = Translator.locale(event.getPlayer()); + + event.setCancelMessage(translator.of("siegewar_plugin_prefix") + translator.of("msg_err_cannot_spawn_outpost_during_battle_session")); + event.setCancelled(true); //Stop the teleport + } } } - } diff --git a/src/main/java/com/gmail/goosius/siegewar/settings/ConfigNodes.java b/src/main/java/com/gmail/goosius/siegewar/settings/ConfigNodes.java index ed8538002..e58af88cb 100644 --- a/src/main/java/com/gmail/goosius/siegewar/settings/ConfigNodes.java +++ b/src/main/java/com/gmail/goosius/siegewar/settings/ConfigNodes.java @@ -91,6 +91,11 @@ public enum ConfigNodes { "", "# If this setting is true, then during Battle Sessions, only town residents are permitted to spawn into Siege-Zones OR besieged towns.", "# This setting is recommended to protect players from accidentally spawning into a PVP-active-area while unprepared."), + WAR_SIEGE_OUTPOST_TELEPORTATION_DISABLED( + "war.siege.switches.outpost_teleportation_disabled", + "false", + "", + "# If true, then during Battle Sessions, teleportation to outposts for towns engaged in a siege is disabled."), WAR_SIEGE_BESIEGED_TOWN_RECRUITMENT_DISABLED( "war.siege.switches.besieged_town_recruitment_disabled", "true", diff --git a/src/main/java/com/gmail/goosius/siegewar/settings/SiegeWarSettings.java b/src/main/java/com/gmail/goosius/siegewar/settings/SiegeWarSettings.java index fc6750034..b1c3fc3e5 100644 --- a/src/main/java/com/gmail/goosius/siegewar/settings/SiegeWarSettings.java +++ b/src/main/java/com/gmail/goosius/siegewar/settings/SiegeWarSettings.java @@ -131,6 +131,10 @@ public static boolean getWarSiegeNonResidentSpawnIntoSiegeZonesOrBesiegedTownsDi return Settings.getBoolean(ConfigNodes.WAR_SIEGE_NON_RESIDENT_SPAWN_INTO_SIEGE_ZONES_OR_BESIEGED_TOWNS_DISABLED); } + public static boolean getWarSiegeOutpostTeleportationDisabled() { + return Settings.getBoolean(ConfigNodes.WAR_SIEGE_OUTPOST_TELEPORTATION_DISABLED); + } + public static int getWarSiegeMaxActiveSiegeAttacksPerNation() { return Settings.getInt(ConfigNodes.WAR_SIEGE_MAX_ACTIVE_SIEGE_ATTACKS_PER_NATION); } diff --git a/src/main/resources/lang/en-US.yml b/src/main/resources/lang/en-US.yml index a53062a29..90fa4a312 100644 --- a/src/main/resources/lang/en-US.yml +++ b/src/main/resources/lang/en-US.yml @@ -652,6 +652,8 @@ msg_err_cannot_spawn_battle_commander_offline: "&cYou cannot spawn to the siege msg_err_cannot_spawn_battle_commander_dead: "&cYou cannot spawn to the siege because your team's battle commander is dead." msg_err_cannot_spawn_battle_commander_not_in_siegezone: "&cYou cannot spawn to the siege because your team's battle commander is not in the Siege-Zone." msg_err_cannot_spawn_not_in_homeblock: "&cYou must be in your town homeblock to spawn to the siege." +msg_err_cannot_spawn_outpost_during_battle_session: "&cYou cannot spawn to an outpost while your nation has an offensive siege during a battle session." + #Added in 2.8.0 msg_swa_remove_siege: '&bSiege of %s removed via admin commands.'