Compare commits

...

23 Commits

Author SHA1 Message Date
YouHaveTrouble eb657798e4 bump version 2023-11-20 22:50:29 +01:00
YouHaveTrouble d15ccbef20 add missing static handler list getters 2023-11-20 22:49:37 +01:00
YouHaveTrouble 8ae6e9212c actually check for empty string and not for null 2023-06-22 15:36:08 +02:00
YouHaveTrouble fec1dd2ab6 bump version 2023-06-22 15:32:09 +02:00
YouHaveTrouble fba2f03aab don't send messages or action bars if the message content is empty 2023-06-22 15:28:41 +02:00
YouHaveTrouble e30437694c bump version 2023-05-23 00:09:14 +02:00
YouHaveTrouble 8fcf34e310 more fixes for #15 2023-05-20 12:06:16 +02:00
YouHaveTrouble aac1f99374 potential fix for #15 2023-05-20 00:09:02 +02:00
YouHaveTrouble 7f1da79444 bump version 2023-05-03 21:49:15 +02:00
YouHaveTrouble 244ceb4dcc don't send the message if tempting was cancelled 2023-05-03 21:29:07 +02:00
YouHaveTrouble ac1cafe0d9 tempting horses should not cause player to enter combat 2023-05-03 21:26:38 +02:00
YouHaveTrouble b3ce1913fd add placeholder for forced pvp state 2023-05-03 19:18:02 +02:00
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
26 changed files with 414 additions and 193 deletions
+10 -6
View File
@@ -6,7 +6,7 @@
<groupId>me.youhavetrouble</groupId> <groupId>me.youhavetrouble</groupId>
<artifactId>PreventStabby</artifactId> <artifactId>PreventStabby</artifactId>
<version>1.7.1</version> <version>1.11.1</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>PreventStabby</name> <name>PreventStabby</name>
@@ -49,6 +49,10 @@
<pattern>org.bstats</pattern> <pattern>org.bstats</pattern>
<shadedPattern>bstats</shadedPattern> <shadedPattern>bstats</shadedPattern>
</relocation> </relocation>
<relocation>
<pattern>net.kyori</pattern>
<shadedPattern>kyori</shadedPattern>
</relocation>
</relocations> </relocations>
</configuration> </configuration>
</execution> </execution>
@@ -96,7 +100,7 @@
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<version>1.18.24</version> <version>1.18.26</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
@@ -130,25 +134,25 @@
<dependency> <dependency>
<groupId>net.kyori</groupId> <groupId>net.kyori</groupId>
<artifactId>adventure-api</artifactId> <artifactId>adventure-api</artifactId>
<version>4.11.0</version> <version>4.12.0</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>net.kyori</groupId> <groupId>net.kyori</groupId>
<artifactId>adventure-platform-bukkit</artifactId> <artifactId>adventure-platform-bukkit</artifactId>
<version>4.1.1</version> <version>4.2.0</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>net.kyori</groupId> <groupId>net.kyori</groupId>
<artifactId>adventure-text-minimessage</artifactId> <artifactId>adventure-text-minimessage</artifactId>
<version>4.11.0</version> <version>4.12.0</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>net.kyori</groupId> <groupId>net.kyori</groupId>
<artifactId>adventure-text-serializer-legacy</artifactId> <artifactId>adventure-text-serializer-legacy</artifactId>
<version>4.11.0</version> <version>4.12.0</version>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>
@@ -76,13 +76,6 @@ public final class PreventStabby extends JavaPlugin {
pvpCommand.setExecutor(mainCommand); pvpCommand.setExecutor(mainCommand);
pvpCommand.setTabCompleter(mainCommand); pvpCommand.setTabCompleter(mainCommand);
try {
WorldGuardHook.init();
worldGuardHook = true;
} catch (NoClassDefFoundError e) {
worldGuardHook = false;
}
if (getServer().getPluginManager().getPlugin("PlaceholderAPI") != null) { if (getServer().getPluginManager().getPlugin("PlaceholderAPI") != null) {
new PlacoholderApiHook(this).register(); new PlacoholderApiHook(this).register();
} }
@@ -90,6 +83,18 @@ public final class PreventStabby extends JavaPlugin {
Metrics metrics = new Metrics(this, 14074); 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() { public static boolean worldGuardHookEnabled() {
return worldGuardHook; return worldGuardHook;
} }
@@ -45,4 +45,9 @@ public class PlayerEnterCombatEvent extends Event implements Cancellable {
public HandlerList getHandlers() { public HandlerList getHandlers() {
return HANDLERS; return HANDLERS;
} }
@NotNull
public static HandlerList getHandlerList() {
return HANDLERS;
}
} }
@@ -45,4 +45,9 @@ public class PlayerLeaveCombatEvent extends Event implements Cancellable {
public HandlerList getHandlers() { public HandlerList getHandlers() {
return HANDLERS; return HANDLERS;
} }
@NotNull
public static HandlerList getHandlerList() {
return HANDLERS;
}
} }
@@ -1,42 +1,55 @@
package me.youhavetrouble.preventstabby.api.event; 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.Event;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
* NOT IMPLEMENTED YET * Fired when player gets their personal pvp state toggled.
*/ */
@Deprecated
public class PlayerTogglePvpEvent extends Event { public class PlayerTogglePvpEvent extends Event {
private static final HandlerList HANDLERS = new HandlerList(); private static final HandlerList HANDLERS = new HandlerList();
private final Player player; private final OfflinePlayer player;
private boolean newState, sendMessage; 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.player = player;
this.newState = newState; this.newState = newState;
this.sendMessage = sendMessage; this.sendMessage = sendMessage;
} }
public Player getPlayer() { public OfflinePlayer getPlayer() {
return player; 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() { public boolean newState() {
return 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() { public boolean isSendMessage() {
return sendMessage; 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) { public void setSendMessage(boolean sendMessage) {
this.sendMessage = sendMessage; this.sendMessage = sendMessage;
} }
@@ -46,4 +59,9 @@ public class PlayerTogglePvpEvent extends Event {
public HandlerList getHandlers() { public HandlerList getHandlers() {
return HANDLERS; return HANDLERS;
} }
@NotNull
public static HandlerList getHandlerList() {
return HANDLERS;
}
} }
@@ -3,7 +3,6 @@ package me.youhavetrouble.preventstabby.commands;
import me.youhavetrouble.preventstabby.PreventStabby; import me.youhavetrouble.preventstabby.PreventStabby;
import me.youhavetrouble.preventstabby.config.PreventStabbyPermission; import me.youhavetrouble.preventstabby.config.PreventStabbyPermission;
import me.youhavetrouble.preventstabby.util.PluginMessages; import me.youhavetrouble.preventstabby.util.PluginMessages;
import net.kyori.adventure.audience.MessageType;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@@ -11,6 +10,12 @@ import org.bukkit.command.CommandSender;
public class HelpCommand { public class HelpCommand {
public static void help(CommandSender sender, String[] args) { public static void help(CommandSender sender, String[] args) {
Bukkit.getScheduler().runTaskAsynchronously(PreventStabby.getPlugin(), () -> { Bukkit.getScheduler().runTaskAsynchronously(PreventStabby.getPlugin(), () -> {
if (!PreventStabbyPermission.COMMAND_HELP.doesCommandSenderHave(sender)) {
PluginMessages.sendMessage(sender, PreventStabby.getPlugin().getConfigCache().getNo_permission());
return;
}
Component helpComponent = PluginMessages.MINIMESSAGE Component helpComponent = PluginMessages.MINIMESSAGE
.deserialize("<aqua><strikethrough> </strikethrough> PreventStabby Help <strikethrough> </strikethrough>") .deserialize("<aqua><strikethrough> </strikethrough> PreventStabby Help <strikethrough> </strikethrough>")
.append(Component.newline()) .append(Component.newline())
@@ -23,7 +28,7 @@ public class HelpCommand {
.append(Component.newline()) .append(Component.newline())
.append(PluginMessages.MINIMESSAGE.deserialize("<blue>/pvp <aqua><bold>toggle</bold> <white>- toggles PvP status")); .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<>(); private final HashMap<String, PreventStabbyPermission> subCommands = new HashMap<>();
public MainCommand() { public MainCommand() {
subCommands.put("help", PreventStabbyPermission.COMMAND); subCommands.put("help", PreventStabbyPermission.COMMAND_HELP);
subCommands.put("toggle", PreventStabbyPermission.COMMAND_TOGGLE); subCommands.put("toggle", PreventStabbyPermission.COMMAND_TOGGLE);
subCommands.put("on", PreventStabbyPermission.COMMAND_TOGGLE); subCommands.put("on", PreventStabbyPermission.COMMAND_TOGGLE);
subCommands.put("enable", 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()); PluginMessages.sendMessage(sender, PreventStabby.getPlugin().getConfigCache().getNo_permission());
return true; return true;
} }
if (args.length >= 1) { if (args.length == 0) {
switch (args[0].toLowerCase()) { PvpToggleCommand.toggle(sender, args);
case "help": return true;
HelpCommand.help(sender, args); }
break;
case "toggle": switch (args[0].toLowerCase()) {
PvpToggleCommand.toggle(sender, args); case "help":
break; HelpCommand.help(sender, args);
case "on": break;
case "enable": case "toggle":
PvpToggleCommand.enable(sender, args); PvpToggleCommand.toggle(sender, args);
break; break;
case "off": case "on":
case "disable": case "enable":
PvpToggleCommand.disable(sender, args); PvpToggleCommand.enable(sender, args);
break; break;
case "reload": case "off":
ReloadCommand.reload(sender); case "disable":
break; PvpToggleCommand.disable(sender, args);
case "override": break;
GlobalToggleCommand.globalToggle(sender, args); case "reload":
break; ReloadCommand.reload(sender);
default: break;
PluginMessages.sendMessage(sender, PreventStabby.getPlugin().getConfigCache().getNo_such_command()); case "override":
break; GlobalToggleCommand.globalToggle(sender, args);
} break;
} else { default:
HelpCommand.help(sender, args); PluginMessages.sendMessage(sender, PreventStabby.getPlugin().getConfigCache().getNo_such_command());
break;
} }
return true; return true;
} }
@@ -1,6 +1,7 @@
package me.youhavetrouble.preventstabby.commands; package me.youhavetrouble.preventstabby.commands;
import me.youhavetrouble.preventstabby.PreventStabby; import me.youhavetrouble.preventstabby.PreventStabby;
import me.youhavetrouble.preventstabby.api.event.PlayerTogglePvpEvent;
import me.youhavetrouble.preventstabby.config.PreventStabbyPermission; import me.youhavetrouble.preventstabby.config.PreventStabbyPermission;
import me.youhavetrouble.preventstabby.util.CombatTimer; import me.youhavetrouble.preventstabby.util.CombatTimer;
import me.youhavetrouble.preventstabby.util.PluginMessages; import me.youhavetrouble.preventstabby.util.PluginMessages;
@@ -17,7 +18,7 @@ public class PvpToggleCommand {
return; return;
} }
if (args.length == 1) { if (args.length <= 1) {
if (sender instanceof Player) { if (sender instanceof Player) {
Player player = (Player) sender; Player player = (Player) sender;
if (CombatTimer.isInCombat(player.getUniqueId())) { if (CombatTimer.isInCombat(player.getUniqueId())) {
@@ -25,12 +26,17 @@ public class PvpToggleCommand {
return; return;
} }
boolean currentState = PreventStabby.getPlugin().getPlayerManager().togglePlayerPvpState(player.getUniqueId()); boolean currentState = PreventStabby.getPlugin().getPlayerManager().togglePlayerPvpState(player.getUniqueId());
PlayerTogglePvpEvent toggleEvent = new PlayerTogglePvpEvent(player, currentState, true);
if (currentState) { Bukkit.getScheduler().runTask(PreventStabby.getPlugin(), () -> {
PluginMessages.sendMessage(sender, PreventStabby.getPlugin().getConfigCache().getPvp_enabled()); Bukkit.getPluginManager().callEvent(toggleEvent);
} else { if (toggleEvent.isSendMessage()) {
PluginMessages.sendMessage(sender, PreventStabby.getPlugin().getConfigCache().getPvp_disabled()); if (currentState) {
} PluginMessages.sendMessage(sender, PreventStabby.getPlugin().getConfigCache().getPvp_enabled());
} else {
PluginMessages.sendMessage(sender, PreventStabby.getPlugin().getConfigCache().getPvp_disabled());
}
}
});
} else { } else {
PluginMessages.sendMessage(sender, "Try /pvp toggle <player>"); PluginMessages.sendMessage(sender, "Try /pvp toggle <player>");
} }
@@ -53,6 +59,18 @@ public class PvpToggleCommand {
return; return;
} }
boolean currentState = PreventStabby.getPlugin().getPlayerManager().togglePlayerPvpState(player.getUniqueId()); 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; String message;
if (currentState) { if (currentState) {
message = PreventStabby.getPlugin().getConfigCache().getPvp_enabled_other(); message = PreventStabby.getPlugin().getConfigCache().getPvp_enabled_other();
@@ -60,12 +78,6 @@ public class PvpToggleCommand {
message = PreventStabby.getPlugin().getConfigCache().getPvp_disabled_other(); message = PreventStabby.getPlugin().getConfigCache().getPvp_disabled_other();
} }
PluginMessages.sendMessage(sender, PluginMessages.parsePlayerName(player, message)); 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; return;
} }
PreventStabby.getPlugin().getPlayerManager().setPlayerPvpState(player.getUniqueId(), true); 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 { } else {
PluginMessages.sendMessage(sender, "Try /pvp enable <player>"); PluginMessages.sendMessage(sender, "Try /pvp enable <player>");
} }
@@ -107,6 +125,13 @@ public class PvpToggleCommand {
String message = PreventStabby.getPlugin().getConfigCache().getPvp_enabled_other(); String message = PreventStabby.getPlugin().getConfigCache().getPvp_enabled_other();
PluginMessages.sendMessage(sender, PluginMessages.parsePlayerName(player, message)); PluginMessages.sendMessage(sender, PluginMessages.parsePlayerName(player, message));
PreventStabby.getPlugin().getPlayerManager().setPlayerPvpState(player.getUniqueId(), true); 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 { } else {
if (PreventStabbyPermission.COMMAND_TOGGLE_OTHERS.doesCommandSenderHave(sender)) { if (PreventStabbyPermission.COMMAND_TOGGLE_OTHERS.doesCommandSenderHave(sender)) {
PluginMessages.sendMessage(sender, "Try /pvp enable <player>"); PluginMessages.sendMessage(sender, "Try /pvp enable <player>");
@@ -129,7 +154,13 @@ public class PvpToggleCommand {
return; return;
} }
PreventStabby.getPlugin().getPlayerManager().setPlayerPvpState(player.getUniqueId(), false); 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 { } else {
PluginMessages.sendMessage(sender, "Try /pvp disable <player>"); PluginMessages.sendMessage(sender, "Try /pvp disable <player>");
} }
@@ -152,7 +183,14 @@ public class PvpToggleCommand {
} }
String message = PreventStabby.getPlugin().getConfigCache().getPvp_disabled_other(); String message = PreventStabby.getPlugin().getConfigCache().getPvp_disabled_other();
PluginMessages.sendMessage(sender, PluginMessages.parsePlayerName(player, message)); 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 { } else {
if (PreventStabbyPermission.COMMAND_TOGGLE_OTHERS.doesCommandSenderHave(sender)) { if (PreventStabbyPermission.COMMAND_TOGGLE_OTHERS.doesCommandSenderHave(sender)) {
@@ -21,7 +21,8 @@ public class ConfigCache {
cannot_attack_pets_victim, cannot_attack_pets_attacker, no_permission, no_such_command, pvp_enabled_other, 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, 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, force_pvp_on, 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; force_pvp_off, force_pvp_none, placeholder_combat_time, placeholder_not_in_combat, cannot_attack_pvp_force_off,
placeholder_pvp_forced_true, placeholder_pvp_forced_false, placeholder_pvp_forced_none;
@Getter private final double lava_and_fire_stopper_radius; @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 long cache_time, combat_time, login_protection_time, teleport_protection_time;
@Getter private final Set<String> combatBlockedCommands = new HashSet<>(); @Getter private final Set<String> combatBlockedCommands = new HashSet<>();
@@ -91,6 +92,9 @@ public class ConfigCache {
addDefault("messages.force_pvp_none", "PvP state is not forced now"); addDefault("messages.force_pvp_none", "PvP state is not forced now");
addDefault("placeholder.placeholder_combat_time", "Combat time: %time%"); addDefault("placeholder.placeholder_combat_time", "Combat time: %time%");
addDefault("placeholder.not_in_combat", "Not in combat"); addDefault("placeholder.not_in_combat", "Not in combat");
addDefault("placeholder.pvp_forced_true", "PvP is forced on");
addDefault("placeholder.pvp_forced_false", "PvP is forced off");
addDefault("placeholder.pvp_forced_none", "PvP is not forced");
} }
}; };
@@ -152,6 +156,10 @@ public class ConfigCache {
this.placeholder_combat_time = config.getString("placeholder.placeholder_combat_time", "Combat time: %time%"); 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"); this.placeholder_not_in_combat = config.getString("placeholder.not_in_combat", "Not in combat");
this.placeholder_pvp_forced_true = config.getString("placeholder.pvp_forced_true", "PvP is forced on");
this.placeholder_pvp_forced_false = config.getString("placeholder.pvp_forced_false", "PvP is forced off");
this.placeholder_pvp_forced_none = config.getString("placeholder.pvp_forced_none", "PvP is not forced");
} }
@@ -8,7 +8,9 @@ public enum PreventStabbyPermission {
COMMAND_TOGGLE("command.toggle"), COMMAND_TOGGLE("command.toggle"),
COMMAND_TOGGLE_OTHERS("command.toggle.others"), COMMAND_TOGGLE_OTHERS("command.toggle.others"),
COMMAND_RELOAD("command.reload"), COMMAND_RELOAD("command.reload"),
COMMAND_GLOBAL_TOGGLE("command.toggle.global"); COMMAND_GLOBAL_TOGGLE("command.toggle.global"),
COMMAND_HELP("command.help");
private final String permission; private final String permission;
PreventStabbyPermission(String permission) { PreventStabbyPermission(String permission) {
@@ -3,6 +3,7 @@ package me.youhavetrouble.preventstabby.hooks;
import me.clip.placeholderapi.expansion.PlaceholderExpansion; import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import me.youhavetrouble.preventstabby.PreventStabby; import me.youhavetrouble.preventstabby.PreventStabby;
import me.youhavetrouble.preventstabby.util.PluginMessages; import me.youhavetrouble.preventstabby.util.PluginMessages;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -37,17 +38,34 @@ public class PlacoholderApiHook extends PlaceholderExpansion {
@Override @Override
public String onRequest(OfflinePlayer player, String params) { public String onRequest(OfflinePlayer player, String params) {
if (params.equalsIgnoreCase("combat_time")) { switch (params) {
return getCombatTimePlaceholder(player.getUniqueId()); 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());
case "pvp_forced":
switch (PreventStabby.getPlugin().getPlayerManager().getForcedPvpState()) {
case NONE:
return legacyComponentSerializer.serialize(PluginMessages.parseMessage(plugin.getConfigCache().getPlaceholder_pvp_forced_none()));
case ENABLED:
return legacyComponentSerializer.serialize(PluginMessages.parseMessage(plugin.getConfigCache().getPlaceholder_pvp_forced_true()));
case DISABLED:
return legacyComponentSerializer.serialize(PluginMessages.parseMessage(plugin.getConfigCache().getPlaceholder_pvp_forced_false()));
}
default:
return null;
} }
if (params.equalsIgnoreCase("in_combat")) {
return String.valueOf(plugin.getPlayerManager().getPlayer(player.getUniqueId()).isInCombat());
}
return null;
} }
private String getCombatTimePlaceholder(UUID uuid) { private String getCombatTimePlaceholder(UUID uuid, OfflinePlayer player) {
long seconds = plugin.getPlayerManager().getPlayer(uuid).getCombattime() - Instant.now().getEpochSecond(); if (!player.isOnline()) {
return legacyComponentSerializer.serialize(PluginMessages.parseMessage(plugin.getConfigCache().getPlaceholder_not_in_combat()));
}
long seconds = plugin.getPlayerManager().getPlayer(uuid).getCombatTime();
if (seconds > 0) { if (seconds > 0) {
String msg = plugin.getConfigCache().getPlaceholder_combat_time(); String msg = plugin.getConfigCache().getPlaceholder_combat_time();
msg = msg.replaceAll("%time%", String.valueOf(seconds)); 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.flags.registry.FlagRegistry;
import com.sk89q.worldguard.protection.regions.RegionContainer; import com.sk89q.worldguard.protection.regions.RegionContainer;
import com.sk89q.worldguard.protection.regions.RegionQuery; import com.sk89q.worldguard.protection.regions.RegionQuery;
import me.youhavetrouble.preventstabby.PreventStabby; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
public class WorldGuardHook { public class WorldGuardHook {
@@ -22,19 +21,18 @@ public class WorldGuardHook {
public static StateFlag FORCE_PVP_FLAG; public static StateFlag FORCE_PVP_FLAG;
public static void init() { public static void init() {
PreventStabby plugin = PreventStabby.getPlugin();
try { try {
Class.forName("com.sk89q.worldguard.protection.flags.registry.FlagRegistry"); Class.forName("com.sk89q.worldguard.protection.flags.registry.FlagRegistry");
WorldGuardPlugin worldGuardPlugin = WorldGuardPlugin.inst(); WorldGuardPlugin worldGuardPlugin = WorldGuardPlugin.inst();
if (WorldGuard.getInstance() == null || worldGuardPlugin == null) return; if (WorldGuard.getInstance() == null || worldGuardPlugin == null) return;
plugin.getLogger().info("Hooking into WorldGuard"); Bukkit.getLogger().info("[PreventStabby] Hooking into WorldGuard");
flagRegistry = WorldGuard.getInstance().getFlagRegistry(); flagRegistry = WorldGuard.getInstance().getFlagRegistry();
createForcePvpFlag(plugin); createForcePvpFlag();
} catch (NoClassDefFoundError | ClassNotFoundException ignored) { } catch (NoClassDefFoundError | ClassNotFoundException ignored) {
} }
} }
private static void createForcePvpFlag(Plugin plugin) { private static void createForcePvpFlag() {
if (flagRegistry == null) return; if (flagRegistry == null) return;
String flagName = "preventstabby-force-pvp"; String flagName = "preventstabby-force-pvp";
try { try {
@@ -46,7 +44,7 @@ public class WorldGuardHook {
if (existing instanceof StateFlag) { if (existing instanceof StateFlag) {
FORCE_PVP_FLAG = (StateFlag) existing; FORCE_PVP_FLAG = (StateFlag) existing;
} else { } else {
plugin.getLogger().severe("There is a conflict between flag names!"); Bukkit.getLogger().severe("[PreventStabby] There is a conflict between flag names!");
} }
} }
} }
@@ -3,6 +3,7 @@ package me.youhavetrouble.preventstabby.listeners.pets;
import me.youhavetrouble.preventstabby.PreventStabby; import me.youhavetrouble.preventstabby.PreventStabby;
import me.youhavetrouble.preventstabby.players.SmartCache; import me.youhavetrouble.preventstabby.players.SmartCache;
import me.youhavetrouble.preventstabby.util.PreventStabbyListener; import me.youhavetrouble.preventstabby.util.PreventStabbyListener;
import me.youhavetrouble.preventstabby.util.PvpState;
import me.youhavetrouble.preventstabby.util.Util; import me.youhavetrouble.preventstabby.util.Util;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Tameable; import org.bukkit.entity.Tameable;
@@ -14,8 +15,12 @@ import org.bukkit.event.Listener;
@PreventStabbyListener @PreventStabbyListener
public class PetTargettingMountListener implements Listener { public class PetTargettingMountListener implements Listener {
/**
* TODO - this needs to pass canDamage() in the future, for now it just checks forced pvp state directly
*/
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onWolfAttackMount(org.bukkit.event.entity.EntityDamageByEntityEvent event) { public void onWolfAttackMount(org.bukkit.event.entity.EntityDamageByEntityEvent event) {
if (PreventStabby.getPlugin().getPlayerManager().getForcedPvpState() == PvpState.ENABLED) return;
if (!(event.getDamager() instanceof Tameable) || event.getEntity().getPassengers().isEmpty()) return; if (!(event.getDamager() instanceof Tameable) || event.getEntity().getPassengers().isEmpty()) return;
Tameable damager = (Tameable) event.getDamager(); Tameable damager = (Tameable) event.getDamager();
Entity victim = event.getEntity(); Entity victim = event.getEntity();
@@ -1,7 +1,9 @@
package me.youhavetrouble.preventstabby.listeners.pets; package me.youhavetrouble.preventstabby.listeners.pets;
import me.youhavetrouble.preventstabby.PreventStabby; import me.youhavetrouble.preventstabby.PreventStabby;
import me.youhavetrouble.preventstabby.config.ConfigCache;
import me.youhavetrouble.preventstabby.players.SmartCache; import me.youhavetrouble.preventstabby.players.SmartCache;
import me.youhavetrouble.preventstabby.util.CombatTimer;
import me.youhavetrouble.preventstabby.util.PreventStabbyListener; import me.youhavetrouble.preventstabby.util.PreventStabbyListener;
import org.bukkit.entity.Tameable; import org.bukkit.entity.Tameable;
import org.bukkit.entity.Wolf; import org.bukkit.entity.Wolf;
@@ -14,23 +16,21 @@ public class PetTargettingPetListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onWolfAttackWolf(org.bukkit.event.entity.EntityDamageByEntityEvent event) { public void onWolfAttackWolf(org.bukkit.event.entity.EntityDamageByEntityEvent event) {
if (event.getDamager() instanceof Tameable && event.getEntity() instanceof Tameable) { if (!(event.getDamager() instanceof Tameable) || !(event.getEntity() instanceof Tameable)) return;
Tameable damager = (Tameable) event.getDamager(); Tameable damager = (Tameable) event.getDamager();
Tameable victim = (Tameable) event.getEntity(); Tameable victim = (Tameable) event.getEntity();
if (damager.getOwner() == null || victim.getOwner() == null) return; if (damager.getOwner() == null || victim.getOwner() == null) return;
if (!PreventStabby.getPlugin().getPlayerManager()
.canDamage(
damager.getUniqueId(),
victim.getUniqueId(),
false,
false
))
event.setCancelled(true);
SmartCache smartCache = PreventStabby.getPlugin().getSmartCache();
boolean damagerPvpEnabled = smartCache.getPlayerData(damager.getOwner().getUniqueId()).isPvpEnabled();
boolean victimPvpEnabled = smartCache.getPlayerData(victim.getOwner().getUniqueId()).isPvpEnabled();
if (!victimPvpEnabled || !damagerPvpEnabled) {
if (damager instanceof Wolf) {
Wolf wolf = (Wolf) damager;
wolf.setAngry(false);
}
event.setCancelled(true);
}
}
} }
} }
@@ -8,6 +8,7 @@ import org.bukkit.entity.Tameable;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityTargetEvent;
import java.util.UUID; import java.util.UUID;
@@ -17,7 +18,7 @@ public class PetTargettingPlayerListener implements Listener {
* Stops pets with owners targetting players with pvp off * Stops pets with owners targetting players with pvp off
*/ */
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPetTargetPlayer(org.bukkit.event.entity.EntityTargetEvent event) { public void onPetTargetPlayer(EntityTargetEvent event) {
if (!(event.getEntity() instanceof Tameable)) return; if (!(event.getEntity() instanceof Tameable)) return;
Tameable entity = (Tameable) event.getEntity(); Tameable entity = (Tameable) event.getEntity();
if (!(entity.getOwner() instanceof Player)) return; if (!(entity.getOwner() instanceof Player)) return;
@@ -26,8 +27,15 @@ public class PetTargettingPlayerListener implements Listener {
UUID damager = entity.getOwner().getUniqueId(); UUID damager = entity.getOwner().getUniqueId();
UUID victim = event.getTarget().getUniqueId(); UUID victim = event.getTarget().getUniqueId();
if (PreventStabby.getPlugin().getPlayerManager().canDamage(damager, victim, true, false)) boolean tempting = event.getReason().equals(EntityTargetEvent.TargetReason.TEMPT);
if (PreventStabby.getPlugin().getPlayerManager().canDamage(damager, victim, !tempting, false)) {
if (tempting) {
event.setCancelled(true);
return;
}
CombatTimer.refreshPlayersCombatTime(damager, victim); CombatTimer.refreshPlayersCombatTime(damager, victim);
}
else else
event.setCancelled(true); event.setCancelled(true);
@@ -2,15 +2,14 @@ package me.youhavetrouble.preventstabby.listeners.pets;
import me.youhavetrouble.preventstabby.PreventStabby; import me.youhavetrouble.preventstabby.PreventStabby;
import me.youhavetrouble.preventstabby.config.ConfigCache; import me.youhavetrouble.preventstabby.config.ConfigCache;
import me.youhavetrouble.preventstabby.players.SmartCache;
import me.youhavetrouble.preventstabby.util.CombatTimer; import me.youhavetrouble.preventstabby.util.CombatTimer;
import me.youhavetrouble.preventstabby.util.PluginMessages;
import me.youhavetrouble.preventstabby.util.PreventStabbyListener; import me.youhavetrouble.preventstabby.util.PreventStabbyListener;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.entity.Tameable; import org.bukkit.entity.Tameable;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import java.util.UUID; import java.util.UUID;
@@ -18,32 +17,31 @@ import java.util.UUID;
public class PlayerAttackPetListener implements Listener { public class PlayerAttackPetListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPlayerAttackPet(org.bukkit.event.entity.EntityDamageByEntityEvent event) { public void onPlayerAttackPet(EntityDamageByEntityEvent event) {
if (event.getDamager() instanceof Player && event.getEntity() instanceof Tameable) { if (!(event.getDamager() instanceof Player) || !(event.getEntity() instanceof Tameable)) return;
SmartCache smartCache = PreventStabby.getPlugin().getSmartCache();
Tameable tameable = (Tameable) event.getEntity();
if (tameable.getOwner() == null) return;
UUID damager = event.getDamager().getUniqueId(); Tameable tameable = (Tameable) event.getEntity();
UUID victim = tameable.getOwner().getUniqueId(); if (tameable.getOwner() == null) return;
if (damager.equals(victim)) return; UUID damager = event.getDamager().getUniqueId();
UUID victim = tameable.getOwner().getUniqueId();
ConfigCache config = PreventStabby.getPlugin().getConfigCache(); if (damager.equals(victim)) return;
boolean damagerPvpState = PreventStabby.getPlugin().getPlayerManager().getPlayerPvPState(damager);
if (!damagerPvpState) { ConfigCache config = PreventStabby.getPlugin().getConfigCache();
PluginMessages.sendActionBar(damager, config.getCannot_attack_pets_attacker());
event.setCancelled(true); if (PreventStabby.getPlugin().getPlayerManager()
return; .canDamage(
} damager,
if (!smartCache.getPlayerData(victim).isPvpEnabled()) { victim,
PluginMessages.sendActionBar(damager, config.getCannot_attack_pets_victim()); config.getCannot_attack_pets_attacker(),
event.setCancelled(true); config.getCannot_attack_pets_victim(),
return; false
} ))
CombatTimer.refreshPlayersCombatTime(damager); CombatTimer.refreshPlayersCombatTime(damager);
else
event.setCancelled(true);
}
} }
} }
@@ -18,37 +18,36 @@ import java.util.UUID;
@PreventStabbyListener @PreventStabbyListener
public class PlayerHitPetWithProjectileListener implements Listener { public class PlayerHitPetWithProjectileListener implements Listener {
/** /**
* Cancels damage done by projectiles to pets of players with pvp off * Cancels damage done by projectiles to pets of players with pvp off
*/ */
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPlayerHitPetWithProjectile(org.bukkit.event.entity.EntityDamageByEntityEvent event) { public void onPlayerHitPetWithProjectile(org.bukkit.event.entity.EntityDamageByEntityEvent event) {
if (event.getEntity() instanceof Tameable && event.getDamager() instanceof Projectile) { if (!(event.getEntity() instanceof Tameable) || !(event.getDamager() instanceof Projectile)) return;
Projectile projectile = (Projectile) event.getDamager(); Projectile projectile = (Projectile) event.getDamager();
if (!(projectile.getShooter() instanceof Player)) return; if (!(projectile.getShooter() instanceof Player)) return;
Tameable tameable = (Tameable) event.getEntity(); Tameable tameable = (Tameable) event.getEntity();
if (tameable.getOwner() == null) return; if (tameable.getOwner() == null) return;
UUID damager = ((Player) projectile.getShooter()).getUniqueId(); UUID damager = ((Player) projectile.getShooter()).getUniqueId();
UUID victim = tameable.getOwner().getUniqueId(); UUID victim = tameable.getOwner().getUniqueId();
if (damager.equals(victim)) return; if (damager.equals(victim)) return;
ConfigCache config = PreventStabby.getPlugin().getConfigCache(); ConfigCache config = PreventStabby.getPlugin().getConfigCache();
SmartCache smartCache = PreventStabby.getPlugin().getSmartCache();
if (!smartCache.getPlayerData(damager).isPvpEnabled()) { if (PreventStabby.getPlugin().getPlayerManager()
PluginMessages.sendActionBar(damager, config.getCannot_attack_pets_attacker()); .canDamage(
event.setCancelled(true); damager,
return; victim,
} config.getCannot_attack_pets_attacker(),
if (!smartCache.getPlayerData(victim).isPvpEnabled()) { config.getCannot_attack_pets_victim(),
PluginMessages.sendActionBar(damager, config.getCannot_attack_pets_victim()); false
event.setCancelled(true); ))
return;
}
CombatTimer.refreshPlayersCombatTime(damager); CombatTimer.refreshPlayersCombatTime(damager);
else
event.setCancelled(true);
}
} }
} }
@@ -5,6 +5,9 @@ import me.youhavetrouble.preventstabby.PreventStabby;
import java.time.Instant; import java.time.Instant;
import java.util.UUID; import java.util.UUID;
/**
* PreventStabby player data keeper.<br>
*/
public class PlayerData { public class PlayerData {
private final UUID playerUuid; private final UUID playerUuid;
@@ -18,70 +21,92 @@ public class PlayerData {
this.loginTimestamp = Instant.now().getEpochSecond()-1; this.loginTimestamp = Instant.now().getEpochSecond()-1;
this.teleportTimestamp = Instant.now().getEpochSecond()-1; this.teleportTimestamp = Instant.now().getEpochSecond()-1;
this.inCombat = false; this.inCombat = false;
refreshCachetime(); refreshCacheTime();
} }
public UUID getPlayerUuid() { public UUID getPlayerUuid() {
return playerUuid; 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() { public boolean isPvpEnabled() {
return pvpEnabled; return pvpEnabled;
} }
/**
* Sets player's personal pvp state.
* @param pvpEnabled Pvp state to set.
*/
public void setPvpEnabled(boolean pvpEnabled) { public void setPvpEnabled(boolean pvpEnabled) {
this.pvpEnabled = pvpEnabled; this.pvpEnabled = pvpEnabled;
} }
public long getCachetime() { protected long getCachetime() {
return cachetime; return cachetime;
} }
public void refreshCachetime() { protected void refreshCacheTime() {
this.cachetime = Instant.now().getEpochSecond()+ PreventStabby.getPlugin().getConfigCache().getCache_time(); 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; this.combattime = combattime;
} }
/**
* Sets player in combat and sets combat time to the interval set in config.
* @see PlayerManager#refreshPlayersCombatTime(UUID)
*/
public void refreshCombatTime() { public void refreshCombatTime() {
this.combattime = Instant.now().getEpochSecond()+ PreventStabby.getPlugin().getConfigCache().getCombat_time(); this.combattime = Instant.now().getEpochSecond()+ PreventStabby.getPlugin().getConfigCache().getCombat_time();
} }
public boolean getLastCombatCheck() { protected boolean getLastCombatCheck() {
return lastCombatCheck; return lastCombatCheck;
} }
public void setLastCombatCheck(boolean bool) { protected void setLastCombatCheck(boolean bool) {
lastCombatCheck = bool; lastCombatCheck = bool;
} }
public void setLoginTimestamp(long loginTimestamp) { protected void setLoginTimestamp(long loginTimestamp) {
this.loginTimestamp = loginTimestamp + PreventStabby.getPlugin().getConfigCache().getLogin_protection_time()-1; this.loginTimestamp = loginTimestamp + PreventStabby.getPlugin().getConfigCache().getLogin_protection_time()-1;
} }
public long getLoginTimestamp() { protected long getLoginTimestamp() {
return loginTimestamp; return loginTimestamp;
} }
public void setTeleportTimestamp(long teleportTimestamp) { protected void setTeleportTimestamp(long teleportTimestamp) {
this.teleportTimestamp = teleportTimestamp + PreventStabby.getPlugin().getConfigCache().getTeleport_protection_time()-1; this.teleportTimestamp = teleportTimestamp + PreventStabby.getPlugin().getConfigCache().getTeleport_protection_time()-1;
} }
public long getTeleportTimestamp() { protected long getTeleportTimestamp() {
return teleportTimestamp; return teleportTimestamp;
} }
/**
* Returns player's current combat state.
* @return Player's current combat state.
*/
public boolean isInCombat() { public boolean isInCombat() {
return inCombat; return inCombat;
} }
public void setInCombat(boolean inCombat) { protected void setInCombat(boolean inCombat) {
this.inCombat = 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.PreventStabby;
import me.youhavetrouble.preventstabby.players.PlayerData;
import me.youhavetrouble.preventstabby.util.PreventStabbyListener; import me.youhavetrouble.preventstabby.util.PreventStabbyListener;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; 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.PreventStabby;
import me.youhavetrouble.preventstabby.players.PlayerData; import me.youhavetrouble.preventstabby.players.PlayerData;
@@ -34,7 +34,7 @@ public class PlayerJoinAndLeaveListener implements Listener {
}); });
return; return;
} }
playerData.refreshCachetime(); playerData.refreshCacheTime();
playerData.setLoginTimestamp(time); playerData.setLoginTimestamp(time);
} }
@@ -12,6 +12,7 @@ import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import javax.annotation.Nullable;
import java.time.Instant; import java.time.Instant;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@@ -59,10 +60,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) { public void refreshPlayersCombatTime(UUID uuid) {
PlayerData data = playerList.get(uuid); PlayerData data = playerList.get(uuid);
if (data == null) return; if (data == null) return;
@@ -70,25 +71,46 @@ public class PlayerManager {
if (player == null || player.isDead()) return; if (player == null || player.isDead()) return;
data.refreshCombatTime(); data.refreshCombatTime();
data.setInCombat(true); 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) { public PlayerData getPlayer(UUID uuid) {
return playerList.get(uuid); return playerList.get(uuid);
} }
public void addPlayer(UUID uuid, PlayerData data) { protected void addPlayer(UUID uuid, PlayerData data) {
playerList.put(uuid, 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) { public boolean getPlayerPvPState(UUID uuid) {
return PreventStabby.getPlugin().getSmartCache().getPlayerData(uuid).isPvpEnabled(); 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) { public void setPlayerPvpState(UUID uuid, boolean state) {
PreventStabby.getPlugin().getSmartCache().getPlayerData(uuid).setPvpEnabled(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) { public boolean togglePlayerPvpState(UUID uuid) {
SmartCache smartCache = PreventStabby.getPlugin().getSmartCache(); SmartCache smartCache = PreventStabby.getPlugin().getSmartCache();
if (smartCache.getPlayerData(uuid).isPvpEnabled()) { if (smartCache.getPlayerData(uuid).isPvpEnabled()) {
@@ -100,11 +122,43 @@ 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) { public boolean canDamage(UUID attacker, UUID victim, boolean sendDenyMessage) {
return canDamage(attacker, victim, sendDenyMessage, true); 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) { public boolean canDamage(UUID attacker, UUID victim, boolean sendDenyMessage, boolean checkVictimSpawnProtection) {
ConfigCache config = PreventStabby.getPlugin().getConfigCache();
String attackerMessage = sendDenyMessage ? config.getCannot_attack_attacker() : null;
String victimMessage = sendDenyMessage ? config.getCannot_attack_victim() : null;
return canDamage(attacker, victim, attackerMessage, victimMessage, checkVictimSpawnProtection);
}
/**
* Check if attacker can harm the victim.
* @param attacker Atacker's UUID.
* @param victim Victim's UUID.
* @param attackerDenyMessage Message that action was denied to the attacker.
* @param victimDenyMessage Message that action was denied to the victim.
* @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, @Nullable String attackerDenyMessage, @Nullable String victimDenyMessage, boolean checkVictimSpawnProtection) {
if (hasLoginProtection(attacker) || hasTeleportProtection(attacker)) return false; if (hasLoginProtection(attacker) || hasTeleportProtection(attacker)) return false;
if (checkVictimSpawnProtection && hasLoginProtection(victim)) return false; if (checkVictimSpawnProtection && hasLoginProtection(victim)) return false;
@@ -128,9 +182,8 @@ public class PlayerManager {
if (PreventStabby.worldGuardHookEnabled() && attackerPlayer != null && WorldGuardHook.isPlayerForcedToPvp(attackerPlayer)) if (PreventStabby.worldGuardHookEnabled() && attackerPlayer != null && WorldGuardHook.isPlayerForcedToPvp(attackerPlayer))
return true; return true;
if (sendDenyMessage) { if (attackerDenyMessage != null) {
ConfigCache config = PreventStabby.getPlugin().getConfigCache(); PluginMessages.sendActionBar(attacker, attackerDenyMessage);
PluginMessages.sendActionBar(attacker, config.getCannot_attack_attacker());
} }
return false; return false;
} }
@@ -139,7 +192,7 @@ public class PlayerManager {
if (PreventStabby.worldGuardHookEnabled() && victimPlayer != null && WorldGuardHook.isPlayerForcedToPvp(victimPlayer)) if (PreventStabby.worldGuardHookEnabled() && victimPlayer != null && WorldGuardHook.isPlayerForcedToPvp(victimPlayer))
return true; return true;
if (sendDenyMessage) { if (victimDenyMessage != null) {
ConfigCache config = PreventStabby.getPlugin().getConfigCache(); ConfigCache config = PreventStabby.getPlugin().getConfigCache();
PluginMessages.sendActionBar(attacker, config.getCannot_attack_victim()); PluginMessages.sendActionBar(attacker, config.getCannot_attack_victim());
} }
@@ -149,8 +202,8 @@ public class PlayerManager {
} }
/** /**
* @param uuid Player UUIDs * @param uuid Player UUIDs.
* @return true if any of the provided UUIDs has spawn protection * @return True if any of the provided UUIDs has spawn protection.
*/ */
public boolean hasLoginProtection(UUID... uuid) { public boolean hasLoginProtection(UUID... uuid) {
SmartCache smartCache = PreventStabby.getPlugin().getSmartCache(); SmartCache smartCache = PreventStabby.getPlugin().getSmartCache();
@@ -161,15 +214,27 @@ public class PlayerManager {
return false; return false;
} }
/**
* @param uuid Player UUID.
* @return True if player tied to the uuid currently has teleport protection.
*/
public boolean hasTeleportProtection(UUID uuid) { public boolean hasTeleportProtection(UUID uuid) {
SmartCache smartCache = PreventStabby.getPlugin().getSmartCache(); SmartCache smartCache = PreventStabby.getPlugin().getSmartCache();
return Instant.now().getEpochSecond() < smartCache.getPlayerData(uuid).getTeleportTimestamp(); return Instant.now().getEpochSecond() < smartCache.getPlayerData(uuid).getTeleportTimestamp();
} }
/**
* Returns current forced pvp state.
* @return Current forced pvp state.
*/
public PvpState getForcedPvpState() { public PvpState getForcedPvpState() {
return pvpForcedState; return pvpForcedState;
} }
/**
* Sets current forced pvp state.
* @param forcedPvpState New forced pvp state.
*/
public void setForcedPvpState(PvpState forcedPvpState) { public void setForcedPvpState(PvpState forcedPvpState) {
this.pvpForcedState = 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.PreventStabby;
import me.youhavetrouble.preventstabby.players.PlayerData;
import me.youhavetrouble.preventstabby.players.SmartCache;
import me.youhavetrouble.preventstabby.util.PreventStabbyListener; import me.youhavetrouble.preventstabby.util.PreventStabbyListener;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@@ -16,7 +16,7 @@ public class SmartCache {
try { try {
Player player = Bukkit.getPlayer(e.getKey()); Player player = Bukkit.getPlayer(e.getKey());
if (player != null && player.isOnline()) { if (player != null && player.isOnline()) {
PreventStabby.getPlugin().getPlayerManager().refreshPlayersCacheTime(e.getKey()); e.getValue().refreshCacheTime();
} }
} catch (NullPointerException ignored) {} } catch (NullPointerException ignored) {}
} }
@@ -32,8 +32,9 @@ public class SmartCache {
public PlayerData getPlayerData(UUID uuid) { public PlayerData getPlayerData(UUID uuid) {
// Try to get data from cache and refresh it // Try to get data from cache and refresh it
try { try {
PreventStabby.getPlugin().getPlayerManager().refreshPlayersCacheTime(uuid); PlayerData data = PreventStabby.getPlugin().getPlayerManager().getPlayer(uuid);
return PreventStabby.getPlugin().getPlayerManager().getPlayer(uuid); data.refreshCacheTime();
return data;
} catch (NullPointerException e) { } catch (NullPointerException e) {
// If player data is not in cache get it from database and put into cache // If player data is not in cache get it from database and put into cache
try { try {
@@ -11,8 +11,7 @@ public class CombatTimer {
public static void refreshPlayersCombatTime(UUID uuid) { public static void refreshPlayersCombatTime(UUID uuid) {
try { 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); Player player = Bukkit.getPlayer(uuid);
if (player == null || !player.isOnline()) return; if (player == null || !player.isOnline()) return;
@@ -22,7 +21,7 @@ public class CombatTimer {
if (playerEnterCombatEvent.isCancelled()) return; if (playerEnterCombatEvent.isCancelled()) return;
PreventStabby.getPlugin().getPlayerManager().refreshPlayersCombatTime(uuid); PreventStabby.getPlugin().getPlayerManager().refreshPlayersCombatTime(uuid);
if (combattime <= now) { if (combattime <= 0) {
PluginMessages.sendActionBar(uuid, PreventStabby.getPlugin().getConfigCache().getEntering_combat()); PluginMessages.sendActionBar(uuid, PreventStabby.getPlugin().getConfigCache().getEntering_combat());
} }
}); });
@@ -38,7 +37,7 @@ public class CombatTimer {
public static boolean isInCombat(UUID uuid) { public static boolean isInCombat(UUID uuid) {
try { try {
return PreventStabby.getPlugin().getPlayerManager().getPlayer(uuid).getCombattime() >= Instant.now().getEpochSecond(); return PreventStabby.getPlugin().getPlayerManager().getPlayer(uuid).getCombatTime() >= Instant.now().getEpochSecond();
} catch (Exception e) { } catch (Exception e) {
return false; return false;
} }
@@ -24,7 +24,6 @@ public class PluginMessages {
} }
public static Component parseMessage(CommandSender sender,String message) { public static Component parseMessage(CommandSender sender,String message) {
if (sender instanceof Player && isPlaceholderApiEnabled()) { if (sender instanceof Player && isPlaceholderApiEnabled()) {
Player player = (Player) sender; Player player = (Player) sender;
message = PlaceholderAPI.setPlaceholders(player, message); message = PlaceholderAPI.setPlaceholders(player, message);
@@ -37,15 +36,18 @@ public class PluginMessages {
} }
public static void sendMessage(CommandSender sender, String message) { public static void sendMessage(CommandSender sender, String message) {
if ("".equals(message)) return;
audiences.sender(sender).sendMessage(parseMessage(sender, message)); audiences.sender(sender).sendMessage(parseMessage(sender, message));
} }
public static void sendActionBar(Player player, String message) { public static void sendActionBar(Player player, String message) {
if ("".equals(message)) return;
Component parsedMessage = parseMessage(player, message); Component parsedMessage = parseMessage(player, message);
audiences.player(player).sendActionBar(parsedMessage); audiences.player(player).sendActionBar(parsedMessage);
} }
public static void sendActionBar(UUID uuid, String message) { public static void sendActionBar(UUID uuid, String message) {
if ("".equals(message)) return;
Player player = Bukkit.getPlayer(uuid); Player player = Bukkit.getPlayer(uuid);
if (player == null) return; if (player == null) return;
sendActionBar(player, message); sendActionBar(player, message);
@@ -57,6 +59,7 @@ public class PluginMessages {
} }
public static void broadcastMessage(Player player, String message) { public static void broadcastMessage(Player player, String message) {
if ("".equals(message)) return;
message = parsePlayerName(player, message); message = parsePlayerName(player, message);
if (PreventStabby.getPlugin().getServer().getPluginManager().getPlugin("PlaceholderAPI") != null) { if (PreventStabby.getPlugin().getServer().getPluginManager().getPlugin("PlaceholderAPI") != null) {
message = PlaceholderAPI.setPlaceholders(player, message); message = PlaceholderAPI.setPlaceholders(player, message);
@@ -65,6 +68,7 @@ public class PluginMessages {
} }
public static void broadcastMessage(String message) { public static void broadcastMessage(String message) {
if ("".equals(message)) return;
audiences.all().sendMessage(parseMessage(message)); audiences.all().sendMessage(parseMessage(message));
} }
+13
View File
@@ -22,15 +22,28 @@ permissions:
preventstabby.command: preventstabby.command:
default: true default: true
description: Allows usage of /pvp command 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: preventstabby.command.toggle:
default: true default: true
description: Allows usage of /pvp <toggle/enable/disable> command description: Allows usage of /pvp <toggle/enable/disable> command
children:
preventstabby.command: true
preventstabby.command.toggle.others: preventstabby.command.toggle.others:
default: op default: op
description: Allows usage of /pvp <toggle/enable/disable> <player> command description: Allows usage of /pvp <toggle/enable/disable> <player> command
children:
preventstabby.command: true
preventstabby.command.toggle.global: preventstabby.command.toggle.global:
default: op default: op
description: Allows usage of /pvp override <enabled/disabled/none> command description: Allows usage of /pvp override <enabled/disabled/none> command
children:
preventstabby.command: true
preventstabby.command.reload: preventstabby.command.reload:
default: op default: op
description: Allows usage of /pvp reload command description: Allows usage of /pvp reload command
children:
preventstabby.command: true