Skip to content

Commit

Permalink
Fix errors while getting dispenser owner #39
Browse files Browse the repository at this point in the history
  • Loading branch information
ceskyDJ committed Apr 20, 2023
1 parent 16ba250 commit d344716
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
34 changes: 30 additions & 4 deletions src/main/java/cz/ceskydj/netherwater/database/DB.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d344716

Please sign in to comment.