mirror of
https://github.com/YouHaveTrouble/PreventStabby.git
synced 2026-05-12 05:16:55 +00:00
Improve exception handling and PvP process in DatabaseSQLite and PlayerManager
Refactored exception handling in DatabaseSQLite to use logger instead of printStackTrace() for better error tracking. Streamlined player PvP process in PlayerManager with clear conditions and appropriate responses. Enhancements in other classes were also made to ensure smooth execution and maintain code standards.
This commit is contained in:
@@ -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,
|
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;
|
placeholder_pvp_forced_true, placeholder_pvp_forced_false, placeholder_pvp_forced_none;
|
||||||
|
|
||||||
public final String cannotAttackForcedPvpOff, cannotAttackTeleportOrSpawnProtectionAttacker,
|
public final String cannot_attack_forced_pvp_off, cannot_attack_teleport_or_spawn_protection_attacker,
|
||||||
cannotAttackTeleportOrSpawnProtectionVictim, cannotAttackPetsTeleportOrSpawnProtectionAttacker,
|
cannot_attack_pets_teleport_or_spawn_protection_attacker, cannot_attack_mounts_teleport_or_spawn_protection_attacker,
|
||||||
cannotAttackMountsTeleportOrSpawnProtectionAttacker;
|
cannot_attack_teleport_or_spawn_protection_victim;
|
||||||
|
|
||||||
public final double lava_and_fire_stopper_radius;
|
public final double lava_and_fire_stopper_radius;
|
||||||
public final long combat_time, login_protection_time, teleport_protection_time, cache_time;
|
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_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.placeholder_pvp_forced_none = config.getString("placeholder.pvp_forced_none", "PvP is not forced");
|
||||||
|
|
||||||
this.cannotAttackForcedPvpOff = config.getString("messages.cannot_attack_pvp_force_off", "<red>PvP is forcibly disabled");
|
this.cannot_attack_forced_pvp_off = config.getString("messages.cannot_attack_pvp_force_off", "<red>PvP is forcibly disabled");
|
||||||
this.cannotAttackTeleportOrSpawnProtectionAttacker = config.getString("messages.cannot_attack_teleport_or_spawn_protection_attacker", "<red>You can't attack players while they have teleport or spawn protection");
|
this.cannot_attack_teleport_or_spawn_protection_attacker = config.getString("messages.cannot_attack_teleport_or_spawn_protection_attacker", "<red>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", "<red>You can't attack pets while you 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", "<red>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", "<red>You can't attack mounts 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", "<red>You can't attack mounts while you have teleport or spawn protection");
|
||||||
this.cannotAttackTeleportOrSpawnProtectionVictim = config.getString("messages.cannot_attack_teleport_or_spawn_protection_victim", "<red>You can't attack players 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", "<red>You can't attack players while you have teleport or spawn protection");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import java.util.UUID;
|
|||||||
/**
|
/**
|
||||||
* @param ableToDamage Result of the damage check
|
* @param ableToDamage Result of the damage check
|
||||||
* @param feedbackForAttacker Feedback to send to the attacker
|
* @param feedbackForAttacker Feedback to send to the attacker
|
||||||
* @param feedbackForVictim Feedback to send to the victim
|
|
||||||
* @param attackerId UUID of attacker player
|
* @param attackerId UUID of attacker player
|
||||||
* @param victimId UUID of victim player
|
* @param victimId UUID of victim player
|
||||||
*/
|
*/
|
||||||
@@ -15,7 +14,7 @@ public record DamageCheckResult(
|
|||||||
@Nullable UUID attackerId,
|
@Nullable UUID attackerId,
|
||||||
@Nullable UUID victimId,
|
@Nullable UUID victimId,
|
||||||
@Nullable String feedbackForAttacker,
|
@Nullable String feedbackForAttacker,
|
||||||
@Nullable String feedbackForVictim
|
boolean shouldVictimBePutInCombat
|
||||||
) {
|
) {
|
||||||
|
|
||||||
public static DamageCheckResult positive() {
|
public static DamageCheckResult positive() {
|
||||||
@@ -24,17 +23,17 @@ public record DamageCheckResult(
|
|||||||
null,
|
null,
|
||||||
null,
|
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(
|
return new DamageCheckResult(
|
||||||
true,
|
true,
|
||||||
attackerId,
|
attackerId,
|
||||||
victimId,
|
victimId,
|
||||||
null,
|
null,
|
||||||
null
|
shouldVictimBePutInCombat
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,22 +10,31 @@ 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.EntityDeathEvent;
|
import org.bukkit.event.entity.EntityDeathEvent;
|
||||||
import org.bukkit.event.player.PlayerJoinEvent;
|
import org.bukkit.event.player.*;
|
||||||
import org.bukkit.event.player.PlayerQuitEvent;
|
|
||||||
import org.bukkit.event.player.PlayerTeleportEvent;
|
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
public class PlayerListener implements Listener {
|
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)
|
@EventHandler(ignoreCancelled = true)
|
||||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||||
Player player = event.getPlayer();
|
PlayerData playerData = PreventStabby.getPlugin().getPlayerManager().getPlayer(event.getPlayer().getUniqueId());
|
||||||
UUID uuid = player.getUniqueId();
|
playerData.setLoginTimestamp(System.currentTimeMillis());
|
||||||
PreventStabby.getPlugin().getPlayerManager().addPlayer(uuid, new PlayerData(uuid, false));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ public class PlayerManager {
|
|||||||
if (ownerId == null) return;
|
if (ownerId == null) return;
|
||||||
getPlayerData(ownerId);
|
getPlayerData(ownerId);
|
||||||
}));
|
}));
|
||||||
}), 0, 20 * 15);
|
}), 5, 20 * 15);
|
||||||
|
|
||||||
Bukkit.getGlobalRegionScheduler().runAtFixedRate(plugin, (task) -> {
|
Bukkit.getGlobalRegionScheduler().runAtFixedRate(plugin, (task) -> {
|
||||||
for (PlayerData playerData : playerList.values()) {
|
for (PlayerData playerData : playerList.values()) {
|
||||||
@@ -108,6 +108,7 @@ public class PlayerManager {
|
|||||||
if (attacker == null || victim == null) return;
|
if (attacker == null || victim == null) return;
|
||||||
if (!damageCheckResult.ableToDamage()) return;
|
if (!damageCheckResult.ableToDamage()) return;
|
||||||
attacker.markInCombat();
|
attacker.markInCombat();
|
||||||
|
if (!damageCheckResult.shouldVictimBePutInCombat()) return;
|
||||||
victim.markInCombat();
|
victim.markInCombat();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,28 +138,63 @@ public class PlayerManager {
|
|||||||
return DamageCheckResult.positive();
|
return DamageCheckResult.positive();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (attackerPlayerData.getPlayerUuid().equals(victimPlayerData.getPlayerUuid())) {
|
||||||
|
return DamageCheckResult.positive(attackerId, victimId, false);
|
||||||
|
}
|
||||||
|
|
||||||
if (attackerPlayerData.isProtected()) {
|
if (attackerPlayerData.isProtected()) {
|
||||||
String message = switch (victimClassifier) {
|
String message = switch (victimClassifier) {
|
||||||
case PLAYER -> plugin.getConfigCache().cannotAttackTeleportOrSpawnProtectionAttacker;
|
case PLAYER -> plugin.getConfigCache().cannot_attack_teleport_or_spawn_protection_attacker;
|
||||||
case PET -> plugin.getConfigCache().cannotAttackPetsTeleportOrSpawnProtectionAttacker;
|
case PET -> plugin.getConfigCache().cannot_attack_pets_teleport_or_spawn_protection_attacker;
|
||||||
case MOUNT -> plugin.getConfigCache().cannotAttackMountsTeleportOrSpawnProtectionAttacker;
|
case MOUNT -> plugin.getConfigCache().cannot_attack_mounts_teleport_or_spawn_protection_attacker;
|
||||||
default -> null;
|
default -> null;
|
||||||
};
|
};
|
||||||
return new DamageCheckResult(false, attackerId, victimId, message, null);
|
return new DamageCheckResult(false, attackerId, victimId, message, false);
|
||||||
}
|
}
|
||||||
if (victimPlayerData.isProtected()) {
|
if (victimPlayerData.isProtected()) {
|
||||||
String message = null;
|
String message = null;
|
||||||
if (victimClassifier == Target.EntityClassifier.PLAYER) {
|
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()) {
|
switch (getForcedPvpState()) {
|
||||||
case DISABLED -> new DamageCheckResult(false, attackerId, victimId, plugin.getConfigCache().cannotAttackForcedPvpOff, null);
|
case DISABLED -> {
|
||||||
case ENABLED -> DamageCheckResult.positive(attackerId, victimId);
|
return new DamageCheckResult(false, attackerId, victimId, plugin.getConfigCache().cannot_attack_forced_pvp_off, false);
|
||||||
default -> DamageCheckResult.positive(attackerId, victimId);
|
}
|
||||||
};
|
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)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ import java.util.UUID;
|
|||||||
|
|
||||||
public class Target {
|
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.
|
* The unique identifier for a player.
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ public class PlayerDamageListener implements Listener {
|
|||||||
Entity victim = event.getEntity();
|
Entity victim = event.getEntity();
|
||||||
|
|
||||||
DamageCheckResult result = plugin.getPlayerManager().canDamage(attacker, victim);
|
DamageCheckResult result = plugin.getPlayerManager().canDamage(attacker, victim);
|
||||||
|
|
||||||
if (!result.ableToDamage()) {
|
if (!result.ableToDamage()) {
|
||||||
event.setCancelled(true);
|
event.setCancelled(true);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,24 +31,26 @@ public class DatabaseSQLite {
|
|||||||
String sql = "CREATE TABLE IF NOT EXISTS `players` (`player_uuid` varchar(36) UNIQUE PRIMARY KEY, `pvpenabled` boolean);";
|
String sql = "CREATE TABLE IF NOT EXISTS `players` (`player_uuid` varchar(36) UNIQUE PRIMARY KEY, `pvpenabled` boolean);";
|
||||||
statement.execute(sql);
|
statement.execute(sql);
|
||||||
} catch (SQLException exception) {
|
} catch (SQLException exception) {
|
||||||
exception.printStackTrace();
|
logger.warning(exception.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public PlayerData getPlayerInfo(UUID uuid) {
|
public PlayerData getPlayerInfo(UUID uuid) {
|
||||||
try (Connection conn = DriverManager.getConnection(url)) {
|
try (Connection conn = DriverManager.getConnection(url)) {
|
||||||
PreparedStatement statement = conn.prepareStatement(
|
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.setString(1, uuid.toString());
|
||||||
statement.setBoolean(2, PreventStabby.getPlugin().getConfigCache().pvp_enabled_by_default);
|
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();
|
statement.executeQuery();
|
||||||
ResultSet result = statement.getResultSet();
|
ResultSet result = statement.executeQuery();
|
||||||
boolean state = result.getBoolean("pvpenabled");
|
boolean state = result.getBoolean("pvpenabled");
|
||||||
return new PlayerData(uuid, state);
|
return new PlayerData(uuid, state);
|
||||||
} catch (SQLException exception) {
|
} catch (SQLException exception) {
|
||||||
exception.printStackTrace();
|
logger.warning(exception.getMessage());
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -61,10 +63,10 @@ public class DatabaseSQLite {
|
|||||||
insertnewuser.setString(2, uuid.toString());
|
insertnewuser.setString(2, uuid.toString());
|
||||||
} catch (SQLException exception) {
|
} catch (SQLException exception) {
|
||||||
logger.severe("Error while saving player data!");
|
logger.severe("Error while saving player data!");
|
||||||
exception.printStackTrace();
|
logger.warning(exception.getMessage());
|
||||||
}
|
}
|
||||||
} catch (SQLException exception) {
|
} catch (SQLException exception) {
|
||||||
exception.printStackTrace();
|
logger.warning(exception.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -71,9 +71,6 @@ public class PluginMessages {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void sendOutMessages(DamageCheckResult damageCheckResult) {
|
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) {
|
if (damageCheckResult.attackerId() != null && damageCheckResult.feedbackForAttacker() != null) {
|
||||||
sendActionBar(damageCheckResult.attackerId(), damageCheckResult.feedbackForAttacker());
|
sendActionBar(damageCheckResult.attackerId(), damageCheckResult.feedbackForAttacker());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user