Working on the faction perms as well now

This commit is contained in:
Olof Larsson 2011-10-23 23:17:02 +02:00
parent 21783ef9d4
commit 371261a07f
8 changed files with 272 additions and 53 deletions

View File

@ -33,6 +33,7 @@ permissions:
factions.kit.fullplayer: true
factions.bypass: true
factions.flag.any: true
factions.perm.any: true
factions.kick.any: true
factions.ownershipbypass: true
factions.kit.fullplayer:
@ -52,6 +53,7 @@ permissions:
factions.description: true
factions.disband: true
factions.flag: true
factions.perm: true
factions.help: true
factions.home: true
factions.invite: true
@ -102,6 +104,10 @@ permissions:
description: change faction flags
factions.flag.any:
description: change all flags for all factions
factions.perm:
description: change faction permissions
factions.perm.any:
description: change all permissions for all factions
factions.help:
description: display a help page
factions.home:

View File

@ -6,6 +6,8 @@ import org.bukkit.*;
import org.bukkit.entity.CreatureType;
import com.massivecraft.factions.struct.FactionFlag;
import com.massivecraft.factions.struct.FactionPerm;
import com.massivecraft.factions.struct.Rel;
public class Conf
{
@ -27,6 +29,8 @@ public class Conf
// REFACTOR ASJDKJASDKFJKASDF
public static Map<FactionFlag, Boolean> factionFlagDefaults;
public static Map<FactionFlag, Boolean> factionFlagIsChangeable;
public static Map<FactionPerm, Set<Rel>> factionPermDefaults;
// REFACTOR ASJDKJASDKFJKASDF EEEEEENNNNDD
@ -285,6 +289,12 @@ public class Conf
factionFlagIsChangeable.put(flag, flag.defaultDefaultChangeable);
}
factionPermDefaults = new LinkedHashMap<FactionPerm, Set<Rel>>();
for (FactionPerm perm: FactionPerm.values())
{
factionPermDefaults.put(perm, perm.defaultDefaultValue);
}
territoryEnemyDenyCommands.add("home");
territoryEnemyDenyCommands.add("sethome");
territoryEnemyDenyCommands.add("spawn");

View File

@ -12,6 +12,7 @@ import com.massivecraft.factions.iface.EconomyParticipator;
import com.massivecraft.factions.iface.RelationParticipator;
import com.massivecraft.factions.integration.Econ;
import com.massivecraft.factions.struct.FactionFlag;
import com.massivecraft.factions.struct.FactionPerm;
import com.massivecraft.factions.struct.Permission;
import com.massivecraft.factions.struct.Rel;
import com.massivecraft.factions.util.*;
@ -39,25 +40,6 @@ public class Faction extends Entity implements EconomyParticipator
public boolean getOpen() { return open; }
public void setOpen(boolean isOpen) { open = isOpen; }
// FIELD: peaceful
// "peaceful" status can only be set by server admins/moderators/ops, and prevents PvP and land capture to/from the faction
//private boolean peaceful;
//public boolean isPeaceful() { return this.peaceful; }
//public void setPeaceful(boolean isPeaceful) { this.peaceful = isPeaceful; }
// FIELD: peacefulExplosionsEnabled
//private boolean peacefulExplosionsEnabled;
//public void setPeacefulExplosionsEnabled(boolean val) { peacefulExplosionsEnabled = val; }
//public boolean getPeacefulExplosionsEnabled(){ return this.peacefulExplosionsEnabled; }
//public boolean noExplosionsInTerritory() { return this.peaceful && ! peacefulExplosionsEnabled; }
// FIELD: permanent
// "permanent" status can only be set by server admins/moderators/ops, and allows the faction to remain even with 0 members
//private boolean permanent;
//public boolean isPermanent() { return permanent; }
//public void setPermanent(boolean isPermanent) { permanent = isPermanent; }
// FIELD: tag
private String tag;
public String getTag() { return this.tag; }
@ -140,15 +122,31 @@ public class Faction extends Entity implements EconomyParticipator
}
public void setFlag(FactionFlag flag, boolean value)
{
if (Conf.factionFlagDefaults.get(flag) == value)
if (Conf.factionFlagDefaults.get(flag).equals(value))
{
this.flagOverrides.remove(flag);
return;
}
this.flagOverrides.put(flag, value);
}
// FIELDS: Permission <-> Groups management
private Map<FactionPerm, Set<Rel>> permOverrides; // Contains the modifications to the default values
public Set<Rel> getPerm(FactionPerm perm)
{
Set<Rel> ret = this.permOverrides.get(perm);
if (ret == null) ret = perm.getDefault();
return ret;
}
public void setPerm(FactionPerm perm, Set<Rel> value)
{
if (Conf.factionPermDefaults.get(perm).equals(value))
{
this.permOverrides.remove(perm);
return;
}
this.permOverrides.put(perm, value);
}
// -------------------------------------------- //
@ -169,15 +167,6 @@ public class Faction extends Entity implements EconomyParticipator
this.flagOverrides = new LinkedHashMap<FactionFlag, Boolean>();
}
// -------------------------------------------- //
// Extra Getters And Setters
// -------------------------------------------- //
//public boolean noPvPInTerritory() { return isSafeZone() || (peaceful && Conf.peacefulTerritoryDisablePVP); }
//public boolean noMonstersInTerritory() { return isSafeZone() || (peaceful && Conf.peacefulTerritoryDisableMonsters); }
// -------------------------------
// Understand the types
@ -196,22 +185,6 @@ public class Faction extends Entity implements EconomyParticipator
return this.getId().equals("0");
}
/*public boolean isSafeZone()
{
return this.getId().equals("-1");
}
public boolean isWarZone()
{
return this.getId().equals("-2");
}
public boolean isPlayerFreeType()
{
return this.isSafeZone() || this.isWarZone();
}*/
// -------------------------------
// Relation and relation colors
// -------------------------------

View File

@ -0,0 +1,88 @@
package com.massivecraft.factions.cmd;
import java.util.Set;
import com.massivecraft.factions.Faction;
import com.massivecraft.factions.struct.FactionPerm;
import com.massivecraft.factions.struct.Permission;
import com.massivecraft.factions.struct.Rel;
import com.massivecraft.factions.zcore.util.TextUtil;
public class CmdPerm extends FCommand
{
public CmdPerm()
{
super();
this.aliases.add("perm");
//this.requiredArgs.add("");
this.optionalArgs.put("faction", "your");
this.optionalArgs.put("perm", "all");
this.optionalArgs.put("relationdelta", "read");
this.permission = Permission.PERM.node;
this.disableOnLock = true;
this.errorOnToManyArgs = false;
senderMustBePlayer = false;
senderMustBeMember = false;
senderMustBeOfficer = false;
senderMustBeLeader = false;
}
@Override
public void perform()
{
Faction faction = myFaction;
if (this.argIsSet(0))
{
faction = this.argAsFaction(0);
}
if (faction == null) return;
msg(p.txt.titleize("Perm(s) for " + faction.describeTo(fme)));
if ( ! this.argIsSet(1))
{
for (FactionPerm perm : FactionPerm.values())
{
msg(perm.getStateInfo(faction.getPerm(perm), true));
}
return;
}
FactionPerm perm = this.argAsFactionPerm(1);
if (perm == null) return;
if ( ! this.argIsSet(2))
{
msg(perm.getStateInfo(faction.getPerm(perm), true));
return;
}
// TODO: Awesomesause parser for deltas...
Set<Rel> targetValue = FactionPerm.parseRelDeltas(TextUtil.implode(args.subList(2, args.size()-1), " "), faction.getPerm(perm));
// Do the sender have the right to change perms for this faction?
if (Permission.PERM_ANY.has(sender))
{
// This sender may modify any perm for anyone
}
else if (faction != myFaction)
{
msg("<b>You are not a member in that faction.");
return;
}
else if (fme.getRole().isLessThan(Rel.OFFICER))
{
msg("<b>You must be faction leader or officer to change your faction permissions.");
return;
}
// Do the change
faction.setPerm(perm, targetValue);
msg(perm.getStateInfo(faction.getPerm(perm), true));
}
}

View File

@ -31,6 +31,7 @@ public class FCmdRoot extends FCommand
public CmdOpen cmdOpen = new CmdOpen();
public CmdOwner cmdOwner = new CmdOwner();
public CmdOwnerList cmdOwnerList = new CmdOwnerList();
public CmdPerm cmdPerm = new CmdPerm();
public CmdPower cmdPower = new CmdPower();
public CmdRelationAlly cmdRelationAlly = new CmdRelationAlly();
public CmdRelationEnemy cmdRelationEnemy = new CmdRelationEnemy();
@ -93,6 +94,7 @@ public class FCmdRoot extends FCommand
this.addSubCommand(this.cmdOpen);
this.addSubCommand(this.cmdOwner);
this.addSubCommand(this.cmdOwnerList);
this.addSubCommand(this.cmdPerm);
this.addSubCommand(this.cmdPower);
this.addSubCommand(this.cmdRelationAlly);
this.addSubCommand(this.cmdRelationEnemy);

View File

@ -13,6 +13,7 @@ import com.massivecraft.factions.Faction;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.P;
import com.massivecraft.factions.struct.FactionFlag;
import com.massivecraft.factions.struct.FactionPerm;
import com.massivecraft.factions.struct.Rel;
import com.massivecraft.factions.zcore.MCommand;
@ -308,6 +309,40 @@ public abstract class FCommand extends MCommand<P>
return this.argAsFactionFlag(idx, null);
}
// FACTION PERM ======================
public FactionPerm strAsFactionPerm(String name, FactionPerm def, boolean msg)
{
FactionPerm ret = def;
if (name != null)
{
FactionPerm perm = FactionPerm.parse(name);
if (perm != null)
{
ret = perm;
}
}
if (msg && ret == null)
{
this.msg("<b>The faction-perm \"<p>%s<b>\" could not be found.", name);
}
return ret;
}
public FactionPerm argAsFactionPerm(int idx, FactionPerm def, boolean msg)
{
return this.strAsFactionPerm(this.argAsString(idx), def, msg);
}
public FactionPerm argAsFactionPerm(int idx, FactionPerm def)
{
return this.argAsFactionPerm(idx, def, true);
}
public FactionPerm argAsFactionPerm(int idx)
{
return this.argAsFactionPerm(idx, null);
}
// -------------------------------------------- //
// Commonly used logic

View File

@ -1,16 +1,119 @@
package com.massivecraft.factions.struct;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.massivecraft.factions.Conf;
import com.massivecraft.factions.zcore.util.TextUtil;
/**
* Permissions that you (a player) may or may not have in the territory of a certain faction.
* Each faction have many Rel's assigned to each one of these Perms.
*/
public enum FactionPerm
{
BUILD, // This player can build in the faction
PAINBUILD, // This player can build in the faction BUT will take damage each time. This is overridden by BUILD if player has both
DOOR,
WORKBENCH,
CONTAINER,
BUTTON,
LEVER,
BUILD("build", "edit the terrain", Rel.MEMBER),
PAINBUILD("painbuild", "edit but take damage", Rel.ALLY),
DOOR("door", "use doors etc.", Rel.MEMBER, Rel.ALLY),
CONTAINER("container", "use chests etc.", Rel.MEMBER),
BUTTON("button", "use stone buttons", Rel.MEMBER, Rel.ALLY),
LEVER("lever", "use levers", Rel.MEMBER, Rel.ALLY),
;
private final String nicename;
private final String desc;
public final Set<Rel> defaultDefaultValue;
private FactionPerm(final String nicename, final String desc, final Rel... rels)
{
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;
}
public static FactionPerm parse(String str)
{
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;
return null;
}
public String getStateInfo(Set<Rel> value, boolean withDesc)
{
String ret = "<h>"+this.getNicename()+ " ";
List<String> rels = new ArrayList<String>();
for (Rel rel : value)
{
rels.add("<i>"+rel.nicename);
}
ret += TextUtil.implode(rels, "<n> ,");
if (withDesc)
{
ret += " " + this.getDescription();
}
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;
}
}

View File

@ -20,6 +20,8 @@ public enum Permission
DISBAND_ANY("disband.any"),
FLAG("flag"),
FLAG_ANY("flag.any"),
PERM("perm"),
PERM_ANY("perm.any"),
HELP("help"),
HOME("home"),
INVITE("invite"),