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

@@ -55,7 +55,6 @@ public class CmdFactions extends FactionsCommand
public CmdFactionsPerm cmdFactionsPerm = new CmdFactionsPerm();
public CmdFactionsFlag cmdFactionsFlag = new CmdFactionsFlag();
public CmdFactionsUnstuck cmdFactionsUnstuck = new CmdFactionsUnstuck();
public CmdFactionsExpansions cmdFactionsExpansions = new CmdFactionsExpansions();
public CmdFactionsOverride cmdFactionsOverride = new CmdFactionsOverride();
public CmdFactionsDisband cmdFactionsDisband = new CmdFactionsDisband();
public CmdFactionsPowerBoost cmdFactionsPowerBoost = new CmdFactionsPowerBoost();

View File

@@ -1,40 +0,0 @@
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.event.EventFactionsExpansions;
import com.massivecraft.massivecore.util.Txt;
import java.util.Map.Entry;
public class CmdFactionsExpansions extends FactionsCommand
{
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public void perform()
{
// Event
EventFactionsExpansions event = new EventFactionsExpansions(sender);
event.run();
// Title
Object title = "Factions Expansions";
title = Txt.titleize(title);
message(title);
// Lines
for (Entry<String, Boolean> entry : event.getExpansions().entrySet())
{
String name = entry.getKey();
Boolean installed = entry.getValue();
String format = (installed ? "<g>[X] <h>%s" : "<b>[ ] <h>%s");
msg(format, name);
}
// URL Suggestion
msg("<i>Learn all about expansions in the online documentation:");
msg("<aqua>https://www.massivecraft.com/factions");
}
}

View File

@@ -8,6 +8,7 @@ public class CmdFactionsPerm extends FactionsCommand
CmdFactionsPermList cmdFactionsPermList = new CmdFactionsPermList();
CmdFactionsPermShow cmdFactionsPermShow = new CmdFactionsPermShow();
CmdFactionsPermShow2 cmdFactionsPermShow2 = new CmdFactionsPermShow2();
CmdFactionsPermSet cmdFactionsPermSet = new CmdFactionsPermSet();
}

View File

@@ -14,19 +14,6 @@ import java.util.List;
public class CmdFactionsPermList extends FactionsCommand
{
// -------------------------------------------- //
// REUSABLE PREDICATE
// -------------------------------------------- //
private static final Predicate<MPerm> PREDICATE_MPERM_VISIBLE = new Predicate<MPerm>()
{
@Override
public boolean apply(MPerm mperm)
{
return mperm.isVisible();
}
};
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
@@ -49,16 +36,8 @@ public class CmdFactionsPermList extends FactionsCommand
// Pager create
String title = String.format("Perms for %s", msenderFaction.describeTo(msender));
final Pager<MPerm> pager = new Pager<>(this, title, page, new Stringifier<MPerm>()
{
@Override
public String toString(MPerm mperm, int index)
{
return mperm.getDesc(true, true);
}
});
final Predicate<MPerm> predicate = msender.isOverriding() ? null : PREDICATE_MPERM_VISIBLE;
final Pager<MPerm> pager = new Pager<>(this, title, page, (Stringifier<MPerm>) (mp, i) -> mp.getDesc(true, true));
final Predicate<MPerm> predicate = msender.isOverriding() ? null : MPerm::isVisible;
Bukkit.getScheduler().runTaskAsynchronously(Factions.get(), new Runnable()
{

View File

@@ -4,17 +4,12 @@ import com.massivecraft.factions.cmd.type.TypeFaction;
import com.massivecraft.factions.cmd.type.TypeMPerm;
import com.massivecraft.factions.cmd.type.TypeMPermable;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.MConf;
import com.massivecraft.factions.entity.MPerm;
import com.massivecraft.factions.entity.MPlayer;
import com.massivecraft.factions.event.EventFactionsPermChange;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.command.type.primitive.TypeBooleanYes;
import com.massivecraft.massivecore.util.Txt;
import java.util.ArrayList;
import java.util.List;
public class CmdFactionsPermSet extends FactionsCommand
{
// -------------------------------------------- //
@@ -43,7 +38,6 @@ public class CmdFactionsPermSet extends FactionsCommand
Faction faction = this.readArgAt(3, msenderFaction);
MPerm.MPermable permable = TypeMPermable.get(faction).read(this.argAt(1), sender);
// Do the sender have the right to change perms for this faction?
if ( ! MPerm.getPermPerms().has(msender, faction, true)) return;
@@ -51,8 +45,12 @@ public class CmdFactionsPermSet extends FactionsCommand
// Is this perm editable?
if ( ! msender.isOverriding() && ! perm.isEditable())
{
msg("<b>The perm <h>%s <b>is not editable.", perm.getName());
return;
throw new MassiveException().addMsg("<b>The perm <h>%s <b>is not editable.", perm.getName());
}
if (permable == faction)
{
throw new MassiveException().addMsg("<b>A faction can't have perms for itself. Perhaps try ranks.");
}
// Event
@@ -67,35 +65,18 @@ public class CmdFactionsPermSet extends FactionsCommand
// No change
if (!change)
{
msg("%s <i>already has %s <i>set to %s <i>for %s<i>.", faction.describeTo(msender), perm.getDesc(true, false), Txt.parse(value ? "<g>YES" : "<b>NOO"), permable.getColor() + permable.getName() + "s");
return;
throw new MassiveException().addMsg("%s <i>already has %s <i>set to %s <i>for %s<i>.", faction.describeTo(msender), perm.getDesc(true, false), Txt.parse(value ? "<g>YES" : "<b>NOO"), permable.getDisplayName(msender));
}
// The following is to make sure the leader always has the right to change perms if that is our goal.
if (perm == MPerm.getPermPerms() && MConf.get().defaultPermsLeader.contains(MPerm.ID_PERMS))
if (perm == MPerm.getPermPerms() && MPerm.getPermPerms().getStandard().contains("LEADER"))
{
faction.setPermitted( faction.getLeaderRank(), MPerm.getPermPerms(), true);
}
// Create messages
List<Object> messages = new ArrayList<>();
// Inform sender
messages.add(Txt.titleize("Perm for " + faction.describeTo(msender, true)));
messages.add(MPerm.getStateHeaders(faction));
messages.add(Txt.parse(perm.getStateInfo(faction, true)));
message(messages);
// Inform faction (their message is slighly different)
List<MPlayer> recipients = faction.getMPlayers();
recipients.remove(msender);
for (MPlayer recipient : recipients)
{
recipient.msg("<h>%s <i>set a perm for <h>%s<i>.", msender.describeTo(recipient, true), faction.describeTo(recipient, true));
recipient.message(messages);
}
String yesNo = Txt.parse(value ? "<g>YES" : "<b>NOO");
msg("<i>Set perm <h>%s <i>to <h>%s <i>for <reset>%s<i> in <reset>%s<i>.", perm.getName(), yesNo, permable.getDisplayName(msender), faction.describeTo(msender));
}
}

View File

@@ -1,16 +1,19 @@
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.Rel;
import com.massivecraft.factions.cmd.type.TypeFaction;
import com.massivecraft.factions.cmd.type.TypeMPerm;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.FactionColl;
import com.massivecraft.factions.entity.MPerm;
import com.massivecraft.factions.entity.MPlayer;
import com.massivecraft.factions.entity.MPlayerColl;
import com.massivecraft.factions.entity.Rank;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.command.type.container.TypeSet;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.util.Txt;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
public class CmdFactionsPermShow extends FactionsCommand
{
@@ -21,8 +24,8 @@ public class CmdFactionsPermShow extends FactionsCommand
public CmdFactionsPermShow()
{
// Parameters
this.addParameter(TypeMPerm.get(), "perm");
this.addParameter(TypeFaction.get(), "faction", "you");
this.addParameter(TypeSet.get(TypeMPerm.get()), "perms", "all", true);
}
// -------------------------------------------- //
@@ -33,21 +36,48 @@ public class CmdFactionsPermShow extends FactionsCommand
public void perform() throws MassiveException
{
// Arg: Faction
MPerm mperm = this.readArg();
Faction faction = this.readArg(msenderFaction);
Collection<MPerm> mperms = this.readArg(MPerm.getAll());
var permittedIds = faction.getPerms().get(mperm.getId());
var permables = new MassiveList<MPerm.MPermable>();
for (var permitted : permittedIds)
{
permables.add(idToMPermable(permitted));
}
var removeString = Txt.parse(" of ") + faction.getDisplayName(msender);
var permableList = permables.stream()
.map(permable -> permable.getDisplayName(msender))
.map(s -> s.replace(removeString, ""))
.collect(Collectors.toList());
String permableNames = Txt.implodeCommaAnd(permableList, Txt.parse("<i>"));
// Create messages
List<Object> messages = new ArrayList<>();
msg("<i>In <reset>%s <i>permission <h>%s <i>is granted to <reset>%s<i>.", faction.describeTo(msender), mperm.getName(), permableNames);
}
messages.add(Txt.titleize("Perm for " + faction.describeTo(msender, true)));
messages.add(MPerm.getStateHeaders(faction));
for (MPerm mperm : mperms)
public static MPerm.MPermable idToMPermable(String id)
{
MPlayer mplayer = MPlayerColl.get().get(id, false);
if (mplayer != null) return mplayer;
Faction faction = Faction.get(id);
if (faction != null) return faction;
for (var f : FactionColl.get().getAll())
{
messages.add(Txt.parse(mperm.getStateInfo(faction, true)));
Rank rank = f.getRank(id);
if (rank != null) return rank;
}
// Send messages
message(messages);
if (Rel.ALLY.name().equalsIgnoreCase(id)) return Rel.ALLY;
if (Rel.TRUCE.name().equalsIgnoreCase(id)) return Rel.TRUCE;
if (Rel.NEUTRAL.name().equalsIgnoreCase(id)) return Rel.NEUTRAL;
if (Rel.ENEMY.name().equalsIgnoreCase(id)) return Rel.ENEMY;
throw new RuntimeException(id);
}
}

View File

@@ -0,0 +1,100 @@
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.cmd.type.TypeFaction;
import com.massivecraft.factions.cmd.type.TypeMPermable;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.MPerm;
import com.massivecraft.factions.entity.MPlayer;
import com.massivecraft.factions.entity.Rank;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.mson.Mson;
import com.massivecraft.massivecore.util.MUtil;
import com.massivecraft.massivecore.util.Txt;
import org.bukkit.ChatColor;
import java.util.stream.Collectors;
public class CmdFactionsPermShow2 extends FactionsCommand
{
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public CmdFactionsPermShow2()
{
// Parameters
this.addParameter(TypeMPermable.get(), "rank/rel/player/faction");
this.addParameter(TypeFaction.get(), "faction", "you");
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public void perform() throws MassiveException
{
// Arg: Faction
Faction faction = this.readArgAt(1, msenderFaction);
var permableType = TypeMPermable.get(faction);
MPerm.MPermable permable = permableType.read(this.argAt(0), sender);
if (permable == faction)
{
throw new MassiveException().addMsg("<b>A faction can't have perms for itself.");
}
var perms = new MassiveList<MPerm>();
for (var mperm : MPerm.getAll())
{
if (faction.isPermitted(permable.getId(), mperm.getId())) perms.add(mperm);
}
if (perms.isEmpty())
{
msg("<i>In <reset>%s <reset>%s <i>specifically has <b>no permissions<i>.", faction.describeTo(msender), permable.getDisplayName(sender));
}
else
{
var permNames = perms.stream().map(perm -> Txt.parse("<h>") + perm.getName()).collect(Collectors.toList());
String names = Txt.implodeCommaAnd(permNames, Txt.parse("<i>"));
// Create messages
var permissionSingularPlural = permNames.size() == 1 ? "permission" : "permissions";
msg("<i>In <reset>%s <reset>%s <i>specifically has the %s: <reset>%s<i>.", faction.describeTo(msender), permable.getDisplayName(sender), permissionSingularPlural, names);
}
if (permable instanceof MPlayer)
{
MPlayer mplayer = (MPlayer) permable;
msg("<i>They may have other permissions through their faction membership, rank or relation to <reset>%s<i>.", faction.describeTo(msender));
var msons = new MassiveList<Mson>();
if (mplayer.getFaction() != faction) msons.add(Mson.parse("<command>[faction]").command(this, mplayer.getFaction().getName(), faction.getName()));
msons.add(Mson.parse("<command>[rank]").command(this, mplayer.getFaction().getName() + "-" + mplayer.getRank().getName(), faction.getName()));
if (mplayer.getFaction() != faction) msons.add(Mson.parse("<command>[relation]").command(this, faction.getRelationTo(mplayer).toString(), faction.getName()));
Mson msons2 = Mson.implode(msons, Mson.SPACE);
message(mson(mson("Commands: ").color(ChatColor.YELLOW), msons2));
}
if (permable instanceof Faction)
{
Faction faction1 = (Faction) permable;
msg("<i>They may have other permissions through their relation to <reset>%s<i>.", faction.describeTo(msender));
Mson msonRelation = Mson.parse("<command>[relation]").command(this, faction.getRelationTo(faction1).toString(), faction.getName());
Mson msons = Mson.implode(MUtil.list(msonRelation), Mson.SPACE);
message(mson(mson("Commands: ").color(ChatColor.YELLOW), msons));
}
if (permable instanceof Rank && !faction.hasRank((Rank) permable))
{
Rank rank = (Rank) permable;
msg("<i>They may have other permissions thorugh their faction membership or relation to <reset>%s<i>.", faction.describeTo(msender));
Mson msonFaction = Mson.parse("<command>[faction]").command(this, rank.getFaction().getName(), faction.getName());
Mson msonRelation = Mson.parse("<command>[relation]").command(this, faction.getRelationTo(rank.getFaction()).toString(), faction.getName());
Mson msons = Mson.implode(MUtil.list(msonFaction, msonRelation), Mson.SPACE);
message(mson(mson("Commands: ").color(ChatColor.YELLOW), msons));
}
}
}

View File

@@ -6,6 +6,7 @@ import com.massivecraft.factions.entity.Rank;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.command.type.primitive.TypeInteger;
import com.massivecraft.massivecore.command.type.primitive.TypeString;
import com.massivecraft.massivecore.command.type.primitive.TypeStringParsed;
public class CmdFactionsRankEditCreate extends FactionsCommand
{
@@ -18,7 +19,7 @@ public class CmdFactionsRankEditCreate extends FactionsCommand
// Parameters
this.addParameter(TypeString.get(), "name");
this.addParameter(TypeInteger.get(), "priority");
this.addParameter("", TypeString.get(), "prefix", "none");
this.addParameter("", TypeStringParsed.get(), "prefix", "none");
this.addParameter(TypeFaction.get(), "faction", "you");
}

View File

@@ -1,13 +1,20 @@
package com.massivecraft.factions.cmd.type;
import com.massivecraft.factions.Rel;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.FactionColl;
import com.massivecraft.factions.entity.MPerm;
import com.massivecraft.factions.entity.MPlayer;
import com.massivecraft.factions.entity.Rank;
import com.massivecraft.massivecore.command.type.TypeAbstractChoice;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.command.type.TypeAbstract;
import org.bukkit.command.CommandSender;
import java.util.Collections;
import java.util.Collection;
import java.util.stream.Collectors;
public class TypeMPermable extends TypeAbstractChoice<MPerm.MPermable>
public class TypeMPermable extends TypeAbstract<MPerm.MPermable>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
@@ -20,7 +27,6 @@ public class TypeMPermable extends TypeAbstractChoice<MPerm.MPermable>
super(Rank.class);
this.faction = null;
this.setAll(Collections.emptyList());
}
public static TypeMPermable get(Faction faction) { return new TypeMPermable(faction); }
@@ -30,10 +36,6 @@ public class TypeMPermable extends TypeAbstractChoice<MPerm.MPermable>
if (faction == null) throw new NullPointerException("faction");
this.faction = faction;
var permables = MPerm.getPermables(faction);
this.setAll(permables);
}
// -------------------------------------------- //
@@ -43,4 +45,100 @@ public class TypeMPermable extends TypeAbstractChoice<MPerm.MPermable>
private final Faction faction;
public Faction getFaction() { return this.faction; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public MPerm.MPermable read(String arg, CommandSender sender) throws MassiveException
{
TypeRank typeRank = new TypeRank(this.getFaction());
try
{
Rank rank = typeRank.read(arg, sender);
return rank;
}
catch (MassiveException ex)
{
// Do nothing
}
try
{
Rel rel = TypeRelation.get().read(arg, sender);
return rel;
}
catch (MassiveException ex)
{
// Do nothing
}
try
{
MPlayer mplayer = TypeMPlayer.get().read(arg, sender);
return mplayer;
}
catch (MassiveException ex)
{
// Do nothing
}
try
{
Faction faction = TypeFaction.get().read(arg, sender);
return faction;
}
catch (MassiveException ex)
{
// Do nothing
}
if (arg.contains("-"))
{
int idx = arg.indexOf('-');
String factionName = arg.substring(0, idx);
String rankName = arg.substring(idx+1);
Faction faction = TypeFaction.get().read(factionName, sender);
Rank rank = TypeRank.get(faction).read(rankName, sender);
return rank;
}
throw new MassiveException().addMsg("<b>No rank, relation, player or faction matches: <h>%s<b>.", arg);
}
public Collection<String> getTabList(CommandSender sender, String arg)
{
var ret = new MassiveList<String>();
var faction = this.getFaction();
if (faction == null) faction = MPlayer.get(sender).getFaction();
ret.addAll(faction.getRanks().getAll().stream().map(Rank::getName).collect(Collectors.toList()));
ret.addAll(TypeRelation.get().getTabList(sender, arg));
ret.addAll(TypeFaction.get().getTabList(sender, arg));
// Faction specific ranks
if (arg.length() >= 3)
{
for (var f : FactionColl.get().getAll())
{
String name = f.getName();
if (arg.length() <= name.length() && !name.toLowerCase().startsWith(arg.toLowerCase())) continue;
if (arg.length() > name.length() && !arg.toLowerCase().startsWith(name.toLowerCase())) continue;
ret.addAll(f.getRanks().getAll().stream().map(r -> name + "-" + r.getName()).collect(Collectors.toList()));
}
}
return ret;
}
@Override
public boolean isValid(String arg, CommandSender sender)
{
// In the generic case accept all
if (this.getFaction() == null) return true;
else return super.isValid(arg, sender);
}
}