Faction Bans

Adds /f ban, /f unban, and /f banlist
Permission: factions.ban - included with factions.kithalfplayer
Also added as a /f perm that can be granted. Otherwise, defaults to faction mods.
Number of bans now shows up in f show
Banning a player will notify your faction and target player. It'll also kick the player from your faction if they are currently in it.
TODO: make /f banlist prettier
This commit is contained in:
Trent Hensler 2018-02-28 20:23:37 -08:00
parent cca207c1a5
commit 151d38fd7b
16 changed files with 360 additions and 12 deletions

View File

@ -2,6 +2,7 @@ package com.massivecraft.factions;
import com.massivecraft.factions.iface.EconomyParticipator;
import com.massivecraft.factions.iface.RelationParticipator;
import com.massivecraft.factions.struct.BanInfo;
import com.massivecraft.factions.struct.Relation;
import com.massivecraft.factions.struct.Role;
import com.massivecraft.factions.util.LazyLocation;
@ -56,6 +57,14 @@ public interface Faction extends EconomyParticipator {
public boolean isInvited(FPlayer fplayer);
public void ban(FPlayer target, FPlayer banner);
public void unban(FPlayer player);
public boolean isBanned(FPlayer player);
public Set<BanInfo> getBannedPlayers();
public boolean getOpen();
public void setOpen(boolean isOpen);

View File

@ -0,0 +1,88 @@
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.P;
import com.massivecraft.factions.event.FPlayerLeaveEvent;
import com.massivecraft.factions.struct.Permission;
import com.massivecraft.factions.struct.Role;
import com.massivecraft.factions.zcore.fperms.Access;
import com.massivecraft.factions.zcore.fperms.PermissableAction;
import com.massivecraft.factions.zcore.util.TL;
import org.bukkit.Bukkit;
import java.util.logging.Level;
public class CmdBan extends FCommand {
public CmdBan() {
super();
this.aliases.add("ban");
this.requiredArgs.add("target");
this.permission = Permission.BAN.node;
this.disableOnLock = true;
senderMustBePlayer = true;
senderMustBeMember = false;
senderMustBeModerator = false;
senderMustBeAdmin = false;
}
@Override
public void perform() {
Access access = myFaction.getAccess(fme, PermissableAction.BAN);
if (access == Access.DENY) {
fme.msg(TL.GENERIC_NOPERMISSION, "ban");
return;
}
// Can the player set the home for this faction?
// Check for ALLOW access as well before we check for role.
if (access != Access.ALLOW) {
if (!Permission.BAN.has(sender) && !(assertMinRole(Role.MODERATOR))) {
return;
}
} else {
if (!Permission.BAN.has(sender, true)) {
return;
}
}
// Good on permission checks. Now lets just ban the player.
FPlayer target = argAsFPlayer(0);
if (target == null) {
return; // the above method sends a message if fails to find someone.
}
// Ban the user.
myFaction.ban(target, fme);
myFaction.deinvite(target); // can't hurt
// If in same Faction, lets make sure to kick them and throw an event.
if (target.getFaction() == myFaction) {
FPlayerLeaveEvent event = new FPlayerLeaveEvent(target, myFaction, FPlayerLeaveEvent.PlayerLeaveReason.BANNED);
Bukkit.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
// if someone cancels a ban, we'll get people complaining here. So lets log it.
P.p.log(Level.WARNING, "Attempted to ban {0} but someone cancelled the kick event. This isn't good.", target.getName());
return;
}
// Didn't get cancelled so remove them and reset their invite.
myFaction.removeFPlayer(target);
target.resetFactionData();
}
// Lets inform the people!
target.msg(TL.COMMAND_BAN_TARGET, myFaction.getTag(target.getFaction()));
myFaction.msg(TL.COMMAND_BAN_BANNED, fme.getName(), target.getName());
}
@Override
public TL getUsageTranslation() {
return TL.COMMAND_BAN_DESCRIPTION;
}
}

View File

@ -0,0 +1,67 @@
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.FPlayers;
import com.massivecraft.factions.Faction;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.struct.BanInfo;
import com.massivecraft.factions.struct.Permission;
import com.massivecraft.factions.zcore.util.TL;
import java.util.ArrayList;
import java.util.List;
public class CmdBanlist extends FCommand {
public CmdBanlist() {
super();
this.aliases.add("banlist");
this.aliases.add("bans");
this.aliases.add("banl");
this.optionalArgs.put("faction", "faction");
this.permission = Permission.BAN.node;
this.disableOnLock = true;
senderMustBePlayer = true;
senderMustBeMember = false;
senderMustBeModerator = false;
senderMustBeAdmin = false;
}
@Override
public void perform() {
Faction target = myFaction;
if (!args.isEmpty()) {
target = argAsFaction(0);
}
if (target == Factions.getInstance().getWilderness()) {
sender.sendMessage(TL.COMMAND_BANLIST_NOFACTION.toString());
return;
}
List<String> lines = new ArrayList<>();
lines.add(TL.COMMAND_BANLIST_HEADER.format(target.getBannedPlayers().size(), target.getTag(myFaction)));
int i = 1;
for (BanInfo info : target.getBannedPlayers()) {
FPlayer banned = FPlayers.getInstance().getById(info.getBanned());
FPlayer banner = FPlayers.getInstance().getById(info.getBanner());
String timestamp = TL.sdf.format(info.getTime());
lines.add(TL.COMMAND_BANLIST_ENTRY.format(i, banned.getName(), banner.getName(), timestamp));
i++;
}
for (String s : lines) {
fme.sendMessage(s);
}
}
@Override
public TL getUsageTranslation() {
return TL.COMMAND_BANLIST_DESCRIPTION;
}
}

View File

@ -31,13 +31,13 @@ public class CmdInvite extends FCommand {
@Override
public void perform() {
FPlayer you = this.argAsBestFPlayerMatch(0);
if (you == null) {
FPlayer target = this.argAsBestFPlayerMatch(0);
if (target == null) {
return;
}
if (you.getFaction() == myFaction) {
msg(TL.COMMAND_INVITE_ALREADYMEMBER, you.getName(), myFaction.getTag());
if (target.getFaction() == myFaction) {
msg(TL.COMMAND_INVITE_ALREADYMEMBER, target.getName(), myFaction.getTag());
msg(TL.GENERIC_YOUMAYWANT.toString() + p.cmdBase.cmdKick.getUseageTemplate(false));
return;
}
@ -47,32 +47,37 @@ public class CmdInvite extends FCommand {
return;
}
Access access = myFaction.getAccess(you, PermissableAction.INVITE);
Access access = myFaction.getAccess(target, PermissableAction.INVITE);
if (access == Access.DENY || (access == Access.UNDEFINED && !assertMinRole(Role.MODERATOR))) {
fme.msg(TL.GENERIC_NOPERMISSION, "invite");
return;
}
myFaction.invite(you);
if (!you.isOnline()) {
if (myFaction.isBanned(target)) {
fme.msg(TL.COMMAND_INVITE_BANNED, target.getName());
return;
}
myFaction.invite(target);
if (!target.isOnline()) {
return;
}
// Tooltips, colors, and commands only apply to the string immediately before it.
FancyMessage message = new FancyMessage(fme.describeTo(you, true))
FancyMessage message = new FancyMessage(fme.describeTo(target, true))
.tooltip(TL.COMMAND_INVITE_CLICKTOJOIN.toString())
.command("/" + Conf.baseCommandAliases.get(0) + " join " + myFaction.getTag())
.then(TL.COMMAND_INVITE_INVITEDYOU.toString())
.color(ChatColor.YELLOW)
.tooltip(TL.COMMAND_INVITE_CLICKTOJOIN.toString())
.command("/" + Conf.baseCommandAliases.get(0) + " join " + myFaction.getTag())
.then(myFaction.describeTo(you)).tooltip(TL.COMMAND_INVITE_CLICKTOJOIN.toString())
.then(myFaction.describeTo(target)).tooltip(TL.COMMAND_INVITE_CLICKTOJOIN.toString())
.command("/" + Conf.baseCommandAliases.get(0) + " join " + myFaction.getTag());
message.send(you.getPlayer());
message.send(target.getPlayer());
//you.msg("%s<i> invited you to %s", fme.describeTo(you, true), myFaction.describeTo(you));
myFaction.msg(TL.COMMAND_INVITE_INVITED, fme.describeTo(myFaction, true), you.describeTo(myFaction));
myFaction.msg(TL.COMMAND_INVITE_INVITED, fme.describeTo(myFaction, true), target.describeTo(myFaction));
}
@Override

View File

@ -79,6 +79,12 @@ public class CmdJoin extends FCommand {
return;
}
// Check for ban
if (!fme.isAdminBypassing() && faction.isBanned(fme)) {
fme.msg(TL.COMMAND_JOIN_BANNED, faction.getTag(fme));
return;
}
// trigger the join event (cancellable)
FPlayerJoinEvent joinEvent = new FPlayerJoinEvent(FPlayers.getInstance().getByPlayer(me), faction, FPlayerJoinEvent.PlayerJoinReason.COMMAND);
Bukkit.getServer().getPluginManager().callEvent(joinEvent);

View File

@ -0,0 +1,69 @@
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.struct.Permission;
import com.massivecraft.factions.struct.Role;
import com.massivecraft.factions.zcore.fperms.Access;
import com.massivecraft.factions.zcore.fperms.PermissableAction;
import com.massivecraft.factions.zcore.util.TL;
public class CmdUnban extends FCommand {
public CmdUnban() {
super();
this.aliases.add("unban");
this.requiredArgs.add("target");
this.permission = Permission.BAN.node;
this.disableOnLock = true;
senderMustBePlayer = true;
senderMustBeMember = false;
senderMustBeModerator = false;
senderMustBeAdmin = false;
}
@Override
public void perform() {
Access access = myFaction.getAccess(fme, PermissableAction.BAN);
if (access == Access.DENY) {
fme.msg(TL.GENERIC_NOPERMISSION, "ban");
return;
}
// Can the player set the home for this faction?
// Check for ALLOW access as well before we check for role.
// TODO: no more duplicate code :(
if (access != Access.ALLOW) {
if (!Permission.BAN.has(sender) && !(assertMinRole(Role.MODERATOR))) {
return;
}
} else {
if (!Permission.BAN.has(sender, true)) {
return;
}
}
// Good on permission checks. Now lets just ban the player.
FPlayer target = argAsFPlayer(0);
if (target == null) {
return; // the above method sends a message if fails to find someone.
}
if (!myFaction.isBanned(target)) {
fme.msg(TL.COMMAND_UNBAN_NOTBANNED, target.getName());
return;
}
myFaction.unban(target);
myFaction.msg(TL.COMMAND_UNBAN_UNBANNED, fme.getName(), target.getName());
target.msg(TL.COMMAND_UNBAN_TARGET, myFaction.getTag(target));
}
@Override
public TL getUsageTranslation() {
return TL.COMMAND_UNBAN_DESCRIPTION;
}
}

View File

@ -76,6 +76,9 @@ public class FCmdRoot extends FCommand {
public CmdSetDefaultRole cmdSetDefaultRole = new CmdSetDefaultRole();
public CmdMapHeight cmdMapHeight = new CmdMapHeight();
public CmdClaimAt cmdClaimAt = new CmdClaimAt();
public CmdBan cmdban = new CmdBan();
public CmdUnban cmdUnban = new CmdUnban();
public CmdBanlist cmdbanlist = new CmdBanlist();
public FCmdRoot() {
super();
@ -166,6 +169,9 @@ public class FCmdRoot extends FCommand {
this.addSubCommand(this.cmdSetDefaultRole);
this.addSubCommand(this.cmdMapHeight);
this.addSubCommand(this.cmdClaimAt);
this.addSubCommand(this.cmdban);
this.addSubCommand(this.cmdUnban);
this.addSubCommand(this.cmdbanlist);
if (P.p.isHookedPlayervaults()) {
P.p.log("Found playervaults hook, adding /f vault and /f setmaxvault commands.");
this.addSubCommand(new CmdSetMaxVaults());

View File

@ -10,7 +10,7 @@ public class FPlayerLeaveEvent extends FactionPlayerEvent implements Cancellable
boolean cancelled = false;
public enum PlayerLeaveReason {
KICKED, DISBAND, RESET, JOINOTHER, LEAVE
KICKED, DISBAND, RESET, JOINOTHER, LEAVE, BANNED
}
public FPlayerLeaveEvent(FPlayer p, Faction f, PlayerLeaveReason r) {

View File

@ -0,0 +1,42 @@
package com.massivecraft.factions.struct;
public class BanInfo {
// FPlayer IDs
private final String banner;
private final String banned;
private final long time;
public BanInfo(String banner, String banned, long time) {
this.banner = banner;
this.banned = banned;
this.time = time;
}
/**
* Get the FPlayer ID of the player who issued the ban.
*
* @return FPlayer ID.
*/
public String getBanner() {
return this.banner;
}
/**
* Get the FPlayer ID of the player who got banned.
*
* @return FPlayer ID.
*/
public String getBanned() {
return banned;
}
/**
* Get the server time when the ban was issued.
*
* @return system timestamp.
*/
public long getTime() {
return time;
}
}

View File

@ -13,6 +13,7 @@ public enum Permission {
ANNOUNCE("announce"),
AUTOCLAIM("autoclaim"),
AUTO_LEAVE_BYPASS("autoleavebypass"),
BAN("ban"),
BYPASS("bypass"),
CHAT("chat"),
CHATSPY("chatspy"),

View File

@ -1,6 +1,7 @@
package com.massivecraft.factions.zcore.fperms;
public enum PermissableAction {
BAN("ban"),
BUILD("build"),
DESTROY("destroy"),
FROST_WALK("frostwalk"),

View File

@ -4,6 +4,7 @@ import com.massivecraft.factions.*;
import com.massivecraft.factions.iface.EconomyParticipator;
import com.massivecraft.factions.iface.RelationParticipator;
import com.massivecraft.factions.integration.Econ;
import com.massivecraft.factions.struct.BanInfo;
import com.massivecraft.factions.struct.Permission;
import com.massivecraft.factions.struct.Relation;
import com.massivecraft.factions.struct.Role;
@ -50,6 +51,7 @@ public abstract class MemoryFaction implements Faction, EconomyParticipator {
protected int maxVaults;
protected Role defaultRole;
protected Map<Permissable, Map<PermissableAction, Access>> permissions = new HashMap<>();
protected Set<BanInfo> bans = new HashSet<>();
public HashMap<String, List<String>> getAnnouncements() {
return this.announcements;
@ -147,6 +149,34 @@ public abstract class MemoryFaction implements Faction, EconomyParticipator {
return this.invites.contains(fplayer.getId());
}
public void ban(FPlayer target, FPlayer banner) {
BanInfo info = new BanInfo(banner.getId(), target.getId(), System.currentTimeMillis());
this.bans.add(info);
}
public void unban(FPlayer player) {
Iterator<BanInfo> iter = bans.iterator();
while (iter.hasNext()) {
if (iter.next().getBanned().equalsIgnoreCase(player.getId())) {
iter.remove();
}
}
}
public boolean isBanned(FPlayer player) {
for (BanInfo info : bans) {
if (info.getBanned().equalsIgnoreCase(player.getId())) {
return true;
}
}
return false;
}
public Set<BanInfo> getBannedPlayers() {
return this.bans;
}
public boolean getOpen() {
return open;
}

View File

@ -75,6 +75,15 @@ public enum TL {
COMMAND_AUTOHELP_HELPFOR("Help for command \""),
COMMAND_BAN_DESCRIPTION("Ban players from joining your Faction."),
COMMAND_BAN_TARGET("&cYou were banned from &7%1$s"), // banned player perspective
COMMAND_BAN_BANNED("&e%1$s &cbanned &7%2$s"),
COMMAND_BANLIST_DESCRIPTION("View a Faction's ban list"),
COMMAND_BANLIST_HEADER("&6There are &c%d&6 bans for %s"),
COMMAND_BANLIST_ENTRY("&7%d. &c%s &r- &a%s &r- &e%s"),
COMMAND_BANLIST_NOFACTION("&4You are not in a Faction."),
COMMAND_BOOM_PEACEFULONLY("<b>This command is only usable by factions which are specifically designated as peaceful."),
COMMAND_BOOM_TOTOGGLE("to toggle explosions"),
COMMAND_BOOM_FORTOGGLE("for toggling explosions"),
@ -244,6 +253,7 @@ public enum TL {
COMMAND_INVITE_INVITED("%1$s<i> invited %2$s<i> to your faction."),
COMMAND_INVITE_ALREADYMEMBER("%1$s<i> is already a member of %2$s"),
COMMAND_INVITE_DESCRIPTION("Invite a player to your faction"),
COMMAND_INVITE_BANNED("&7%1$s &cis banned from your Faction. Not sending an invite."),
COMMAND_JOIN_CANNOTFORCE("<b>You do not have permission to move other players into a faction."),
COMMAND_JOIN_SYSTEMFACTION("<b>Players may only join normal factions. This is a system faction."),
@ -261,6 +271,7 @@ public enum TL {
COMMAND_JOIN_JOINEDLOG("%1$s joined the faction %2$s."),
COMMAND_JOIN_MOVEDLOG("%1$s moved the player %2$s into the faction %3$s."),
COMMAND_JOIN_DESCRIPTION("Join a faction"),
COMMAND_JOIN_BANNED("&cYou are banned from %1$s &c:("),
COMMAND_KICK_CANDIDATES("Players you can kick: "),
COMMAND_KICK_CLICKTOKICK("Click to kick "),
@ -534,6 +545,11 @@ public enum TL {
COMMAND_TOP_LINE("%d. &6%s: &c%s"), // Rank. Faction: Value
COMMAND_TOP_INVALID("Could not sort by %s. Try balance, online, members, power or land."),
COMMAND_UNBAN_DESCRIPTION("Unban someone from your Faction"),
COMMAND_UNBAN_NOTBANNED("&7%s &cisn't banned. Not doing anything."),
COMMAND_UNBAN_UNBANNED("&e%1$s &cunbanned &7%2$s"),
COMMAND_UNBAN_TARGET("&aYou were unbanned from &r%s"),
COMMAND_UNCLAIM_SAFEZONE_SUCCESS("<i>Safe zone was unclaimed."),
COMMAND_UNCLAIM_SAFEZONE_NOPERM("<b>This is a safe zone. You lack permissions to unclaim."),
COMMAND_UNCLAIM_WARZONE_SUCCESS("<i>War zone was unclaimed."),

View File

@ -71,6 +71,7 @@ public enum TagReplacer {
FACTION_SIZE(TagType.FACTION, "{members}"),
FACTION_KILLS(TagType.FACTION, "{faction-kills}"),
FACTION_DEATHS(TagType.FACTION, "{faction-deaths}"),
FACTION_BANCOUNT(TagType.FACTION, "{faction-bancount}"),
/**
* General variables, require no faction or player
@ -233,6 +234,8 @@ public enum TagReplacer {
return String.valueOf(fac.getKills());
case FACTION_DEATHS:
return String.valueOf(fac.getDeaths());
case FACTION_BANCOUNT:
return String.valueOf(fac.getBannedPlayers().size());
}
return null;
}

View File

@ -244,6 +244,7 @@ show:
- '<a>This faction is permanent, remaining even with no members.' # only shows if faction is permanent
- '<a>Land value: <i>{land-value} {land-refund}'
- '<a>Balance: <i>{balance}'
- '<a>Bans: <i>{faction-bancount}'
- '<a>Allies(<i>{allies}<a>/<i>{max-allies}<a>): {allies-list} '
- '<a>Online: (<i>{online}<a>/<i>{members}<a>): {online-list}'
- '<a>Offline: (<i>{offline}<a>/<i>{members}<a>): {offline-list}'
@ -372,6 +373,7 @@ help:
# - {world}, {x}, {y}, {z} : Faction home variables. You don't need to use them all.
# - {faction-kills} : # of kills the faction has
# - {faction-deaths}: # of deaths the faction has
# - {faction-bancount} : # of bans the faction has
# General variables. Can be used anywhere.
# - {total-online} : Total # of players on the server
# - {max-warps} : Max # of warps a faction can set

View File

@ -101,6 +101,7 @@ permissions:
factions.togglealliancechat: true
factions.vault: true
factions.mapheight: true
factions.ban: true
factions.admin:
description: hand over your admin rights
factions.admin.any:
@ -280,3 +281,5 @@ permissions:
description: Set a faction's max vaults.
factions.mapheight:
description: Set your /f map height.
factions.ban:
description: Ban players from Factions