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;