From 699c22f6554ac4cdaf50d9e8b5ea79a1024c8c0b Mon Sep 17 00:00:00 2001 From: Brettflan Date: Thu, 4 Aug 2011 00:07:38 -0500 Subject: [PATCH] New conf.json settings "territoryNeutralDenyCommands" and "territoryEnemyDenyCommands", which can have lists of commands added to them that will be denied while in the territory of a neutral or enemy faction. It is recommended you use /f config to add/remove such commands, for example /f config territoryEnemyDenyCommands spawn would prevent players from using /spawn in enemy territory. You can add the slash to the command string or not, it will work either way. If the used command starts with one of your denied commands, it will be prevented. For example, if you deny "time", both "/time night" and "/time day" will be prevented. However, if you deny "time n", /time night will be prevented but /time day won't. (thanks for some code contribution from Cal27) --- src/com/massivecraft/factions/Conf.java | 4 ++ src/com/massivecraft/factions/FPlayer.java | 18 +++++- src/com/massivecraft/factions/Factions.java | 1 + .../listeners/FactionsPlayerListener.java | 57 +++++++++++++++++++ 4 files changed, 77 insertions(+), 3 deletions(-) diff --git a/src/com/massivecraft/factions/Conf.java b/src/com/massivecraft/factions/Conf.java index 9a95a8be..e6a485b8 100644 --- a/src/com/massivecraft/factions/Conf.java +++ b/src/com/massivecraft/factions/Conf.java @@ -85,6 +85,10 @@ public class Conf { public static int actionDeniedPainAmount = 1; + // commands which will be prevented when in claimed territory of another faction + public static Set territoryNeutralDenyCommands = new HashSet(); + public static Set territoryEnemyDenyCommands = new HashSet(); + public static double territoryShieldFactor = 0.3; public static boolean territoryDenyBuild = true; public static boolean territoryDenyBuildWhenOffline = true; diff --git a/src/com/massivecraft/factions/FPlayer.java b/src/com/massivecraft/factions/FPlayer.java index 49cb74fe..91d106b8 100644 --- a/src/com/massivecraft/factions/FPlayer.java +++ b/src/com/massivecraft/factions/FPlayer.java @@ -329,6 +329,10 @@ public class FPlayer { return this.getFaction().getRelation(fplayer); } + public Relation getRelationToLocation() { + return Board.getFactionAt(new FLocation(this)).getRelation(this); + } + public ChatColor getRelationColor(Faction faction) { return faction.getRelationColor(this); } @@ -417,10 +421,18 @@ public class FPlayer { return idHere > 0 && idHere != this.factionId; } - public boolean isInEnemyTerritory() { - return Board.getFactionAt(new FLocation(this)).getRelation(this) == Relation.ENEMY; + public boolean isInAllyTerritory() { + return Board.getFactionAt(new FLocation(this)).getRelation(this).isAlly(); } - + + public boolean isInNeutralTerritory() { + return Board.getFactionAt(new FLocation(this)).getRelation(this).isNeutral(); + } + + public boolean isInEnemyTerritory() { + return Board.getFactionAt(new FLocation(this)).getRelation(this).isEnemy(); + } + public void sendFactionHereMessage() { Faction factionHere = Board.getFactionAt(new FLocation(this)); String msg = Conf.colorSystem+" ~ "+factionHere.getTag(this); diff --git a/src/com/massivecraft/factions/Factions.java b/src/com/massivecraft/factions/Factions.java index 0423d0c6..5238eeee 100644 --- a/src/com/massivecraft/factions/Factions.java +++ b/src/com/massivecraft/factions/Factions.java @@ -153,6 +153,7 @@ public class Factions extends JavaPlugin { PluginManager pm = this.getServer().getPluginManager(); pm.registerEvent(Event.Type.PLAYER_CHAT, this.playerListener, Event.Priority.Highest, this); pm.registerEvent(Event.Type.PLAYER_CHAT, this.chatEarlyListener, Event.Priority.Lowest, this); + pm.registerEvent(Event.Type.PLAYER_COMMAND_PREPROCESS, this.playerListener, Event.Priority.Normal, this); pm.registerEvent(Event.Type.PLAYER_INTERACT, this.playerListener, Event.Priority.Normal, this); pm.registerEvent(Event.Type.PLAYER_MOVE, this.playerListener, Event.Priority.Normal, this); pm.registerEvent(Event.Type.PLAYER_JOIN, this.playerListener, Event.Priority.Normal, this); diff --git a/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java b/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java index 3c6cb9f8..58fb7497 100644 --- a/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java +++ b/src/com/massivecraft/factions/listeners/FactionsPlayerListener.java @@ -1,6 +1,7 @@ package com.massivecraft.factions.listeners; import java.util.logging.Logger; +import java.util.Iterator; import org.bukkit.ChatColor; import org.bukkit.Location; @@ -12,6 +13,7 @@ import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerBucketEmptyEvent; import org.bukkit.event.player.PlayerBucketFillEvent; import org.bukkit.event.player.PlayerChatEvent; +import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerListener; @@ -27,6 +29,7 @@ import com.massivecraft.factions.FPlayer; import com.massivecraft.factions.Faction; import com.massivecraft.factions.Factions; import com.massivecraft.factions.struct.Role; +import com.massivecraft.factions.struct.Relation; import com.massivecraft.factions.util.TextUtil; @@ -393,4 +396,58 @@ public class FactionsPlayerListener extends PlayerListener{ return; } } + + @Override + public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) { + if (event.isCancelled() || (Conf.territoryNeutralDenyCommands.isEmpty() && Conf.territoryNeutralDenyCommands.isEmpty())) { + return; + } + + FPlayer me = FPlayer.get(event.getPlayer()); + + if (!me.isInOthersTerritory()) { + return; + } + + Relation rel = me.getRelationToLocation(); + if (rel.isAtLeast(Relation.ALLY)) { + return; + } + + String fullCmd = event.getMessage().toLowerCase(); + String shortCmd = fullCmd.substring(1); // Get rid of the slash at the beginning + + if ( + rel.isNeutral() + && !Conf.territoryNeutralDenyCommands.isEmpty() + && !Conf.adminBypassPlayers.contains(me.getName()) + ) { + Iterator iter = Conf.territoryNeutralDenyCommands.iterator(); + String cmdCheck; + while (iter.hasNext()) { + cmdCheck = iter.next().toLowerCase(); + if (fullCmd.startsWith(cmdCheck) || shortCmd.startsWith(cmdCheck)) { + me.sendMessage("You can't use the command \""+fullCmd+"\" in neutral territory."); + event.setCancelled(true); + return; + } + } + } + else if ( + rel.isEnemy() + && !Conf.territoryEnemyDenyCommands.isEmpty() + && !Conf.adminBypassPlayers.contains(me.getName()) + ) { + Iterator iter = Conf.territoryEnemyDenyCommands.iterator(); + String cmdCheck; + while (iter.hasNext()) { + cmdCheck = iter.next().toLowerCase(); + if (fullCmd.startsWith(cmdCheck) || shortCmd.startsWith(cmdCheck)) { + me.sendMessage("You can't use the command \""+fullCmd+"\" in enemy territory."); + event.setCancelled(true); + return; + } + } + } + } }