Perm rework

This commit is contained in:
Magnus Ulf
2018-12-22 01:55:46 +01:00
parent cee15b6333
commit 595a051a23
16 changed files with 370 additions and 151 deletions

View File

@ -158,7 +158,7 @@ public class Faction extends Entity<Faction> implements FactionsParticipator, MP
// The perm overrides are modifications to the default values.
// Null means default.
private Map<String, Set<String>> perms = new MassiveMap<>();
private Map<String, Set<String>> perms = this.createNewPermMap();
// -------------------------------------------- //
// FIELD: id
@ -751,7 +751,7 @@ public class Faction extends Entity<Faction> implements FactionsParticipator, MP
}
// -------------------------------------------- //
// FIELD: permOverrides
// FIELD: perms
// -------------------------------------------- //
public Map<String, Set<String>> getPerms()
@ -759,6 +759,32 @@ public class Faction extends Entity<Faction> implements FactionsParticipator, MP
return this.perms;
}
public Map<String, Set<String>> createNewPermMap()
{
Map<String, Set<String>> ret = new MassiveMap<>();
var leaderId = this.getRanks().getAll().stream().filter(r -> r.getName().equalsIgnoreCase("leader")).map(Rank::getId).findFirst();
var officerId = this.getRanks().getAll().stream().filter(r -> r.getName().equalsIgnoreCase("officer")).map(Rank::getId).findFirst();
var memberId = this.getRanks().getAll().stream().filter(r -> r.getName().equalsIgnoreCase("member")).map(Rank::getId).findFirst();
var recruitId = this.getRanks().getAll().stream().filter(r -> r.getName().equalsIgnoreCase("recruit")).map(Rank::getId).findAny();
for (var mperm : MPerm.getAll())
{
var id = mperm.getId();
var value = new MassiveSet<>(mperm.getStandard());
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;
}
// IS PERMITTED
public boolean isPlayerPermitted(MPlayer mplayer, String permId)
@ -789,19 +815,30 @@ public class Faction extends Entity<Faction> implements FactionsParticipator, MP
return isFactionPermitted(faction, mperm.getId());
}
@Deprecated
public boolean isPermablePermitted(MPerm.MPermable permable, String permId)
public Set<String> getPermitted(String permId)
{
return isPermitted(permable.getId(), permId);
if (permId == null) throw new NullPointerException("permId");
var permables = this.perms.get(permId);
if (permables == null)
{
// No perms was found, but likely this is just a new MPerm.
// So if this does not exist in the database, throw an error.
if (!doesPermExist(permId)) throw new NullPointerException(permId + " caused null");
permables = new MassiveSet<>();
this.perms.put(permId, permables);
}
return permables;
}
@Deprecated
public boolean isPermablePermitted(MPerm.MPermable permable, MPerm mperm)
public Set<String> getPermitted(MPerm mperm)
{
return isPermablePermitted(permable, mperm.getId());
return getPermitted(mperm.getId());
}
private boolean isPermitted(String permableId, String permId)
public boolean isPermitted(String permableId, String permId)
{
if (permableId == null) throw new NullPointerException("permableId");
if (permId == null) throw new NullPointerException("permId");
@ -817,7 +854,7 @@ public class Faction extends Entity<Faction> implements FactionsParticipator, MP
return false;
}
return permables.contains(permableId);
return getPermitted(permId).contains(permableId);
}
// SET PERMITTED
@ -873,7 +910,6 @@ public class Faction extends Entity<Faction> implements FactionsParticipator, MP
return MPermColl.get().getFixed(permId) != null;
}
// -------------------------------------------- //
// OVERRIDE: RelationParticipator
// -------------------------------------------- //
@ -907,7 +943,20 @@ public class Faction extends Entity<Faction> implements FactionsParticipator, MP
{
return RelationUtil.getColorOfThatToMe(this, observer);
}
// -------------------------------------------- //
// OVERRIDE: permable
// -------------------------------------------- //
@Override
public String getDisplayName(Object senderObject)
{
MPlayer mplayer = MPlayer.get(senderObject);
if (mplayer == null) return this.getName();
return this.describeTo(mplayer);
}
// -------------------------------------------- //
// POWER
// -------------------------------------------- //

View File

@ -280,14 +280,14 @@ public class MConf extends Entity<MConf>
// PERMS
// -------------------------------------------- //
public List<String> defaultPermsEnemy = MUtil.list(MPerm.ID_DEPOSIT);
/*public List<String> defaultPermsEnemy = MUtil.list(MPerm.ID_DEPOSIT);
public List<String> defaultPermsNeutral = MUtil.list(MPerm.ID_DEPOSIT);
public List<String> defaultPermsTruce = MUtil.list(MPerm.ID_DEPOSIT);
public List<String> defaultPermsAlly = MUtil.list(MPerm.ID_DOOR, MPerm.ID_BUTTON, MPerm.ID_LEVER, MPerm.ID_HOME, MPerm.ID_CLAIMNEAR);
public List<String> defaultPermsRecruit = MUtil.list(MPerm.ID_DOOR, MPerm.ID_BUTTON, MPerm.ID_LEVER, MPerm.ID_LEVER, MPerm.ID_HOME, MPerm.ID_CLAIMNEAR);
public List<String> defaultPermsMember = MUtil.list(MPerm.ID_BUILD, MPerm.ID_DOOR, MPerm.ID_BUTTON, MPerm.ID_LEVER, MPerm.ID_LEVER, MPerm.ID_CONTAINER, MPerm.ID_HOME, MPerm.ID_CLAIMNEAR);
public List<String> defaultPermsOfficer = MUtil.list(MPerm.ID_BUILD, MPerm.ID_DOOR, MPerm.ID_BUTTON, MPerm.ID_LEVER, MPerm.ID_LEVER, MPerm.ID_CONTAINER, MPerm.ID_DESC, MPerm.ID_MOTD, MPerm.ID_INVITE, MPerm.ID_KICK, MPerm.ID_RANK, MPerm.ID_TITLE, MPerm.ID_HOME, MPerm.ID_SETHOME, MPerm.ID_TERRITORY, MPerm.ID_ACCESS, MPerm.ID_CLAIMNEAR, MPerm.ID_REL);
public List<String> defaultPermsLeader = MUtil.list(MPerm.ID_BUILD, MPerm.ID_DOOR, MPerm.ID_BUTTON, MPerm.ID_LEVER, MPerm.ID_LEVER, MPerm.ID_CONTAINER, MPerm.ID_NAME, MPerm.ID_DESC, MPerm.ID_MOTD, MPerm.ID_INVITE, MPerm.ID_KICK, MPerm.ID_RANK, MPerm.ID_TITLE, MPerm.ID_HOME, MPerm.ID_SETHOME, MPerm.ID_WITHDRAW, MPerm.ID_TERRITORY, MPerm.ID_ACCESS, MPerm.ID_CLAIMNEAR, MPerm.ID_REL, MPerm.ID_DISBAND, MPerm.ID_FLAGS, MPerm.ID_FLAGS);
public List<String> defaultPermsLeader = MUtil.list(MPerm.ID_BUILD, MPerm.ID_DOOR, MPerm.ID_BUTTON, MPerm.ID_LEVER, MPerm.ID_LEVER, MPerm.ID_CONTAINER, MPerm.ID_NAME, MPerm.ID_DESC, MPerm.ID_MOTD, MPerm.ID_INVITE, MPerm.ID_KICK, MPerm.ID_RANK, MPerm.ID_TITLE, MPerm.ID_HOME, MPerm.ID_SETHOME, MPerm.ID_WITHDRAW, MPerm.ID_TERRITORY, MPerm.ID_ACCESS, MPerm.ID_CLAIMNEAR, MPerm.ID_REL, MPerm.ID_DISBAND, MPerm.ID_FLAGS, MPerm.ID_FLAGS);*/
// -------------------------------------------- //
// TERRITORY INFO

View File

@ -18,7 +18,6 @@ import com.massivecraft.massivecore.ps.PS;
import com.massivecraft.massivecore.store.Entity;
import com.massivecraft.massivecore.util.MUtil;
import com.massivecraft.massivecore.util.Txt;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import java.util.ArrayList;
@ -417,7 +416,7 @@ public class MPerm extends Entity<MPerm> implements Prioritized, Registerable, N
return list;
}
public static String getStateHeaders(Faction faction)
/*public static String getStateHeaders(Faction faction)
{
if (faction == null) throw new NullPointerException("faction");
@ -469,7 +468,7 @@ public class MPerm extends Entity<MPerm> implements Prioritized, Registerable, N
if (withDesc) ret += " <i>" + this.getDesc();
return ret;
}
}*/
public interface MPermable extends Named, Identified
{
@ -488,22 +487,7 @@ public class MPerm extends Entity<MPerm> implements Prioritized, Registerable, N
return this.getName().substring(0, 3).toUpperCase();
}
default ChatColor getColor()
{
if (this.isRelation())
{
throw new RuntimeException();
}
else if (this.isRank())
{
return MConf.get().colorMember;
}
else
{
throw new RuntimeException();
}
}
String getDisplayName(Object senderObject);
}
}

View File

@ -1,6 +1,8 @@
package com.massivecraft.factions.entity;
import com.massivecraft.massivecore.store.EntityInternal;
import com.massivecraft.massivecore.store.EntityInternalMap;
import com.massivecraft.massivecore.util.Txt;
import org.bukkit.ChatColor;
public class Rank extends EntityInternal<Rank> implements MPerm.MPermable
@ -17,6 +19,20 @@ public class Rank extends EntityInternal<Rank> implements MPerm.MPermable
return this;
}
@Override
public void preDetach(String id)
{
for (var f : FactionColl.get().getAll())
{
for (var it = f.getPerms().entrySet().iterator(); it.hasNext();)
{
var entry = it.next();
var value = entry.getValue();
value.remove(id);
}
}
}
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
@ -33,6 +49,13 @@ public class Rank extends EntityInternal<Rank> implements MPerm.MPermable
public String getPrefix() { return this.prefix; }
public void setPrefix(String prefix) { this.prefix = prefix; this.changed(); }
public Faction getFaction()
{
var internalMap = (EntityInternalMap<Rank>) this.getContainer();
var faction = (Faction) internalMap.getEntity();
return faction;
}
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
@ -54,6 +77,14 @@ public class Rank extends EntityInternal<Rank> implements MPerm.MPermable
// VISUAL
// -------------------------------------------- //
@Override
public String getDisplayName(Object senderObject)
{
String ret = this.getVisual();
ret += Txt.parse(" of ") + this.getFaction().getDisplayName(senderObject);
return ret;
}
public String getVisual()
{
String ret = "";