From 62e4114466f232febe9da8e7509e1df7480e2f02 Mon Sep 17 00:00:00 2001 From: Driftay Date: Mon, 6 Apr 2020 16:19:41 -0400 Subject: [PATCH] Cooldowns initiated & Disable Open Broadcasts --- .../massivecraft/factions/cmd/CmdCreate.java | 7 +++++ .../massivecraft/factions/cmd/CmdDisband.java | 7 +++++ .../massivecraft/factions/cmd/CmdOpen.java | 8 ++++++ .../com/massivecraft/factions/cmd/CmdTag.java | 10 +++++++ .../listeners/FactionsBlockListener.java | 4 +-- .../massivecraft/factions/util/Cooldown.java | 27 +++++++++++++++++++ .../massivecraft/factions/zcore/util/TL.java | 2 ++ src/main/resources/config.yml | 4 +-- 8 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/massivecraft/factions/util/Cooldown.java diff --git a/src/main/java/com/massivecraft/factions/cmd/CmdCreate.java b/src/main/java/com/massivecraft/factions/cmd/CmdCreate.java index 13ab1833..ca5d16e9 100644 --- a/src/main/java/com/massivecraft/factions/cmd/CmdCreate.java +++ b/src/main/java/com/massivecraft/factions/cmd/CmdCreate.java @@ -8,6 +8,7 @@ import com.massivecraft.factions.event.FactionCreateEvent; import com.massivecraft.factions.integration.Econ; import com.massivecraft.factions.struct.Permission; import com.massivecraft.factions.struct.Role; +import com.massivecraft.factions.util.Cooldown; import com.massivecraft.factions.util.MiscUtil; import com.massivecraft.factions.zcore.util.TL; import net.dv8tion.jda.core.entities.Member; @@ -70,6 +71,11 @@ public class CmdCreate extends FCommand { return; } + if(Cooldown.isOnCooldown(context.fPlayer.getPlayer(), "createCooldwn")){ + context.msg(TL.COMMAND_COOLDOWN); + return; + } + // trigger the faction creation event (cancellable) FactionCreateEvent createEvent = new FactionCreateEvent(context.player, tag); Bukkit.getServer().getPluginManager().callEvent(createEvent); @@ -105,6 +111,7 @@ public class CmdCreate extends FCommand { // That way we don't have to mess up deleting more stuff. // And prevent the user from being returned to NORMAL after deleting his old faction. context.fPlayer.setRole(Role.LEADER); + Cooldown.setCooldown(context.fPlayer.getPlayer(), "createCooldown", FactionsPlugin.getInstance().getConfig().getInt("fcooldowns.f-create")); if (FactionsPlugin.getInstance().getConfig().getBoolean("faction-creation-broadcast", true)) { for (FPlayer follower : FPlayers.getInstance().getOnlinePlayers()) { follower.msg(TL.COMMAND_CREATE_CREATED, context.fPlayer.getName(), faction.getTag(follower)); diff --git a/src/main/java/com/massivecraft/factions/cmd/CmdDisband.java b/src/main/java/com/massivecraft/factions/cmd/CmdDisband.java index 59ca066e..d54517c4 100644 --- a/src/main/java/com/massivecraft/factions/cmd/CmdDisband.java +++ b/src/main/java/com/massivecraft/factions/cmd/CmdDisband.java @@ -4,6 +4,7 @@ import com.massivecraft.factions.*; import com.massivecraft.factions.event.FactionDisbandEvent.PlayerDisbandReason; import com.massivecraft.factions.struct.Permission; import com.massivecraft.factions.struct.Role; +import com.massivecraft.factions.util.Cooldown; import com.massivecraft.factions.zcore.fperms.Access; import com.massivecraft.factions.zcore.fperms.PermissableAction; import com.massivecraft.factions.zcore.frame.fdisband.FDisbandFrame; @@ -84,6 +85,10 @@ public class CmdDisband extends FCommand { } } + if(Cooldown.isOnCooldown(context.fPlayer.getPlayer(), "disbandCooldown")){ + context.msg(TL.COMMAND_COOLDOWN); + return; + } // check for tnt before disbanding. if (!disbandMap.containsKey(context.player.getUniqueId().toString()) && faction.getTnt() > 0) { @@ -106,12 +111,14 @@ public class CmdDisband extends FCommand { if (FactionsPlugin.getInstance().getConfig().getBoolean("enable-faction-flight")) { faction.disband(context.player, PlayerDisbandReason.COMMAND); context.fPlayer.setFFlying(false, false); + Cooldown.setCooldown(context.fPlayer.getPlayer(), "disbandCooldown", FactionsPlugin.getInstance().getConfig().getInt("fcooldowns.f-disband")); return; } } else { context.player.sendMessage(String.valueOf(TL.COMMAND_DISBAND_PLAYER)); } faction.disband(context.player, PlayerDisbandReason.COMMAND); + Cooldown.setCooldown(context.fPlayer.getPlayer(), "disbandCooldown", FactionsPlugin.getInstance().getConfig().getInt("fcooldowns.f-disband")); if (!context.fPlayer.canFlyAtLocation() && FactionsPlugin.getInstance().getConfig().getBoolean("enable-faction-flight")) { context.fPlayer.setFFlying(false, false); } diff --git a/src/main/java/com/massivecraft/factions/cmd/CmdOpen.java b/src/main/java/com/massivecraft/factions/cmd/CmdOpen.java index c1417647..eb6d4f60 100644 --- a/src/main/java/com/massivecraft/factions/cmd/CmdOpen.java +++ b/src/main/java/com/massivecraft/factions/cmd/CmdOpen.java @@ -6,6 +6,7 @@ import com.massivecraft.factions.FPlayers; import com.massivecraft.factions.FactionsPlugin; import com.massivecraft.factions.struct.Permission; import com.massivecraft.factions.struct.Role; +import com.massivecraft.factions.util.Cooldown; import com.massivecraft.factions.zcore.util.TL; public class CmdOpen extends FCommand { @@ -38,12 +39,19 @@ public class CmdOpen extends FCommand { String open = context.faction.getOpen() ? TL.COMMAND_OPEN_OPEN.toString() : TL.COMMAND_OPEN_CLOSED.toString(); + if(Cooldown.isOnCooldown(context.fPlayer.getPlayer(), "openCooldown")){ + context.msg(TL.COMMAND_COOLDOWN); + return; + } + // Inform for (FPlayer fplayer : FPlayers.getInstance().getOnlinePlayers()) { if (fplayer.getFactionId().equals(context.faction.getId())) { fplayer.msg(TL.COMMAND_OPEN_CHANGES, context.fPlayer.getName(), open); + Cooldown.setCooldown(fplayer.getPlayer(), "openCooldown", FactionsPlugin.getInstance().getConfig().getInt("fcooldowns.f-open")); continue; } + if(FactionsPlugin.getInstance().getConfig().getBoolean("faction-open-broadcast")) return; fplayer.msg(TL.COMMAND_OPEN_CHANGED, context.faction.getTag(fplayer.getFaction()), open); } }); diff --git a/src/main/java/com/massivecraft/factions/cmd/CmdTag.java b/src/main/java/com/massivecraft/factions/cmd/CmdTag.java index 356972d4..e3448422 100644 --- a/src/main/java/com/massivecraft/factions/cmd/CmdTag.java +++ b/src/main/java/com/massivecraft/factions/cmd/CmdTag.java @@ -7,6 +7,7 @@ import com.massivecraft.factions.event.FactionRenameEvent; import com.massivecraft.factions.scoreboards.FTeamWrapper; import com.massivecraft.factions.struct.Permission; import com.massivecraft.factions.struct.Role; +import com.massivecraft.factions.util.Cooldown; import com.massivecraft.factions.util.MiscUtil; import com.massivecraft.factions.zcore.util.TL; import org.bukkit.Bukkit; @@ -53,6 +54,11 @@ public class CmdTag extends FCommand { return; } + if(Cooldown.isOnCooldown(context.player, "tagCooldown")){ + context.msg(TL.COMMAND_COOLDOWN); + return; + } + // trigger the faction rename event (cancellable) FactionRenameEvent renameEvent = new FactionRenameEvent(context.fPlayer, tag); Bukkit.getServer().getPluginManager().callEvent(renameEvent); @@ -65,8 +71,11 @@ public class CmdTag extends FCommand { return; } + + String oldtag = context.faction.getTag(); context.faction.setTag(tag); + Discord.changeFactionTag(context.faction, oldtag); FactionsPlugin.instance.logFactionEvent(context.faction, FLogType.FTAG_EDIT, context.fPlayer.getName(), tag); @@ -75,6 +84,7 @@ public class CmdTag extends FCommand { for (FPlayer fplayer : FPlayers.getInstance().getOnlinePlayers()) { if (fplayer.getFactionId().equals(context.faction.getId())) { fplayer.msg(TL.COMMAND_TAG_FACTION, context.fPlayer.describeTo(context.faction, true), context.faction.getTag(context.faction)); + Cooldown.setCooldown(fplayer.getPlayer(), "tagCooldown", FactionsPlugin.getInstance().getConfig().getInt("fcooldowns.f-tag")); continue; } // Broadcast the tag change (if applicable) diff --git a/src/main/java/com/massivecraft/factions/listeners/FactionsBlockListener.java b/src/main/java/com/massivecraft/factions/listeners/FactionsBlockListener.java index 6eb07329..e4a19149 100644 --- a/src/main/java/com/massivecraft/factions/listeners/FactionsBlockListener.java +++ b/src/main/java/com/massivecraft/factions/listeners/FactionsBlockListener.java @@ -425,8 +425,7 @@ public class FactionsBlockListener implements Listener { if (!justCheck) fPlayer.setLastFrostwalkerMessage(); // Check if they have build permissions here. If not, block this from happening. - if (!playerCanBuildDestroyBlock(player, location, PermissableAction.FROST_WALK.toString(), justCheck)) - event.setCancelled(true); + if (!playerCanBuildDestroyBlock(player, location, PermissableAction.FROST_WALK.toString(), justCheck)) event.setCancelled(true); } @EventHandler @@ -486,7 +485,6 @@ public class FactionsBlockListener implements Listener { Faction at = Board.getInstance().getFactionAt(new FLocation(block)); boolean isSpawner = event.getBlock().getType().equals(XMaterial.SPAWNER.parseMaterial()); - if (!playerCanBuildDestroyBlock(event.getPlayer(), event.getBlock().getLocation(), "destroy", false)) { event.setCancelled(true); return; diff --git a/src/main/java/com/massivecraft/factions/util/Cooldown.java b/src/main/java/com/massivecraft/factions/util/Cooldown.java new file mode 100644 index 00000000..c9011fd3 --- /dev/null +++ b/src/main/java/com/massivecraft/factions/util/Cooldown.java @@ -0,0 +1,27 @@ +package com.massivecraft.factions.util; + +import com.massivecraft.factions.FactionsPlugin; +import org.bukkit.entity.Player; +import org.bukkit.metadata.FixedMetadataValue; +import org.bukkit.metadata.MetadataValue; + +import java.util.concurrent.TimeUnit; + +/** + * Factions - Developed by Driftay. + * All rights reserved 2020. + * Creation Date: 4/6/2020 + */ +public class Cooldown { + + public static void setCooldown(Player player, String name, int seconds) { + player.setMetadata(name, new FixedMetadataValue(FactionsPlugin.getInstance(), System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(seconds))); + } + + public static boolean isOnCooldown(Player player, String name) { + if (!player.hasMetadata(name) || player.getMetadata(name).size() <= 0) return false; + long time = player.getMetadata(name).get(0).asLong(); + return (time > System.currentTimeMillis()); + } + +} 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 54198ab5..96f748f5 100644 --- a/src/main/java/com/massivecraft/factions/zcore/util/TL.java +++ b/src/main/java/com/massivecraft/factions/zcore/util/TL.java @@ -611,6 +611,8 @@ public enum TL { COMMAND_MONEYWITHDRAW_DESCRIPTION("Withdraw money"), COMMAND_MONEYWITHDRAW_WITHDRAW("&c&l[!]&7 &c%1$s&7 withdrew&c %2$s &7from the faction bank:&c %3$s"), + + COMMAND_COOLDOWN("&c&l[!] &7You are currently on cooldown for this command!"), COMMAND_OPEN_TOOPEN("to open or close the faction"), COMMAND_OPEN_FOROPEN("for opening or closing the faction"), COMMAND_OPEN_OPEN("open"), diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 275bdc93..aeffffbc 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -594,11 +594,12 @@ fwarp-gui: - ' ' ############################################################ # +------------------------------------------------------+ # -# | Faction Creation/Disband Broadcast | # +# | Faction Broadcasts | # # +------------------------------------------------------+ # ############################################################ faction-creation-broadcast: true #Disabling this will not make faction creation broadcasts appear in chat. faction-disband-broadcast: true #Disabling this will not make faction disband broadcasts appear in chat. +faction-open-broadcast: true #Disabling this will not make faction open broadcasts appear in chat. ############################################################ # +------------------------------------------------------+ # @@ -729,7 +730,6 @@ F-Shop: # | Faction Command Cooldowns | # # +------------------------------------------------------+ # ############################################################ -#To-Do# fcooldowns: f-open: 5 f-disband: 10