<commit_before>package com.lagopusempire.multihomes.homeIO.database;

import com.lagopusempire.multihomes.MultiHomes;
import com.lagopusempire.multihomes.home.Home;
import com.lagopusempire.multihomes.home.LoadResult;
import com.lagopusempire.multihomes.homeIO.HomeIO;
import com.lagopusempire.multihomes.homeIO.HomeListLoadedCallback;
import com.lagopusempire.multihomes.homeIO.HomeLoadedCallback;
import com.lagopusempire.multihomes.homeIO.HomesLoadedCallback;
import com.lagopusempire.multihomes.util.Util;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;

import static com.lagopusempire.multihomes.homeIO.database.ScriptKeys.*;

/**
 *
 * @author MrZoraman
 */
public class DBHomeIO implements HomeIO
{
    private final MultiHomes plugin;
    private Connection conn;
    
    public DBHomeIO(MultiHomes plugin, Connection conn)
    {
        this.plugin = plugin;
        this.conn = conn;
    }

    @Override
    public void saveHome(final Home home, final Runnable callback)
    {
        final UUID uuid = home.getOwner();
        
        plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> 
        {
            verifyConnection();
            
            if(getHome(uuid, home.getName()).getLoadResult() == LoadResult.DOES_NOT_EXIST)
            {
                //home does not exist
                final String query = Scripts.getScript(CREATE_HOME);
                try(final PreparedStatement stmt = conn.prepareStatement(query))
                {
                    stmt.setString(1, home.getOwner().toString());
                    stmt.setString(2, home.getName());
                    stmt.setDouble(3, home.getLoc().getX());
                    stmt.setDouble(4, home.getLoc().getY());
                    stmt.setDouble(5, home.getLoc().getZ());
                    stmt.setFloat (6, home.getLoc().getYaw());
                    stmt.setFloat (7, home.getLoc().getPitch());
                    stmt.setString(8, home.getLoc().getWorld().getName());
                    
                    stmt.execute();
                    
                    plugin.getServer().getScheduler().runTask(plugin, () -> callback.run());
                }
                catch (SQLException e)
                {
                    e.printStackTrace();
                }
            }
            else
            {
                //home exists, update it
                final String query = Scripts.getScript(UPDATE_HOME);
                try(final PreparedStatement stmt = conn.prepareStatement(query))
                {
                    stmt.setDouble(1, home.getLoc().getX());
                    stmt.setDouble(2, home.getLoc().getY());
                    stmt.setDouble(3, home.getLoc().getZ());
                    stmt.setFloat (4, home.getLoc().getYaw());
                    stmt.setFloat (5, home.getLoc().getPitch());
                    stmt.setString(6, home.getLoc().getWorld().getName());
                    
                    stmt.setString(7, uuid.toString());
                    stmt.setString(8, home.getName());
                    
                    stmt.execute();
                    
                    plugin.getServer().getScheduler().runTask(plugin, () -> callback.run());
                }
                catch (SQLException e)
                {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void loadHomes(UUID uuid, HomesLoadedCallback callback)
    {
        plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> 
        {
            verifyConnection();
            
            final Map<String, Home> homes = new HashMap<>();
            
            final String query = Scripts.getScript(LOAD_HOMES);
            try(final PreparedStatement stmt = conn.prepareStatement(query))
            {
                stmt.setString(1, uuid.toString());

                try(final ResultSet rs = stmt.executeQuery())
                {
                    while(rs.next())
                    {
                        final String homeName = rs.getString("home_name");
                        final double x = rs.getDouble("x");
                        final double y = rs.getDouble("y");
                        final double z = rs.getDouble("z");
                        final float yaw = rs.getFloat("yaw");
                        final float pitch = rs.getFloat("pitch");
                        final String worldName = rs.getString("world_name");
                        
                        final World world = Bukkit.getWorld(worldName);
                        final Home home;
                        if(world == null)
                        {
                            home = new Home(uuid, homeName, LoadResult.NO_WORLD);
                        }
                        else
                        {
                            final Location loc = new Location(world, x, y, z, yaw, pitch);
                            
                            home = new Home(uuid, homeName, loc);
                        }
                        
                        homes.put(homeName, home);
                    }
                }
            }
            catch (SQLException ex)
            {
                ex.printStackTrace();
            }
            
            plugin.getServer().getScheduler().runTask(plugin, () -> callback.homesLoaded(homes));
        });
    }

    @Override
    public void loadHome(UUID uuid, String homeName, HomeLoadedCallback callback)
    {
        plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> 
        {
            verifyConnection();
            
            final Home home = getHome(uuid, homeName);
            
            plugin.getServer().getScheduler().runTask(plugin, () -> callback.homeLoaded(home));
        });
    }

    @Override
    public void getHomeList(UUID uuid, HomeListLoadedCallback callback)
    {
        plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> 
        {
            verifyConnection();
            
            final List<String> homes = new ArrayList<>();
            
            final String query = Scripts.getScript(LIST_HOMES);
            try(final PreparedStatement stmt = conn.prepareStatement(query))
            {
                stmt.setString(1, uuid.toString());

                try(final ResultSet rs = stmt.executeQuery())
                {
                    while(rs.next())
                    {
                        homes.add(rs.getString("home_name"));
                    }
                }
            }
            catch (SQLException ex)
            {
                ex.printStackTrace();
            }
            
            plugin.getServer().getScheduler().runTask(plugin, () -> callback.homeListLoaded(homes));
        });
    }
    
    private Home getHome(UUID uuid, String homeName)
    {
        final String loadHomeQuery = Scripts.getScript(LOAD_HOME);
        try(final PreparedStatement stmt = conn.prepareStatement(loadHomeQuery))
        {
            stmt.setString(1, uuid.toString());
            stmt.setString(2, homeName);

            try(final ResultSet rs = stmt.executeQuery())
            {
                if(rs.next())
                {
                    final double x = rs.getDouble("x");
                    final double y = rs.getDouble("y");
                    final double z = rs.getDouble("z");
                    final float yaw = rs.getFloat("yaw");
                    final float pitch = rs.getFloat("pitch");
                    final String worldName = rs.getString("world_name");

                    final World world = Bukkit.getWorld(worldName);
                    if(world == null)
                    {
                        return new Home(uuid, homeName, LoadResult.NO_WORLD);
                    }
                    else<commit_msg>Home list sorted in dbhomeio<commit_after>package com.lagopusempire.multihomes.homeIO.database;

import com.lagopusempire.multihomes.MultiHomes;
import com.lagopusempire.multihomes.home.Home;
import com.lagopusempire.multihomes.home.LoadResult;
import com.lagopusempire.multihomes.homeIO.HomeIO;
import com.lagopusempire.multihomes.homeIO.HomeListLoadedCallback;
import com.lagopusempire.multihomes.homeIO.HomeLoadedCallback;
import com.lagopusempire.multihomes.homeIO.HomesLoadedCallback;
import com.lagopusempire.multihomes.util.Util;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;

import static com.lagopusempire.multihomes.homeIO.database.ScriptKeys.*;
import java.util.Collections;

/**
 *
 * @author MrZoraman
 */
public class DBHomeIO implements HomeIO
{
    private final MultiHomes plugin;
    private Connection conn;
    
    public DBHomeIO(MultiHomes plugin, Connection conn)
    {
        this.plugin = plugin;
        this.conn = conn;
    }

    @Override
    public void saveHome(final Home home, final Runnable callback)
    {
        final UUID uuid = home.getOwner();
        
        plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> 
        {
            verifyConnection();
            
            if(getHome(uuid, home.getName()).getLoadResult() == LoadResult.DOES_NOT_EXIST)
            {
                //home does not exist
                final String query = Scripts.getScript(CREATE_HOME);
                try(final PreparedStatement stmt = conn.prepareStatement(query))
                {
                    stmt.setString(1, home.getOwner().toString());
                    stmt.setString(2, home.getName());
                    stmt.setDouble(3, home.getLoc().getX());
                    stmt.setDouble(4, home.getLoc().getY());
                    stmt.setDouble(5, home.getLoc().getZ());
                    stmt.setFloat (6, home.getLoc().getYaw());
                    stmt.setFloat (7, home.getLoc().getPitch());
                    stmt.setString(8, home.getLoc().getWorld().getName());
                    
                    stmt.execute();
                    
                    plugin.getServer().getScheduler().runTask(plugin, () -> callback.run());
                }
                catch (SQLException e)
                {
                    e.printStackTrace();
                }
            }
            else
            {
                //home exists, update it
                final String query = Scripts.getScript(UPDATE_HOME);
                try(final PreparedStatement stmt = conn.prepareStatement(query))
                {
                    stmt.setDouble(1, home.getLoc().getX());
                    stmt.setDouble(2, home.getLoc().getY());
                    stmt.setDouble(3, home.getLoc().getZ());
                    stmt.setFloat (4, home.getLoc().getYaw());
                    stmt.setFloat (5, home.getLoc().getPitch());
                    stmt.setString(6, home.getLoc().getWorld().getName());
                    
                    stmt.setString(7, uuid.toString());
                    stmt.setString(8, home.getName());
                    
                    stmt.execute();
                    
                    plugin.getServer().getScheduler().runTask(plugin, () -> callback.run());
                }
                catch (SQLException e)
                {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void loadHomes(UUID uuid, HomesLoadedCallback callback)
    {
        plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> 
        {
            verifyConnection();
            
            final Map<String, Home> homes = new HashMap<>();
            
            final String query = Scripts.getScript(LOAD_HOMES);
            try(final PreparedStatement stmt = conn.prepareStatement(query))
            {
                stmt.setString(1, uuid.toString());

                try(final ResultSet rs = stmt.executeQuery())
                {
                    while(rs.next())
                    {
                        final String homeName = rs.getString("home_name");
                        final double x = rs.getDouble("x");
                        final double y = rs.getDouble("y");
                        final double z = rs.getDouble("z");
                        final float yaw = rs.getFloat("yaw");
                        final float pitch = rs.getFloat("pitch");
                        final String worldName = rs.getString("world_name");
                        
                        final World world = Bukkit.getWorld(worldName);
                        final Home home;
                        if(world == null)
                        {
                            home = new Home(uuid, homeName, LoadResult.NO_WORLD);
                        }
                        else
                        {
                            final Location loc = new Location(world, x, y, z, yaw, pitch);
                            
                            home = new Home(uuid, homeName, loc);
                        }
                        
                        homes.put(homeName, home);
                    }
                }
            }
            catch (SQLException ex)
            {
                ex.printStackTrace();
            }
            
            plugin.getServer().getScheduler().runTask(plugin, () -> callback.homesLoaded(homes));
        });
    }

    @Override
    public void loadHome(UUID uuid, String homeName, HomeLoadedCallback callback)
    {
        plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> 
        {
            verifyConnection();
            
            final Home home = getHome(uuid, homeName);
            
            plugin.getServer().getScheduler().runTask(plugin, () -> callback.homeLoaded(home));
        });
    }

    @Override
    public void getHomeList(UUID uuid, HomeListLoadedCallback callback)
    {
        plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> 
        {
            verifyConnection();
            
            final List<String> homes = new ArrayList<>();
            
            final String query = Scripts.getScript(LIST_HOMES);
            try(final PreparedStatement stmt = conn.prepareStatement(query))
            {
                stmt.setString(1, uuid.toString());

                try(final ResultSet rs = stmt.executeQuery())
                {
                    while(rs.next())
                    {
                        homes.add(rs.getString("home_name"));
                    }
                }
            }
            catch (SQLException ex)
            {
                ex.printStackTrace();
            }
            
            Collections.sort(homes);
            
            plugin.getServer().getScheduler().runTask(plugin, () -> callback.homeListLoaded(homes));
        });
    }
    
    private Home getHome(UUID uuid, String homeName)
    {
        final String loadHomeQuery = Scripts.getScript(LOAD_HOME);
        try(final PreparedStatement stmt = conn.prepareStatement(loadHomeQuery))
        {
            stmt.setString(1, uuid.toString());
            stmt.setString(2, homeName);

            try(final ResultSet rs = stmt.executeQuery())
            {
                if(rs.next())
                {
                    final double x = rs.getDouble("x");
                    final double y = rs.getDouble("y");
                    final double z = rs.getDouble("z");
                    final float yaw = rs.getFloat("yaw");
                    final float pitch = rs.getFloat("pitch");
                    final String worldName = rs.getString("world_name");

                    final World world = Bukkit.getWorld(worldName);
                    if(world == null)
                    {
                        return new Home(uuid, homeName, LoadResult.NO_WORLD);
                    }
                    else
{'fraction_non_alphanumeric': 0.10580246165039714, 'fraction_numerical': 0.002425271327229734, 'mean_word_length': 1.393209518282066, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '680780', 'n_tokens_mistral': 4280, 'n_tokens_neox': 4027, 'n_words': 746}