From d8139a16b36fc10dd810be63c073ceff6a9a2293 Mon Sep 17 00:00:00 2001 From: Magnus Ulf Date: Wed, 22 Jan 2020 02:49:04 +0100 Subject: [PATCH] Make default ranks customisable. Close #42 --- .../massivecraft/factions/entity/Faction.java | 51 ++++++++++--------- .../massivecraft/factions/entity/MConf.java | 14 ++++- .../massivecraft/factions/entity/Rank.java | 5 ++ 3 files changed, 45 insertions(+), 25 deletions(-) diff --git a/src/com/massivecraft/factions/entity/Faction.java b/src/com/massivecraft/factions/entity/Faction.java index a3eeab3b..06246552 100644 --- a/src/com/massivecraft/factions/entity/Faction.java +++ b/src/com/massivecraft/factions/entity/Faction.java @@ -540,15 +540,10 @@ public class Faction extends Entity implements FactionsParticipator, MP private EntityInternalMap createRankMap() { EntityInternalMap 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); - ret.attach(officer); - ret.attach(member); - ret.attach(recruit); + MConf.get().defaultRanks.stream() + .map(Rank::copy) + .forEach(ret::attach); return ret; } @@ -794,29 +789,37 @@ public class Faction extends Entity implements FactionsParticipator, MP public Map> createNewPermMap() { Map> ret = new MassiveMap<>(); - - Optional leaderId = this.getRanks().getAll().stream().filter(r -> r.getName().equalsIgnoreCase("leader")).map(Rank::getId).findFirst(); - Optional officerId = this.getRanks().getAll().stream().filter(r -> r.getName().equalsIgnoreCase("officer")).map(Rank::getId).findFirst(); - Optional memberId = this.getRanks().getAll().stream().filter(r -> r.getName().equalsIgnoreCase("member")).map(Rank::getId).findFirst(); - Optional recruitId = this.getRanks().getAll().stream().filter(r -> r.getName().equalsIgnoreCase("recruit")).map(Rank::getId).findAny(); - for (MPerm mperm : MPerm.getAll()) { - String id = mperm.getId(); + Set granteds = mperm2granteds(mperm); - MassiveSet value = new MassiveSet<>(MConf.get().perm2default.get(id)); - - 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); + ret.put(mperm.getId(), granteds); } - return ret; } + private Set mperm2granteds(MPerm mperm) + { + String permId = mperm.getId(); + Set value = new MassiveSet<>(MConf.get().perm2default.get(permId)); + Set additions = new MassiveSet<>(); + outer: + for (Iterator 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 diff --git a/src/com/massivecraft/factions/entity/MConf.java b/src/com/massivecraft/factions/entity/MConf.java index ffa532c6..26f52288 100644 --- a/src/com/massivecraft/factions/entity/MConf.java +++ b/src/com/massivecraft/factions/entity/MConf.java @@ -536,7 +536,19 @@ public class MConf extends Entity // How often should the task be run? public long taxTaskPeriodMillis = TimeUnit.MILLIS_PER_DAY; - + + // -------------------------------------------- // + // RANKS + // -------------------------------------------- // + + @EditorVisible(false) + public List defaultRanks = MUtil.list( + new Rank("Leader", 400, "**"), + new Rank("Officer", 300, "*"), + new Rank("Member", 200, "+"), + new Rank("Recruit", 100, "-") + ); + // -------------------------------------------- // // PERMISSIONS // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/entity/Rank.java b/src/com/massivecraft/factions/entity/Rank.java index ae25f900..68a589b4 100644 --- a/src/com/massivecraft/factions/entity/Rank.java +++ b/src/com/massivecraft/factions/entity/Rank.java @@ -75,6 +75,11 @@ public class Rank extends EntityInternal implements MPerm.MPermable this.prefix = prefix; } + public Rank copy() + { + return new Rank(this.name, this.priority, this.prefix); + } + // -------------------------------------------- // // VISUAL // -------------------------------------------- //