From d34471612f066735332b8014f847d0ea3fc92061 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C5=A0mahel?= <48548230+ceskyDJ@users.noreply.github.com> Date: Thu, 20 Apr 2023 19:46:33 +0200 Subject: [PATCH] Fix errors while getting dispenser owner #39 --- .../cz/ceskydj/netherwater/database/DB.java | 34 ++++++++++++++++--- .../listeners/WaterDispenseListener.java | 6 +++- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/main/java/cz/ceskydj/netherwater/database/DB.java b/src/main/java/cz/ceskydj/netherwater/database/DB.java index 3a16794..c95db83 100644 --- a/src/main/java/cz/ceskydj/netherwater/database/DB.java +++ b/src/main/java/cz/ceskydj/netherwater/database/DB.java @@ -306,12 +306,38 @@ public OfflinePlayer getDispenserOwner(Block dispenser) { query.setString(4, dispenser.getWorld().getName()); ResultSet resultSet = query.executeQuery(); - // Get first result item - resultSet.next(); + // Get first result item (if there is any) + if (!resultSet.next()) { + this.messageManager.dump("Found unregistered dispenser block (not found in DB) on [" + dispenser.getX() + ", " + dispenser.getY() + ", " + dispenser.getZ() + "]"); - String playerUUID = resultSet.getString("player"); + return null; + } + + try { + String stringyPlayerUUID = resultSet.getString("player"); + if (stringyPlayerUUID == null) { + this.messageManager.dump("Found corrupted dispenser record in DB (column player has value NULL) for dispenser on [" + dispenser.getX() + ", " + dispenser.getY() + ", " + dispenser.getZ() + "]. Removing..."); + this.deleteDispenser(dispenser); + + return null; + } - return Bukkit.getOfflinePlayer(UUID.fromString(playerUUID)); + UUID playerUUID = UUID.fromString(stringyPlayerUUID); + OfflinePlayer player = Bukkit.getOfflinePlayer(playerUUID); + if (player.getName() == null) { + this.messageManager.dump("Found registered dispenser with unknown owner (player couldn't been found using stored UUID) on [" + dispenser.getX() + ", " + dispenser.getY() + ", " + dispenser.getZ() + "]. Removing..."); + this.deleteDispenser(dispenser); + + return null; + } + + return player; + } catch (IllegalArgumentException e) { + this.messageManager.dump("Found registered dispenser with invalid player UUID on [" + dispenser.getX() + ", " + dispenser.getY() + ", " + dispenser.getZ() + "]. Removing..."); + this.deleteDispenser(dispenser); + + return null; + } } catch (SQLException e) { this.messageManager.dump("DB error: " + e.getMessage()); diff --git a/src/main/java/cz/ceskydj/netherwater/listeners/WaterDispenseListener.java b/src/main/java/cz/ceskydj/netherwater/listeners/WaterDispenseListener.java index af4caaf..6b09c01 100644 --- a/src/main/java/cz/ceskydj/netherwater/listeners/WaterDispenseListener.java +++ b/src/main/java/cz/ceskydj/netherwater/listeners/WaterDispenseListener.java @@ -61,7 +61,11 @@ public void onBlockDispense(BlockDispenseEvent event) { this.messageManager.dump("- Block's metadata: " + block.getBlockData().getAsString(true)); this.messageManager.dump("- Item: " + item.getType().name()); this.messageManager.dump("- Item's metadata: " + item.getItemMeta()); - this.messageManager.dump("- Responsible player: " + player + (fromDb ? "(from DB)" : "")); + if (player != null) { + this.messageManager.dump("- Responsible player: " + player.getName() + (fromDb ? " (from DB)" : "")); + } else { + this.messageManager.dump("- Responsible player: unknown (no closed player and dispensed isn't registered)"); + } if (block.getType() != Material.DISPENSER) { return;