From 196689db62838ab6fd26fe5afd499e62e0f0d08d Mon Sep 17 00:00:00 2001 From: FTTT <87281759+zFTTT@users.noreply.github.com> Date: Sun, 7 Nov 2021 01:50:13 -0700 Subject: [PATCH 1/2] Require confirmation for significant actions - Users will now be required to repeat the command when disbanding, leaving, kicking a player from, or transferring ownership within a guild. --- .../java/me/totalfreedom/tfguilds/Common.java | 5 ++- .../me/totalfreedom/tfguilds/TFGuilds.java | 4 +- .../tfguilds/command/DisbandSubCommand.java | 38 ++++++++++++++++--- .../tfguilds/command/KickSubCommand.java | 31 +++++++++++++-- .../tfguilds/command/LeaveSubCommand.java | 31 +++++++++++++-- .../tfguilds/command/SetOwnerSubCommand.java | 33 ++++++++++++++-- 6 files changed, 122 insertions(+), 20 deletions(-) diff --git a/src/main/java/me/totalfreedom/tfguilds/Common.java b/src/main/java/me/totalfreedom/tfguilds/Common.java index ffd279a..1531141 100644 --- a/src/main/java/me/totalfreedom/tfguilds/Common.java +++ b/src/main/java/me/totalfreedom/tfguilds/Common.java @@ -13,8 +13,9 @@ public class Common public static final String IN_GUILD = PREFIX + "You are already in a guild."; public static final String PLAYER_NOT_FOUND = PREFIX + "That player is not online."; public static final String PLAYER_NOT_IN_GUILD = PREFIX + "That player is not in your guild."; - public static final String IN_GAME_ONLY = PREFIX + "You must be in-game to interact with guilds"; - public static final String USAGE = PREFIX + "Correct usage: " + ChatColor.GOLD; + public static final String IN_GAME_ONLY = PREFIX + "You must be in-game to interact with guilds."; + public static final String USAGE = PREFIX + "Usage: " + ChatColor.GOLD; + public static final String WARN = PREFIX + ChatColor.RED + "Warning: " + ChatColor.GRAY; public static final TFMBridge tfmBridge = TFGuilds.getPlugin().getTfmBridge(); diff --git a/src/main/java/me/totalfreedom/tfguilds/TFGuilds.java b/src/main/java/me/totalfreedom/tfguilds/TFGuilds.java index 3d2cd17..8ae4e12 100644 --- a/src/main/java/me/totalfreedom/tfguilds/TFGuilds.java +++ b/src/main/java/me/totalfreedom/tfguilds/TFGuilds.java @@ -22,7 +22,7 @@ public class TFGuilds extends JavaPlugin private Config config; private SQLDatabase sqlDatabase; private TFMBridge tfmBridge; - private Map subCommands = new HashMap<>(); + private final Map subCommands = new HashMap<>(); public static TFGuilds getPlugin() { @@ -32,7 +32,7 @@ public class TFGuilds extends JavaPlugin @Override public void onEnable() { - this.plugin = this; + plugin = this; config = new Config("config.yml"); sqlDatabase = new SQLDatabase(this); User.loadAll(); diff --git a/src/main/java/me/totalfreedom/tfguilds/command/DisbandSubCommand.java b/src/main/java/me/totalfreedom/tfguilds/command/DisbandSubCommand.java index 35f8e14..609438d 100644 --- a/src/main/java/me/totalfreedom/tfguilds/command/DisbandSubCommand.java +++ b/src/main/java/me/totalfreedom/tfguilds/command/DisbandSubCommand.java @@ -1,16 +1,21 @@ package me.totalfreedom.tfguilds.command; +import java.util.HashMap; import me.totalfreedom.tfguilds.Common; +import me.totalfreedom.tfguilds.TFGuilds; import me.totalfreedom.tfguilds.guild.Guild; import org.apache.commons.lang.StringUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitRunnable; public class DisbandSubCommand extends Common implements SubCommand { + private final HashMap confirm = new HashMap<>(); + @Override public void execute(CommandSender sender, Player playerSender, String[] args) { @@ -35,9 +40,7 @@ public class DisbandSubCommand extends Common implements SubCommand return; } - guild.disband(); - sender.sendMessage(PREFIX + "The guild " + ChatColor.GOLD + guild.getName() + ChatColor.GRAY + " has been disbanded."); - Bukkit.broadcastMessage(PREFIX + ChatColor.GOLD + sender.getName() + ChatColor.GRAY + " has disbanded the guild " + ChatColor.GOLD + guild.getName()); + disband(sender, guild); return; } @@ -54,8 +57,31 @@ public class DisbandSubCommand extends Common implements SubCommand return; } - sender.sendMessage(PREFIX + "The guild " + ChatColor.GOLD + guild.getName() + ChatColor.GRAY + " has been disbanded."); - Bukkit.broadcastMessage(PREFIX + ChatColor.GOLD + sender.getName() + ChatColor.GRAY + " has disbanded the guild " + ChatColor.GOLD + guild.getName()); - guild.disband(); + disband(sender, guild); + } + private void disband(CommandSender sender, Guild guild) + { + if (!confirm.containsKey(sender)) + { + sender.sendMessage(WARN + "Are you sure you want to disband the guild " + + ChatColor.GOLD + guild.getName() + ChatColor.GRAY + "? Type " + + ChatColor.GOLD + "/g disband" + ChatColor.GRAY + " again within 10 seconds to confirm."); + confirm.put(sender, true); + new BukkitRunnable() + { + @Override + public void run() + { + confirm.remove(sender); + } + }.runTaskLater(TFGuilds.getPlugin(), 10 * 20); + } + else + { + confirm.remove(sender); + guild.disband(); + sender.sendMessage(PREFIX + "The guild " + ChatColor.GOLD + guild.getName() + ChatColor.GRAY + " has been disbanded."); + Bukkit.broadcastMessage(PREFIX + ChatColor.GOLD + sender.getName() + ChatColor.GRAY + " has disbanded the guild " + ChatColor.GOLD + guild.getName()); + } } } diff --git a/src/main/java/me/totalfreedom/tfguilds/command/KickSubCommand.java b/src/main/java/me/totalfreedom/tfguilds/command/KickSubCommand.java index f869833..ae75c32 100644 --- a/src/main/java/me/totalfreedom/tfguilds/command/KickSubCommand.java +++ b/src/main/java/me/totalfreedom/tfguilds/command/KickSubCommand.java @@ -1,15 +1,20 @@ package me.totalfreedom.tfguilds.command; +import java.util.HashMap; import me.totalfreedom.tfguilds.Common; +import me.totalfreedom.tfguilds.TFGuilds; import me.totalfreedom.tfguilds.guild.Guild; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitRunnable; public class KickSubCommand extends Common implements SubCommand { + private final HashMap confirm = new HashMap<>(); + @Override public void execute(CommandSender sender, Player playerSender, String[] args) { @@ -63,8 +68,28 @@ public class KickSubCommand extends Common implements SubCommand return; } - guild.removeMember(player); - sender.sendMessage(PREFIX + "Successfully kicked " + ChatColor.GOLD + player.getName() + ChatColor.GRAY + " from your guild."); - player.sendMessage(PREFIX + ChatColor.GOLD + sender.getName() + ChatColor.GRAY + " has kicked you from " + ChatColor.GOLD + guild.getName()); + if (!confirm.containsKey(sender) || !confirm.containsValue(player)) + { + sender.sendMessage(WARN + "Are you sure you want to kick " + + ChatColor.GOLD + player.getName() + ChatColor.GRAY + " from the guild? Type " + + ChatColor.GOLD + "/g kick " + player.getName() + + ChatColor.GRAY + " again within 10 seconds to confirm."); + confirm.put(sender, player); + new BukkitRunnable() + { + @Override + public void run() + { + confirm.remove(player); + } + }.runTaskLater(TFGuilds.getPlugin(), 10 * 20); + } + else + { + confirm.remove(sender); + guild.removeMember(player); + sender.sendMessage(PREFIX + "Successfully kicked " + ChatColor.GOLD + player.getName() + ChatColor.GRAY + " from the guild."); + player.sendMessage(PREFIX + ChatColor.GOLD + sender.getName() + ChatColor.GRAY + " has kicked you from " + ChatColor.GOLD + guild.getName()); + } } } diff --git a/src/main/java/me/totalfreedom/tfguilds/command/LeaveSubCommand.java b/src/main/java/me/totalfreedom/tfguilds/command/LeaveSubCommand.java index 1663ffd..ef5b8e2 100644 --- a/src/main/java/me/totalfreedom/tfguilds/command/LeaveSubCommand.java +++ b/src/main/java/me/totalfreedom/tfguilds/command/LeaveSubCommand.java @@ -1,14 +1,19 @@ package me.totalfreedom.tfguilds.command; +import java.util.HashMap; import me.totalfreedom.tfguilds.Common; +import me.totalfreedom.tfguilds.TFGuilds; import me.totalfreedom.tfguilds.guild.Guild; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitRunnable; public class LeaveSubCommand extends Common implements SubCommand { + private final HashMap confirm = new HashMap<>(); + @Override public void execute(CommandSender sender, Player playerSender, String[] args) { @@ -31,8 +36,28 @@ public class LeaveSubCommand extends Common implements SubCommand return; } - guild.removeMember(playerSender); - guild.broadcast(PREFIX + ChatColor.GOLD + sender.getName() + ChatColor.GRAY + " has left the guild."); - sender.sendMessage(PREFIX + "You have left the guild."); + if (!confirm.containsKey(sender)) + { + sender.sendMessage(WARN + "Are you sure you want to leave " + + ChatColor.GOLD + guild.getName() + ChatColor.GRAY + "? Type " + + ChatColor.GOLD + "/g leave" + + ChatColor.GRAY + " again within 10 seconds to confirm."); + confirm.put(sender, true); + new BukkitRunnable() + { + @Override + public void run() + { + confirm.remove(sender); + } + }.runTaskLater(TFGuilds.getPlugin(), 10 * 20); + } + else + { + confirm.remove(sender); + guild.removeMember(playerSender); + guild.broadcast(PREFIX + ChatColor.GOLD + sender.getName() + ChatColor.GRAY + " has left the guild."); + sender.sendMessage(PREFIX + "You have left the guild."); + } } } diff --git a/src/main/java/me/totalfreedom/tfguilds/command/SetOwnerSubCommand.java b/src/main/java/me/totalfreedom/tfguilds/command/SetOwnerSubCommand.java index 93b005c..ceffa3c 100644 --- a/src/main/java/me/totalfreedom/tfguilds/command/SetOwnerSubCommand.java +++ b/src/main/java/me/totalfreedom/tfguilds/command/SetOwnerSubCommand.java @@ -1,14 +1,18 @@ package me.totalfreedom.tfguilds.command; +import java.util.HashMap; import me.totalfreedom.tfguilds.Common; +import me.totalfreedom.tfguilds.TFGuilds; import me.totalfreedom.tfguilds.guild.Guild; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitRunnable; public class SetOwnerSubCommand extends Common implements SubCommand { + private final HashMap confirm = new HashMap<>(); @Override public void execute(CommandSender sender, Player playerSender, String[] args) @@ -57,9 +61,30 @@ public class SetOwnerSubCommand extends Common implements SubCommand return; } - guild.setOwner(player); - sender.sendMessage(PREFIX + "Successfully set " + ChatColor.GOLD + player.getName() + ChatColor.GRAY + " as the new owner of the guild."); - guild.broadcast(PREFIX + ChatColor.GOLD + sender.getName() + ChatColor.GRAY + " has set " + ChatColor.GOLD + player.getName() + ChatColor.GRAY + " as the new owner of the guild."); - player.sendMessage(PREFIX + ChatColor.GOLD + sender.getName() + ChatColor.GRAY + " has set you as the owner of " + ChatColor.GOLD + guild.getName()); + if (!confirm.containsKey(sender) || !confirm.containsValue(player)) + { + sender.sendMessage(WARN + "Are you sure you want to transfer ownership of " + + ChatColor.GOLD + guild.getName() + ChatColor.GRAY + " to " + + ChatColor.GOLD + player.getName() + ChatColor.GRAY + "? Type " + + ChatColor.GOLD + "/g setowner " + player.getName() + + ChatColor.GRAY + " again within 10 seconds to confirm."); + confirm.put(sender, player); + new BukkitRunnable() + { + @Override + public void run() + { + confirm.remove(sender); + } + }.runTaskLater(TFGuilds.getPlugin(), 10 * 20); + } + else + { + confirm.remove(sender); + guild.setOwner(player); + sender.sendMessage(PREFIX + "Successfully set " + ChatColor.GOLD + player.getName() + ChatColor.GRAY + " as the new owner of the guild."); + guild.broadcast(PREFIX + ChatColor.GOLD + sender.getName() + ChatColor.GRAY + " has set " + ChatColor.GOLD + player.getName() + ChatColor.GRAY + " as the new owner of the guild."); + player.sendMessage(PREFIX + ChatColor.GOLD + sender.getName() + ChatColor.GRAY + " has set you as the owner of " + ChatColor.GOLD + guild.getName()); + } } } From 5654fc9a0f3f1b06f83b9a49e15a6715cfa630be Mon Sep 17 00:00:00 2001 From: FTTT <87281759+zFTTT@users.noreply.github.com> Date: Sun, 7 Nov 2021 17:31:11 -0800 Subject: [PATCH 2/2] Use appropriate data structure - Convert the HashMaps in DisbandSubCommand and LeaveSubCommand to Lists --- .../totalfreedom/tfguilds/command/DisbandSubCommand.java | 9 +++++---- .../totalfreedom/tfguilds/command/LeaveSubCommand.java | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/main/java/me/totalfreedom/tfguilds/command/DisbandSubCommand.java b/src/main/java/me/totalfreedom/tfguilds/command/DisbandSubCommand.java index 609438d..99c4897 100644 --- a/src/main/java/me/totalfreedom/tfguilds/command/DisbandSubCommand.java +++ b/src/main/java/me/totalfreedom/tfguilds/command/DisbandSubCommand.java @@ -1,6 +1,7 @@ package me.totalfreedom.tfguilds.command; -import java.util.HashMap; +import java.util.ArrayList; +import java.util.List; import me.totalfreedom.tfguilds.Common; import me.totalfreedom.tfguilds.TFGuilds; import me.totalfreedom.tfguilds.guild.Guild; @@ -14,7 +15,7 @@ import org.bukkit.scheduler.BukkitRunnable; public class DisbandSubCommand extends Common implements SubCommand { - private final HashMap confirm = new HashMap<>(); + private final List confirm = new ArrayList<>(); @Override public void execute(CommandSender sender, Player playerSender, String[] args) @@ -61,12 +62,12 @@ public class DisbandSubCommand extends Common implements SubCommand } private void disband(CommandSender sender, Guild guild) { - if (!confirm.containsKey(sender)) + if (!confirm.contains(sender)) { sender.sendMessage(WARN + "Are you sure you want to disband the guild " + ChatColor.GOLD + guild.getName() + ChatColor.GRAY + "? Type " + ChatColor.GOLD + "/g disband" + ChatColor.GRAY + " again within 10 seconds to confirm."); - confirm.put(sender, true); + confirm.add(sender); new BukkitRunnable() { @Override diff --git a/src/main/java/me/totalfreedom/tfguilds/command/LeaveSubCommand.java b/src/main/java/me/totalfreedom/tfguilds/command/LeaveSubCommand.java index ef5b8e2..3e259a9 100644 --- a/src/main/java/me/totalfreedom/tfguilds/command/LeaveSubCommand.java +++ b/src/main/java/me/totalfreedom/tfguilds/command/LeaveSubCommand.java @@ -1,6 +1,7 @@ package me.totalfreedom.tfguilds.command; -import java.util.HashMap; +import java.util.ArrayList; +import java.util.List; import me.totalfreedom.tfguilds.Common; import me.totalfreedom.tfguilds.TFGuilds; import me.totalfreedom.tfguilds.guild.Guild; @@ -12,7 +13,7 @@ import org.bukkit.scheduler.BukkitRunnable; public class LeaveSubCommand extends Common implements SubCommand { - private final HashMap confirm = new HashMap<>(); + private final List confirm = new ArrayList<>(); @Override public void execute(CommandSender sender, Player playerSender, String[] args) @@ -36,13 +37,13 @@ public class LeaveSubCommand extends Common implements SubCommand return; } - if (!confirm.containsKey(sender)) + if (!confirm.contains(sender)) { sender.sendMessage(WARN + "Are you sure you want to leave " + ChatColor.GOLD + guild.getName() + ChatColor.GRAY + "? Type " + ChatColor.GOLD + "/g leave" + ChatColor.GRAY + " again within 10 seconds to confirm."); - confirm.put(sender, true); + confirm.add(sender); new BukkitRunnable() { @Override