mirror of
https://github.com/YouHaveTrouble/PreventStabby.git
synced 2026-05-12 05:16:55 +00:00
implement pvp state override command and api
This commit is contained in:
@@ -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<br>
|
||||
* true - force enable PvP for every player<br>
|
||||
* false - force disable PvP for every player<br>
|
||||
* null - don't force PvP state<br>
|
||||
*/
|
||||
public static void setForcedPvpState(PvpState newForcedPvpState) {
|
||||
PreventStabby.getPlugin().getPlayerManager().setForcedPvpState(newForcedPvpState);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -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 <enabled/disabled/none>");
|
||||
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 <enabled/disabled/none>");
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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<String> combatBlockedCommands = new HashSet<>();
|
||||
@@ -76,6 +76,7 @@ public class ConfigCache {
|
||||
addDefault("messages.cannot_attack_pets_attacker", "<red>You can't attack pets while you have PvP turned off");
|
||||
addDefault("messages.cannot_attack_mounts_victim", "<red>You can't attack mounts of players that have PvP turned off");
|
||||
addDefault("messages.cannot_attack_mounts_attacker", "<red>You can't attack mounts while you have PvP turned off");
|
||||
addDefault("messages.cannot_attack_pvp_force_off", "<red>PvP is forcibly disabled");
|
||||
addDefault("messages.no_permission", "<red>You don't have permission to use that.");
|
||||
addDefault("messages.no_such_command", "<red>No such command.");
|
||||
addDefault("messages.pvp_enabled_others", "<red>You've enabled %player%'s PvP.");
|
||||
@@ -83,6 +84,9 @@ public class ConfigCache {
|
||||
addDefault("messages.entering_combat", "<red>Entering combat");
|
||||
addDefault("messages.leaving_combat", "<red>Leaving combat");
|
||||
addDefault("messages.cant_do_that_during_combat", "<red>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", "<red>You can't attack pets of players that have PvP turned off");
|
||||
this.cannot_attack_mounts_victim = config.getString("messages.cannot_attack_mounts_victim", "<red>You can't attack mounts of players that have PvP turned off");
|
||||
this.cannot_attack_mounts_attacker = config.getString("messages.cannot_attack_mounts_attacker", "<red>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", "<red>PvP is forcibly disabled");
|
||||
this.no_permission = config.getString("messages.no_permission", "<red>You don't have permission to use that.");
|
||||
this.no_such_command = config.getString("messages.no_such_command", "<red>No such command.");
|
||||
this.pvp_enabled_other = config.getString("messages.pvp_enabled_others", "<red>You've enabled %player%'s PvP.");
|
||||
@@ -138,6 +143,9 @@ public class ConfigCache {
|
||||
this.entering_combat = config.getString("messages.entering_combat", "<red>Entering combat");
|
||||
this.leaving_combat = config.getString("messages.leaving_combat", "<red>Leaving combat");
|
||||
this.cant_do_that_during_combat = config.getString("messages.cant_do_that_during_combat", "<red>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");
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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<UUID, PlayerData> 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 '&'.
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package me.youhavetrouble.preventstabby.util;
|
||||
|
||||
public enum PvpState {
|
||||
/**
|
||||
* PvP Force enabled
|
||||
*/
|
||||
ENABLED,
|
||||
/**
|
||||
* PvP force disabled
|
||||
*/
|
||||
DISABLED,
|
||||
/**
|
||||
* PvP not forced
|
||||
*/
|
||||
NONE
|
||||
}
|
||||
@@ -28,6 +28,9 @@ permissions:
|
||||
preventstabby.command.toggle.others:
|
||||
default: op
|
||||
description: Allows usage of /pvp <toggle/enable/disable> <player> command
|
||||
preventstabby.command.toggle.global:
|
||||
default: op
|
||||
description: Allows usage of /pvp override <enabled/disabled/none> command
|
||||
preventstabby.command.reload:
|
||||
default: op
|
||||
description: Allows usage of /pvp reload command
|
||||
Reference in New Issue
Block a user