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();
}
}
}
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);
}
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);
}
Aggregations