From 9b18c435f6a238e681121df17f6ba19bfbf89917 Mon Sep 17 00:00:00 2001 From: YouHaveTrouble Date: Fri, 17 Jun 2022 21:12:55 +0200 Subject: [PATCH] implement pvp state override command and api --- pom.xml | 2 +- .../preventstabby/api/PreventStabbyAPI.java | 21 ++++++++ .../commands/GlobalToggleCommand.java | 48 +++++++++++++++++++ .../preventstabby/commands/MainCommand.java | 16 ++++++- .../commands/PvpToggleCommand.java | 2 +- .../preventstabby/config/ConfigCache.java | 12 ++++- .../config/PreventStabbyPermission.java | 11 +++-- .../preventstabby/players/PlayerManager.java | 38 ++++++++++----- .../preventstabby/util/PluginMessages.java | 6 +++ .../preventstabby/util/PvpState.java | 16 +++++++ src/main/resources/plugin.yml | 3 ++ 11 files changed, 153 insertions(+), 22 deletions(-) create mode 100644 src/main/java/me/youhavetrouble/preventstabby/commands/GlobalToggleCommand.java create mode 100644 src/main/java/me/youhavetrouble/preventstabby/util/PvpState.java diff --git a/pom.xml b/pom.xml index 7890f60..3b09c92 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ me.youhavetrouble PreventStabby - 1.5.1 + 1.6.0 jar PreventStabby diff --git a/src/main/java/me/youhavetrouble/preventstabby/api/PreventStabbyAPI.java b/src/main/java/me/youhavetrouble/preventstabby/api/PreventStabbyAPI.java index f71047a..0238169 100644 --- a/src/main/java/me/youhavetrouble/preventstabby/api/PreventStabbyAPI.java +++ b/src/main/java/me/youhavetrouble/preventstabby/api/PreventStabbyAPI.java @@ -2,7 +2,10 @@ package me.youhavetrouble.preventstabby.api; import me.youhavetrouble.preventstabby.PreventStabby; import me.youhavetrouble.preventstabby.util.CombatTimer; +import me.youhavetrouble.preventstabby.util.PvpState; import org.bukkit.entity.Player; + +import javax.annotation.Nullable; import java.util.UUID; public class PreventStabbyAPI { @@ -139,5 +142,23 @@ public class PreventStabbyAPI { return CombatTimer.isInCombat(player.getUniqueId()); } + /** + * @return Current state of forced PvP + */ + public static PvpState getForcedPvpState() { + return PreventStabby.getPlugin().getPlayerManager().getForcedPvpState(); + } + + /** + * + * @param newForcedPvpState
+ * true - force enable PvP for every player
+ * false - force disable PvP for every player
+ * null - don't force PvP state
+ */ + public static void setForcedPvpState(PvpState newForcedPvpState) { + PreventStabby.getPlugin().getPlayerManager().setForcedPvpState(newForcedPvpState); + } } + diff --git a/src/main/java/me/youhavetrouble/preventstabby/commands/GlobalToggleCommand.java b/src/main/java/me/youhavetrouble/preventstabby/commands/GlobalToggleCommand.java new file mode 100644 index 0000000..43adddf --- /dev/null +++ b/src/main/java/me/youhavetrouble/preventstabby/commands/GlobalToggleCommand.java @@ -0,0 +1,48 @@ +package me.youhavetrouble.preventstabby.commands; + +import me.youhavetrouble.preventstabby.PreventStabby; +import me.youhavetrouble.preventstabby.config.PreventStabbyPermission; +import me.youhavetrouble.preventstabby.util.PluginMessages; +import me.youhavetrouble.preventstabby.util.PvpState; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; + +import java.util.Locale; + +public class GlobalToggleCommand { + public static void globalToggle(CommandSender sender, String[] args) { + Bukkit.getScheduler().runTaskAsynchronously(PreventStabby.getPlugin(), () -> { + if (!PreventStabbyPermission.COMMAND_GLOBAL_TOGGLE.doesCommandSenderHave(sender)) { + PluginMessages.sendMessage(sender, PreventStabby.getPlugin().getConfigCache().getNo_permission()); + return; + } + + if (args.length != 2) { + PluginMessages.sendMessage(sender, "Try /pvp override "); + return; + } + + PvpState pvpState = PvpState.valueOf(args[1].toUpperCase(Locale.ROOT)); + + switch (pvpState) { + case ENABLED: + PreventStabby.getPlugin().getPlayerManager().setForcedPvpState(PvpState.ENABLED); + PluginMessages.broadcastMessage(PreventStabby.getPlugin().getConfigCache().getForce_pvp_on()); + break; + case DISABLED: + PreventStabby.getPlugin().getPlayerManager().setForcedPvpState(PvpState.DISABLED); + PluginMessages.broadcastMessage(PreventStabby.getPlugin().getConfigCache().getForce_pvp_off()); + break; + case NONE: + PreventStabby.getPlugin().getPlayerManager().setForcedPvpState(PvpState.NONE); + PluginMessages.broadcastMessage(PreventStabby.getPlugin().getConfigCache().getForce_pvp_none()); + break; + default: + PluginMessages.sendMessage(sender, "Try /pvp override "); + } + + }); + + } + +} diff --git a/src/main/java/me/youhavetrouble/preventstabby/commands/MainCommand.java b/src/main/java/me/youhavetrouble/preventstabby/commands/MainCommand.java index 41818be..b01ca71 100644 --- a/src/main/java/me/youhavetrouble/preventstabby/commands/MainCommand.java +++ b/src/main/java/me/youhavetrouble/preventstabby/commands/MainCommand.java @@ -8,6 +8,7 @@ import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabExecutor; import org.bukkit.entity.Player; +import org.bukkit.util.StringUtil; import java.util.ArrayList; import java.util.HashMap; @@ -26,6 +27,7 @@ public class MainCommand implements TabExecutor { subCommands.put("off", PreventStabbyPermission.COMMAND_TOGGLE); subCommands.put("disable", PreventStabbyPermission.COMMAND_TOGGLE); subCommands.put("reload", PreventStabbyPermission.COMMAND_RELOAD); + subCommands.put("override", PreventStabbyPermission.COMMAND_GLOBAL_TOGGLE); } @Override @@ -53,6 +55,9 @@ public class MainCommand implements TabExecutor { case "reload": ReloadCommand.reload(sender); break; + case "override": + GlobalToggleCommand.globalToggle(sender, args); + break; default: PluginMessages.sendMessage(sender, PreventStabby.getPlugin().getConfigCache().getNo_such_command()); break; @@ -74,7 +79,7 @@ public class MainCommand implements TabExecutor { commands.add(entry.getKey()); } return commands; - } else if (args.length == 2 && PreventStabbyPermission.COMMAND_TOGGLE.doesCommandSenderHave(sender)) { + } else if (args.length == 2) { switch (args[0].toLowerCase()) { default: break; @@ -83,12 +88,19 @@ public class MainCommand implements TabExecutor { case "enable": case "off": case "disable": + if (!PreventStabbyPermission.COMMAND_TOGGLE_OTHERS.doesCommandSenderHave(sender)) break; for (Player player : Bukkit.getOnlinePlayers()) { commands.add(player.getName()); } break; + case "override": + if (!PreventStabbyPermission.COMMAND_GLOBAL_TOGGLE.doesCommandSenderHave(sender)) break; + commands.add("enabled"); + commands.add("disabled"); + commands.add("none"); + break; } - return commands; + return StringUtil.copyPartialMatches(args[1], commands, new ArrayList<>()); } return null; } diff --git a/src/main/java/me/youhavetrouble/preventstabby/commands/PvpToggleCommand.java b/src/main/java/me/youhavetrouble/preventstabby/commands/PvpToggleCommand.java index 1a6c04d..5eaba0c 100644 --- a/src/main/java/me/youhavetrouble/preventstabby/commands/PvpToggleCommand.java +++ b/src/main/java/me/youhavetrouble/preventstabby/commands/PvpToggleCommand.java @@ -13,7 +13,7 @@ public class PvpToggleCommand { public static void toggle(CommandSender sender, String[] args) { Bukkit.getScheduler().runTaskAsynchronously(PreventStabby.getPlugin(), () -> { if (!PreventStabbyPermission.COMMAND_TOGGLE.doesCommandSenderHave(sender)) { - PluginMessages.parseMessage(sender, PreventStabby.getPlugin().getConfigCache().getNo_permission()); + PluginMessages.sendMessage(sender, PreventStabby.getPlugin().getConfigCache().getNo_permission()); return; } diff --git a/src/main/java/me/youhavetrouble/preventstabby/config/ConfigCache.java b/src/main/java/me/youhavetrouble/preventstabby/config/ConfigCache.java index 5ed0fcd..29cd1b5 100644 --- a/src/main/java/me/youhavetrouble/preventstabby/config/ConfigCache.java +++ b/src/main/java/me/youhavetrouble/preventstabby/config/ConfigCache.java @@ -20,8 +20,8 @@ public class ConfigCache { @Getter private final String pvp_enabled, pvp_disabled, cannot_attack_victim, cannot_attack_attacker, cannot_attack_pets_victim, cannot_attack_pets_attacker, no_permission, no_such_command, pvp_enabled_other, pvp_disabled_other, punish_for_combat_logout_message, entering_combat, leaving_combat, - cant_do_that_during_combat, cannot_attack_mounts_attacker, cannot_attack_mounts_victim, - placeholder_combat_time, placeholder_not_in_combat; + cant_do_that_during_combat, cannot_attack_mounts_attacker, cannot_attack_mounts_victim, force_pvp_on, + force_pvp_off, force_pvp_none, placeholder_combat_time, placeholder_not_in_combat, cannot_attack_pvp_force_off; @Getter private final double lava_and_fire_stopper_radius; @Getter private final long cache_time, combat_time, login_protection_time, teleport_protection_time; @Getter private final Set combatBlockedCommands = new HashSet<>(); @@ -76,6 +76,7 @@ public class ConfigCache { addDefault("messages.cannot_attack_pets_attacker", "You can't attack pets while you have PvP turned off"); addDefault("messages.cannot_attack_mounts_victim", "You can't attack mounts of players that have PvP turned off"); addDefault("messages.cannot_attack_mounts_attacker", "You can't attack mounts while you have PvP turned off"); + addDefault("messages.cannot_attack_pvp_force_off", "PvP is forcibly disabled"); addDefault("messages.no_permission", "You don't have permission to use that."); addDefault("messages.no_such_command", "No such command."); addDefault("messages.pvp_enabled_others", "You've enabled %player%'s PvP."); @@ -83,6 +84,9 @@ public class ConfigCache { addDefault("messages.entering_combat", "Entering combat"); addDefault("messages.leaving_combat", "Leaving combat"); addDefault("messages.cant_do_that_during_combat", "You can't do that while in combat!"); + addDefault("messages.force_pvp_on", "PvP is now force enabled"); + addDefault("messages.force_pvp_off", "PvP is now force disabled"); + addDefault("messages.force_pvp_none", "PvP state is not forced now"); addDefault("placeholder.placeholder_combat_time", "Combat time: %time%"); addDefault("placeholder.not_in_combat", "Not in combat"); } @@ -131,6 +135,7 @@ public class ConfigCache { this.cannot_attack_pets_attacker = config.getString("messages.cannot_attack_pets_attacker", "You can't attack pets of players that have PvP turned off"); this.cannot_attack_mounts_victim = config.getString("messages.cannot_attack_mounts_victim", "You can't attack mounts of players that have PvP turned off"); this.cannot_attack_mounts_attacker = config.getString("messages.cannot_attack_mounts_attacker", "You can't attack mounts while you have PvP turned off"); + this.cannot_attack_pvp_force_off = config.getString("messages.cannot_attack_pvp_force_off", "PvP is forcibly disabled"); this.no_permission = config.getString("messages.no_permission", "You don't have permission to use that."); this.no_such_command = config.getString("messages.no_such_command", "No such command."); this.pvp_enabled_other = config.getString("messages.pvp_enabled_others", "You've enabled %player%'s PvP."); @@ -138,6 +143,9 @@ public class ConfigCache { this.entering_combat = config.getString("messages.entering_combat", "Entering combat"); this.leaving_combat = config.getString("messages.leaving_combat", "Leaving combat"); this.cant_do_that_during_combat = config.getString("messages.cant_do_that_during_combat", "You can't do that while in combat!"); + this.force_pvp_on = config.getString("messages.force_pvp_on", "PvP is now force enabled"); + this.force_pvp_off = config.getString("messages.force_pvp_off", "PvP is now force disabled"); + this.force_pvp_none = config.getString("messages.force_pvp_none", "PvP state is not forced now"); this.placeholder_combat_time = config.getString("placeholder.placeholder_combat_time", "Combat time: %time%"); this.placeholder_not_in_combat = config.getString("placeholder.not_in_combat", "Not in combat"); diff --git a/src/main/java/me/youhavetrouble/preventstabby/config/PreventStabbyPermission.java b/src/main/java/me/youhavetrouble/preventstabby/config/PreventStabbyPermission.java index 51d95c1..e9c1658 100644 --- a/src/main/java/me/youhavetrouble/preventstabby/config/PreventStabbyPermission.java +++ b/src/main/java/me/youhavetrouble/preventstabby/config/PreventStabbyPermission.java @@ -4,14 +4,15 @@ import org.bukkit.command.CommandSender; public enum PreventStabbyPermission { - COMMAND("preventstabby.command"), - COMMAND_TOGGLE("preventstabby.command.toggle"), - COMMAND_TOGGLE_OTHERS("preventstabby.command.toggle.others"), - COMMAND_RELOAD("preventstabby.command.reload"); + COMMAND("command"), + COMMAND_TOGGLE("command.toggle"), + COMMAND_TOGGLE_OTHERS("command.toggle.others"), + COMMAND_RELOAD("command.reload"), + COMMAND_GLOBAL_TOGGLE("command.toggle.global"); private final String permission; PreventStabbyPermission(String permission) { - this.permission = permission; + this.permission = "preventstabby."+permission; } public boolean doesCommandSenderHave(CommandSender sender) { diff --git a/src/main/java/me/youhavetrouble/preventstabby/players/PlayerManager.java b/src/main/java/me/youhavetrouble/preventstabby/players/PlayerManager.java index dc048b5..788716a 100644 --- a/src/main/java/me/youhavetrouble/preventstabby/players/PlayerManager.java +++ b/src/main/java/me/youhavetrouble/preventstabby/players/PlayerManager.java @@ -7,6 +7,7 @@ import me.youhavetrouble.preventstabby.hooks.WorldGuardHook; import me.youhavetrouble.preventstabby.util.CombatTimer; import me.youhavetrouble.preventstabby.util.PluginMessages; import lombok.Getter; +import me.youhavetrouble.preventstabby.util.PvpState; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitTask; @@ -20,6 +21,8 @@ public class PlayerManager { @Getter ConcurrentHashMap playerList = new ConcurrentHashMap<>(); + private PvpState pvpForcedState = PvpState.NONE; + public final BukkitTask combatTrackerTask; public PlayerManager() { @@ -61,18 +64,13 @@ public class PlayerManager { } public void refreshPlayersCombatTime(UUID uuid) { - try { - PlayerData data = playerList.get(uuid); - if (data == null) - return; - Player player = Bukkit.getPlayer(uuid); - if (player == null || player.isDead()) - return; + PlayerData data = playerList.get(uuid); + if (data == null) return; + Player player = Bukkit.getPlayer(uuid); + if (player == null || player.isDead()) return; + data.refreshCombatTime(); + data.setInCombat(true); - data.refreshCombatTime(); - data.setInCombat(true); - } catch (Exception ignored) { - } } public PlayerData getPlayer(UUID uuid) { @@ -112,6 +110,17 @@ public class PlayerManager { if (checkVictimSpawnProtection && hasLoginProtection(victim)) return false; if (checkVictimSpawnProtection && hasTeleportProtection(victim)) return false; + switch (pvpForcedState) { + case ENABLED: + return true; + case DISABLED: + PluginMessages.sendActionBar(attacker, PreventStabby.getPlugin().getConfigCache().getCannot_attack_pvp_force_off()); + return false; + case NONE: + default: + break; + } + SmartCache smartCache = PreventStabby.getPlugin().getSmartCache(); if (!smartCache.getPlayerData(attacker).isPvpEnabled()) { @@ -157,4 +166,11 @@ public class PlayerManager { return Instant.now().getEpochSecond() < smartCache.getPlayerData(uuid).getTeleportTimestamp(); } + public PvpState getForcedPvpState() { + return pvpForcedState; + } + + public void setForcedPvpState(PvpState forcedPvpState) { + this.pvpForcedState = forcedPvpState; + } } diff --git a/src/main/java/me/youhavetrouble/preventstabby/util/PluginMessages.java b/src/main/java/me/youhavetrouble/preventstabby/util/PluginMessages.java index 456e773..70c27ae 100644 --- a/src/main/java/me/youhavetrouble/preventstabby/util/PluginMessages.java +++ b/src/main/java/me/youhavetrouble/preventstabby/util/PluginMessages.java @@ -5,6 +5,7 @@ import me.youhavetrouble.preventstabby.PreventStabby; import net.kyori.adventure.platform.bukkit.BukkitAudiences; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -18,6 +19,7 @@ public class PluginMessages { public static Component parseMessage(String message) { message = makeColorsWork('&', message); + message = makeColorsWork(LegacyComponentSerializer.SECTION_CHAR, message); return MINIMESSAGE.deserialize(message); } @@ -62,6 +64,10 @@ public class PluginMessages { audiences.all().sendMessage(parseMessage(message)); } + public static void broadcastMessage(String message) { + audiences.all().sendMessage(parseMessage(message)); + } + /** * Swaps most legacy color codes to adventure minimessage tags. * @param symbol Usually '&'. diff --git a/src/main/java/me/youhavetrouble/preventstabby/util/PvpState.java b/src/main/java/me/youhavetrouble/preventstabby/util/PvpState.java new file mode 100644 index 0000000..9aac00b --- /dev/null +++ b/src/main/java/me/youhavetrouble/preventstabby/util/PvpState.java @@ -0,0 +1,16 @@ +package me.youhavetrouble.preventstabby.util; + +public enum PvpState { + /** + * PvP Force enabled + */ + ENABLED, + /** + * PvP force disabled + */ + DISABLED, + /** + * PvP not forced + */ + NONE +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 2667bd0..99a5457 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -28,6 +28,9 @@ permissions: preventstabby.command.toggle.others: default: op description: Allows usage of /pvp command + preventstabby.command.toggle.global: + default: op + description: Allows usage of /pvp override command preventstabby.command.reload: default: op description: Allows usage of /pvp reload command \ No newline at end of file