Skip to content

Commit

Permalink
Merge pull request #18 from whimc/track_gamemode
Browse files Browse the repository at this point in the history
Track gamemode
  • Loading branch information
Geph authored Nov 5, 2024
2 parents 82b3095 + 4a067ff commit 40e668c
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 43 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>edu.whimc</groupId>
<artifactId>WHIMC-PositionTracker</artifactId>
<version>3.0.0</version>
<version>3.1.0</version>
<name>WHIMC Position Tracker</name>
<description>Track player positions to a database</description>

Expand Down
43 changes: 6 additions & 37 deletions src/main/java/edu/whimc/positiontracker/sql/MySQLConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mysql.cj.jdbc.MysqlConnectionPoolDataSource;
import com.mysql.cj.jdbc.MysqlDataSource;
import edu.whimc.positiontracker.PositionTracker;
import edu.whimc.positiontracker.sql.migration.SchemaManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
Expand All @@ -14,36 +15,9 @@
*/
public class MySQLConnection {

/** The SQL command to create a table. */
public static final String CREATE_POSITIONS_TABLE =
"CREATE TABLE IF NOT EXISTS `whimc_player_positions` (" +
" `rowid` BIGINT AUTO_INCREMENT NOT NULL," +
" `x` INT NOT NULL," +
" `y` INT NOT NULL," +
" `z` INT NOT NULL," +
" `world` VARCHAR(64) NOT NULL," +
" `biome` VARCHAR(64) NOT NULL," +
" `username` VARCHAR(16) NOT NULL," +
" `uuid` VARCHAR(36) NOT NULL," +
" `time` BIGINT NOT NULL," +
" PRIMARY KEY (`rowid`));";
private final MysqlDataSource dataSource;

public static final String CREATE_REGIONS_TABLE =
"CREATE TABLE IF NOT EXISTS `whimc_player_region_events` (" +
" `rowid` BIGINT AUTO_INCREMENT NOT NULL," +
" `region` VARCHAR(64) NOT NULL," +
" `trigger` VARCHAR(16) NOT NULL," +
" `isEnter` BIT(1) NOT NULL," +
" `x` INT NOT NULL," +
" `y` INT NOT NULL," +
" `z` INT NOT NULL," +
" `world` VARCHAR(64) NOT NULL," +
" `username` VARCHAR(16) NOT NULL," +
" `uuid` VARCHAR(36) NOT NULL," +
" `time` BIGINT NOT NULL," +
" PRIMARY KEY (`rowid`));";

private MysqlDataSource dataSource;
private final PositionTracker plugin;

/**
* Constructs a MySQLConnection.
Expand All @@ -52,6 +26,7 @@ public class MySQLConnection {
*/
public MySQLConnection(PositionTracker plugin) {
this.dataSource = new MysqlConnectionPoolDataSource();
this.plugin = plugin;

ConfigurationSection config = plugin.getConfig();
this.dataSource.setServerName(config.getString("mysql.host", "localhost"));
Expand All @@ -72,17 +47,11 @@ public boolean initialize() {
return false;
}

try (PreparedStatement statement = connection.prepareStatement(CREATE_POSITIONS_TABLE)) {
statement.execute();
}
try (PreparedStatement statement = connection.prepareStatement(CREATE_REGIONS_TABLE)) {
statement.execute();
}
SchemaManager manager = new SchemaManager(this.plugin, this);
return manager.initialize();
} catch (SQLException unused) {
return false;
}

return true;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
public class PositionEntry extends DataEntry {

public static final String INSERT_QUERY =
"INSERT INTO `whimc_player_positions` (`x`, `y`, `z`, `world`, `biome`, `username`, `uuid`, `time`) " +
"VALUES(?, ?, ?, ?, ?, ?, ?, ?)";
"INSERT INTO `whimc_player_positions` (`x`, `y`, `z`, `world`, `biome`, `username`, `gamemode`, `uuid`, `time`) " +
"VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)";

/**
* The x, y, and z coordinates representing the position.
Expand All @@ -23,7 +23,7 @@ public class PositionEntry extends DataEntry {
/**
* The current world, biome, and player's username.
*/
private final String world, biome, username;
private final String world, biome, username, gamemode;
/**
* The unique ID for this entry.
*/
Expand Down Expand Up @@ -52,6 +52,7 @@ public PositionEntry(Player player) {
}
this.biome = biome;
this.username = player.getName();
this.gamemode = player.getGameMode().name();
this.uuid = player.getUniqueId();
this.time = new Timestamp(System.currentTimeMillis());
}
Expand All @@ -69,8 +70,9 @@ public void addInsertionToBatch(PreparedStatement statement) throws SQLException
statement.setString(4, this.world);
statement.setString(5, this.biome);
statement.setString(6, this.username);
statement.setString(7, this.uuid.toString());
statement.setLong(8, this.time.getTime() / 1000);
statement.setString(7, this.gamemode);
statement.setString(8, this.uuid.toString());
statement.setLong(9, this.time.getTime() / 1000);
statement.addBatch();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package edu.whimc.positiontracker.sql.migration;

import edu.whimc.positiontracker.PositionTracker;
import edu.whimc.positiontracker.sql.MySQLConnection;
import edu.whimc.positiontracker.sql.migration.schemas.Schema_1;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.sql.Connection;
import java.sql.SQLException;

public class SchemaManager {

public static final String VERSION_FILE_NAME = ".schema_version";

private static final SchemaVersion BASE_SCHEMA = new Schema_1();

private final PositionTracker plugin;

private final MySQLConnection connection;

public SchemaManager(PositionTracker plugin, MySQLConnection connection) {
this.plugin = plugin;
this.connection = connection;
}

protected Connection getConnection() {
try {
return this.connection.getConnection();
} catch (SQLException e) {
return null;
}
}

protected File getVersionFile() {
return new File(this.plugin.getDataFolder(), VERSION_FILE_NAME);
}

private int getCurrentVersion() {
try {
return Integer.parseInt(new String(Files.readAllBytes(getVersionFile().toPath())));
} catch (NumberFormatException | IOException exc) {
return 0;
}
}

public boolean initialize() {
int curVersion = getCurrentVersion();

SchemaVersion schema = BASE_SCHEMA;
while (schema != null) {
if (schema.getVersion() > curVersion) {
this.plugin.getLogger().info("Migrating to schema " + schema.getVersion() + "...");
if (!schema.migrate(this)) {
return false;
}
}
schema = schema.getNextSchema();
}

return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package edu.whimc.positiontracker.sql.migration;

import com.google.common.io.Files;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

public abstract class SchemaVersion {

private final int version;
private final SchemaVersion nextSchema;

protected SchemaVersion(int version, SchemaVersion next) {
this.version = version;
this.nextSchema = next;
}

public int getVersion() {
return this.version;
}

public SchemaVersion getNextSchema() {
return this.nextSchema;
}

protected abstract void migrateRoutine(Connection connection) throws SQLException;

public final boolean migrate(SchemaManager manager) {
// Migrate the database
try {
migrateRoutine(manager.getConnection());
} catch (SQLException exc) {
exc.printStackTrace();
return false;
}

// Update the schema version
try {
Files.write(String.valueOf(this.version).getBytes(), manager.getVersionFile());
} catch (IOException exc) {
exc.printStackTrace();
return false;
}

return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package edu.whimc.positiontracker.sql.migration.schemas;

import edu.whimc.positiontracker.sql.migration.SchemaVersion;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Schema_1 extends SchemaVersion {

/** Table that will store player positions. */
private static final String CREATE_POSITIONS_TABLE =
"CREATE TABLE IF NOT EXISTS `whimc_player_positions` (" +
" `rowid` BIGINT AUTO_INCREMENT NOT NULL," +
" `x` INT NOT NULL," +
" `y` INT NOT NULL," +
" `z` INT NOT NULL," +
" `world` VARCHAR(64) NOT NULL," +
" `biome` VARCHAR(64) NOT NULL," +
" `username` VARCHAR(16) NOT NULL," +
" `uuid` VARCHAR(36) NOT NULL," +
" `time` BIGINT NOT NULL," +
" PRIMARY KEY (`rowid`));";

/** Table that will store region change events. */
private static final String CREATE_REGIONS_TABLE =
"CREATE TABLE IF NOT EXISTS `whimc_player_region_events` (" +
" `rowid` BIGINT AUTO_INCREMENT NOT NULL," +
" `region` VARCHAR(64) NOT NULL," +
" `trigger` VARCHAR(16) NOT NULL," +
" `isEnter` BIT(1) NOT NULL," +
" `x` INT NOT NULL," +
" `y` INT NOT NULL," +
" `z` INT NOT NULL," +
" `world` VARCHAR(64) NOT NULL," +
" `username` VARCHAR(16) NOT NULL," +
" `uuid` VARCHAR(36) NOT NULL," +
" `time` BIGINT NOT NULL," +
" PRIMARY KEY (`rowid`));";


public Schema_1() {
super(1, new Schema_2());
}

@Override
protected void migrateRoutine(Connection connection) throws SQLException {
try (PreparedStatement createPosTable = connection.prepareStatement(CREATE_POSITIONS_TABLE)) {
createPosTable.execute();
}
try (PreparedStatement createRegionTable = connection.prepareStatement(CREATE_REGIONS_TABLE)) {
createRegionTable.execute();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package edu.whimc.positiontracker.sql.migration.schemas;

import edu.whimc.positiontracker.sql.migration.SchemaVersion;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Schema_2 extends SchemaVersion {

private static final String ADD_GAMEMODE =
"ALTER TABLE whimc_player_positions ADD COLUMN gamemode VARCHAR(16);";

public Schema_2() {
super(2, null);
}

@Override
protected void migrateRoutine(Connection connection) throws SQLException {
try (PreparedStatement addGamemode = connection.prepareStatement(ADD_GAMEMODE)) {
addGamemode.execute();
}
}
}

0 comments on commit 40e668c

Please sign in to comment.