mirror of
https://github.com/YouHaveTrouble/PreventStabby.git
synced 2026-05-12 05:16:55 +00:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7d2c41646e | |||
| 61908b8fab | |||
| 5abf99cd6c | |||
| 3c014304ba |
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
<groupId>me.youhavetrouble</groupId>
|
<groupId>me.youhavetrouble</groupId>
|
||||||
<artifactId>PreventStabby</artifactId>
|
<artifactId>PreventStabby</artifactId>
|
||||||
<version>1.8.0</version>
|
<version>1.8.1</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>PreventStabby</name>
|
<name>PreventStabby</name>
|
||||||
@@ -100,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>
|
||||||
|
|||||||
@@ -76,18 +76,23 @@ public final class PreventStabby extends JavaPlugin {
|
|||||||
pvpCommand.setExecutor(mainCommand);
|
pvpCommand.setExecutor(mainCommand);
|
||||||
pvpCommand.setTabCompleter(mainCommand);
|
pvpCommand.setTabCompleter(mainCommand);
|
||||||
|
|
||||||
|
if (getServer().getPluginManager().getPlugin("PlaceholderAPI") != null) {
|
||||||
|
new PlacoholderApiHook(this).register();
|
||||||
|
}
|
||||||
|
|
||||||
|
Metrics metrics = new Metrics(this, 14074);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onLoad() {
|
||||||
|
if (getServer().getPluginManager().getPlugin("WorldGuard") != null) {
|
||||||
try {
|
try {
|
||||||
WorldGuardHook.init();
|
WorldGuardHook.init();
|
||||||
worldGuardHook = true;
|
worldGuardHook = true;
|
||||||
} catch (NoClassDefFoundError e) {
|
} catch (NoClassDefFoundError e) {
|
||||||
worldGuardHook = false;
|
worldGuardHook = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (getServer().getPluginManager().getPlugin("PlaceholderAPI") != null) {
|
|
||||||
new PlacoholderApiHook(this).register();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Metrics metrics = new Metrics(this, 14074);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean worldGuardHookEnabled() {
|
public static boolean worldGuardHookEnabled() {
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
@@ -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);
|
||||||
|
Bukkit.getScheduler().runTask(PreventStabby.getPlugin(), () -> {
|
||||||
|
Bukkit.getPluginManager().callEvent(toggleEvent);
|
||||||
|
if (toggleEvent.isSendMessage()) {
|
||||||
if (currentState) {
|
if (currentState) {
|
||||||
PluginMessages.sendMessage(sender, PreventStabby.getPlugin().getConfigCache().getPvp_enabled());
|
PluginMessages.sendMessage(sender, PreventStabby.getPlugin().getConfigCache().getPvp_enabled());
|
||||||
} else {
|
} else {
|
||||||
PluginMessages.sendMessage(sender, PreventStabby.getPlugin().getConfigCache().getPvp_disabled());
|
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();
|
||||||
@@ -77,7 +95,13 @@ public class PvpToggleCommand {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
PreventStabby.getPlugin().getPlayerManager().setPlayerPvpState(player.getUniqueId(), true);
|
PreventStabby.getPlugin().getPlayerManager().setPlayerPvpState(player.getUniqueId(), true);
|
||||||
|
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());
|
PluginMessages.sendMessage(sender, PreventStabby.getPlugin().getConfigCache().getPvp_enabled());
|
||||||
|
}
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
PluginMessages.sendMessage(sender, "Try /pvp enable <player>");
|
PluginMessages.sendMessage(sender, "Try /pvp enable <player>");
|
||||||
}
|
}
|
||||||
@@ -101,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>");
|
||||||
@@ -123,7 +154,13 @@ public class PvpToggleCommand {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
PreventStabby.getPlugin().getPlayerManager().setPlayerPvpState(player.getUniqueId(), false);
|
PreventStabby.getPlugin().getPlayerManager().setPlayerPvpState(player.getUniqueId(), false);
|
||||||
|
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());
|
PluginMessages.sendMessage(sender, PreventStabby.getPlugin().getConfigCache().getPvp_disabled());
|
||||||
|
}
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
PluginMessages.sendMessage(sender, "Try /pvp disable <player>");
|
PluginMessages.sendMessage(sender, "Try /pvp disable <player>");
|
||||||
}
|
}
|
||||||
@@ -146,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)) {
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ public class PlacoholderApiHook extends PlaceholderExpansion {
|
|||||||
if (!player.isOnline()) {
|
if (!player.isOnline()) {
|
||||||
return legacyComponentSerializer.serialize(PluginMessages.parseMessage(plugin.getConfigCache().getPlaceholder_not_in_combat()));
|
return legacyComponentSerializer.serialize(PluginMessages.parseMessage(plugin.getConfigCache().getPlaceholder_not_in_combat()));
|
||||||
}
|
}
|
||||||
long seconds = plugin.getPlayerManager().getPlayer(uuid).getCombattime() - Instant.now().getEpochSecond();
|
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!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
-2
@@ -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;
|
||||||
+2
-2
@@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -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) {
|
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 +70,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,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) {
|
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) {
|
||||||
|
|
||||||
if (hasLoginProtection(attacker) || hasTeleportProtection(attacker)) return false;
|
if (hasLoginProtection(attacker) || hasTeleportProtection(attacker)) return false;
|
||||||
@@ -149,8 +186,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 +198,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
-3
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user