From 7ab0d700cc70c54ed0b434e52ebbab926e96de21 Mon Sep 17 00:00:00 2001 From: Brettflan Date: Fri, 10 Jun 2011 14:14:02 -0500 Subject: [PATCH] Added new "territoryDenyBuildWhenOffline" option to go with other two new offline prevention options by thomastanck; slight speed improvement for offline check --- src/org/mcteam/factions/Conf.java | 1 + src/org/mcteam/factions/Faction.java | 29 +++++++++++++++++++ .../listeners/FactionsBlockListener.java | 9 ++++-- .../listeners/FactionsEntityListener.java | 9 ++++-- .../listeners/FactionsPlayerListener.java | 7 ++--- 5 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/org/mcteam/factions/Conf.java b/src/org/mcteam/factions/Conf.java index ef1a69f6..ba0965a7 100644 --- a/src/org/mcteam/factions/Conf.java +++ b/src/org/mcteam/factions/Conf.java @@ -65,6 +65,7 @@ public class Conf { public static double territoryShieldFactor = 0.3; public static boolean territoryDenyBuild = true; + public static boolean territoryDenyBuildWhenOffline = true; public static boolean territoryDenyUseage = true; public static boolean territoryBlockCreepers = false; public static boolean territoryBlockFireballs = false; diff --git a/src/org/mcteam/factions/Faction.java b/src/org/mcteam/factions/Faction.java index 03d6a7b1..304b70dd 100644 --- a/src/org/mcteam/factions/Faction.java +++ b/src/org/mcteam/factions/Faction.java @@ -213,26 +213,36 @@ public class Faction { public ArrayList getFPlayers() { ArrayList ret = new ArrayList(); + if (id <= 0) + return ret; + for (FPlayer fplayer : FPlayer.getAll()) { if (fplayer.getFaction() == this) { ret.add(fplayer); } } + return ret; } public ArrayList getFPlayersWhereOnline(boolean online) { ArrayList ret = new ArrayList(); + if (id <= 0) + return ret; + for (FPlayer fplayer : FPlayer.getAll()) { if (fplayer.getFaction() == this && fplayer.isOnline() == online) { ret.add(fplayer); } } + return ret; } public ArrayList getFPlayersWhereRole(Role role) { ArrayList ret = new ArrayList(); + if (id <= 0) + return ret; for (FPlayer fplayer : FPlayer.getAll()) { if (fplayer.getFaction() == this && fplayer.getRole() == role) { @@ -245,15 +255,34 @@ public class Faction { public ArrayList getOnlinePlayers() { ArrayList ret = new ArrayList(); + if (id <= 0) + return ret; + for (Player player: Factions.instance.getServer().getOnlinePlayers()) { FPlayer fplayer = FPlayer.get(player); if (fplayer.getFaction() == this) { ret.add(player); } } + return ret; } + // slightly faster check than getOnlinePlayers() if you just want to see if there are any players online + public boolean HasPlayersOnline() { + // only real factions can have players online, not wilderness / safe zone / war zone + if (id <= 0) + return false; + + for (Player player: Factions.instance.getServer().getOnlinePlayers()) { + FPlayer fplayer = FPlayer.get(player); + if (fplayer.getFaction() == this) { + return true; + } + } + return false; + } + //----------------------------------------------// // Faction tag //----------------------------------------------// diff --git a/src/org/mcteam/factions/listeners/FactionsBlockListener.java b/src/org/mcteam/factions/listeners/FactionsBlockListener.java index 82ce4643..07dd24e8 100644 --- a/src/org/mcteam/factions/listeners/FactionsBlockListener.java +++ b/src/org/mcteam/factions/listeners/FactionsBlockListener.java @@ -88,9 +88,12 @@ public class FactionsBlockListener extends BlockListener { Faction myFaction = me.getFaction(); // Cancel if we are not in our own territory - if (myFaction != otherFaction && Conf.territoryDenyBuild) { - me.sendMessage("You can't "+action+" in the territory of "+otherFaction.getTag(myFaction)); - return false; + if (myFaction != otherFaction) { + boolean online = otherFaction.HasPlayersOnline(); + if ((online && Conf.territoryDenyBuild) || (!online && Conf.territoryDenyBuildWhenOffline)) { + me.sendMessage("You can't "+action+" in the territory of "+otherFaction.getTag(myFaction)); + return false; + } } return true; diff --git a/src/org/mcteam/factions/listeners/FactionsEntityListener.java b/src/org/mcteam/factions/listeners/FactionsEntityListener.java index 6fff82b6..81a5ff4a 100644 --- a/src/org/mcteam/factions/listeners/FactionsEntityListener.java +++ b/src/org/mcteam/factions/listeners/FactionsEntityListener.java @@ -303,9 +303,12 @@ public class FactionsEntityListener extends EntityListener { Faction myFaction = me.getFaction(); // Cancel if we are not in our own territory - if (myFaction != otherFaction && Conf.territoryDenyBuild) { - me.sendMessage("You can't "+action+" paintings in the territory of "+otherFaction.getTag(myFaction)); - return false; + if (myFaction != otherFaction) { + boolean online = otherFaction.HasPlayersOnline(); + if ((online && Conf.territoryDenyBuild) || (!online && Conf.territoryDenyBuildWhenOffline)) { + me.sendMessage("You can't "+action+" paintings in the territory of "+otherFaction.getTag(myFaction)); + return false; + } } return true; diff --git a/src/org/mcteam/factions/listeners/FactionsPlayerListener.java b/src/org/mcteam/factions/listeners/FactionsPlayerListener.java index 50910bfa..6d8b7bc3 100644 --- a/src/org/mcteam/factions/listeners/FactionsPlayerListener.java +++ b/src/org/mcteam/factions/listeners/FactionsPlayerListener.java @@ -204,7 +204,7 @@ public class FactionsPlayerListener extends PlayerListener{ Faction otherFaction = Board.getFactionAt(new FLocation(block)); - if (otherFaction.getOnlinePlayers()!=null){ + if (otherFaction.HasPlayersOnline()){ if ( ! Conf.territoryDenyUseageMaterials.contains(material)) { return true; // Item isn't one we're preventing for online factions. } @@ -261,7 +261,7 @@ public class FactionsPlayerListener extends PlayerListener{ Faction otherFaction = Board.getFactionAt(new FLocation(block)); // We only care about some material types. - if (otherFaction.getOnlinePlayers()!=null){ + if (otherFaction.HasPlayersOnline()){ if ( ! Conf.territoryProtectedMaterials.contains(material)) { return true; } @@ -274,13 +274,12 @@ public class FactionsPlayerListener extends PlayerListener{ FPlayer me = FPlayer.get(player); Faction myFaction = me.getFaction(); - // In safe zones you may use any block... + // You may use any block unless it is another faction's territory... if (otherFaction.isNormal() && myFaction != otherFaction) { me.sendMessage("You can't use "+TextUtil.getMaterialName(material)+" in the territory of "+otherFaction.getTag(myFaction)); return false; } - // You may use doors in both safeZone and wilderness return true; }