package com.rsscripting.rstaxi.stops; import com.rsscripting.rstaxi.RSTaxi; import org.bukkit.Location; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public final class StopManager { private final RSTaxi plugin; public StopManager(RSTaxi plugin) { this.plugin = plugin; } public record StopEntry(String name, String icon) { } public record StopLocation( String name, String world, double x, double y, double z, float yaw, float pitch ) { } public boolean exists(String name) throws SQLException { String sql = "SELECT 1 FROM stops WHERE name = ?"; try (PreparedStatement statement = connection() .prepareStatement(sql)) { statement.setString(1, name); try (ResultSet results = statement.executeQuery()) { return results.next(); } } } public StopLocation getStopLocation(String name) throws SQLException { String sql = """ SELECT name, world, x, y, z, yaw, pitch FROM stops WHERE name = ? """; try (PreparedStatement statement = connection() .prepareStatement(sql)) { statement.setString(1, name); try (ResultSet results = statement.executeQuery()) { if (!results.next()) { return null; } return new StopLocation( results.getString("name"), results.getString("world"), results.getDouble("x"), results.getDouble("y"), results.getDouble("z"), results.getFloat("yaw"), results.getFloat("pitch") ); } } } public boolean delete(String name) throws SQLException { String sql = "DELETE FROM stops WHERE name = ?"; try (PreparedStatement statement = connection() .prepareStatement(sql)) { statement.setString(1, name); return statement.executeUpdate() == 1; } } public boolean add( String name, String icon, Location location ) throws SQLException { String sql = """ INSERT OR IGNORE INTO stops (name, icon, world, x, y, z, yaw, pitch) VALUES (?, ?, ?, ?, ?, ?, ?, ?) """; try (PreparedStatement statement = connection() .prepareStatement(sql)) { statement.setString(1, name); statement.setString(2, icon); statement.setString( 3, location.getWorld().getName() ); statement.setDouble(4, location.getX()); statement.setDouble(5, location.getY()); statement.setDouble(6, location.getZ()); statement.setFloat(7, location.getYaw()); statement.setFloat(8, location.getPitch()); return statement.executeUpdate() == 1; } } public List getAllStops() throws SQLException { String sql = """ SELECT name, icon FROM stops ORDER BY name COLLATE NOCASE """; List stops = new ArrayList<>(); try (PreparedStatement statement = connection() .prepareStatement(sql); ResultSet results = statement.executeQuery()) { while (results.next()) { stops.add(new StopEntry( results.getString("name"), results.getString("icon") )); } } return stops; } public List searchByName(String name) throws SQLException { String sql = """ SELECT name, icon FROM stops WHERE name LIKE ? ESCAPE '\\' ORDER BY name COLLATE NOCASE """; List stops = new ArrayList<>(); String escapedName = name .replace("\\", "\\\\") .replace("%", "\\%") .replace("_", "\\_"); try (PreparedStatement statement = connection() .prepareStatement(sql)) { statement.setString(1, "%" + escapedName + "%"); try (ResultSet results = statement.executeQuery()) { while (results.next()) { stops.add(new StopEntry( results.getString("name"), results.getString("icon") )); } } } return stops; } public List searchByIcon(String icon) throws SQLException { String sql = """ SELECT name, icon FROM stops WHERE icon = ? ORDER BY name COLLATE NOCASE """; List stops = new ArrayList<>(); try (PreparedStatement statement = connection() .prepareStatement(sql)) { statement.setString(1, icon); try (ResultSet results = statement.executeQuery()) { while (results.next()) { stops.add(new StopEntry( results.getString("name"), results.getString("icon") )); } } } return stops; } public List getUsedIcons() throws SQLException { String sql = """ SELECT DISTINCT icon FROM stops ORDER BY icon COLLATE NOCASE """; List icons = new ArrayList<>(); try (PreparedStatement statement = connection() .prepareStatement(sql); ResultSet results = statement.executeQuery()) { while (results.next()) { icons.add(results.getString("icon")); } } return icons; } private Connection connection() { return plugin.getDatabaseManager().getConnection(); } }