Search in sources :

Example 1 with DataManager

use of org.black_ixx.playerpoints.manager.DataManager in project PlayerPoints by Rosewood-Development.

the class _1_Create_Tables method migrate.

@Override
public void migrate(DatabaseConnector connector, Connection connection, String tablePrefix) throws SQLException {
    String autoIncrement = connector instanceof MySQLConnector ? " AUTO_INCREMENT" : "";
    String query;
    if (connector instanceof SQLiteConnector) {
        query = "SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?";
    } else {
        query = "SHOW TABLES LIKE ?";
    }
    // Check if the old table already exists, if it does then try renaming the table to playerpoints_points and the 'playername' column to 'uuid'
    boolean exists;
    try (PreparedStatement statement = connection.prepareStatement(query)) {
        statement.setString(1, "playerpoints");
        exists = statement.executeQuery().next();
    }
    if (exists) {
        try (Statement statement = connection.createStatement()) {
            statement.executeUpdate("ALTER TABLE playerpoints RENAME TO " + tablePrefix + "points");
        } catch (Exception ignored) {
        }
        try (Statement statement = connection.createStatement()) {
            statement.executeUpdate("ALTER TABLE " + tablePrefix + "points RENAME COLUMN playername TO uuid");
        } catch (Exception ignored) {
        }
    } else {
        // Create points table
        try (Statement statement = connection.createStatement()) {
            statement.executeUpdate("CREATE TABLE " + tablePrefix + "points (" + "id INTEGER PRIMARY KEY" + autoIncrement + ", " + "uuid VARCHAR(36) NOT NULL, " + "points INTEGER NOT NULL, " + "UNIQUE (uuid)" + ")");
        }
    }
    // Attempt to import legacy data if it exists and we are using SQLite
    // First make sure there isn't already any data in the database for some reason
    PlayerPoints plugin = PlayerPoints.getInstance();
    DataManager dataManager = plugin.getManager(DataManager.class);
    File file = new File(plugin.getDataFolder(), "storage.yml");
    if (dataManager.getTopSortedPoints(1).isEmpty() && file.exists() && connector instanceof SQLiteConnector) {
        try {
            FileConfiguration configuration = YamlConfiguration.loadConfiguration(file);
            ConfigurationSection section = configuration.getConfigurationSection("Points");
            if (section == null)
                section = configuration.getConfigurationSection("Players");
            if (section == null) {
                plugin.getLogger().warning("Malformed storage.yml file.");
                return;
            }
            SortedSet<SortedPlayer> data = new TreeSet<>();
            for (String uuid : section.getKeys(false)) data.add(new SortedPlayer(UUID.fromString(uuid), section.getInt(uuid)));
            plugin.getManager(DataManager.class).importData(data);
            plugin.getLogger().warning("Imported legacy data from storage.yml");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) DataManager(org.black_ixx.playerpoints.manager.DataManager) SQLException(java.sql.SQLException) FileConfiguration(org.bukkit.configuration.file.FileConfiguration) TreeSet(java.util.TreeSet) SQLiteConnector(dev.rosewood.rosegarden.database.SQLiteConnector) MySQLConnector(dev.rosewood.rosegarden.database.MySQLConnector) SortedPlayer(org.black_ixx.playerpoints.models.SortedPlayer) PlayerPoints(org.black_ixx.playerpoints.PlayerPoints) File(java.io.File) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 2 with DataManager

use of org.black_ixx.playerpoints.manager.DataManager in project PlayerPoints by Rosewood-Development.

the class PlayerPointsAPI method set.

/**
 * Sets a player's points to a specified amount
 *
 * @param playerId The player to set the points of
 * @param amount The amount of points to set to
 * @return true if the transaction was successful, false otherwise
 */
public boolean set(@NotNull UUID playerId, int amount) {
    Objects.requireNonNull(playerId);
    DataManager dataManager = this.plugin.getManager(DataManager.class);
    int points = dataManager.getEffectivePoints(playerId);
    PlayerPointsChangeEvent event = new PlayerPointsChangeEvent(playerId, amount - points);
    Bukkit.getPluginManager().callEvent(event);
    if (event.isCancelled())
        return false;
    return dataManager.setPoints(playerId, amount);
}
Also used : DataManager(org.black_ixx.playerpoints.manager.DataManager) PlayerPointsChangeEvent(org.black_ixx.playerpoints.event.PlayerPointsChangeEvent)

Example 3 with DataManager

use of org.black_ixx.playerpoints.manager.DataManager in project PlayerPoints by Rosewood-Development.

the class GamePointsConverter method convert.

@Override
public void convert() {
    SortedSet<SortedPlayer> users = GamePoints.getInstance().getData().getUsers().stream().filter(x -> x.getBalance() > 0).map(x -> new SortedPlayer(x.getUUID(), x.getBalance())).collect(Collectors.toCollection(TreeSet::new));
    DataManager dataManager = this.rosePlugin.getManager(DataManager.class);
    dataManager.importData(users);
}
Also used : SortedPlayer(org.black_ixx.playerpoints.models.SortedPlayer) SortedSet(java.util.SortedSet) GamePoints(su.nightexpress.gamepoints.GamePoints) CurrencyConverter(org.black_ixx.playerpoints.conversion.CurrencyConverter) DataManager(org.black_ixx.playerpoints.manager.DataManager) Collectors(java.util.stream.Collectors) RosePlugin(dev.rosewood.rosegarden.RosePlugin) TreeSet(java.util.TreeSet) DataManager(org.black_ixx.playerpoints.manager.DataManager) SortedPlayer(org.black_ixx.playerpoints.models.SortedPlayer)

Aggregations

DataManager (org.black_ixx.playerpoints.manager.DataManager)3 TreeSet (java.util.TreeSet)2 SortedPlayer (org.black_ixx.playerpoints.models.SortedPlayer)2 RosePlugin (dev.rosewood.rosegarden.RosePlugin)1 MySQLConnector (dev.rosewood.rosegarden.database.MySQLConnector)1 SQLiteConnector (dev.rosewood.rosegarden.database.SQLiteConnector)1 File (java.io.File)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 SortedSet (java.util.SortedSet)1 Collectors (java.util.stream.Collectors)1 PlayerPoints (org.black_ixx.playerpoints.PlayerPoints)1 CurrencyConverter (org.black_ixx.playerpoints.conversion.CurrencyConverter)1 PlayerPointsChangeEvent (org.black_ixx.playerpoints.event.PlayerPointsChangeEvent)1 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)1 FileConfiguration (org.bukkit.configuration.file.FileConfiguration)1 GamePoints (su.nightexpress.gamepoints.GamePoints)1