2011-10-23 17:30:41 +02:00
|
|
|
package com.massivecraft.factions.struct;
|
|
|
|
|
2011-10-23 23:17:02 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2011-10-24 01:37:51 +02:00
|
|
|
import org.bukkit.Location;
|
|
|
|
|
|
|
|
import com.massivecraft.factions.Board;
|
2011-10-23 23:17:02 +02:00
|
|
|
import com.massivecraft.factions.Conf;
|
2011-10-24 01:37:51 +02:00
|
|
|
import com.massivecraft.factions.FLocation;
|
|
|
|
import com.massivecraft.factions.FPlayer;
|
|
|
|
import com.massivecraft.factions.Faction;
|
|
|
|
import com.massivecraft.factions.iface.RelationParticipator;
|
|
|
|
import com.massivecraft.factions.util.RelationUtil;
|
2011-10-23 23:17:02 +02:00
|
|
|
|
2011-10-23 17:30:41 +02:00
|
|
|
/**
|
|
|
|
* Permissions that you (a player) may or may not have in the territory of a certain faction.
|
2011-10-23 20:50:49 +02:00
|
|
|
* Each faction have many Rel's assigned to each one of these Perms.
|
2011-10-23 17:30:41 +02:00
|
|
|
*/
|
2011-10-24 01:37:51 +02:00
|
|
|
public enum FPerm
|
2011-10-23 17:30:41 +02:00
|
|
|
{
|
2011-10-24 11:56:41 +02:00
|
|
|
BUILD("build", "edit the terrain", Rel.LEADER, Rel.OFFICER, Rel.MEMBER),
|
2011-10-23 23:17:02 +02:00
|
|
|
PAINBUILD("painbuild", "edit but take damage", Rel.ALLY),
|
2011-10-24 11:56:41 +02:00
|
|
|
DOOR("door", "use doors", Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.ALLY),
|
|
|
|
CONTAINER("container", "use containers", Rel.LEADER, Rel.OFFICER, Rel.MEMBER),
|
|
|
|
BUTTON("button", "use stone buttons", Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.ALLY),
|
|
|
|
LEVER("lever", "use levers", Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.ALLY),
|
|
|
|
WITHDRAW("withdraw", "withdraw faction money", Rel.LEADER, Rel.OFFICER),
|
2011-10-23 23:17:02 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
private final String nicename;
|
|
|
|
private final String desc;
|
|
|
|
public final Set<Rel> defaultDefaultValue;
|
|
|
|
|
2011-10-24 01:37:51 +02:00
|
|
|
private FPerm(final String nicename, final String desc, final Rel... rels)
|
2011-10-23 23:17:02 +02:00
|
|
|
{
|
|
|
|
this.nicename = nicename;
|
|
|
|
this.desc = desc;
|
|
|
|
this.defaultDefaultValue = new HashSet<Rel>();
|
|
|
|
this.defaultDefaultValue.addAll(Arrays.asList(rels));
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getNicename()
|
|
|
|
{
|
|
|
|
return this.nicename;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getDescription()
|
|
|
|
{
|
|
|
|
return this.desc;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Set<Rel> getDefault()
|
|
|
|
{
|
|
|
|
Set<Rel> ret = Conf.factionPermDefaults.get(this);
|
|
|
|
if (ret == null) return this.defaultDefaultValue;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-10-24 01:37:51 +02:00
|
|
|
public static FPerm parse(String str)
|
2011-10-23 23:17:02 +02:00
|
|
|
{
|
|
|
|
str = str.toLowerCase();
|
|
|
|
if (str.startsWith("bui")) return BUILD;
|
|
|
|
if (str.startsWith("p")) return PAINBUILD;
|
|
|
|
if (str.startsWith("d")) return DOOR;
|
|
|
|
if (str.startsWith("c")) return CONTAINER;
|
|
|
|
if (str.startsWith("but")) return BUTTON;
|
|
|
|
if (str.startsWith("l")) return LEVER;
|
2011-10-24 11:56:41 +02:00
|
|
|
if (str.startsWith("w")) return WITHDRAW;
|
2011-10-23 23:17:02 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2011-10-24 11:56:41 +02:00
|
|
|
public static String getStateHeaders()
|
2011-10-23 23:17:02 +02:00
|
|
|
{
|
2011-10-24 11:56:41 +02:00
|
|
|
String ret = "";
|
|
|
|
for (Rel rel : Rel.values())
|
2011-10-24 09:28:08 +02:00
|
|
|
{
|
2011-10-24 11:56:41 +02:00
|
|
|
ret += rel.getColor().toString();
|
|
|
|
ret += rel.toString().substring(0, 3);
|
|
|
|
ret += " ";
|
2011-10-24 09:28:08 +02:00
|
|
|
}
|
2011-10-24 11:56:41 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getStateInfo(Set<Rel> value, boolean withDesc)
|
|
|
|
{
|
|
|
|
String ret = "";
|
|
|
|
|
|
|
|
for (Rel rel : Rel.values())
|
2011-10-24 09:28:08 +02:00
|
|
|
{
|
2011-10-24 11:56:41 +02:00
|
|
|
if (value.contains(rel))
|
|
|
|
{
|
|
|
|
ret += "<g>YES";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret += "<b>NOO";
|
|
|
|
}
|
|
|
|
ret += " ";
|
2011-10-24 09:28:08 +02:00
|
|
|
}
|
2011-10-23 23:17:02 +02:00
|
|
|
|
2011-10-24 11:56:41 +02:00
|
|
|
ret +="<h>"+this.getNicename();
|
2011-10-23 23:17:02 +02:00
|
|
|
if (withDesc)
|
|
|
|
{
|
2011-10-24 11:56:41 +02:00
|
|
|
ret += " <i>" + this.getDescription();
|
2011-10-23 23:17:02 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Set<Rel> parseRelDeltas(String str, Set<Rel> current)
|
|
|
|
{
|
|
|
|
Set<Rel> ret = new HashSet<Rel>();
|
|
|
|
ret.addAll(current);
|
|
|
|
|
|
|
|
List<String> nodes = new ArrayList<String>(Arrays.asList(str.split("\\s+")));
|
|
|
|
|
|
|
|
for (String node : nodes)
|
|
|
|
{
|
|
|
|
boolean add = true;
|
|
|
|
if (node.startsWith("-"))
|
|
|
|
{
|
|
|
|
add = false;
|
|
|
|
node = node.substring(1);
|
|
|
|
}
|
|
|
|
else if (node.startsWith("+"))
|
|
|
|
{
|
|
|
|
node = node.substring(1);
|
|
|
|
}
|
|
|
|
Rel rel = Rel.parse(node);
|
|
|
|
|
|
|
|
if (rel == null) continue;
|
|
|
|
|
|
|
|
if (add)
|
|
|
|
{
|
|
|
|
ret.add(rel);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret.remove(rel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2011-10-24 01:37:51 +02:00
|
|
|
|
2011-10-24 02:33:30 +02:00
|
|
|
private static final String errorpattern = "<b>%s<b> can't %s in the territory of %s<b>.";
|
2011-10-24 11:56:41 +02:00
|
|
|
public boolean has(RelationParticipator testSubject, Faction hostFaction, boolean informIfNot)
|
2011-10-24 01:37:51 +02:00
|
|
|
{
|
|
|
|
Faction factionDoer = RelationUtil.getFaction(testSubject);
|
2011-10-24 11:56:41 +02:00
|
|
|
boolean ret = hostFaction.getPermittedRelations(this).contains(hostFaction.getRelationTo(factionDoer));
|
2011-10-24 01:37:51 +02:00
|
|
|
if (!ret && informIfNot && testSubject instanceof FPlayer)
|
|
|
|
{
|
|
|
|
FPlayer fplayer = (FPlayer)testSubject;
|
2011-10-24 11:56:41 +02:00
|
|
|
fplayer.msg(errorpattern, fplayer.describeTo(fplayer, true), this.getDescription(), hostFaction.describeTo(fplayer));
|
2011-10-24 01:37:51 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2011-10-24 11:56:41 +02:00
|
|
|
public boolean has(RelationParticipator testSubject, Faction hostFaction)
|
|
|
|
{
|
|
|
|
return this.has(testSubject, hostFaction, false);
|
|
|
|
}
|
|
|
|
public boolean has(RelationParticipator testSubject, FLocation floc, boolean informIfNot)
|
|
|
|
{
|
|
|
|
Faction factionThere = Board.getFactionAt(floc);
|
|
|
|
return this.has(testSubject, factionThere, informIfNot);
|
|
|
|
}
|
2011-10-24 01:37:51 +02:00
|
|
|
public boolean has(RelationParticipator testSubject, Location loc, boolean informIfNot)
|
|
|
|
{
|
|
|
|
FLocation floc = new FLocation(loc);
|
|
|
|
return this.has(testSubject, floc, informIfNot);
|
|
|
|
}
|
|
|
|
public boolean has(RelationParticipator testSubject, Location loc)
|
|
|
|
{
|
|
|
|
return this.has(testSubject, loc, false);
|
|
|
|
}
|
|
|
|
public boolean has(RelationParticipator testSubject, FLocation floc)
|
|
|
|
{
|
|
|
|
return this.has(testSubject, floc, false);
|
|
|
|
}
|
2011-10-23 17:30:41 +02:00
|
|
|
}
|