Compare commits

...

17 Commits

Author SHA1 Message Date
YouHaveTrouble 9a707b34a3 bump version 2023-03-26 03:17:46 +02:00
YouHaveTrouble d52648b4ff Merge pull request #13 from YouHaveTrouble/api-changes
Api changes + worldguard flag fix for 1.19.4
2023-03-26 03:15:42 +02:00
YouHaveTrouble 7d2c41646e make worldguard hook register on load 2023-03-26 03:09:07 +02:00
YouHaveTrouble 61908b8fab more api changes and javadocs 2022-12-31 17:09:20 +01:00
YouHaveTrouble 5abf99cd6c semi-major api changes, javadocs 2022-12-31 16:44:50 +01:00
YouHaveTrouble 3c014304ba implement PlayerTogglePvpEvent 2022-12-31 15:58:06 +01:00
YouHaveTrouble b361681600 bump version 2022-12-31 15:03:15 +01:00
YouHaveTrouble 7ada9eab29 permission for help command 2022-12-31 15:03:09 +01:00
YouHaveTrouble 78504ee097 command without arguments now acts as toggle subcommand 2022-12-31 14:43:44 +01:00
YouHaveTrouble 7da6ff0ac9 placeholder for pvp state and offline player fallbacks to not cause sync db calls 2022-12-31 13:46:01 +01:00
YouHaveTrouble c83888837b fix dependency issues on older server versions 2022-12-04 22:26:45 +01:00
YouHaveTrouble 4052afe4d3 version bump 2022-08-07 12:13:15 +02:00
YouHaveTrouble 93efa37e77 flip the conditional 2022-08-07 11:59:22 +02:00
YouHaveTrouble 44a5c6d800 bump version 2022-07-19 17:36:14 +02:00
YouHaveTrouble 2ee577a08c setting to allow players being pulled with fishing rods while having pvp off 2022-07-19 17:35:04 +02:00
YouHaveTrouble f5d38eefc3 update readme 2022-06-17 23:35:43 +02:00
YouHaveTrouble e5b566db67 fix javadoc 2022-06-17 22:56:31 +02:00
21 changed files with 317 additions and 168 deletions
+10 -6
View File
@@ -6,7 +6,7 @@
<groupId>me.youhavetrouble</groupId>
<artifactId>PreventStabby</artifactId>
<version>1.6.0</version>
<version>1.9.0</version>
<packaging>jar</packaging>
<name>PreventStabby</name>
@@ -49,6 +49,10 @@
<pattern>org.bstats</pattern>
<shadedPattern>bstats</shadedPattern>
</relocation>
<relocation>
<pattern>net.kyori</pattern>
<shadedPattern>kyori</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
@@ -96,7 +100,7 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<version>1.18.26</version>
<scope>provided</scope>
</dependency>
<dependency>
@@ -130,25 +134,25 @@
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-api</artifactId>
<version>4.11.0</version>
<version>4.12.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-platform-bukkit</artifactId>
<version>4.1.1</version>
<version>4.2.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-text-minimessage</artifactId>
<version>4.11.0</version>
<version>4.12.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-text-serializer-legacy</artifactId>
<version>4.11.0</version>
<version>4.12.0</version>
</dependency>
</dependencies>
</project>
+28 -37
View File
@@ -1,43 +1,34 @@
<h2>PreventStabby ⚔</h2>
<h1>PreventStabby ⚔</h1>
Use Issues section to report bugs or submit suggestions.
<h3>Commands</h3>
Permission: <code>preventstabby.command</code> (given by default to everyone) :
You can find available commands <a href="https://github.com/YouHaveTrouble/PreventStabby/wiki/Commands">HERE</a>.
<h3>Configuration</h3>
Explanation of all available config options is <a href="https://github.com/YouHaveTrouble/PreventStabby/wiki/Config">HERE</a>.
<h3>Placeholders</h3>
List of all PlaceholderAPI placeholders can be found <a href="https://github.com/YouHaveTrouble/PreventStabby/wiki/Placeholders">HERE</a>.
<h3>Developer API</h3>
Developer API documetation is <a href="https://github.com/YouHaveTrouble/PreventStabby/wiki/Developer-API">HERE</a>!
<h2>Features</h2>
<ul>
<li>/pvp help- displays help message</li>
<li>Per-player PvP Toggle</li>
<li>Global PvP override, allowing to ignore personal toggles temporarily</li>
<li>Block all forms of player-induced damage when PvP off</li>
<li>Block damage done to pets and mounts when PvP off</li>
<li>Block the ability to pour lava and set fire under player's feet</li>
<li>Block channeling tridents spawning fire under players with PvP off</li>
<li>Block fishing pole pulling for players with PvP off</li>
<li>Pets do not attack players with PvP off</li>
<li>Configurable PvP protection after login</li>
<li>Configurable PvP protection after teleport</li>
<li>Kill players that logged out during combat</li>
<li>Block specific commands while in combat</li>
<li>Block teleports in combat (might cause issues with some plugins)</li>
<li>Toggles for eggs and snowballs applying knockback</li>
<li>Extensive developer API</li>
</ul>
Permission: <code>preventstabby.command.toggle</code> (given by default to everyone) :
<ul>
<li>/pvp toggle - toggles PvP</li>
<li>/pvp on - enables PvP (aliases: enable)</li>
<li>/pvp off - disables PvP (aliases: disable)</li>
</ul>
Permission: <code>preventstabby.command.toggle.others</code> (given by default to ops) :
<ul>
<li>/pvp toggle [player] - toggles other player's PvP</li>
<li>/pvp on [player] - enables other player's PvP (aliases: enable)</li>
<li>/pvp off [player] - disables other player's PvP (aliases: disable)</li>
</ul>
<h3>Features</h3>
Player who disables PvP cannot get hurt by any player induced damage.
They also can't deal any damage to other players.
This includes basic attacks, projectiles, splash potions,
lingering potions and lightning strikes from channeling trident enchant.
Additionally lightning strikes from channeling cannot spawn fire under
players, who have pvp disabled and it's impossible to use
fishing rod on players who have pvp disabled.
There is also a feature that disallows pouring lava or using
flint and steel nearby players with disabled pvp. You can
either disable it in config or set its radius.
All the protections are extended to tameable entities (pets) that set player's uuid as their owner on tame.
Pets will not attack players with pvp off or if their owner has pvp off.
@@ -76,13 +76,6 @@ public final class PreventStabby extends JavaPlugin {
pvpCommand.setExecutor(mainCommand);
pvpCommand.setTabCompleter(mainCommand);
try {
WorldGuardHook.init();
worldGuardHook = true;
} catch (NoClassDefFoundError e) {
worldGuardHook = false;
}
if (getServer().getPluginManager().getPlugin("PlaceholderAPI") != null) {
new PlacoholderApiHook(this).register();
}
@@ -90,6 +83,18 @@ public final class PreventStabby extends JavaPlugin {
Metrics metrics = new Metrics(this, 14074);
}
@Override
public void onLoad() {
if (getServer().getPluginManager().getPlugin("WorldGuard") != null) {
try {
WorldGuardHook.init();
worldGuardHook = true;
} catch (NoClassDefFoundError e) {
worldGuardHook = false;
}
}
}
public static boolean worldGuardHookEnabled() {
return worldGuardHook;
}
@@ -150,11 +150,7 @@ public class PreventStabbyAPI {
}
/**
*
* @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>
* Sets forced PvP state until server restart or changed with override command or this method.
*/
public static void setForcedPvpState(PvpState newForcedPvpState) {
PreventStabby.getPlugin().getPlayerManager().setForcedPvpState(newForcedPvpState);
@@ -1,42 +1,55 @@
package me.youhavetrouble.preventstabby.api.event;
import org.bukkit.entity.Player;
import me.youhavetrouble.preventstabby.PreventStabby;
import me.youhavetrouble.preventstabby.players.PlayerData;
import org.bukkit.OfflinePlayer;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
/**
* NOT IMPLEMENTED YET
* Fired when player gets their personal pvp state toggled.
*/
@Deprecated
public class PlayerTogglePvpEvent extends Event {
private static final HandlerList HANDLERS = new HandlerList();
private final Player player;
private boolean newState, sendMessage;
private final OfflinePlayer player;
private final boolean newState;
private boolean sendMessage;
public PlayerTogglePvpEvent(Player player, boolean newState, boolean sendMessage) {
public PlayerTogglePvpEvent(OfflinePlayer player, boolean newState, boolean sendMessage) {
this.player = player;
this.newState = newState;
this.sendMessage = sendMessage;
}
public Player getPlayer() {
public OfflinePlayer getPlayer() {
return player;
}
public PlayerData getPlayerData() {
return PreventStabby.getPlugin().getPlayerManager().getPlayer(player.getUniqueId());
}
/**
* Returns the state player's pvp state was toggled to.
* @return The state player's pvp state was toggled to.
*/
public boolean newState() {
return newState;
}
public void setNewState(boolean newState) {
this.newState = newState;
}
/**
* Returns true if the state message will be sent to the player, false otherwise
* @return True if the state message will be sent to the player, false otherwise
*/
public boolean isSendMessage() {
return sendMessage;
}
/**
* If this is true at the end of event pipeline, message with the current state will be sent to player.
*/
public void setSendMessage(boolean sendMessage) {
this.sendMessage = sendMessage;
}
@@ -3,7 +3,6 @@ package me.youhavetrouble.preventstabby.commands;
import me.youhavetrouble.preventstabby.PreventStabby;
import me.youhavetrouble.preventstabby.config.PreventStabbyPermission;
import me.youhavetrouble.preventstabby.util.PluginMessages;
import net.kyori.adventure.audience.MessageType;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
@@ -11,6 +10,12 @@ import org.bukkit.command.CommandSender;
public class HelpCommand {
public static void help(CommandSender sender, String[] args) {
Bukkit.getScheduler().runTaskAsynchronously(PreventStabby.getPlugin(), () -> {
if (!PreventStabbyPermission.COMMAND_HELP.doesCommandSenderHave(sender)) {
PluginMessages.sendMessage(sender, PreventStabby.getPlugin().getConfigCache().getNo_permission());
return;
}
Component helpComponent = PluginMessages.MINIMESSAGE
.deserialize("<aqua><strikethrough> </strikethrough> PreventStabby Help <strikethrough> </strikethrough>")
.append(Component.newline())
@@ -23,7 +28,7 @@ public class HelpCommand {
.append(Component.newline())
.append(PluginMessages.MINIMESSAGE.deserialize("<blue>/pvp <aqua><bold>toggle</bold> <white>- toggles PvP status"));
}
PreventStabby.getAudiences().sender(sender).sendMessage(helpComponent, MessageType.SYSTEM);
PreventStabby.getAudiences().sender(sender).sendMessage(helpComponent);
});
}
@@ -20,7 +20,7 @@ public class MainCommand implements TabExecutor {
private final HashMap<String, PreventStabbyPermission> subCommands = new HashMap<>();
public MainCommand() {
subCommands.put("help", PreventStabbyPermission.COMMAND);
subCommands.put("help", PreventStabbyPermission.COMMAND_HELP);
subCommands.put("toggle", PreventStabbyPermission.COMMAND_TOGGLE);
subCommands.put("on", PreventStabbyPermission.COMMAND_TOGGLE);
subCommands.put("enable", PreventStabbyPermission.COMMAND_TOGGLE);
@@ -36,34 +36,35 @@ public class MainCommand implements TabExecutor {
PluginMessages.sendMessage(sender, PreventStabby.getPlugin().getConfigCache().getNo_permission());
return true;
}
if (args.length >= 1) {
switch (args[0].toLowerCase()) {
case "help":
HelpCommand.help(sender, args);
break;
case "toggle":
PvpToggleCommand.toggle(sender, args);
break;
case "on":
case "enable":
PvpToggleCommand.enable(sender, args);
break;
case "off":
case "disable":
PvpToggleCommand.disable(sender, args);
break;
case "reload":
ReloadCommand.reload(sender);
break;
case "override":
GlobalToggleCommand.globalToggle(sender, args);
break;
default:
PluginMessages.sendMessage(sender, PreventStabby.getPlugin().getConfigCache().getNo_such_command());
break;
}
} else {
HelpCommand.help(sender, args);
if (args.length == 0) {
PvpToggleCommand.toggle(sender, args);
return true;
}
switch (args[0].toLowerCase()) {
case "help":
HelpCommand.help(sender, args);
break;
case "toggle":
PvpToggleCommand.toggle(sender, args);
break;
case "on":
case "enable":
PvpToggleCommand.enable(sender, args);
break;
case "off":
case "disable":
PvpToggleCommand.disable(sender, args);
break;
case "reload":
ReloadCommand.reload(sender);
break;
case "override":
GlobalToggleCommand.globalToggle(sender, args);
break;
default:
PluginMessages.sendMessage(sender, PreventStabby.getPlugin().getConfigCache().getNo_such_command());
break;
}
return true;
}
@@ -1,6 +1,7 @@
package me.youhavetrouble.preventstabby.commands;
import me.youhavetrouble.preventstabby.PreventStabby;
import me.youhavetrouble.preventstabby.api.event.PlayerTogglePvpEvent;
import me.youhavetrouble.preventstabby.config.PreventStabbyPermission;
import me.youhavetrouble.preventstabby.util.CombatTimer;
import me.youhavetrouble.preventstabby.util.PluginMessages;
@@ -17,7 +18,7 @@ public class PvpToggleCommand {
return;
}
if (args.length == 1) {
if (args.length <= 1) {
if (sender instanceof Player) {
Player player = (Player) sender;
if (CombatTimer.isInCombat(player.getUniqueId())) {
@@ -25,12 +26,17 @@ public class PvpToggleCommand {
return;
}
boolean currentState = PreventStabby.getPlugin().getPlayerManager().togglePlayerPvpState(player.getUniqueId());
if (currentState) {
PluginMessages.sendMessage(sender, PreventStabby.getPlugin().getConfigCache().getPvp_enabled());
} else {
PluginMessages.sendMessage(sender, PreventStabby.getPlugin().getConfigCache().getPvp_disabled());
}
PlayerTogglePvpEvent toggleEvent = new PlayerTogglePvpEvent(player, currentState, true);
Bukkit.getScheduler().runTask(PreventStabby.getPlugin(), () -> {
Bukkit.getPluginManager().callEvent(toggleEvent);
if (toggleEvent.isSendMessage()) {
if (currentState) {
PluginMessages.sendMessage(sender, PreventStabby.getPlugin().getConfigCache().getPvp_enabled());
} else {
PluginMessages.sendMessage(sender, PreventStabby.getPlugin().getConfigCache().getPvp_disabled());
}
}
});
} else {
PluginMessages.sendMessage(sender, "Try /pvp toggle <player>");
}
@@ -53,6 +59,18 @@ public class PvpToggleCommand {
return;
}
boolean currentState = PreventStabby.getPlugin().getPlayerManager().togglePlayerPvpState(player.getUniqueId());
PlayerTogglePvpEvent toggleEvent = new PlayerTogglePvpEvent(player, currentState, false);
Bukkit.getScheduler().runTask(PreventStabby.getPlugin(), () -> {
Bukkit.getPluginManager().callEvent(toggleEvent);
if (toggleEvent.isSendMessage()) {
if (currentState) {
PluginMessages.sendMessage(player, PreventStabby.getPlugin().getConfigCache().getPvp_enabled());
} else {
PluginMessages.sendMessage(player, PreventStabby.getPlugin().getConfigCache().getPvp_disabled());
}
}
});
String message;
if (currentState) {
message = PreventStabby.getPlugin().getConfigCache().getPvp_enabled_other();
@@ -60,12 +78,6 @@ public class PvpToggleCommand {
message = PreventStabby.getPlugin().getConfigCache().getPvp_disabled_other();
}
PluginMessages.sendMessage(sender, PluginMessages.parsePlayerName(player, message));
} else {
if (PreventStabbyPermission.COMMAND_TOGGLE_OTHERS.doesCommandSenderHave(sender)) {
PluginMessages.sendMessage(sender, "Try /pvp toggle <player>");
} else {
PluginMessages.sendMessage(sender, "Try /pvp toggle");
}
}
});
}
@@ -83,7 +95,13 @@ public class PvpToggleCommand {
return;
}
PreventStabby.getPlugin().getPlayerManager().setPlayerPvpState(player.getUniqueId(), true);
PluginMessages.sendMessage(sender, PreventStabby.getPlugin().getConfigCache().getPvp_enabled());
PlayerTogglePvpEvent toggleEvent = new PlayerTogglePvpEvent(player, true, true);
Bukkit.getScheduler().runTask(PreventStabby.getPlugin(), () -> {
Bukkit.getPluginManager().callEvent(toggleEvent);
if (toggleEvent.isSendMessage()) {
PluginMessages.sendMessage(sender, PreventStabby.getPlugin().getConfigCache().getPvp_enabled());
}
});
} else {
PluginMessages.sendMessage(sender, "Try /pvp enable <player>");
}
@@ -107,6 +125,13 @@ public class PvpToggleCommand {
String message = PreventStabby.getPlugin().getConfigCache().getPvp_enabled_other();
PluginMessages.sendMessage(sender, PluginMessages.parsePlayerName(player, message));
PreventStabby.getPlugin().getPlayerManager().setPlayerPvpState(player.getUniqueId(), true);
PlayerTogglePvpEvent toggleEvent = new PlayerTogglePvpEvent(player, true, false);
Bukkit.getScheduler().runTask(PreventStabby.getPlugin(), () -> {
Bukkit.getPluginManager().callEvent(toggleEvent);
if (toggleEvent.isSendMessage()) {
PluginMessages.sendMessage(player, PreventStabby.getPlugin().getConfigCache().getPvp_enabled());
}
});
} else {
if (PreventStabbyPermission.COMMAND_TOGGLE_OTHERS.doesCommandSenderHave(sender)) {
PluginMessages.sendMessage(sender, "Try /pvp enable <player>");
@@ -129,7 +154,13 @@ public class PvpToggleCommand {
return;
}
PreventStabby.getPlugin().getPlayerManager().setPlayerPvpState(player.getUniqueId(), false);
PluginMessages.sendMessage(sender, PreventStabby.getPlugin().getConfigCache().getPvp_disabled());
PlayerTogglePvpEvent toggleEvent = new PlayerTogglePvpEvent(player, false, true);
Bukkit.getScheduler().runTask(PreventStabby.getPlugin(), () -> {
Bukkit.getPluginManager().callEvent(toggleEvent);
if (toggleEvent.isSendMessage()) {
PluginMessages.sendMessage(sender, PreventStabby.getPlugin().getConfigCache().getPvp_disabled());
}
});
} else {
PluginMessages.sendMessage(sender, "Try /pvp disable <player>");
}
@@ -152,7 +183,14 @@ public class PvpToggleCommand {
}
String message = PreventStabby.getPlugin().getConfigCache().getPvp_disabled_other();
PluginMessages.sendMessage(sender, PluginMessages.parsePlayerName(player, message));
PreventStabby.getPlugin().getPlayerManager().setPlayerPvpState(player.getUniqueId(), true);
PreventStabby.getPlugin().getPlayerManager().setPlayerPvpState(player.getUniqueId(), false);
PlayerTogglePvpEvent toggleEvent = new PlayerTogglePvpEvent(player, false, false);
Bukkit.getScheduler().runTask(PreventStabby.getPlugin(), () -> {
Bukkit.getPluginManager().callEvent(toggleEvent);
if (toggleEvent.isSendMessage()) {
PluginMessages.sendMessage(player, PreventStabby.getPlugin().getConfigCache().getPvp_disabled());
}
});
} else {
if (PreventStabbyPermission.COMMAND_TOGGLE_OTHERS.doesCommandSenderHave(sender)) {
@@ -16,7 +16,7 @@ public class ConfigCache {
@Getter private final boolean pvp_enabled_by_default, lava_and_fire_stopper_enabled, channeling_enchant_disabled,
punish_for_combat_logout, punish_for_combat_logout_announce, only_owner_can_interact_with_pet,
snowballs_knockback, egg_knockback, block_commands_in_combat, block_teleports_in_combat;
snowballs_knockback, egg_knockback, block_commands_in_combat, block_teleports_in_combat, allow_fishing_rod_pull;
@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,
@@ -57,6 +57,8 @@ public class ConfigCache {
addDefault("settings.eggs_do_knockback", false, "Set to true if eggs should cause knockback to players");
addDefault("settings.allow_fishing_rod_pull", false, "Allows players with pvp off to be pulled by fishing rods");
addComment("settings.block_in_combat", "Set what actions should be blocked while in combat");
addDefault("settings.block_in_combat.block_commands.enabled", true);
List<String> defaultCommandsBlocked = new ArrayList<>();
@@ -113,6 +115,7 @@ public class ConfigCache {
this.punish_for_combat_logout_message = config.getString("settings.punish_for_combat_logout.message", "%player%<reset><white> logged out while in combat. What a loser.");
this.only_owner_can_interact_with_pet = config.getBoolean("settings.only_owner_can_interact_with_pet", false);
this.allow_fishing_rod_pull = config.getBoolean("settings.allow_fishing_rod_pull", false);
this.snowballs_knockback = config.getBoolean("settings.snowballs_do_knockback", false);
this.egg_knockback = config.getBoolean("settings.eggs_do_knockback", false);
this.block_commands_in_combat = config.getBoolean("settings.block_in_combat.block_commands", true);
@@ -8,7 +8,9 @@ public enum PreventStabbyPermission {
COMMAND_TOGGLE("command.toggle"),
COMMAND_TOGGLE_OTHERS("command.toggle.others"),
COMMAND_RELOAD("command.reload"),
COMMAND_GLOBAL_TOGGLE("command.toggle.global");
COMMAND_GLOBAL_TOGGLE("command.toggle.global"),
COMMAND_HELP("command.help");
private final String permission;
PreventStabbyPermission(String permission) {
@@ -37,17 +37,25 @@ public class PlacoholderApiHook extends PlaceholderExpansion {
@Override
public String onRequest(OfflinePlayer player, String params) {
if (params.equalsIgnoreCase("combat_time")) {
return getCombatTimePlaceholder(player.getUniqueId());
switch (params) {
case "pvp":
if (!player.isOnline()) return String.valueOf(false);
return String.valueOf(plugin.getPlayerManager().getPlayer(player.getUniqueId()).isPvpEnabled());
case "combat_time":
return getCombatTimePlaceholder(player.getUniqueId(), player);
case "in_combat":
if (!player.isOnline()) return String.valueOf(false);
return String.valueOf(plugin.getPlayerManager().getPlayer(player.getUniqueId()).isInCombat());
default:
return null;
}
if (params.equalsIgnoreCase("in_combat")) {
return String.valueOf(plugin.getPlayerManager().getPlayer(player.getUniqueId()).isInCombat());
}
return null;
}
private String getCombatTimePlaceholder(UUID uuid) {
long seconds = plugin.getPlayerManager().getPlayer(uuid).getCombattime() - Instant.now().getEpochSecond();
private String getCombatTimePlaceholder(UUID uuid, OfflinePlayer player) {
if (!player.isOnline()) {
return legacyComponentSerializer.serialize(PluginMessages.parseMessage(plugin.getConfigCache().getPlaceholder_not_in_combat()));
}
long seconds = plugin.getPlayerManager().getPlayer(uuid).getCombatTime();
if (seconds > 0) {
String msg = plugin.getConfigCache().getPlaceholder_combat_time();
msg = msg.replaceAll("%time%", String.valueOf(seconds));
@@ -12,9 +12,8 @@ import com.sk89q.worldguard.protection.flags.registry.FlagConflictException;
import com.sk89q.worldguard.protection.flags.registry.FlagRegistry;
import com.sk89q.worldguard.protection.regions.RegionContainer;
import com.sk89q.worldguard.protection.regions.RegionQuery;
import me.youhavetrouble.preventstabby.PreventStabby;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
public class WorldGuardHook {
@@ -22,19 +21,18 @@ public class WorldGuardHook {
public static StateFlag FORCE_PVP_FLAG;
public static void init() {
PreventStabby plugin = PreventStabby.getPlugin();
try {
Class.forName("com.sk89q.worldguard.protection.flags.registry.FlagRegistry");
WorldGuardPlugin worldGuardPlugin = WorldGuardPlugin.inst();
if (WorldGuard.getInstance() == null || worldGuardPlugin == null) return;
plugin.getLogger().info("Hooking into WorldGuard");
Bukkit.getLogger().info("[PreventStabby] Hooking into WorldGuard");
flagRegistry = WorldGuard.getInstance().getFlagRegistry();
createForcePvpFlag(plugin);
createForcePvpFlag();
} catch (NoClassDefFoundError | ClassNotFoundException ignored) {
}
}
private static void createForcePvpFlag(Plugin plugin) {
private static void createForcePvpFlag() {
if (flagRegistry == null) return;
String flagName = "preventstabby-force-pvp";
try {
@@ -46,7 +44,7 @@ public class WorldGuardHook {
if (existing instanceof StateFlag) {
FORCE_PVP_FLAG = (StateFlag) existing;
} else {
plugin.getLogger().severe("There is a conflict between flag names!");
Bukkit.getLogger().severe("[PreventStabby] There is a conflict between flag names!");
}
}
}
@@ -19,6 +19,7 @@ public class FishingListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onFish(org.bukkit.event.player.PlayerFishEvent event) {
if (event.getCaught() instanceof Player) {
if (PreventStabby.getPlugin().getConfigCache().isAllow_fishing_rod_pull()) return;
UUID damager = event.getPlayer().getUniqueId();
UUID victim = event.getCaught().getUniqueId();
if (damager == victim)
@@ -5,6 +5,9 @@ import me.youhavetrouble.preventstabby.PreventStabby;
import java.time.Instant;
import java.util.UUID;
/**
* PreventStabby player data keeper.<br>
*/
public class PlayerData {
private final UUID playerUuid;
@@ -18,70 +21,92 @@ public class PlayerData {
this.loginTimestamp = Instant.now().getEpochSecond()-1;
this.teleportTimestamp = Instant.now().getEpochSecond()-1;
this.inCombat = false;
refreshCachetime();
refreshCacheTime();
}
public UUID getPlayerUuid() {
return playerUuid;
}
/**
* Returns true if player has personal pvp enabled, false otherwise.
* @return True if player has personal pvp enabled, false otherwise.
* @see PlayerManager#getPlayerPvPState(UUID)
*/
public boolean isPvpEnabled() {
return pvpEnabled;
}
/**
* Sets player's personal pvp state.
* @param pvpEnabled Pvp state to set.
*/
public void setPvpEnabled(boolean pvpEnabled) {
this.pvpEnabled = pvpEnabled;
}
public long getCachetime() {
protected long getCachetime() {
return cachetime;
}
public void refreshCachetime() {
this.cachetime = Instant.now().getEpochSecond()+ PreventStabby.getPlugin().getConfigCache().getCache_time();
protected void refreshCacheTime() {
this.cachetime = Instant.now().getEpochSecond() + PreventStabby.getPlugin().getConfigCache().getCache_time();
}
public long getCombattime() {
return combattime;
/**
* Time left until the end of combat in seconds.
* @return Time left until the end of combat in seconds.<br>
* Return of 0 means out of combat or about to be out of combat.
*/
public long getCombatTime() {
return Math.max(combattime - Instant.now().getEpochSecond(), 0);
}
public void setCombattime(long combattime) {
protected void setCombattime(long combattime) {
this.combattime = combattime;
}
/**
* Sets player in combat and sets combat time to the interval set in config.
* @see PlayerManager#refreshPlayersCombatTime(UUID)
*/
public void refreshCombatTime() {
this.combattime = Instant.now().getEpochSecond()+ PreventStabby.getPlugin().getConfigCache().getCombat_time();
}
public boolean getLastCombatCheck() {
protected boolean getLastCombatCheck() {
return lastCombatCheck;
}
public void setLastCombatCheck(boolean bool) {
protected void setLastCombatCheck(boolean bool) {
lastCombatCheck = bool;
}
public void setLoginTimestamp(long loginTimestamp) {
protected void setLoginTimestamp(long loginTimestamp) {
this.loginTimestamp = loginTimestamp + PreventStabby.getPlugin().getConfigCache().getLogin_protection_time()-1;
}
public long getLoginTimestamp() {
protected long getLoginTimestamp() {
return loginTimestamp;
}
public void setTeleportTimestamp(long teleportTimestamp) {
protected void setTeleportTimestamp(long teleportTimestamp) {
this.teleportTimestamp = teleportTimestamp + PreventStabby.getPlugin().getConfigCache().getTeleport_protection_time()-1;
}
public long getTeleportTimestamp() {
protected long getTeleportTimestamp() {
return teleportTimestamp;
}
/**
* Returns player's current combat state.
* @return Player's current combat state.
*/
public boolean isInCombat() {
return inCombat;
}
public void setInCombat(boolean inCombat) {
protected void setInCombat(boolean inCombat) {
this.inCombat = inCombat;
}
}
@@ -1,7 +1,6 @@
package me.youhavetrouble.preventstabby.listeners.player;
package me.youhavetrouble.preventstabby.players;
import me.youhavetrouble.preventstabby.PreventStabby;
import me.youhavetrouble.preventstabby.players.PlayerData;
import me.youhavetrouble.preventstabby.util.PreventStabbyListener;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@@ -1,4 +1,4 @@
package me.youhavetrouble.preventstabby.listeners.player;
package me.youhavetrouble.preventstabby.players;
import me.youhavetrouble.preventstabby.PreventStabby;
import me.youhavetrouble.preventstabby.players.PlayerData;
@@ -34,7 +34,7 @@ public class PlayerJoinAndLeaveListener implements Listener {
});
return;
}
playerData.refreshCachetime();
playerData.refreshCacheTime();
playerData.setLoginTimestamp(time);
}
@@ -59,10 +59,10 @@ public class PlayerManager {
}
public void refreshPlayersCacheTime(UUID uuid) {
playerList.get(uuid).refreshCachetime();
}
/**
* Sets player in combat and sets combat time to the interval set in config.
* @see PlayerData#refreshCombatTime()
*/
public void refreshPlayersCombatTime(UUID uuid) {
PlayerData data = playerList.get(uuid);
if (data == null) return;
@@ -70,25 +70,46 @@ public class PlayerManager {
if (player == null || player.isDead()) return;
data.refreshCombatTime();
data.setInCombat(true);
}
/**
* Gets player's PlayerData object. Returns null when player with provided UUID doesn't exist.
* @param uuid Player's UUID.
* @return Player's PlayerData object or null if player doesn't exist.
*/
public PlayerData getPlayer(UUID uuid) {
return playerList.get(uuid);
}
public void addPlayer(UUID uuid, PlayerData data) {
protected void addPlayer(UUID uuid, PlayerData data) {
playerList.put(uuid, data);
}
/**
* Returns true if player has personal pvp enabled, false otherwise.
* @param uuid Player's UUID.
* @return True if player has personal pvp enabled, false otherwise.
* @see PlayerData#isPvpEnabled()
*/
public boolean getPlayerPvPState(UUID uuid) {
return PreventStabby.getPlugin().getSmartCache().getPlayerData(uuid).isPvpEnabled();
}
/**
* Sets player's personal pvp state.
* @param uuid Player's UUID.
* @param state Pvp state to set.
* @see PlayerData#setPvpEnabled(boolean)
*/
public void setPlayerPvpState(UUID uuid, boolean state) {
PreventStabby.getPlugin().getSmartCache().getPlayerData(uuid).setPvpEnabled(state);
}
/**
* Toggles player's personal pvp state.
* @param uuid Player's UUID.
* @return Player's personal pvp state after the change.
*/
public boolean togglePlayerPvpState(UUID uuid) {
SmartCache smartCache = PreventStabby.getPlugin().getSmartCache();
if (smartCache.getPlayerData(uuid).isPvpEnabled()) {
@@ -100,10 +121,26 @@ public class PlayerManager {
}
}
/**
* Check if attacker can harm the victim. Both of them have to have personal pvp enabled and none of them can have
* any kind of spawn or teleport protection.
* @param attacker Atacker's UUID.
* @param victim Victim's UUID.
* @param sendDenyMessage Should plugin send a message that there was attempt at damaging to both players?
* @return Whenever attacker can harm the victim.
*/
public boolean canDamage(UUID attacker, UUID victim, boolean sendDenyMessage) {
return canDamage(attacker, victim, sendDenyMessage, true);
}
/**
* Check if attacker can harm the victim.
* @param attacker Atacker's UUID.
* @param victim Victim's UUID.
* @param sendDenyMessage Should plugin send a message that there was attempt at damaging to both players?
* @param checkVictimSpawnProtection Should teleport and spawn protections be taken into account?
* @return Whenever attacker can harm the victim.
*/
public boolean canDamage(UUID attacker, UUID victim, boolean sendDenyMessage, boolean checkVictimSpawnProtection) {
if (hasLoginProtection(attacker) || hasTeleportProtection(attacker)) return false;
@@ -149,8 +186,8 @@ public class PlayerManager {
}
/**
* @param uuid Player UUIDs
* @return true if any of the provided UUIDs has spawn protection
* @param uuid Player UUIDs.
* @return True if any of the provided UUIDs has spawn protection.
*/
public boolean hasLoginProtection(UUID... uuid) {
SmartCache smartCache = PreventStabby.getPlugin().getSmartCache();
@@ -161,15 +198,27 @@ public class PlayerManager {
return false;
}
/**
* @param uuid Player UUID.
* @return True if player tied to the uuid currently has teleport protection.
*/
public boolean hasTeleportProtection(UUID uuid) {
SmartCache smartCache = PreventStabby.getPlugin().getSmartCache();
return Instant.now().getEpochSecond() < smartCache.getPlayerData(uuid).getTeleportTimestamp();
}
/**
* Returns current forced pvp state.
* @return Current forced pvp state.
*/
public PvpState getForcedPvpState() {
return pvpForcedState;
}
/**
* Sets current forced pvp state.
* @param forcedPvpState New forced pvp state.
*/
public void setForcedPvpState(PvpState forcedPvpState) {
this.pvpForcedState = forcedPvpState;
}
@@ -1,8 +1,6 @@
package me.youhavetrouble.preventstabby.listeners.player;
package me.youhavetrouble.preventstabby.players;
import me.youhavetrouble.preventstabby.PreventStabby;
import me.youhavetrouble.preventstabby.players.PlayerData;
import me.youhavetrouble.preventstabby.players.SmartCache;
import me.youhavetrouble.preventstabby.util.PreventStabbyListener;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@@ -16,7 +16,7 @@ public class SmartCache {
try {
Player player = Bukkit.getPlayer(e.getKey());
if (player != null && player.isOnline()) {
PreventStabby.getPlugin().getPlayerManager().refreshPlayersCacheTime(e.getKey());
e.getValue().refreshCacheTime();
}
} catch (NullPointerException ignored) {}
}
@@ -32,8 +32,9 @@ public class SmartCache {
public PlayerData getPlayerData(UUID uuid) {
// Try to get data from cache and refresh it
try {
PreventStabby.getPlugin().getPlayerManager().refreshPlayersCacheTime(uuid);
return PreventStabby.getPlugin().getPlayerManager().getPlayer(uuid);
PlayerData data = PreventStabby.getPlugin().getPlayerManager().getPlayer(uuid);
data.refreshCacheTime();
return data;
} catch (NullPointerException e) {
// If player data is not in cache get it from database and put into cache
try {
@@ -11,8 +11,7 @@ public class CombatTimer {
public static void refreshPlayersCombatTime(UUID uuid) {
try {
long now = Instant.now().getEpochSecond();
long combattime = PreventStabby.getPlugin().getSmartCache().getPlayerData(uuid).getCombattime();
long combattime = PreventStabby.getPlugin().getSmartCache().getPlayerData(uuid).getCombatTime();
Player player = Bukkit.getPlayer(uuid);
if (player == null || !player.isOnline()) return;
@@ -22,7 +21,7 @@ public class CombatTimer {
if (playerEnterCombatEvent.isCancelled()) return;
PreventStabby.getPlugin().getPlayerManager().refreshPlayersCombatTime(uuid);
if (combattime <= now) {
if (combattime <= 0) {
PluginMessages.sendActionBar(uuid, PreventStabby.getPlugin().getConfigCache().getEntering_combat());
}
});
@@ -38,7 +37,7 @@ public class CombatTimer {
public static boolean isInCombat(UUID uuid) {
try {
return PreventStabby.getPlugin().getPlayerManager().getPlayer(uuid).getCombattime() >= Instant.now().getEpochSecond();
return PreventStabby.getPlugin().getPlayerManager().getPlayer(uuid).getCombatTime() >= Instant.now().getEpochSecond();
} catch (Exception e) {
return false;
}
+13
View File
@@ -22,15 +22,28 @@ permissions:
preventstabby.command:
default: true
description: Allows usage of /pvp command
preventstabby.command.help:
default: true
description: Allows usage of /pvp help command
children:
preventstabby.command: true
preventstabby.command.toggle:
default: true
description: Allows usage of /pvp <toggle/enable/disable> command
children:
preventstabby.command: true
preventstabby.command.toggle.others:
default: op
description: Allows usage of /pvp <toggle/enable/disable> <player> command
children:
preventstabby.command: true
preventstabby.command.toggle.global:
default: op
description: Allows usage of /pvp override <enabled/disabled/none> command
children:
preventstabby.command: true
preventstabby.command.reload:
default: op
description: Allows usage of /pvp reload command
children:
preventstabby.command: true