Fixing faction permOverides and making use of a predictate to send faction messages.

This commit is contained in:
Olof Larsson 2013-04-17 13:16:22 +02:00
parent 818c051ae1
commit 0776e5ae55
3 changed files with 184 additions and 112 deletions

View File

@ -1,8 +1,9 @@
package com.massivecraft.factions; package com.massivecraft.factions;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.HashSet; import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
import org.bukkit.Location; import org.bukkit.Location;
@ -18,6 +19,10 @@ import com.massivecraft.mcore.ps.PS;
*/ */
public enum FPerm public enum FPerm
{ {
// -------------------------------------------- //
// ENUM
// -------------------------------------------- //
BUILD("build", "edit the terrain", Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY), BUILD("build", "edit the terrain", Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY),
PAINBUILD("painbuild", "edit but take damage"), PAINBUILD("painbuild", "edit but take damage"),
DOOR("door", "use doors", Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY), DOOR("door", "use doors", Rel.LEADER, Rel.OFFICER, Rel.MEMBER, Rel.RECRUIT, Rel.ALLY),
@ -33,34 +38,46 @@ public enum FPerm
ACCESS("access", "grant territory access", Rel.LEADER, Rel.OFFICER), ACCESS("access", "grant territory access", Rel.LEADER, Rel.OFFICER),
DISBAND("disband", "disband the faction", Rel.LEADER), DISBAND("disband", "disband the faction", Rel.LEADER),
PERMS("perms", "manage permissions", Rel.LEADER), PERMS("perms", "manage permissions", Rel.LEADER),
// END OF LIST
; ;
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private final String nicename; private final String nicename;
public String getNicename() { return this.nicename; }
private final String desc; private final String desc;
public String getDescription() { return this.desc; }
public final Set<Rel> defaultDefaultValue; public final Set<Rel> defaultDefaultValue;
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
private FPerm(final String nicename, final String desc, final Rel... rels) private FPerm(final String nicename, final String desc, final Rel... rels)
{ {
this.nicename = nicename; this.nicename = nicename;
this.desc = desc; this.desc = desc;
this.defaultDefaultValue = new HashSet<Rel>();
this.defaultDefaultValue.addAll(Arrays.asList(rels)); Set<Rel> defaultDefaultValue = new LinkedHashSet<Rel>();
defaultDefaultValue.addAll(Arrays.asList(rels));
defaultDefaultValue = Collections.unmodifiableSet(defaultDefaultValue);
this.defaultDefaultValue = defaultDefaultValue;
} }
public String getNicename() // -------------------------------------------- //
{ // FROOODLDLLD
return this.nicename; // -------------------------------------------- //
}
public String getDescription()
{
return this.desc;
}
public Set<Rel> getDefault() public Set<Rel> getDefault()
{ {
Set<Rel> ret = ConfServer.factionPermDefaults.get(this); Set<Rel> ret = ConfServer.factionPermDefaults.get(this);
if (ret == null) return this.defaultDefaultValue; if (ret == null) ret = this.defaultDefaultValue;
ret = new LinkedHashSet<Rel>(ret);
return ret; return ret;
} }

View File

@ -13,6 +13,7 @@ import com.massivecraft.factions.iface.RelationParticipator;
import com.massivecraft.factions.integration.Econ; import com.massivecraft.factions.integration.Econ;
import com.massivecraft.factions.integration.SpoutFeatures; import com.massivecraft.factions.integration.SpoutFeatures;
import com.massivecraft.factions.util.*; import com.massivecraft.factions.util.*;
import com.massivecraft.mcore.mixin.Mixin;
import com.massivecraft.mcore.ps.PS; import com.massivecraft.mcore.ps.PS;
import com.massivecraft.mcore.store.Entity; import com.massivecraft.mcore.store.Entity;
import com.massivecraft.mcore.util.SenderUtil; import com.massivecraft.mcore.util.SenderUtil;
@ -46,7 +47,7 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
this.setInvitedPlayerIds(that.invitedPlayerIds); this.setInvitedPlayerIds(that.invitedPlayerIds);
this.setRelationWishes(that.relationWish); this.setRelationWishes(that.relationWish);
this.setFlags(that.flagOverrides); this.setFlags(that.flagOverrides);
this.permOverrides = that.permOverrides; this.setPerms(that.permOverrides);
return this; return this;
} }
@ -54,7 +55,6 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
// -------------------------------------------- // // -------------------------------------------- //
// FIELDS: RAW // FIELDS: RAW
// -------------------------------------------- // // -------------------------------------------- //
// TODO
// In this section of the source code we place the field declarations only. // In this section of the source code we place the field declarations only.
// Each field has it's own section further down since even the getter and setter logic takes up quite some place. // Each field has it's own section further down since even the getter and setter logic takes up quite some place.
@ -92,66 +92,11 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
// The keys in this map are factionIds. // The keys in this map are factionIds.
private Map<String, Rel> relationWish = null; private Map<String, Rel> relationWish = null;
// The flag overrides are the modifications to the default values. // The flag overrides are modifications to the default values.
private Map<FFlag, Boolean> flagOverrides; private Map<FFlag, Boolean> flagOverrides = null;
// FIELDS: Permission <-> Groups management // The perm overrides are modifications to the default values.
private Map<FPerm, Set<Rel>> permOverrides; // Contains the modifications to the default values private Map<FPerm, Set<Rel>> permOverrides = null;
public Set<Rel> getPermittedRelations(FPerm perm)
{
Set<Rel> ret = this.permOverrides.get(perm);
if (ret == null) ret = perm.getDefault();
return ret;
}
/*
public void addPermittedRelation(FPerm perm, Rel rel)
{
Set<Rel> newPermittedRelations = EnumSet.noneOf(Rel.class);
newPermittedRelations.addAll(this.getPermittedRelations(perm));
newPermittedRelations.add(rel);
this.setPermittedRelations(perm, newPermittedRelations);
}
public void removePermittedRelation(FPerm perm, Rel rel)
{
Set<Rel> newPermittedRelations = EnumSet.noneOf(Rel.class);
newPermittedRelations.addAll(this.getPermittedRelations(perm));
newPermittedRelations.remove(rel);
this.setPermittedRelations(perm, newPermittedRelations);
}*/
public void setRelationPermitted(FPerm perm, Rel rel, boolean permitted)
{
Set<Rel> newPermittedRelations = EnumSet.noneOf(Rel.class);
newPermittedRelations.addAll(this.getPermittedRelations(perm));
if (permitted)
{
newPermittedRelations.add(rel);
}
else
{
newPermittedRelations.remove(rel);
}
this.setPermittedRelations(perm, newPermittedRelations);
}
public void setPermittedRelations(FPerm perm, Set<Rel> rels)
{
if (perm.getDefault().equals(rels))
{
this.permOverrides.remove(perm);
return;
}
this.permOverrides.put(perm, rels);
}
public void setPermittedRelations(FPerm perm, Rel... rels)
{
Set<Rel> temp = new HashSet<Rel>();
temp.addAll(Arrays.asList(rels));
this.setPermittedRelations(perm, temp);
}
// -------------------------------------------- // // -------------------------------------------- //
// CONSTRUCT // CONSTRUCT
@ -159,8 +104,7 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
public Faction() public Faction()
{ {
this.flagOverrides = new LinkedHashMap<FFlag, Boolean>();
this.permOverrides = new LinkedHashMap<FPerm, Set<Rel>>();
} }
// -------------------------------------------- // // -------------------------------------------- //
@ -275,8 +219,6 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
// FIELD: home // FIELD: home
// -------------------------------------------- // // -------------------------------------------- //
// TODO: Checkery is a bit weird?
public PS getHome() public PS getHome()
{ {
this.verifyHomeIsValid(); this.verifyHomeIsValid();
@ -592,7 +534,104 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
} }
// -------------------------------------------- // // -------------------------------------------- //
// RELATION AND COLORS // FIELD: permOverrides
// -------------------------------------------- //
// RAW
public Map<FPerm, Set<Rel>> getPerms()
{
Map<FPerm, Set<Rel>> ret = new LinkedHashMap<FPerm, Set<Rel>>();
for (FPerm fperm : FPerm.values())
{
ret.put(fperm, fperm.getDefault());
}
if (this.permOverrides != null)
{
for (Entry<FPerm, Set<Rel>> entry : this.permOverrides.entrySet())
{
ret.put(entry.getKey(), new LinkedHashSet<Rel>(entry.getValue()));
}
}
return ret;
}
public void setPerms(Map<FPerm, Set<Rel>> perms)
{
Map<FPerm, Set<Rel>> target = new LinkedHashMap<FPerm, Set<Rel>>();
if (perms != null)
{
for (Entry<FPerm, Set<Rel>> entry : perms.entrySet())
{
target.put(entry.getKey(), new LinkedHashSet<Rel>(entry.getValue()));
}
}
Iterator<Entry<FPerm, Set<Rel>>> iter = target.entrySet().iterator();
while (iter.hasNext())
{
Entry<FPerm, Set<Rel>> entry = iter.next();
if (entry.getKey().getDefault().equals(entry.getValue()))
{
iter.remove();
}
}
if (target == null || target.isEmpty())
{
this.permOverrides = null;
}
else
{
this.permOverrides = target;
}
this.changed();
}
// FINER
public Set<Rel> getPermittedRelations(FPerm perm)
{
return this.getPerms().get(perm);
}
public void setPermittedRelations(FPerm perm, Set<Rel> rels)
{
Map<FPerm, Set<Rel>> perms = this.getPerms();
perms.put(perm, rels);
this.setPerms(perms);
}
public void setPermittedRelations(FPerm perm, Rel... rels)
{
Set<Rel> temp = new HashSet<Rel>();
temp.addAll(Arrays.asList(rels));
this.setPermittedRelations(perm, temp);
}
public void setRelationPermitted(FPerm perm, Rel rel, boolean permitted)
{
Map<FPerm, Set<Rel>> perms = this.getPerms();
Set<Rel> rels = perms.get(perm);
if (permitted)
{
rels.add(rel);
}
else
{
rels.remove(rel);
}
this.setPerms(perms);
}
// -------------------------------------------- //
// OVERRIDE: RelationParticipator
// -------------------------------------------- // // -------------------------------------------- //
@Override @Override
@ -693,7 +732,7 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
} }
// -------------------------------------------- // // -------------------------------------------- //
// FPLAYERS // FOREIGN KEYS: FPLAYERS
// -------------------------------------------- // // -------------------------------------------- //
public List<FPlayer> getFPlayers() public List<FPlayer> getFPlayers()
@ -821,64 +860,40 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
// -------------------------------------------- // // -------------------------------------------- //
// MESSAGES // MESSAGES
// -------------------------------------------- // // -------------------------------------------- //
// These methods are simply proxied in from the SenderEntity class using a for loop. // These methods are simply proxied in from the Mixin.
// CONVENIENCE SEND MESSAGE // CONVENIENCE SEND MESSAGE
public boolean sendMessage(String message) public boolean sendMessage(String message)
{ {
for (FPlayer fplayer : this.getFPlayers()) return Mixin.message(new FactionEqualsPredictate(this), message);
{
fplayer.sendMessage(message);
}
return true;
} }
public boolean sendMessage(String... messages) public boolean sendMessage(String... messages)
{ {
for (FPlayer fplayer : this.getFPlayers()) return Mixin.message(new FactionEqualsPredictate(this), messages);
{
fplayer.sendMessage(messages);
}
return true;
} }
public boolean sendMessage(Collection<String> messages) public boolean sendMessage(Collection<String> messages)
{ {
for (FPlayer fplayer : this.getFPlayers()) return Mixin.message(new FactionEqualsPredictate(this), messages);
{
fplayer.sendMessage(messages);
}
return true;
} }
// CONVENIENCE MSG // CONVENIENCE MSG
public boolean msg(String msg) public boolean msg(String msg)
{ {
for (FPlayer fplayer : this.getFPlayers()) return Mixin.msg(new FactionEqualsPredictate(this), msg);
{
fplayer.msg(msg);
}
return true;
} }
public boolean msg(String msg, Object... args) public boolean msg(String msg, Object... args)
{ {
for (FPlayer fplayer : this.getFPlayers()) return Mixin.msg(new FactionEqualsPredictate(this), msg, args);
{
fplayer.msg(msg, args);
}
return true;
} }
public boolean msg(Collection<String> msgs) public boolean msg(Collection<String> msgs)
{ {
for (FPlayer fplayer : this.getFPlayers()) return Mixin.msg(new FactionEqualsPredictate(this), msgs);
{
fplayer.msg(msgs);
}
return true;
} }
} }

View File

@ -0,0 +1,40 @@
package com.massivecraft.factions;
import java.io.Serializable;
import org.bukkit.command.CommandSender;
import com.massivecraft.mcore.Predictate;
public class FactionEqualsPredictate implements Predictate<CommandSender>, Serializable
{
private static final long serialVersionUID = 1L;
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private final String factionId;
public String getFactionId() { return this.factionId; }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public FactionEqualsPredictate(Faction faction)
{
this.factionId = faction.getId();
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public boolean apply(CommandSender sender)
{
FPlayer fplayer = FPlayer.get(sender);
return this.getFactionId().equals(fplayer.getFactionId());
}
}