From ae442c677b02a5ec594a9c7318b3e5e4b61a1e42 Mon Sep 17 00:00:00 2001 From: Magnus Ulf Date: Mon, 15 Apr 2019 13:55:20 +0200 Subject: [PATCH] New access commands --- plugin.yml | 39 ++++++-- src/com/massivecraft/factions/Perm.java | 9 ++ .../factions/TerritoryAccess.java | 5 + .../factions/cmd/CmdFactionsAccess.java | 5 +- .../cmd/CmdFactionsAccessAbstract.java | 72 +++++++++----- .../factions/cmd/CmdFactionsAccessDeny.java | 26 +++++ .../cmd/CmdFactionsAccessFaction.java | 45 --------- .../factions/cmd/CmdFactionsAccessGrant.java | 26 +++++ .../cmd/CmdFactionsAccessInspect.java | 96 +++++++++++++++++++ .../factions/cmd/CmdFactionsAccessPlayer.java | 45 --------- .../cmd/CmdFactionsAccessSetCircle.java | 45 +++++++++ .../cmd/CmdFactionsAccessSetFill.java | 57 +++++++++++ .../factions/cmd/CmdFactionsAccessSetOne.java | 44 +++++++++ .../cmd/CmdFactionsAccessSetSquare.java | 45 +++++++++ .../factions/cmd/CmdFactionsAccessSetX.java | 68 +++++++++++++ .../cmd/CmdFactionsAccessSetXRadius.java | 51 ++++++++++ .../cmd/CmdFactionsAccessSetXSimple.java | 21 ++++ .../factions/cmd/CmdFactionsAccessView.java | 2 +- .../factions/cmd/CmdFactionsSetCircle.java | 25 +---- .../factions/cmd/CmdFactionsSetFill.java | 66 ++----------- .../factions/cmd/CmdFactionsSetSquare.java | 22 +---- .../factions/cmd/CmdFactionsSetXRadius.java | 6 -- 22 files changed, 589 insertions(+), 231 deletions(-) create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsAccessDeny.java delete mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsAccessFaction.java create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsAccessGrant.java create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsAccessInspect.java delete mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsAccessPlayer.java create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsAccessSetCircle.java create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsAccessSetFill.java create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsAccessSetOne.java create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsAccessSetSquare.java create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsAccessSetX.java create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsAccessSetXRadius.java create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsAccessSetXSimple.java diff --git a/plugin.yml b/plugin.yml index 1e365be4..7a0641b6 100644 --- a/plugin.yml +++ b/plugin.yml @@ -12,8 +12,17 @@ permissions: # THE REAL NODES # -------------------------------------------- # factions.access: {description: manage access, with the proper fperm, default: false} - factions.access.faction: {description: grant faction access, with the proper fperm, default: false} - factions.access.player: {description: grant player access, with the proper fperm, default: false} + factions.access.deny: {description: deny faction access, default: false} + factions.access.deny.one: {description: deny access in a single chunk, default: false} + factions.access.deny.fill: {description: deny access by filling, default: false} + factions.access.deny.square: {description: deny access by square and radius, default: false} + factions.access.deny.circle: {description: deny access by circle and radius, default: false} + factions.access.grant: {description: grant faction access, default: false} + factions.access.grant.one: {description: grant access in a single chunk, default: false} + factions.access.grant.fill: {description: grant access by filling, default: false} + factions.access.grant.square: {description: grant access by square and radius, default: false} + factions.access.grant.circle: {description: grant access by circle and radius, default: false} + factions.access.inspect: {description: inspect where someone has access, default: false} factions.access.view: {description: view access, default: false} factions.override: {description: enable override mode, default: false} factions.basecommand: {description: use factions base command, default: false} @@ -134,8 +143,17 @@ permissions: factions.*: children: factions.access: true - factions.access.faction: true - factions.access.player: true + factions.access.deny: true + factions.access.deny.one: true + factions.access.deny.fill: true + factions.access.deny.square: true + factions.access.deny.circle: true + factions.access.grant: true + factions.access.grant.one: true + factions.access.grant.fill: true + factions.access.grant.square: true + factions.access.grant.circle: true + factions.access.inspect: true factions.access.view: true factions.override: true factions.basecommand: true @@ -301,8 +319,17 @@ permissions: default: false children: factions.access: true - factions.access.faction: true - factions.access.player: true + factions.access.deny: true + factions.access.deny.one: true + factions.access.deny.fill: true + factions.access.deny.square: true + factions.access.deny.circle: true + factions.access.grant: true + factions.access.grant.one: true + factions.access.grant.fill: true + factions.access.grant.square: true + factions.access.grant.circle: true + factions.access.inspect: true factions.access.view: true factions.basecommand: true factions.claim: true diff --git a/src/com/massivecraft/factions/Perm.java b/src/com/massivecraft/factions/Perm.java index d20397ac..718f4da3 100644 --- a/src/com/massivecraft/factions/Perm.java +++ b/src/com/massivecraft/factions/Perm.java @@ -11,6 +11,15 @@ public enum Perm implements Identified // ENUM // -------------------------------------------- // + ACCESS_GRANT_ONE, + ACCESS_GRANT_FILL, + ACCESS_GRANT_SQUARE, + ACCESS_GRANT_CIRCLE, + ACCESS_DENY_ONE, + ACCESS_DENY_FILL, + ACCESS_DENY_SQUARE, + ACCESS_DENY_CIRCLE, + CLAIM_ONE, CLAIM_AUTO, CLAIM_FILL, diff --git a/src/com/massivecraft/factions/TerritoryAccess.java b/src/com/massivecraft/factions/TerritoryAccess.java index 6e4dbca1..81a41fd8 100644 --- a/src/com/massivecraft/factions/TerritoryAccess.java +++ b/src/com/massivecraft/factions/TerritoryAccess.java @@ -45,6 +45,11 @@ public class TerritoryAccess public TerritoryAccess withGrantedIds(Collection factionIds) { return valueOf(hostFactionId, hostFactionAllowed, grantedIds); } // The intermediate ones + public TerritoryAccess withGranted(MPermable mpermable, boolean with) + { + return withGrantedId(mpermable.getId(), with); + } + public TerritoryAccess withGrantedId(String grantedId, boolean with) { if (this.getHostFactionId().equals(grantedId)) diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsAccess.java b/src/com/massivecraft/factions/cmd/CmdFactionsAccess.java index ebc64ba4..2823f637 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsAccess.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsAccess.java @@ -9,8 +9,9 @@ public class CmdFactionsAccess extends FactionsCommand // -------------------------------------------- // public CmdFactionsAccessView cmdFactionsAccessView = new CmdFactionsAccessView(); - public CmdFactionsAccessPlayer cmdFactionsAccessPlayer = new CmdFactionsAccessPlayer(); - public CmdFactionsAccessFaction cmdFactionsAccessFaction = new CmdFactionsAccessFaction(); + public CmdFactionsAccessGrant cmdFactionsAccessGrant = new CmdFactionsAccessGrant(); + public CmdFactionsAccessDeny cmdFactionsAccessDeny = new CmdFactionsAccessDeny(); + public CmdFactionsAccessInspect cmdFactionsAccessInspect = new CmdFactionsAccessInspect(); // -------------------------------------------- // // CONSTRUCT diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsAccessAbstract.java b/src/com/massivecraft/factions/cmd/CmdFactionsAccessAbstract.java index 798b15b2..ba58a6f6 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsAccessAbstract.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsAccessAbstract.java @@ -1,18 +1,16 @@ package com.massivecraft.factions.cmd; -import com.massivecraft.factions.RelationParticipator; import com.massivecraft.factions.TerritoryAccess; import com.massivecraft.factions.entity.BoardColl; import com.massivecraft.factions.entity.Faction; -import com.massivecraft.massivecore.MassiveException; +import com.massivecraft.factions.entity.MPerm; +import com.massivecraft.factions.entity.MPerm.MPermable; import com.massivecraft.massivecore.command.requirement.RequirementIsPlayer; import com.massivecraft.massivecore.ps.PS; import com.massivecraft.massivecore.ps.PSFormatHumanSpace; import com.massivecraft.massivecore.util.Txt; -import java.util.ArrayList; import java.util.Collection; -import java.util.List; public abstract class CmdFactionsAccessAbstract extends FactionsCommand @@ -38,18 +36,25 @@ public abstract class CmdFactionsAccessAbstract extends FactionsCommand // -------------------------------------------- // // OVERRIDE // -------------------------------------------- // - + @Override - public void perform() throws MassiveException + public void senderFields(boolean set) { - chunk = PS.valueOf(me.getLocation()).getChunk(true); - ta = BoardColl.get().getTerritoryAccessAt(chunk); - hostFaction = ta.getHostFaction(); - - this.innerPerform(); + super.senderFields(set); + + if (set) + { + chunk = PS.valueOf(me.getLocation()).getChunk(true); + ta = BoardColl.get().getTerritoryAccessAt(chunk); + hostFaction = ta.getHostFaction(); + } + else + { + chunk = null; + ta = null; + hostFaction = null; + } } - - public abstract void innerPerform() throws MassiveException; public void sendAccessInfo() { @@ -61,16 +66,37 @@ public abstract class CmdFactionsAccessAbstract extends FactionsCommand msg("Host Faction Allowed: %s", ta.isHostFactionAllowed() ? Txt.parse("TRUE") : Txt.parse("FALSE")); msg("Granted to: %s", CmdFactionsPermShow.permablesToDisplayString(ta.getGranteds(), msender)); } - - public static String describeRelationParticipators(Collection relationParticipators, RelationParticipator observer) + + public void setAccess(Collection chunks, MPermable mpermable, boolean granted) { - if (relationParticipators.size() == 0) return Txt.parse("none"); - List descriptions = new ArrayList<>(); - for (RelationParticipator relationParticipator : relationParticipators) - { - descriptions.add(relationParticipator.describeTo(observer)); - } - return Txt.implodeCommaAnd(descriptions, Txt.parse(", "), Txt.parse(" and ")); + chunks.forEach(chunk -> setAccess(chunk, mpermable, granted)); } - + + public void setAccess(PS chunk, MPermable mpermable, boolean granted) + { + TerritoryAccess ta = BoardColl.get().getTerritoryAccessAt(chunk); + Faction faction = ta.getHostFaction(); + + String chunkDesc = chunk.toString(PSFormatHumanSpace.get()); + String grantedDenied = granted ? "granted" : "denied"; + String mpermableDesc = mpermable.getDisplayName(msender); + + if ( ! MPerm.getPermAccess().has(msender, faction, false)) + { + msg("You do not have permission to edit access at %s.", chunkDesc); + return; + } + + if (ta.isGranted(mpermable) == granted) + { + msg("Access at %s is already %s to %s.", chunkDesc, grantedDenied, mpermableDesc); + return; + } + + ta = ta.withGranted(mpermable, granted); + BoardColl.get().setTerritoryAccessAt(chunk, ta); + + msg("Access at %s is now %s to %s.", chunkDesc, grantedDenied, mpermableDesc); + } + } diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsAccessDeny.java b/src/com/massivecraft/factions/cmd/CmdFactionsAccessDeny.java new file mode 100644 index 00000000..8a4b49fa --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsAccessDeny.java @@ -0,0 +1,26 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.massivecore.command.requirement.RequirementIsPlayer; + +public class CmdFactionsAccessDeny extends CmdFactionsAccessAbstract +{ + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + public CmdFactionsAccessSetOne cmdFactionsAccessDenyOne = new CmdFactionsAccessSetOne(false); + public CmdFactionsAccessSetSquare cmdFactionsAccessDenySquare = new CmdFactionsAccessSetSquare(false); + public CmdFactionsAccessSetCircle cmdFactionsAccessDenyCircle = new CmdFactionsAccessSetCircle(false); + public CmdFactionsAccessSetFill cmdFactionsAccessDenyFill = new CmdFactionsAccessSetFill(false); + + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public CmdFactionsAccessDeny() + { + // Requirements + this.addRequirements(RequirementIsPlayer.get()); + } + +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsAccessFaction.java b/src/com/massivecraft/factions/cmd/CmdFactionsAccessFaction.java deleted file mode 100644 index 8a8103ce..00000000 --- a/src/com/massivecraft/factions/cmd/CmdFactionsAccessFaction.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.massivecraft.factions.cmd; - -import com.massivecraft.factions.cmd.type.TypeFaction; -import com.massivecraft.factions.entity.BoardColl; -import com.massivecraft.factions.entity.Faction; -import com.massivecraft.factions.entity.MPerm; -import com.massivecraft.massivecore.MassiveException; -import com.massivecraft.massivecore.command.type.primitive.TypeBooleanYes; - -public class CmdFactionsAccessFaction extends CmdFactionsAccessAbstract -{ - // -------------------------------------------- // - // CONSTRUCT - // -------------------------------------------- // - - public CmdFactionsAccessFaction() - { - // Parameters - this.addParameter(TypeFaction.get(), "faction"); - this.addParameter(TypeBooleanYes.get(), "yes/no", "toggle"); - } - - // -------------------------------------------- // - // OVERRIDE - // -------------------------------------------- // - - @Override - public void innerPerform() throws MassiveException - { - // Args - Faction faction = this.readArg(); - boolean newValue = this.readArg(!ta.isGranted(faction)); - - // MPerm - if (!MPerm.getPermAccess().has(msender, hostFaction, true)) return; - - // Apply - ta = ta.withGrantedId(faction.getId(), newValue); - BoardColl.get().setTerritoryAccessAt(chunk, ta); - - // Inform - this.sendAccessInfo(); - } - -} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsAccessGrant.java b/src/com/massivecraft/factions/cmd/CmdFactionsAccessGrant.java new file mode 100644 index 00000000..979ba253 --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsAccessGrant.java @@ -0,0 +1,26 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.massivecore.command.requirement.RequirementIsPlayer; + +public class CmdFactionsAccessGrant extends CmdFactionsAccessAbstract +{ + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + public CmdFactionsAccessSetOne cmdFactionsAccessGrantOne = new CmdFactionsAccessSetOne(true); + public CmdFactionsAccessSetSquare cmdFactionsAccessGrantSquare = new CmdFactionsAccessSetSquare(true); + public CmdFactionsAccessSetCircle cmdFactionsAccessGrantCircle = new CmdFactionsAccessSetCircle(true); + public CmdFactionsAccessSetFill cmdFactionsAccessGrantFill = new CmdFactionsAccessSetFill(true); + + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public CmdFactionsAccessGrant() + { + // Requirements + this.addRequirements(RequirementIsPlayer.get()); + } + +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsAccessInspect.java b/src/com/massivecraft/factions/cmd/CmdFactionsAccessInspect.java new file mode 100644 index 00000000..102f88ba --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsAccessInspect.java @@ -0,0 +1,96 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.factions.cmd.type.TypeFaction; +import com.massivecraft.factions.cmd.type.TypeMPermable; +import com.massivecraft.factions.entity.Board; +import com.massivecraft.factions.entity.BoardColl; +import com.massivecraft.factions.entity.Faction; +import com.massivecraft.factions.entity.MPerm; +import com.massivecraft.factions.entity.MPerm.MPermable; +import com.massivecraft.massivecore.MassiveException; +import com.massivecraft.massivecore.collections.MassiveMap; +import com.massivecraft.massivecore.mixin.MixinWorld; +import com.massivecraft.massivecore.ps.PS; +import com.massivecraft.massivecore.ps.PSFormatHumanSpace; +import com.massivecraft.massivecore.util.Txt; + +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.stream.Collectors; + +public class CmdFactionsAccessInspect extends FactionsCommand +{ + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public CmdFactionsAccessInspect() + { + // Parameters + this.addParameter(TypeMPermable.get(), "rank/rel/player/faction"); + this.addParameter(TypeFaction.get(), "faction", "your"); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public void perform() throws MassiveException + { + // Parameter + Faction faction = this.readArgAt(1, msenderFaction); + MPermable mpermable = TypeMPermable.get(faction).read(this.argAt(0), sender); + + String factionId = faction.getId(); + + // Check if they have access perms, unless they are checking for their own access + if (mpermable != msender && mpermable != msenderFaction && mpermable != msender.getRank()); + { + if ( ! MPerm.getPermAccess().has(msender, faction, true)) return; + } + + // Turn into id->chunks + // And filter the ones that are empty + Map> world2Chunks = new MassiveMap<>(); + for (Board board : BoardColl.get().getAll()) + { + String worldId = board.getId(); + Set chunks = board.getMap().entrySet().stream() + .filter(e -> e.getValue().getHostFactionId().equals(factionId)) + .filter(e -> e.getValue().isGranted(mpermable)) + .map(Entry::getKey) + .collect(Collectors.toSet()); + if ( ! chunks.isEmpty()) world2Chunks.put(worldId, chunks); + } + + if (world2Chunks.isEmpty()) + { + msg("%s has no special access in %s .", mpermable.getDisplayName(msender), faction.describeTo(msender)); + return; + } + + msg("%s has special access in %s in the following chunks:", mpermable.getDisplayName(msender), faction.describeTo(msender)); + + for (Entry> entry : world2Chunks.entrySet()) + { + String worldId = entry.getKey(); + Set chunks = entry.getValue(); + + String worldName = MixinWorld.get().getWorldDisplayName(worldId); + + // Remove world from chunks + List chunkNames = chunks.stream() + .map(PS::getChunkCoords) + .map(PSFormatHumanSpace.get()::format) + .collect(Collectors.toList()); + + String chunkDesc = Txt.implodeCommaAnd(chunkNames, Txt.parse("")); + + msg("%s (%d): %s", worldName, chunks.size(), chunkDesc); + } + } + +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsAccessPlayer.java b/src/com/massivecraft/factions/cmd/CmdFactionsAccessPlayer.java deleted file mode 100644 index 04698fbf..00000000 --- a/src/com/massivecraft/factions/cmd/CmdFactionsAccessPlayer.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.massivecraft.factions.cmd; - -import com.massivecraft.factions.cmd.type.TypeMPlayer; -import com.massivecraft.factions.entity.BoardColl; -import com.massivecraft.factions.entity.MPerm; -import com.massivecraft.factions.entity.MPlayer; -import com.massivecraft.massivecore.MassiveException; -import com.massivecraft.massivecore.command.type.primitive.TypeBooleanYes; - -public class CmdFactionsAccessPlayer extends CmdFactionsAccessAbstract -{ - // -------------------------------------------- // - // CONSTRUCT - // -------------------------------------------- // - - public CmdFactionsAccessPlayer() - { - // Parameters - this.addParameter(TypeMPlayer.get(), "player"); - this.addParameter(TypeBooleanYes.get(), "yes/no", "toggle"); - } - - // -------------------------------------------- // - // OVERRIDE - // -------------------------------------------- // - - @Override - public void innerPerform() throws MassiveException - { - // Args - MPlayer mplayer = this.readArg(); - boolean newValue = this.readArg(!ta.isGranted(mplayer)); - - // MPerm - if (!MPerm.getPermAccess().has(msender, hostFaction, true)) return; - - // Apply - ta = ta.withGrantedId(mplayer.getId(), newValue); - BoardColl.get().setTerritoryAccessAt(chunk, ta); - - // Inform - this.sendAccessInfo(); - } - -} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsAccessSetCircle.java b/src/com/massivecraft/factions/cmd/CmdFactionsAccessSetCircle.java new file mode 100644 index 00000000..339d4a16 --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsAccessSetCircle.java @@ -0,0 +1,45 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.factions.Perm; +import com.massivecraft.massivecore.MassiveException; +import com.massivecraft.massivecore.command.requirement.RequirementHasPerm; +import com.massivecraft.massivecore.command.requirement.RequirementIsPlayer; +import com.massivecraft.massivecore.ps.PS; +import com.massivecraft.massivecore.util.ChunkUtil; + +import java.util.Set; + + +public class CmdFactionsAccessSetCircle extends CmdFactionsAccessSetXRadius +{ + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public CmdFactionsAccessSetCircle(boolean grant) + { + // Super + super(grant); + + // Aliases + this.addAliases("circle"); + + // Requirements + this.addRequirements(RequirementIsPlayer.get()); + Perm perm = grant ? Perm.ACCESS_GRANT_CIRCLE : Perm.ACCESS_DENY_CIRCLE; + this.addRequirements(RequirementHasPerm.get(perm)); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public Set getChunks() throws MassiveException + { + // Common Startup + final PS chunk = PS.valueOf(me.getLocation()).getChunk(true); + return ChunkUtil.getChunksCircle(chunk, this.getRadius()); + } + +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsAccessSetFill.java b/src/com/massivecraft/factions/cmd/CmdFactionsAccessSetFill.java new file mode 100644 index 00000000..25dd1803 --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsAccessSetFill.java @@ -0,0 +1,57 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.factions.Perm; +import com.massivecraft.factions.entity.BoardColl; +import com.massivecraft.factions.entity.Faction; +import com.massivecraft.factions.entity.MConf; +import com.massivecraft.massivecore.MassiveException; +import com.massivecraft.massivecore.command.requirement.RequirementHasPerm; +import com.massivecraft.massivecore.command.requirement.RequirementIsPlayer; +import com.massivecraft.massivecore.predicate.Predicate; +import com.massivecraft.massivecore.ps.PS; +import com.massivecraft.massivecore.util.ChunkUtil; + +import java.util.Set; + + +public class CmdFactionsAccessSetFill extends CmdFactionsAccessSetXSimple +{ + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public CmdFactionsAccessSetFill(boolean claim) + { + // Super + super(claim); + + // Aliases + this.addAliases("fill"); + + // Requirements + this.addRequirements(RequirementIsPlayer.get()); + Perm perm = claim ? Perm.ACCESS_GRANT_FILL : Perm.ACCESS_DENY_FILL; + this.addRequirements(RequirementHasPerm.get(perm)); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public Set getChunks() throws MassiveException + { + // Common Startup + final PS chunk = PS.valueOf(me.getLocation()).getChunk(true); + + // What faction (aka color) resides there? + // NOTE: Wilderness/None is valid. + final Faction color = BoardColl.get().getFactionAt(chunk); + + // Calculate + int max = MConf.get().setFillMax; + Predicate matcher = ps -> BoardColl.get().getFactionAt(ps) == color; + return ChunkUtil.getChunkArea(chunk, matcher, max); + } + +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsAccessSetOne.java b/src/com/massivecraft/factions/cmd/CmdFactionsAccessSetOne.java new file mode 100644 index 00000000..670496d2 --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsAccessSetOne.java @@ -0,0 +1,44 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.factions.Perm; +import com.massivecraft.massivecore.command.requirement.RequirementHasPerm; +import com.massivecraft.massivecore.command.requirement.RequirementIsPlayer; +import com.massivecraft.massivecore.ps.PS; + +import java.util.Collections; +import java.util.Set; + + +public class CmdFactionsAccessSetOne extends CmdFactionsAccessSetXSimple +{ + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public CmdFactionsAccessSetOne(boolean claim) + { + // Super + super(claim); + + // Aliases + this.addAliases("one"); + + // Requirements + this.addRequirements(RequirementIsPlayer.get()); + Perm perm = claim ? Perm.ACCESS_GRANT_ONE : Perm.ACCESS_DENY_ONE; + this.addRequirements(RequirementHasPerm.get(perm)); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public Set getChunks() + { + final PS chunk = PS.valueOf(me.getLocation()).getChunk(true); + final Set chunks = Collections.singleton(chunk); + return chunks; + } + +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsAccessSetSquare.java b/src/com/massivecraft/factions/cmd/CmdFactionsAccessSetSquare.java new file mode 100644 index 00000000..078c5bca --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsAccessSetSquare.java @@ -0,0 +1,45 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.factions.Perm; +import com.massivecraft.massivecore.MassiveException; +import com.massivecraft.massivecore.command.requirement.RequirementHasPerm; +import com.massivecraft.massivecore.command.requirement.RequirementIsPlayer; +import com.massivecraft.massivecore.ps.PS; +import com.massivecraft.massivecore.util.ChunkUtil; + +import java.util.Set; + + +public class CmdFactionsAccessSetSquare extends CmdFactionsAccessSetXRadius +{ + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public CmdFactionsAccessSetSquare(boolean grant) + { + // Super + super(grant); + + // Aliases + this.addAliases("square"); + + // Requirements + this.addRequirements(RequirementIsPlayer.get()); + Perm perm = grant ? Perm.ACCESS_GRANT_SQUARE : Perm.ACCESS_DENY_SQUARE; + this.addRequirements(RequirementHasPerm.get(perm)); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public Set getChunks() throws MassiveException + { + // Common Startup + final PS chunk = PS.valueOf(me.getLocation()).getChunk(true); + return ChunkUtil.getChunksSquare(chunk, this.getRadius()); + } + +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsAccessSetX.java b/src/com/massivecraft/factions/cmd/CmdFactionsAccessSetX.java new file mode 100644 index 00000000..048b6424 --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsAccessSetX.java @@ -0,0 +1,68 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.factions.cmd.type.TypeMPermable; +import com.massivecraft.factions.entity.Faction; +import com.massivecraft.factions.entity.MPerm.MPermable; +import com.massivecraft.massivecore.MassiveException; +import com.massivecraft.massivecore.ps.PS; + +import java.util.Set; + +public abstract class CmdFactionsAccessSetX extends CmdFactionsAccessAbstract +{ + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + private boolean grant = true; + public boolean isGranting() { return this.grant; } + public void setGranting(boolean grant) { this.grant = grant; } + + private int mpermableArgIndex = 0; + public int getMPermableArgIndex() { return this.mpermableArgIndex; } + public void setMPermableArgIndex(int mpermableArgIndex) { this.mpermableArgIndex = mpermableArgIndex; } + + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public CmdFactionsAccessSetX(boolean grant) + { + this.setGranting(grant); + this.setSetupEnabled(false); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public void perform() throws MassiveException + { + // Args + final MPermable mpermable = this.getMPermable(hostFaction); + + final Set chunks = this.getChunks(); + if (chunks == null) throw new NullPointerException("chunks"); + + // Apply / Inform + setAccess(chunks, mpermable, this.isGranting()); + } + + // -------------------------------------------- // + // ABSTRACT + // -------------------------------------------- // + + public abstract Set getChunks() throws MassiveException; + + // -------------------------------------------- // + // EXTRAS + // -------------------------------------------- // + + public MPermable getMPermable(Faction faction) throws MassiveException + { + String arg = this.argAt(this.getMPermableArgIndex()); + return TypeMPermable.get(faction).read(arg, sender); + } + +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsAccessSetXRadius.java b/src/com/massivecraft/factions/cmd/CmdFactionsAccessSetXRadius.java new file mode 100644 index 00000000..1f21d83e --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsAccessSetXRadius.java @@ -0,0 +1,51 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.factions.cmd.type.TypeMPermable; +import com.massivecraft.factions.entity.MConf; +import com.massivecraft.massivecore.MassiveException; +import com.massivecraft.massivecore.command.type.primitive.TypeInteger; + + +public abstract class CmdFactionsAccessSetXRadius extends CmdFactionsAccessSetX +{ + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public CmdFactionsAccessSetXRadius(boolean claim) + { + // Super + super(claim); + + // Parameters + this.addParameter(TypeInteger.get(), "radius"); + + this.addParameter(TypeMPermable.get(), "rank/rel/faction/player"); + this.setMPermableArgIndex(1); + + } + + // -------------------------------------------- // + // EXTRAS + // -------------------------------------------- // + + public Integer getRadius() throws MassiveException + { + int radius = this.readArgAt(0); + + // Radius Claim Min + if (radius < 1) + { + throw new MassiveException().setMsg("If you specify a radius, it must be at least 1."); + } + + // Radius Claim Max + if (radius > MConf.get().setRadiusMax && ! msender.isOverriding()) + { + throw new MassiveException().setMsg("The maximum radius allowed is %s.", MConf.get().setRadiusMax); + } + + return radius; + } + +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsAccessSetXSimple.java b/src/com/massivecraft/factions/cmd/CmdFactionsAccessSetXSimple.java new file mode 100644 index 00000000..5195ca76 --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsAccessSetXSimple.java @@ -0,0 +1,21 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.factions.cmd.type.TypeMPermable; + +public abstract class CmdFactionsAccessSetXSimple extends CmdFactionsAccessSetX +{ + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public CmdFactionsAccessSetXSimple(boolean claim) + { + // Super + super(claim); + + // Parameters + this.addParameter(TypeMPermable.get(), "rank/rel/player/faction"); + this.setMPermableArgIndex(0); + } + +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsAccessView.java b/src/com/massivecraft/factions/cmd/CmdFactionsAccessView.java index 2353901e..82fc8016 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsAccessView.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsAccessView.java @@ -7,7 +7,7 @@ public class CmdFactionsAccessView extends CmdFactionsAccessAbstract // -------------------------------------------- // @Override - public void innerPerform() + public void perform() { this.sendAccessInfo(); } diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsSetCircle.java b/src/com/massivecraft/factions/cmd/CmdFactionsSetCircle.java index 364770ee..d8830cc1 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsSetCircle.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsSetCircle.java @@ -2,12 +2,11 @@ package com.massivecraft.factions.cmd; import com.massivecraft.factions.Perm; import com.massivecraft.massivecore.MassiveException; -import com.massivecraft.massivecore.collections.MassiveSet; import com.massivecraft.massivecore.command.requirement.RequirementHasPerm; import com.massivecraft.massivecore.command.requirement.RequirementIsPlayer; import com.massivecraft.massivecore.ps.PS; +import com.massivecraft.massivecore.util.ChunkUtil; -import java.util.LinkedHashSet; import java.util.Set; @@ -44,27 +43,7 @@ public class CmdFactionsSetCircle extends CmdFactionsSetXRadius { // Common Startup final PS chunk = PS.valueOf(me.getLocation()).getChunk(true); - final Set chunks = new MassiveSet<>(); - - chunks.add(chunk); // The center should come first for pretty messages - - Integer radiusZero = this.getRadiusZero(); - double radiusSquared = radiusZero * radiusZero; - - for (int dx = -radiusZero; dx <= radiusZero; dx++) - { - for (int dz = -radiusZero; dz <= radiusZero; dz++) - { - if (dx*dx + dz*dz > radiusSquared) continue; - - int x = chunk.getChunkX() + dx; - int z = chunk.getChunkZ() + dz; - - chunks.add(chunk.withChunkX(x).withChunkZ(z)); - } - } - - return chunks; + return ChunkUtil.getChunksCircle(chunk, this.getRadius()); } } diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsSetFill.java b/src/com/massivecraft/factions/cmd/CmdFactionsSetFill.java index e25b75eb..0bdc09e1 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsSetFill.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsSetFill.java @@ -5,11 +5,11 @@ import com.massivecraft.factions.entity.BoardColl; import com.massivecraft.factions.entity.Faction; import com.massivecraft.factions.entity.MConf; import com.massivecraft.massivecore.MassiveException; -import com.massivecraft.massivecore.collections.MassiveSet; import com.massivecraft.massivecore.command.requirement.RequirementHasPerm; import com.massivecraft.massivecore.command.requirement.RequirementIsPlayer; +import com.massivecraft.massivecore.predicate.Predicate; import com.massivecraft.massivecore.ps.PS; -import com.massivecraft.massivecore.util.MUtil; +import com.massivecraft.massivecore.util.ChunkUtil; import java.util.Set; @@ -47,69 +47,15 @@ public class CmdFactionsSetFill extends CmdFactionsSetXSimple { // Common Startup final PS chunk = PS.valueOf(me.getLocation()).getChunk(true); - final Set chunks = new MassiveSet<>(); // What faction (aka color) resides there? // NOTE: Wilderness/None is valid. final Faction color = BoardColl.get().getFactionAt(chunk); - // We start where we are! - chunks.add(chunk); - - // Flood! + // Calculate int max = MConf.get().setFillMax; - floodSearch(chunks, color, max); - - // Limit Reached? - if (chunks.size() >= max) - { - throw new MassiveException().addMsg("Fill limit of %d reached.", max); - } - - // OK! - return chunks; + Predicate matcher = ps -> BoardColl.get().getFactionAt(ps) == color; + return ChunkUtil.getChunkArea(chunk, matcher, max); } - - // -------------------------------------------- // - // FLOOD FILL - // -------------------------------------------- // - - public static void floodSearch(Set set, Faction color, int max) - { - // Clean - if (set == null) throw new NullPointerException("set"); - if (color == null) throw new NullPointerException("color"); - - // Expand - Set expansion = new MassiveSet<>(); - for (PS chunk : set) - { - Set neighbours = MUtil.set( - chunk.withChunkX(chunk.getChunkX() + 1), - chunk.withChunkX(chunk.getChunkX() - 1), - chunk.withChunkZ(chunk.getChunkZ() + 1), - chunk.withChunkZ(chunk.getChunkZ() - 1) - ); - - for (PS neighbour : neighbours) - { - if (set.contains(neighbour)) continue; - Faction faction = BoardColl.get().getFactionAt(neighbour); - if (faction == null) continue; - if (faction != color) continue; - expansion.add(neighbour); - } - } - set.addAll(expansion); - - // No Expansion? - if (expansion.isEmpty()) return; - - // Reached Max? - if (set.size() >= max) return; - - // Recurse - floodSearch(set, color, max); - } - + } diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsSetSquare.java b/src/com/massivecraft/factions/cmd/CmdFactionsSetSquare.java index 52cb1f21..8239463f 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsSetSquare.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsSetSquare.java @@ -2,12 +2,11 @@ package com.massivecraft.factions.cmd; import com.massivecraft.factions.Perm; import com.massivecraft.massivecore.MassiveException; -import com.massivecraft.massivecore.collections.MassiveSet; import com.massivecraft.massivecore.command.requirement.RequirementHasPerm; import com.massivecraft.massivecore.command.requirement.RequirementIsPlayer; import com.massivecraft.massivecore.ps.PS; +import com.massivecraft.massivecore.util.ChunkUtil; -import java.util.LinkedHashSet; import java.util.Set; @@ -44,24 +43,7 @@ public class CmdFactionsSetSquare extends CmdFactionsSetXRadius { // Common Startup final PS chunk = PS.valueOf(me.getLocation()).getChunk(true); - final Set chunks = new MassiveSet<>(); - - chunks.add(chunk); // The center should come first for pretty messages - - Integer radiusZero = this.getRadiusZero(); - - for (int dx = -radiusZero; dx <= radiusZero; dx++) - { - for (int dz = -radiusZero; dz <= radiusZero; dz++) - { - int x = chunk.getChunkX() + dx; - int z = chunk.getChunkZ() + dz; - - chunks.add(chunk.withChunkX(x).withChunkZ(z)); - } - } - - return chunks; + return ChunkUtil.getChunksSquare(chunk, this.getRadius()); } } diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsSetXRadius.java b/src/com/massivecraft/factions/cmd/CmdFactionsSetXRadius.java index 92be3bc2..c20c6f23 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsSetXRadius.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsSetXRadius.java @@ -49,10 +49,4 @@ public abstract class CmdFactionsSetXRadius extends CmdFactionsSetX return radius; } - public Integer getRadiusZero() throws MassiveException - { - Integer ret = this.getRadius(); - return ret - 1; - } - }