Refactor denied commands

This commit is contained in:
Olof Larsson 2013-04-23 12:31:07 +02:00
parent 3c60f75bbd
commit ea18f1dd0d
2 changed files with 14 additions and 22 deletions

View File

@ -61,15 +61,12 @@ public class UConf extends Entity<UConf>
public List<String> denyCommandsPermanentFactionMember = new ArrayList<String>(); public List<String> denyCommandsPermanentFactionMember = new ArrayList<String>();
// commands which will be prevented when in claimed territory of another faction // commands which will be prevented when in claimed territory of another faction
public List<String> denyCommandsTerritoryNeutral = new ArrayList<String>(); public Map<Rel, List<String>> denyCommandsTerritoryRelation = MUtil.map(
public List<String> denyCommandsTerritoryEnemy = MUtil.list( Rel.ENEMY, MUtil.list("home", "sethome", "spawn", "tpahere", "tpaccept", "tpa", "warp"),
"home", Rel.NEUTRAL, new ArrayList<String>(),
"sethome", Rel.TRUCE, new ArrayList<String>(),
"spawn", Rel.ALLY, new ArrayList<String>(),
"tpahere", Rel.MEMBER, new ArrayList<String>()
"tpaccept",
"tpa",
"warp"
); );
} }

View File

@ -3,6 +3,7 @@ package com.massivecraft.factions.listeners;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.block.Block; import org.bukkit.block.Block;
@ -351,6 +352,7 @@ public class FactionsListenerMain implements Listener
command = command.toLowerCase(); command = command.toLowerCase();
command = command.trim(); command = command.trim();
// ... the command may be denied for members of permanent factions ...
if (uplayer.hasFaction() && uplayer.getFaction().getFlag(FFlag.PERMANENT) && containsCommand(command, UConf.get(player).denyCommandsPermanentFactionMember)) if (uplayer.hasFaction() && uplayer.getFaction().getFlag(FFlag.PERMANENT) && containsCommand(command, UConf.get(player).denyCommandsPermanentFactionMember))
{ {
uplayer.msg("<b>You can't use \"<h>/%s<b>\" as member of a permanent faction.", command); uplayer.msg("<b>You can't use \"<h>/%s<b>\" as member of a permanent faction.", command);
@ -358,23 +360,16 @@ public class FactionsListenerMain implements Listener
return; return;
} }
// ... the command may be denied in the territory of this relation type ...
Rel rel = uplayer.getRelationToLocation(); Rel rel = uplayer.getRelationToLocation();
PS ps = PS.valueOf(player).getChunk(true); PS ps = PS.valueOf(player).getChunk(true);
if (BoardColls.get().getFactionAt(ps).isNone()) return; if (BoardColls.get().getFactionAt(ps).isNone()) return;
if (rel == Rel.NEUTRAL && containsCommand(command, UConf.get(player).denyCommandsTerritoryNeutral)) List<String> deniedCommands = UConf.get(player).denyCommandsTerritoryRelation.get(rel);
{ if (!containsCommand(command, deniedCommands)) return;
uplayer.msg("<b>You can't use \"<h>/%s<b>\" in neutral territory.", command);
event.setCancelled(true); uplayer.msg("<b>You can't use \"<h>/%s<b>\" in %s territory.", Txt.getNicedEnum(rel), command);
return; event.setCancelled(true);
}
if (rel == Rel.ENEMY && containsCommand(command, UConf.get(player).denyCommandsTerritoryEnemy))
{
uplayer.msg("<b>You can't use \"<h>/%s<b>\" in enemy territory.", command);
event.setCancelled(true);
return;
}
} }
private static boolean containsCommand(String needle, Collection<String> haystack) private static boolean containsCommand(String needle, Collection<String> haystack)