From ce132ed033954ff2bf6950aeb65933e17f909138 Mon Sep 17 00:00:00 2001 From: Trent Hensler Date: Tue, 19 Dec 2017 02:10:52 -0800 Subject: [PATCH] Add password protected warps --- .../java/com/massivecraft/factions/Faction.java | 6 ++++++ .../com/massivecraft/factions/cmd/CmdFWarp.java | 13 ++++++++++++- .../com/massivecraft/factions/cmd/CmdSetFWarp.java | 8 +++++++- .../factions/zcore/persist/MemoryFaction.java | 13 +++++++++++++ .../com/massivecraft/factions/zcore/util/TL.java | 5 +++-- 5 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/massivecraft/factions/Faction.java b/src/main/java/com/massivecraft/factions/Faction.java index 5ff09d14..3cb39583 100644 --- a/src/main/java/com/massivecraft/factions/Faction.java +++ b/src/main/java/com/massivecraft/factions/Faction.java @@ -23,6 +23,12 @@ public interface Faction extends EconomyParticipator { public boolean isWarp(String name); + public boolean hasWarpPassword(String warp); + + public boolean isWarpPassword(String warp, String password); + + public void setWarpPassword(String warp, String password); + public boolean removeWarp(String name); public void clearWarps(); diff --git a/src/main/java/com/massivecraft/factions/cmd/CmdFWarp.java b/src/main/java/com/massivecraft/factions/cmd/CmdFWarp.java index 908f77da..6d944b06 100644 --- a/src/main/java/com/massivecraft/factions/cmd/CmdFWarp.java +++ b/src/main/java/com/massivecraft/factions/cmd/CmdFWarp.java @@ -22,6 +22,7 @@ public class CmdFWarp extends FCommand { this.aliases.add("warp"); this.aliases.add("warps"); this.optionalArgs.put("warpname", "warpname"); + this.optionalArgs.put("password", "password"); this.permission = Permission.WARP.node; this.senderMustBeMember = true; @@ -38,11 +39,21 @@ public class CmdFWarp extends FCommand { msg.then(s + " ").tooltip(TL.COMMAND_FWARP_CLICKTOWARP.toString()).command("/" + Conf.baseCommandAliases.get(0) + " warp " + s).color(ChatColor.WHITE); } sendFancyMessage(msg); - } else if (args.size() > 1) { + } else if (args.size() > 2) { fme.msg(TL.COMMAND_FWARP_COMMANDFORMAT); } else { final String warpName = argAsString(0); + final String passwordAttempt = argAsString(1); + if (myFaction.isWarp(argAsString(0))) { + + // Check if requires password and if so, check if valid. CASE SENSITIVE + if (myFaction.hasWarpPassword(warpName) && !myFaction.isWarpPassword(warpName, passwordAttempt)) { + fme.msg(TL.COMMAND_FWARP_INVALID_PASSWORD); + return; + } + + // Check transaction AFTER password check. if (!transact(fme)) { return; } diff --git a/src/main/java/com/massivecraft/factions/cmd/CmdSetFWarp.java b/src/main/java/com/massivecraft/factions/cmd/CmdSetFWarp.java index 630540c5..a1973341 100644 --- a/src/main/java/com/massivecraft/factions/cmd/CmdSetFWarp.java +++ b/src/main/java/com/massivecraft/factions/cmd/CmdSetFWarp.java @@ -14,6 +14,7 @@ public class CmdSetFWarp extends FCommand { this.aliases.add("setwarp"); this.aliases.add("sw"); this.requiredArgs.add("warp name"); + this.optionalArgs.put("password", "password"); this.senderMustBeMember = true; this.senderMustBeModerator = true; this.senderMustBePlayer = true; @@ -38,9 +39,14 @@ public class CmdSetFWarp extends FCommand { } String warp = argAsString(0); + String password = argAsString(1); + LazyLocation loc = new LazyLocation(fme.getPlayer().getLocation()); myFaction.setWarp(warp, loc); - fme.msg(TL.COMMAND_SETFWARP_SET, warp); + if (password != null) { + myFaction.setWarpPassword(warp, password); + } + fme.msg(TL.COMMAND_SETFWARP_SET, warp, password != null ? password : ""); } private boolean transact(FPlayer player) { diff --git a/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFaction.java b/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFaction.java index 37315f6f..3f9ca6e9 100644 --- a/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFaction.java +++ b/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFaction.java @@ -41,6 +41,7 @@ public abstract class MemoryFaction implements Faction, EconomyParticipator { protected Set invites = new HashSet(); protected HashMap> announcements = new HashMap>(); protected ConcurrentHashMap warps = new ConcurrentHashMap(); + protected ConcurrentHashMap warpPasswords = new ConcurrentHashMap(); private long lastDeath; protected int maxVaults; @@ -92,6 +93,18 @@ public abstract class MemoryFaction implements Faction, EconomyParticipator { return warps.remove(name) != null; } + public boolean isWarpPassword(String warp, String password) { + return hasWarpPassword(warp) && warpPasswords.get(warp.toLowerCase()).equals(password); + } + + public boolean hasWarpPassword(String warp) { + return warpPasswords.containsKey(warp.toLowerCase()); + } + + public void setWarpPassword(String warp, String password) { + warpPasswords.put(warp.toLowerCase(), password); + } + public void clearWarps() { warps.clear(); } diff --git a/src/main/java/com/massivecraft/factions/zcore/util/TL.java b/src/main/java/com/massivecraft/factions/zcore/util/TL.java index 0db20eaa..c709ef7b 100644 --- a/src/main/java/com/massivecraft/factions/zcore/util/TL.java +++ b/src/main/java/com/massivecraft/factions/zcore/util/TL.java @@ -177,13 +177,14 @@ public enum TL { COMMAND_DISBAND_DESCRIPTION("Disband a faction"), COMMAND_FWARP_CLICKTOWARP("Click to warp!"), - COMMAND_FWARP_COMMANDFORMAT("/f warp "), + COMMAND_FWARP_COMMANDFORMAT("/f warp [password]"), COMMAND_FWARP_WARPED("Warped to %1$s"), COMMAND_FWARP_INVALID("Couldn't find warp %1$s"), COMMAND_FWARP_TOWARP("to warp"), COMMAND_FWARP_FORWARPING("for warping"), COMMAND_FWARP_WARPS("Warps: "), COMMAND_FWARP_DESCRIPTION("Teleport to a faction warp"), + COMMAND_FWARP_INVALID_PASSWORD("&4Invalid password!"), COMMAND_HELP_404("This page does not exist"), COMMAND_HELP_NEXTCREATE("Learn how to create a faction on the next page."), @@ -421,7 +422,7 @@ public enum TL { COMMAND_SETFWARP_NOTCLAIMED("You can only set warps in your faction territory."), COMMAND_SETFWARP_LIMIT("Your Faction already has the max amount of warps set (%1$d)."), - COMMAND_SETFWARP_SET("Set warp %1$s to your location."), + COMMAND_SETFWARP_SET("Set warp %1$s and password '%2$s' to your location."), COMMAND_SETFWARP_TOSET("to set warp"), COMMAND_SETFWARP_FORSET("for setting warp"), COMMAND_SETFWARP_DESCRIPTION("Set a faction warp"),