Factions/src/com/massivecraft/factions/FPerm.java

242 lines
7.5 KiB
Java
Raw Normal View History

package com.massivecraft.factions;
import java.util.Arrays;
import java.util.Collections;
New "access" system to replace old ownership system. Access can be granted to build, destroy, and fully interact within any chunk for specific players or factions. Access can also optionally be denied to normal members of the host faction. Some further info display to go with this feature is yet to come, and further testing for possible bugs is also needed. Related info: New FPerm "ACCESS" which is granted to faction leaders and officers by default. This FPerm allows you to bypass access restrictions throughout your faction territory, and (along with the "factions.access" Bukkit permission below) allows you to change access settings for any chunk owned by your faction. New permissions: factions.access - Ability to grant territory access for your faction, if you have the proper "ACCESS" FPerm (defaults to leaders and officers only). Added to factions.kit.halfplayer permission kit. factions.access.any - Ability to grant territory access for any faction on the server. Added to factions.kit.mod permission kit. factions.access.view - Ability to view territory access info for your own faction. Added to factions.kit.halfplayer permission kit. New command: /f access [view|p|f|player|faction=view] [name=you] - view or change the access information for the chunk you are in. If "view" or nothing is specified, it will simply display the info. If "p" or "player" is specified, a player will be granted access, or removed from the list if they were already granted access. If "f" or "faction" is specified, the same will be done for the specified faction. The name defaults to yourself or your faction if not specified. If your own faction is specified, you will toggle restricted access for the chunk so that normal faction members can be denied access, unless they are in the access list. Examples: /f access - view access list, if in your own territory /f access p SomePlayer - grant access to player "SomePlayer" for the current chunk, or remove them from the access list if already there /f access f - toggle restricted access for the current chunk (since faction name isn't specified, uses your own faction), assuming you're in your own factions territory
2012-05-15 04:41:13 +02:00
import java.util.EnumSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import org.bukkit.command.CommandSender;
2011-10-24 01:37:51 +02:00
import com.massivecraft.factions.entity.BoardColls;
import com.massivecraft.factions.entity.UPlayer;
2013-04-22 09:37:53 +02:00
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.UConf;
import com.massivecraft.mcore.ps.PS;
/**
* 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-24 01:37:51 +02:00
public enum FPerm
{
// -------------------------------------------- //
// ENUM
// -------------------------------------------- //
BUILD("build", "edit the terrain", Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.ALLY),
2013-04-25 07:32:33 +02:00
PAINBUILD("painbuild", "edit, take damage"),
DOOR("door", "use doors", Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY),
BUTTON("button", "use stone buttons", Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY),
LEVER("lever", "use levers", Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY),
CONTAINER("container", "use containers", Rel.LEADER, Rel.OFFICER, Rel.MEMBER),
INVITE("invite", "invite players", Rel.LEADER, Rel.OFFICER),
KICK("kick", "kick members", Rel.LEADER, Rel.OFFICER),
SETHOME("sethome", "set the home", Rel.LEADER, Rel.OFFICER),
WITHDRAW("withdraw", "withdraw money", Rel.LEADER, Rel.OFFICER),
TERRITORY("territory", "claim or unclaim", Rel.LEADER, Rel.OFFICER),
2013-04-25 07:32:33 +02:00
ACCESS("access", "grant territory", Rel.LEADER, Rel.OFFICER),
DISBAND("disband", "disband the faction", Rel.LEADER),
PERMS("perms", "manage permissions", Rel.LEADER),
// END OF LIST
;
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private final String nicename;
public String getNicename() { return this.nicename; }
private final String desc;
public String getDescription() { return this.desc; }
public final Set<Rel> defaultDefault;
public Set<Rel> getDefaultDefault() { return new LinkedHashSet<Rel>(this.defaultDefault); }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
2011-10-24 01:37:51 +02:00
private FPerm(final String nicename, final String desc, final Rel... rels)
{
this.nicename = nicename;
this.desc = desc;
Set<Rel> defaultDefaultValue = new LinkedHashSet<Rel>();
defaultDefaultValue.addAll(Arrays.asList(rels));
defaultDefaultValue = Collections.unmodifiableSet(defaultDefaultValue);
this.defaultDefault = defaultDefaultValue;
}
// -------------------------------------------- //
// DEFAULTS
// -------------------------------------------- //
public Set<Rel> getDefault(Object o)
{
2013-04-23 14:00:18 +02:00
Set<Rel> ret = UConf.get(o).defaultFactionPerms.get(this);
if (ret == null) return this.getDefaultDefault();
ret = new LinkedHashSet<Rel>(ret);
return ret;
}
public static Map<FPerm, Set<Rel>> getDefaultDefaults()
{
Map<FPerm, Set<Rel>> ret = new LinkedHashMap<FPerm, Set<Rel>>();
for (FPerm fperm : values())
{
ret.put(fperm, fperm.getDefaultDefault());
}
return ret;
}
// -------------------------------------------- //
// PARSE
// -------------------------------------------- //
2011-10-24 01:37:51 +02:00
public static FPerm parse(String str)
{
str = str.toLowerCase();
New "access" system to replace old ownership system. Access can be granted to build, destroy, and fully interact within any chunk for specific players or factions. Access can also optionally be denied to normal members of the host faction. Some further info display to go with this feature is yet to come, and further testing for possible bugs is also needed. Related info: New FPerm "ACCESS" which is granted to faction leaders and officers by default. This FPerm allows you to bypass access restrictions throughout your faction territory, and (along with the "factions.access" Bukkit permission below) allows you to change access settings for any chunk owned by your faction. New permissions: factions.access - Ability to grant territory access for your faction, if you have the proper "ACCESS" FPerm (defaults to leaders and officers only). Added to factions.kit.halfplayer permission kit. factions.access.any - Ability to grant territory access for any faction on the server. Added to factions.kit.mod permission kit. factions.access.view - Ability to view territory access info for your own faction. Added to factions.kit.halfplayer permission kit. New command: /f access [view|p|f|player|faction=view] [name=you] - view or change the access information for the chunk you are in. If "view" or nothing is specified, it will simply display the info. If "p" or "player" is specified, a player will be granted access, or removed from the list if they were already granted access. If "f" or "faction" is specified, the same will be done for the specified faction. The name defaults to yourself or your faction if not specified. If your own faction is specified, you will toggle restricted access for the chunk so that normal faction members can be denied access, unless they are in the access list. Examples: /f access - view access list, if in your own territory /f access p SomePlayer - grant access to player "SomePlayer" for the current chunk, or remove them from the access list if already there /f access f - toggle restricted access for the current chunk (since faction name isn't specified, uses your own faction), assuming you're in your own factions territory
2012-05-15 04:41:13 +02:00
if (str.startsWith("a")) return ACCESS;
if (str.startsWith("bui")) return BUILD;
if (str.startsWith("pa")) return PAINBUILD;
if (str.startsWith("do")) return DOOR;
if (str.startsWith("but")) return BUTTON;
if (str.startsWith("l")) return LEVER;
if (str.startsWith("co")) return CONTAINER;
if (str.startsWith("i")) return INVITE;
if (str.startsWith("k")) return KICK;
if (str.startsWith("s")) return SETHOME;
if (str.startsWith("w")) return WITHDRAW;
if (str.startsWith("t")) return TERRITORY;
if (str.startsWith("di")) return DISBAND;
if (str.startsWith("pe")) return PERMS;
return null;
}
// -------------------------------------------- //
// UTIL
// -------------------------------------------- //
public static String getStateHeaders()
{
String ret = "";
for (Rel rel : Rel.values())
{
ret += rel.getColor().toString();
ret += rel.toString().substring(0, 3);
ret += " ";
}
return ret;
}
public String getStateInfo(Set<Rel> value, boolean withDesc)
{
String ret = "";
for (Rel rel : Rel.values())
{
if (value.contains(rel))
{
ret += "<g>YES";
}
else
{
ret += "<b>NOO";
}
ret += " ";
}
2011-10-24 13:02:48 +02:00
ret +="<c>"+this.getNicename();
if (withDesc)
{
ret += " <i>" + this.getDescription();
}
return ret;
}
New "access" system to replace old ownership system. Access can be granted to build, destroy, and fully interact within any chunk for specific players or factions. Access can also optionally be denied to normal members of the host faction. Some further info display to go with this feature is yet to come, and further testing for possible bugs is also needed. Related info: New FPerm "ACCESS" which is granted to faction leaders and officers by default. This FPerm allows you to bypass access restrictions throughout your faction territory, and (along with the "factions.access" Bukkit permission below) allows you to change access settings for any chunk owned by your faction. New permissions: factions.access - Ability to grant territory access for your faction, if you have the proper "ACCESS" FPerm (defaults to leaders and officers only). Added to factions.kit.halfplayer permission kit. factions.access.any - Ability to grant territory access for any faction on the server. Added to factions.kit.mod permission kit. factions.access.view - Ability to view territory access info for your own faction. Added to factions.kit.halfplayer permission kit. New command: /f access [view|p|f|player|faction=view] [name=you] - view or change the access information for the chunk you are in. If "view" or nothing is specified, it will simply display the info. If "p" or "player" is specified, a player will be granted access, or removed from the list if they were already granted access. If "f" or "faction" is specified, the same will be done for the specified faction. The name defaults to yourself or your faction if not specified. If your own faction is specified, you will toggle restricted access for the chunk so that normal faction members can be denied access, unless they are in the access list. Examples: /f access - view access list, if in your own territory /f access p SomePlayer - grant access to player "SomePlayer" for the current chunk, or remove them from the access list if already there /f access f - toggle restricted access for the current chunk (since faction name isn't specified, uses your own faction), assuming you're in your own factions territory
2012-05-15 04:41:13 +02:00
// Perms which apply strictly to granting territory access
// TODO: This should be a boolean field within the class itself!
New "access" system to replace old ownership system. Access can be granted to build, destroy, and fully interact within any chunk for specific players or factions. Access can also optionally be denied to normal members of the host faction. Some further info display to go with this feature is yet to come, and further testing for possible bugs is also needed. Related info: New FPerm "ACCESS" which is granted to faction leaders and officers by default. This FPerm allows you to bypass access restrictions throughout your faction territory, and (along with the "factions.access" Bukkit permission below) allows you to change access settings for any chunk owned by your faction. New permissions: factions.access - Ability to grant territory access for your faction, if you have the proper "ACCESS" FPerm (defaults to leaders and officers only). Added to factions.kit.halfplayer permission kit. factions.access.any - Ability to grant territory access for any faction on the server. Added to factions.kit.mod permission kit. factions.access.view - Ability to view territory access info for your own faction. Added to factions.kit.halfplayer permission kit. New command: /f access [view|p|f|player|faction=view] [name=you] - view or change the access information for the chunk you are in. If "view" or nothing is specified, it will simply display the info. If "p" or "player" is specified, a player will be granted access, or removed from the list if they were already granted access. If "f" or "faction" is specified, the same will be done for the specified faction. The name defaults to yourself or your faction if not specified. If your own faction is specified, you will toggle restricted access for the chunk so that normal faction members can be denied access, unless they are in the access list. Examples: /f access - view access list, if in your own territory /f access p SomePlayer - grant access to player "SomePlayer" for the current chunk, or remove them from the access list if already there /f access f - toggle restricted access for the current chunk (since faction name isn't specified, uses your own faction), assuming you're in your own factions territory
2012-05-15 04:41:13 +02:00
private static final Set<FPerm> TerritoryPerms = EnumSet.of(BUILD, DOOR, BUTTON, LEVER, CONTAINER);
public boolean isTerritoryPerm()
{
return TerritoryPerms.contains(this);
}
private static final String errorpattern = "%s<b> does not allow you to %s<b>.";
public boolean has(Object testSubject, Faction hostFaction, boolean informIfNot)
2011-10-24 01:37:51 +02:00
{
RelationParticipator rpSubject = null;
2011-10-24 13:02:48 +02:00
if (testSubject instanceof CommandSender)
{
rpSubject = UPlayer.get(testSubject);
}
else if (testSubject instanceof RelationParticipator)
{
rpSubject = (RelationParticipator) testSubject;
}
else
{
return false;
}
Rel rel = rpSubject.getRelationTo(hostFaction);
// TODO: Create better description messages like: "You must at least be officer".
2011-10-24 13:02:48 +02:00
boolean ret = hostFaction.getPermittedRelations(this).contains(rel);
if (rpSubject instanceof UPlayer && ret == false && ((UPlayer)rpSubject).isUsingAdminMode()) ret = true;
if (!ret && informIfNot && rpSubject instanceof UPlayer)
2011-10-24 01:37:51 +02:00
{
UPlayer uplayer = (UPlayer)rpSubject;
uplayer.msg(errorpattern, hostFaction.describeTo(uplayer, true), this.getDescription());
if (Perm.ADMIN.has(uplayer.getPlayer()))
{
uplayer.msg("<i>You can bypass by using " + Factions.get().getOuterCmdFactions().cmdFactionsAdmin.getUseageTemplate(false));
}
2011-10-24 01:37:51 +02:00
}
return ret;
}
public boolean has(Object testSubject, Faction hostFaction)
{
return this.has(testSubject, hostFaction, false);
}
public boolean has(Object testSubject, PS ps, boolean informIfNot)
{
TerritoryAccess access = BoardColls.get().getTerritoryAccessAt(ps);
New "access" system to replace old ownership system. Access can be granted to build, destroy, and fully interact within any chunk for specific players or factions. Access can also optionally be denied to normal members of the host faction. Some further info display to go with this feature is yet to come, and further testing for possible bugs is also needed. Related info: New FPerm "ACCESS" which is granted to faction leaders and officers by default. This FPerm allows you to bypass access restrictions throughout your faction territory, and (along with the "factions.access" Bukkit permission below) allows you to change access settings for any chunk owned by your faction. New permissions: factions.access - Ability to grant territory access for your faction, if you have the proper "ACCESS" FPerm (defaults to leaders and officers only). Added to factions.kit.halfplayer permission kit. factions.access.any - Ability to grant territory access for any faction on the server. Added to factions.kit.mod permission kit. factions.access.view - Ability to view territory access info for your own faction. Added to factions.kit.halfplayer permission kit. New command: /f access [view|p|f|player|faction=view] [name=you] - view or change the access information for the chunk you are in. If "view" or nothing is specified, it will simply display the info. If "p" or "player" is specified, a player will be granted access, or removed from the list if they were already granted access. If "f" or "faction" is specified, the same will be done for the specified faction. The name defaults to yourself or your faction if not specified. If your own faction is specified, you will toggle restricted access for the chunk so that normal faction members can be denied access, unless they are in the access list. Examples: /f access - view access list, if in your own territory /f access p SomePlayer - grant access to player "SomePlayer" for the current chunk, or remove them from the access list if already there /f access f - toggle restricted access for the current chunk (since faction name isn't specified, uses your own faction), assuming you're in your own factions territory
2012-05-15 04:41:13 +02:00
if (this.isTerritoryPerm())
{
if (access.subjectHasAccess(testSubject)) return true;
if (access.subjectAccessIsRestricted(testSubject))
New "access" system to replace old ownership system. Access can be granted to build, destroy, and fully interact within any chunk for specific players or factions. Access can also optionally be denied to normal members of the host faction. Some further info display to go with this feature is yet to come, and further testing for possible bugs is also needed. Related info: New FPerm "ACCESS" which is granted to faction leaders and officers by default. This FPerm allows you to bypass access restrictions throughout your faction territory, and (along with the "factions.access" Bukkit permission below) allows you to change access settings for any chunk owned by your faction. New permissions: factions.access - Ability to grant territory access for your faction, if you have the proper "ACCESS" FPerm (defaults to leaders and officers only). Added to factions.kit.halfplayer permission kit. factions.access.any - Ability to grant territory access for any faction on the server. Added to factions.kit.mod permission kit. factions.access.view - Ability to view territory access info for your own faction. Added to factions.kit.halfplayer permission kit. New command: /f access [view|p|f|player|faction=view] [name=you] - view or change the access information for the chunk you are in. If "view" or nothing is specified, it will simply display the info. If "p" or "player" is specified, a player will be granted access, or removed from the list if they were already granted access. If "f" or "faction" is specified, the same will be done for the specified faction. The name defaults to yourself or your faction if not specified. If your own faction is specified, you will toggle restricted access for the chunk so that normal faction members can be denied access, unless they are in the access list. Examples: /f access - view access list, if in your own territory /f access p SomePlayer - grant access to player "SomePlayer" for the current chunk, or remove them from the access list if already there /f access f - toggle restricted access for the current chunk (since faction name isn't specified, uses your own faction), assuming you're in your own factions territory
2012-05-15 04:41:13 +02:00
{
if (informIfNot)
{
UPlayer notify = null;
if (testSubject instanceof CommandSender)
notify = UPlayer.get(testSubject);
else if (testSubject instanceof UPlayer)
notify = (UPlayer)testSubject;
New "access" system to replace old ownership system. Access can be granted to build, destroy, and fully interact within any chunk for specific players or factions. Access can also optionally be denied to normal members of the host faction. Some further info display to go with this feature is yet to come, and further testing for possible bugs is also needed. Related info: New FPerm "ACCESS" which is granted to faction leaders and officers by default. This FPerm allows you to bypass access restrictions throughout your faction territory, and (along with the "factions.access" Bukkit permission below) allows you to change access settings for any chunk owned by your faction. New permissions: factions.access - Ability to grant territory access for your faction, if you have the proper "ACCESS" FPerm (defaults to leaders and officers only). Added to factions.kit.halfplayer permission kit. factions.access.any - Ability to grant territory access for any faction on the server. Added to factions.kit.mod permission kit. factions.access.view - Ability to view territory access info for your own faction. Added to factions.kit.halfplayer permission kit. New command: /f access [view|p|f|player|faction=view] [name=you] - view or change the access information for the chunk you are in. If "view" or nothing is specified, it will simply display the info. If "p" or "player" is specified, a player will be granted access, or removed from the list if they were already granted access. If "f" or "faction" is specified, the same will be done for the specified faction. The name defaults to yourself or your faction if not specified. If your own faction is specified, you will toggle restricted access for the chunk so that normal faction members can be denied access, unless they are in the access list. Examples: /f access - view access list, if in your own territory /f access p SomePlayer - grant access to player "SomePlayer" for the current chunk, or remove them from the access list if already there /f access f - toggle restricted access for the current chunk (since faction name isn't specified, uses your own faction), assuming you're in your own factions territory
2012-05-15 04:41:13 +02:00
if (notify != null)
notify.msg("<b>This territory owned by your faction has restricted access.");
}
return false;
}
}
return this.has(testSubject, BoardColls.get().getFactionAt(ps), informIfNot);
2011-10-24 01:37:51 +02:00
}
public boolean has(Object testSubject, PS ps)
2011-10-24 01:37:51 +02:00
{
return this.has(testSubject, ps, false);
2011-10-24 01:37:51 +02:00
}
}