diff --git a/src/main/java/me/youhavetrouble/preventstabby/config/ConfigCache.java b/src/main/java/me/youhavetrouble/preventstabby/config/ConfigCache.java index 0603153..ac78ac0 100644 --- a/src/main/java/me/youhavetrouble/preventstabby/config/ConfigCache.java +++ b/src/main/java/me/youhavetrouble/preventstabby/config/ConfigCache.java @@ -19,9 +19,9 @@ public class ConfigCache { 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; - public final String cannotAttackForcedPvpOff, cannotAttackTeleportOrSpawnProtectionAttacker, - cannotAttackTeleportOrSpawnProtectionVictim, cannotAttackPetsTeleportOrSpawnProtectionAttacker, - cannotAttackMountsTeleportOrSpawnProtectionAttacker; + public final String cannot_attack_forced_pvp_off, cannot_attack_teleport_or_spawn_protection_attacker, + cannot_attack_pets_teleport_or_spawn_protection_attacker, cannot_attack_mounts_teleport_or_spawn_protection_attacker, + cannot_attack_teleport_or_spawn_protection_victim; public final double lava_and_fire_stopper_radius; public final long combat_time, login_protection_time, teleport_protection_time, cache_time; @@ -89,11 +89,11 @@ public class ConfigCache { 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"); - this.cannotAttackForcedPvpOff = config.getString("messages.cannot_attack_pvp_force_off", "PvP is forcibly disabled"); - this.cannotAttackTeleportOrSpawnProtectionAttacker = config.getString("messages.cannot_attack_teleport_or_spawn_protection_attacker", "You can't attack players while they have teleport or spawn protection"); - this.cannotAttackPetsTeleportOrSpawnProtectionAttacker = config.getString("messages.cannot_attack_pets_teleport_or_spawn_protection_attacker", "You can't attack pets while you have teleport or spawn protection"); - this.cannotAttackMountsTeleportOrSpawnProtectionAttacker = config.getString("messages.cannot_attack_mounts_teleport_or_spawn_protection_attacker", "You can't attack mounts while you have teleport or spawn protection"); - this.cannotAttackTeleportOrSpawnProtectionVictim = config.getString("messages.cannot_attack_teleport_or_spawn_protection_victim", "You can't attack players while you have teleport or spawn protection"); + this.cannot_attack_forced_pvp_off = config.getString("messages.cannot_attack_pvp_force_off", "PvP is forcibly disabled"); + this.cannot_attack_teleport_or_spawn_protection_attacker = config.getString("messages.cannot_attack_teleport_or_spawn_protection_attacker", "You can't attack players while they have teleport or spawn protection"); + this.cannot_attack_pets_teleport_or_spawn_protection_attacker = config.getString("messages.cannot_attack_pets_teleport_or_spawn_protection_attacker", "You can't attack pets while you have teleport or spawn protection"); + this.cannot_attack_mounts_teleport_or_spawn_protection_attacker = config.getString("messages.cannot_attack_mounts_teleport_or_spawn_protection_attacker", "You can't attack mounts while you have teleport or spawn protection"); + this.cannot_attack_teleport_or_spawn_protection_victim = config.getString("messages.cannot_attack_teleport_or_spawn_protection_victim", "You can't attack players while you have teleport or spawn protection"); } diff --git a/src/main/java/me/youhavetrouble/preventstabby/data/DamageCheckResult.java b/src/main/java/me/youhavetrouble/preventstabby/data/DamageCheckResult.java index 99c91d0..3444c27 100644 --- a/src/main/java/me/youhavetrouble/preventstabby/data/DamageCheckResult.java +++ b/src/main/java/me/youhavetrouble/preventstabby/data/DamageCheckResult.java @@ -6,7 +6,6 @@ import java.util.UUID; /** * @param ableToDamage Result of the damage check * @param feedbackForAttacker Feedback to send to the attacker - * @param feedbackForVictim Feedback to send to the victim * @param attackerId UUID of attacker player * @param victimId UUID of victim player */ @@ -15,7 +14,7 @@ public record DamageCheckResult( @Nullable UUID attackerId, @Nullable UUID victimId, @Nullable String feedbackForAttacker, - @Nullable String feedbackForVictim + boolean shouldVictimBePutInCombat ) { public static DamageCheckResult positive() { @@ -24,17 +23,17 @@ public record DamageCheckResult( null, null, null, - null + true ); } - public static DamageCheckResult positive(UUID attackerId, UUID victimId) { + public static DamageCheckResult positive(UUID attackerId, UUID victimId, boolean shouldVictimBePutInCombat) { return new DamageCheckResult( true, attackerId, victimId, null, - null + shouldVictimBePutInCombat ); } diff --git a/src/main/java/me/youhavetrouble/preventstabby/data/PlayerListener.java b/src/main/java/me/youhavetrouble/preventstabby/data/PlayerListener.java index f030ad0..7b82cf8 100644 --- a/src/main/java/me/youhavetrouble/preventstabby/data/PlayerListener.java +++ b/src/main/java/me/youhavetrouble/preventstabby/data/PlayerListener.java @@ -10,22 +10,31 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityDeathEvent; -import org.bukkit.event.player.PlayerJoinEvent; -import org.bukkit.event.player.PlayerQuitEvent; -import org.bukkit.event.player.PlayerTeleportEvent; +import org.bukkit.event.player.*; import java.time.Instant; import java.util.UUID; +import java.util.concurrent.ExecutionException; public class PlayerListener implements Listener { - /** - * This event is here to get players saved options on join - */ + + @EventHandler(ignoreCancelled = true) + public void onPlayerLogin(AsyncPlayerPreLoginEvent event) { + if (!event.getLoginResult().equals(AsyncPlayerPreLoginEvent.Result.ALLOWED)) return; + UUID uuid = event.getUniqueId(); + try { + PreventStabby.getPlugin().getPlayerManager().getPlayerData(uuid).get(); + } catch (ExecutionException | InterruptedException e) { + PreventStabby.getPlugin().getLogger().severe(e.getMessage()); + PreventStabby.getPlugin().getLogger().severe("Failed to load data for player %s".formatted(event.getPlayerProfile().getName())); + PreventStabby.getPlugin().getPlayerManager().addPlayer(uuid, new PlayerData(uuid, false)); + } + } + @EventHandler(ignoreCancelled = true) public void onPlayerJoin(PlayerJoinEvent event) { - Player player = event.getPlayer(); - UUID uuid = player.getUniqueId(); - PreventStabby.getPlugin().getPlayerManager().addPlayer(uuid, new PlayerData(uuid, false)); + PlayerData playerData = PreventStabby.getPlugin().getPlayerManager().getPlayer(event.getPlayer().getUniqueId()); + playerData.setLoginTimestamp(System.currentTimeMillis()); } /** diff --git a/src/main/java/me/youhavetrouble/preventstabby/data/PlayerManager.java b/src/main/java/me/youhavetrouble/preventstabby/data/PlayerManager.java index 234ba94..3432325 100644 --- a/src/main/java/me/youhavetrouble/preventstabby/data/PlayerManager.java +++ b/src/main/java/me/youhavetrouble/preventstabby/data/PlayerManager.java @@ -41,7 +41,7 @@ public class PlayerManager { if (ownerId == null) return; getPlayerData(ownerId); })); - }), 0, 20 * 15); + }), 5, 20 * 15); Bukkit.getGlobalRegionScheduler().runAtFixedRate(plugin, (task) -> { for (PlayerData playerData : playerList.values()) { @@ -108,6 +108,7 @@ public class PlayerManager { if (attacker == null || victim == null) return; if (!damageCheckResult.ableToDamage()) return; attacker.markInCombat(); + if (!damageCheckResult.shouldVictimBePutInCombat()) return; victim.markInCombat(); } @@ -137,28 +138,63 @@ public class PlayerManager { return DamageCheckResult.positive(); } + if (attackerPlayerData.getPlayerUuid().equals(victimPlayerData.getPlayerUuid())) { + return DamageCheckResult.positive(attackerId, victimId, false); + } + if (attackerPlayerData.isProtected()) { String message = switch (victimClassifier) { - case PLAYER -> plugin.getConfigCache().cannotAttackTeleportOrSpawnProtectionAttacker; - case PET -> plugin.getConfigCache().cannotAttackPetsTeleportOrSpawnProtectionAttacker; - case MOUNT -> plugin.getConfigCache().cannotAttackMountsTeleportOrSpawnProtectionAttacker; + case PLAYER -> plugin.getConfigCache().cannot_attack_teleport_or_spawn_protection_attacker; + case PET -> plugin.getConfigCache().cannot_attack_pets_teleport_or_spawn_protection_attacker; + case MOUNT -> plugin.getConfigCache().cannot_attack_mounts_teleport_or_spawn_protection_attacker; default -> null; }; - return new DamageCheckResult(false, attackerId, victimId, message, null); + return new DamageCheckResult(false, attackerId, victimId, message, false); } if (victimPlayerData.isProtected()) { String message = null; if (victimClassifier == Target.EntityClassifier.PLAYER) { - message = plugin.getConfigCache().cannotAttackTeleportOrSpawnProtectionVictim; + message = plugin.getConfigCache().cannot_attack_teleport_or_spawn_protection_victim; } - return new DamageCheckResult(false, attackerId, victimId, message, null); + return new DamageCheckResult(false, attackerId, victimId, message, false); } - return switch (getForcedPvpState()) { - case DISABLED -> new DamageCheckResult(false, attackerId, victimId, plugin.getConfigCache().cannotAttackForcedPvpOff, null); - case ENABLED -> DamageCheckResult.positive(attackerId, victimId); - default -> DamageCheckResult.positive(attackerId, victimId); - }; + switch (getForcedPvpState()) { + case DISABLED -> { + return new DamageCheckResult(false, attackerId, victimId, plugin.getConfigCache().cannot_attack_forced_pvp_off, false); + } + case ENABLED -> { + return DamageCheckResult.positive(attackerId, victimId, victimClassifier.equals(Target.EntityClassifier.PLAYER)); + } + } + + if (!attackerPlayerData.isPvpEnabled()) { + String message = switch (victimClassifier) { + case PLAYER -> plugin.getConfigCache().cannot_attack_attacker; + case PET -> plugin.getConfigCache().cannot_attack_pets_victim; + case MOUNT -> plugin.getConfigCache().cannot_attack_mounts_attacker; + default -> null; + }; + return new DamageCheckResult(false, attackerId, victimId, message, victimClassifier.equals(Target.EntityClassifier.PLAYER)); + } + + if (!victimPlayerData.isPvpEnabled()) { + String message = switch (victimClassifier) { + case PLAYER -> plugin.getConfigCache().cannot_attack_victim; + case PET -> plugin.getConfigCache().cannot_attack_pets_attacker; + case MOUNT -> plugin.getConfigCache().cannot_attack_mounts_victim; + default -> null; + }; + return new DamageCheckResult(false, attackerId, victimId, message, victimClassifier.equals(Target.EntityClassifier.PLAYER)); + } + + return new DamageCheckResult( + true, + attackerId, + victimId, + null, + victimClassifier.equals(Target.EntityClassifier.PLAYER) + ); } /** diff --git a/src/main/java/me/youhavetrouble/preventstabby/data/Target.java b/src/main/java/me/youhavetrouble/preventstabby/data/Target.java index 69abaa7..50361cb 100644 --- a/src/main/java/me/youhavetrouble/preventstabby/data/Target.java +++ b/src/main/java/me/youhavetrouble/preventstabby/data/Target.java @@ -15,7 +15,7 @@ import java.util.UUID; public class Target { - public static final NamespacedKey playerSourceIdKey = new NamespacedKey("preventstabby", "playerSource"); + public static final NamespacedKey playerSourceIdKey = new NamespacedKey("preventstabby", "playersource"); /** * The unique identifier for a player. diff --git a/src/main/java/me/youhavetrouble/preventstabby/listeners/PlayerDamageListener.java b/src/main/java/me/youhavetrouble/preventstabby/listeners/PlayerDamageListener.java index 1329f42..c8af303 100644 --- a/src/main/java/me/youhavetrouble/preventstabby/listeners/PlayerDamageListener.java +++ b/src/main/java/me/youhavetrouble/preventstabby/listeners/PlayerDamageListener.java @@ -22,7 +22,6 @@ public class PlayerDamageListener implements Listener { Entity victim = event.getEntity(); DamageCheckResult result = plugin.getPlayerManager().canDamage(attacker, victim); - if (!result.ableToDamage()) { event.setCancelled(true); } diff --git a/src/main/java/me/youhavetrouble/preventstabby/util/DatabaseSQLite.java b/src/main/java/me/youhavetrouble/preventstabby/util/DatabaseSQLite.java index 6f7b275..9a90d4c 100644 --- a/src/main/java/me/youhavetrouble/preventstabby/util/DatabaseSQLite.java +++ b/src/main/java/me/youhavetrouble/preventstabby/util/DatabaseSQLite.java @@ -31,24 +31,26 @@ public class DatabaseSQLite { String sql = "CREATE TABLE IF NOT EXISTS `players` (`player_uuid` varchar(36) UNIQUE PRIMARY KEY, `pvpenabled` boolean);"; statement.execute(sql); } catch (SQLException exception) { - exception.printStackTrace(); + logger.warning(exception.getMessage()); } } public PlayerData getPlayerInfo(UUID uuid) { try (Connection conn = DriverManager.getConnection(url)) { PreparedStatement statement = conn.prepareStatement( - "INSERT OR IGNORE INTO `players` (player_uuid, pvpenabled) VALUES (?, ?); SELECT * FROM `players` WHERE `player_uuid` = ?;" + "INSERT OR IGNORE INTO `players` (player_uuid, pvpenabled) VALUES (?, ?);" ); statement.setString(1, uuid.toString()); statement.setBoolean(2, PreventStabby.getPlugin().getConfigCache().pvp_enabled_by_default); - statement.setString(3, uuid.toString()); + statement.executeUpdate(); + statement = conn.prepareStatement("SELECT * FROM `players` WHERE `player_uuid` = ?;"); + statement.setString(1, uuid.toString()); statement.executeQuery(); - ResultSet result = statement.getResultSet(); + ResultSet result = statement.executeQuery(); boolean state = result.getBoolean("pvpenabled"); return new PlayerData(uuid, state); } catch (SQLException exception) { - exception.printStackTrace(); + logger.warning(exception.getMessage()); } return null; } @@ -61,10 +63,10 @@ public class DatabaseSQLite { insertnewuser.setString(2, uuid.toString()); } catch (SQLException exception) { logger.severe("Error while saving player data!"); - exception.printStackTrace(); + logger.warning(exception.getMessage()); } } catch (SQLException exception) { - exception.printStackTrace(); + logger.warning(exception.getMessage()); } } diff --git a/src/main/java/me/youhavetrouble/preventstabby/util/PluginMessages.java b/src/main/java/me/youhavetrouble/preventstabby/util/PluginMessages.java index 47188c1..6f249e1 100644 --- a/src/main/java/me/youhavetrouble/preventstabby/util/PluginMessages.java +++ b/src/main/java/me/youhavetrouble/preventstabby/util/PluginMessages.java @@ -71,9 +71,6 @@ public class PluginMessages { } public static void sendOutMessages(DamageCheckResult damageCheckResult) { - if (damageCheckResult.victimId() != null && damageCheckResult.feedbackForVictim() != null) { - sendActionBar(damageCheckResult.victimId(), damageCheckResult.feedbackForVictim()); - } if (damageCheckResult.attackerId() != null && damageCheckResult.feedbackForAttacker() != null) { sendActionBar(damageCheckResult.attackerId(), damageCheckResult.feedbackForAttacker()); }