From 0efccf335c60b507627e14dfde9a882608b75455 Mon Sep 17 00:00:00 2001 From: Magnus Ulf Date: Thu, 20 Dec 2018 18:12:16 +0100 Subject: [PATCH] Change permission checking in preparation for permission rework --- .../factions/engine/EngineChunkChange.java | 3 +- .../massivecraft/factions/entity/Faction.java | 59 +++++++++++++++---- .../massivecraft/factions/entity/MPerm.java | 10 ++-- .../massivecraft/factions/entity/MPlayer.java | 42 ++++++------- 4 files changed, 69 insertions(+), 45 deletions(-) diff --git a/src/com/massivecraft/factions/engine/EngineChunkChange.java b/src/com/massivecraft/factions/engine/EngineChunkChange.java index 05283a4e..2cc5bea3 100644 --- a/src/com/massivecraft/factions/engine/EngineChunkChange.java +++ b/src/com/massivecraft/factions/engine/EngineChunkChange.java @@ -170,8 +170,7 @@ public class EngineChunkChange extends Engine MPerm claimnear = MPerm.getPermClaimnear(); for (Faction nearbyFaction : nearbyFactions) { - if (claimnear.has(newFaction, nearbyFaction)) continue; - mplayer.message(claimnear.createDeniedMessage(mplayer, nearbyFaction)); + if (claimnear.has(mplayer, nearbyFaction, true)) continue; event.setCancelled(true); return; } diff --git a/src/com/massivecraft/factions/entity/Faction.java b/src/com/massivecraft/factions/entity/Faction.java index b90f742c..8c7e4091 100644 --- a/src/com/massivecraft/factions/entity/Faction.java +++ b/src/com/massivecraft/factions/entity/Faction.java @@ -39,7 +39,7 @@ import java.util.Map; import java.util.Set; import java.util.stream.Collectors; -public class Faction extends Entity implements FactionsParticipator +public class Faction extends Entity implements FactionsParticipator, MPerm.MPermable { // -------------------------------------------- // // CONSTANTS @@ -761,13 +761,53 @@ public class Faction extends Entity implements FactionsParticipator // IS PERMITTED - public boolean isPermitted(MPerm.MPermable mpermable, String permId) + public boolean isPlayerPermitted(MPlayer mplayer, String permId) { - if (mpermable == null) throw new NullPointerException("mpermable"); + if (isPermitted(mplayer.getId(), permId)) return true; + if (isPermitted(mplayer.getFaction().getId(), permId)) return true; + if (isPermitted(mplayer.getRank().getId(), permId)) return true; + if (isPermitted(RelationUtil.getRelationOfThatToMe(mplayer, this).toString(), permId)) return true; + + return false; + } + + public boolean isPlayerPermitted(MPlayer mplayer, MPerm mperm) + { + return isPlayerPermitted(mplayer, mperm.getId()); + } + + public boolean isFactionPermitted(Faction faction, String permId) + { + if (isPermitted(faction.getId(), permId)) return true; + if (isPermitted(RelationUtil.getRelationOfThatToMe(faction, this).toString(), permId)) return true; + + return false; + } + + public boolean isFactionPermitted(Faction faction, MPerm mperm) + { + return isFactionPermitted(faction, mperm.getId()); + } + + @Deprecated + public boolean isPermablePermitted(MPerm.MPermable permable, String permId) + { + return isPermitted(permable.getId(), permId); + } + + @Deprecated + public boolean isPermablePermitted(MPerm.MPermable permable, MPerm mperm) + { + return isPermablePermitted(permable, mperm.getId()); + } + + private boolean isPermitted(String permableId, String permId) + { + if (permableId == null) throw new NullPointerException("permableId"); if (permId == null) throw new NullPointerException("permId"); - var perms = this.perms.get(permId); - if (perms == null) + 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. @@ -777,14 +817,7 @@ public class Faction extends Entity implements FactionsParticipator return false; } - return perms.contains(mpermable.getId()); - } - - public boolean isPermitted(MPerm.MPermable mpermable, MPerm mperm) - { - if (mpermable == null) throw new NullPointerException("mpermable"); - if (mperm == null) throw new NullPointerException("mperm"); - return isPermitted(mpermable, mperm.getId()); + return permables.contains(permableId); } // SET PERMITTED diff --git a/src/com/massivecraft/factions/entity/MPerm.java b/src/com/massivecraft/factions/entity/MPerm.java index e5a96a32..2e9e063e 100644 --- a/src/com/massivecraft/factions/entity/MPerm.java +++ b/src/com/massivecraft/factions/entity/MPerm.java @@ -350,9 +350,8 @@ public class MPerm extends Entity implements Prioritized, Registerable, N // Null Check if (faction == null) throw new NullPointerException("faction"); if (hostFaction == null) throw new NullPointerException("hostFaction"); - - Rel rel = faction.getRelationTo(hostFaction); - return hostFaction.isPermitted(rel, this); + + return hostFaction.isFactionPermitted(faction, this); } public boolean has(MPlayer mplayer, Faction hostFaction, boolean verboose) @@ -365,7 +364,7 @@ public class MPerm extends Entity implements Prioritized, Registerable, N Rel rel = mplayer.getRelationTo(hostFaction); MPermable permable = rel == Rel.FACTION ? mplayer.getRank() : rel; - if (hostFaction.isPermitted(permable, this)) return true; + if (hostFaction.isPlayerPermitted(mplayer, this)) return true; if (verboose) mplayer.message(this.createDeniedMessage(mplayer, hostFaction)); @@ -441,7 +440,7 @@ public class MPerm extends Entity implements Prioritized, Registerable, N for (MPermable permable : getPermables(faction)) { - if (faction.isPermitted(permable, this)) + if (faction.isPermablePermitted(permable, this)) { ret += "YES"; } @@ -504,6 +503,7 @@ public class MPerm extends Entity implements Prioritized, Registerable, N throw new RuntimeException(); } } + } } diff --git a/src/com/massivecraft/factions/entity/MPlayer.java b/src/com/massivecraft/factions/entity/MPlayer.java index a4713cb1..99970865 100644 --- a/src/com/massivecraft/factions/entity/MPlayer.java +++ b/src/com/massivecraft/factions/entity/MPlayer.java @@ -33,20 +33,18 @@ import java.util.Iterator; import java.util.Map.Entry; import java.util.Set; -public class MPlayer extends SenderEntity implements FactionsParticipator -{ - // -------------------------------------------- // - // META - // -------------------------------------------- // - - public static final transient String NOTITLE = Txt.parse("no title set"); - +public class MPlayer extends SenderEntity implements FactionsParticipator, MPerm.MPermable { // -------------------------------------------- // // META // -------------------------------------------- // - public static MPlayer get(Object oid) - { + public static final transient String NOTITLE = Txt.parse("no title set"); + + // -------------------------------------------- // + // META + // -------------------------------------------- // + + public static MPlayer get(Object oid) { return MPlayerColl.get().get(oid); } @@ -61,8 +59,7 @@ public class MPlayer extends SenderEntity implements FactionsParticipat // -------------------------------------------- // @Override - public MPlayer load(MPlayer that) - { + public MPlayer load(MPlayer that) { this.setLastActivityMillis(that.lastActivityMillis); this.setFactionId(that.factionId); this.rankId = that.rankId; @@ -75,14 +72,13 @@ public class MPlayer extends SenderEntity implements FactionsParticipat return this; } - + // -------------------------------------------- // // IS DEFAULT // -------------------------------------------- // @Override - public boolean isDefault() - { + public boolean isDefault() { // Last activity millis is data we use for clearing out cleanable players. So it does not in itself make the player data worth keeping. if (this.hasFaction()) return false; // Role means nothing without a faction. @@ -101,25 +97,21 @@ public class MPlayer extends SenderEntity implements FactionsParticipat // -------------------------------------------- // @Override - public void postAttach(String id) - { + public void postAttach(String id) { FactionsIndex.get().update(this); } @Override - public void preDetach(String id) - { + public void preDetach(String id) { FactionsIndex.get().update(this); } - + @Override - public void preClean() - { - if (this.getRank().isLeader()) - { + public void preClean() { + if (this.getRank().isLeader()) { this.getFaction().promoteNewLeader(); } - + this.leave(); }