Make default ranks customisable. Close #42

This commit is contained in:
Magnus Ulf 2020-01-22 02:49:04 +01:00
parent abb00e3c81
commit d8139a16b3
3 changed files with 45 additions and 25 deletions

View File

@ -540,15 +540,10 @@ public class Faction extends Entity<Faction> implements FactionsParticipator, MP
private EntityInternalMap<Rank> createRankMap() private EntityInternalMap<Rank> createRankMap()
{ {
EntityInternalMap<Rank> ret = new EntityInternalMap<>(this, Rank.class); EntityInternalMap<Rank> ret = new EntityInternalMap<>(this, Rank.class);
Rank leader = new Rank("Leader", 400, "**");
Rank officer = new Rank("Officer", 300, "*");
Rank member = new Rank("Member", 200, "+");
Rank recruit = new Rank("Recruit", 100, "-");
ret.attach(leader); MConf.get().defaultRanks.stream()
ret.attach(officer); .map(Rank::copy)
ret.attach(member); .forEach(ret::attach);
ret.attach(recruit);
return ret; return ret;
} }
@ -794,29 +789,37 @@ public class Faction extends Entity<Faction> implements FactionsParticipator, MP
public Map<String, Set<String>> createNewPermMap() public Map<String, Set<String>> createNewPermMap()
{ {
Map<String, Set<String>> ret = new MassiveMap<>(); Map<String, Set<String>> ret = new MassiveMap<>();
Optional<String> leaderId = this.getRanks().getAll().stream().filter(r -> r.getName().equalsIgnoreCase("leader")).map(Rank::getId).findFirst();
Optional<String> officerId = this.getRanks().getAll().stream().filter(r -> r.getName().equalsIgnoreCase("officer")).map(Rank::getId).findFirst();
Optional<String> memberId = this.getRanks().getAll().stream().filter(r -> r.getName().equalsIgnoreCase("member")).map(Rank::getId).findFirst();
Optional<String> recruitId = this.getRanks().getAll().stream().filter(r -> r.getName().equalsIgnoreCase("recruit")).map(Rank::getId).findAny();
for (MPerm mperm : MPerm.getAll()) for (MPerm mperm : MPerm.getAll())
{ {
String id = mperm.getId(); Set<String> granteds = mperm2granteds(mperm);
MassiveSet<String> value = new MassiveSet<>(MConf.get().perm2default.get(id)); ret.put(mperm.getId(), granteds);
if (value.remove("LEADER") && leaderId.isPresent()) value.add(leaderId.get());
if (value.remove("OFFICER") && officerId.isPresent()) value.add(officerId.get());
if (value.remove("MEMBER") && memberId.isPresent()) value.add(memberId.get());
if (value.remove("RECRUIT") && recruitId.isPresent()) value.add(recruitId.get());
ret.put(mperm.getId(), value);
} }
return ret; return ret;
} }
private Set<String> mperm2granteds(MPerm mperm)
{
String permId = mperm.getId();
Set<String> value = new MassiveSet<>(MConf.get().perm2default.get(permId));
Set<String> additions = new MassiveSet<>();
outer:
for (Iterator<String> it = value.iterator(); it.hasNext();)
{
String granted = it.next();
for (Rank rank : this.getRanks().getAll())
{
if (granted.equalsIgnoreCase(rank.getName()))
{
it.remove();
additions.add(rank.getId());
continue outer;
}
}
}
value.addAll(additions);
return value;
}
// IS PERMITTED // IS PERMITTED

View File

@ -537,6 +537,18 @@ public class MConf extends Entity<MConf>
// How often should the task be run? // How often should the task be run?
public long taxTaskPeriodMillis = TimeUnit.MILLIS_PER_DAY; public long taxTaskPeriodMillis = TimeUnit.MILLIS_PER_DAY;
// -------------------------------------------- //
// RANKS
// -------------------------------------------- //
@EditorVisible(false)
public List<Rank> defaultRanks = MUtil.list(
new Rank("Leader", 400, "**"),
new Rank("Officer", 300, "*"),
new Rank("Member", 200, "+"),
new Rank("Recruit", 100, "-")
);
// -------------------------------------------- // // -------------------------------------------- //
// PERMISSIONS // PERMISSIONS
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -75,6 +75,11 @@ public class Rank extends EntityInternal<Rank> implements MPerm.MPermable
this.prefix = prefix; this.prefix = prefix;
} }
public Rank copy()
{
return new Rank(this.name, this.priority, this.prefix);
}
// -------------------------------------------- // // -------------------------------------------- //
// VISUAL // VISUAL
// -------------------------------------------- // // -------------------------------------------- //