Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.1.0</version>
<version>3.2.0</version>
<name>WHIMC Position Tracker</name>
<description>Track player positions to a database</description>

Expand Down
11 changes: 10 additions & 1 deletion src/main/java/edu/whimc/positiontracker/PositionTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,22 @@ public void onEnable() {
getConfig().options().copyDefaults(false);
this.debug = getConfig().getBoolean("debug", false);

getCommand("positiontracker").setExecutor(new TrackerCommand(this));
/* Defensive null check */
if (getCommand("positiontracker") != null) {
getCommand("positiontracker").setExecutor(new TrackerCommand(this));
} else {
getLogger().warning("Command 'positiontracker' is not defined in plugin.yml!");
}
Comment on lines +37 to +42
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly this isn't necessary but not worth removing it



// Load WorldGuard-specific things if we have WorldGuard
if (Bukkit.getPluginManager().isPluginEnabled("WorldGuard")) {
this.wgPlayerCache = new WgPlayerCache(this);
Bukkit.getPluginManager().registerEvents(new RegionListeners(this, this.wgPlayerCache), this);
Bukkit.getPluginManager().registerEvents(new RegionEnterLeaveListener(this), this);
this.getLogger().info("WorldGuard integration enabled.");
} else {
this.getLogger().info("WorldGuard not found. Skipping region tracking.");
}

this.dataStore = new DataStore(this, success -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
public class PositionEntry extends DataEntry {

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

/**
* The x, y, and z coordinates representing the position.
* The x, y, and z coordinates representing the position. Yaw and Pitch for player facing.
*/
private final int x, y, z;
private final float yaw, pitch;
/**
* The current world, biome, and player's username.
*/
Expand All @@ -43,7 +44,10 @@ public PositionEntry(Player player) {
this.x = loc.getBlockX();
this.y = loc.getBlockY();
this.z = loc.getBlockZ();
this.world = loc.getWorld().getName();
this.yaw = loc.getYaw();
this.pitch = loc.getPitch();
/* default to unknown if null pointer exception: */
this.world = (loc.getWorld() != null) ? loc.getWorld().getName() : "unknown";
String biome;
try {
biome = loc.getBlock().getBiome().name();
Expand All @@ -67,12 +71,14 @@ public void addInsertionToBatch(PreparedStatement statement) throws SQLException
statement.setInt(1, this.x);
statement.setInt(2, this.y);
statement.setInt(3, this.z);
statement.setString(4, this.world);
statement.setString(5, this.biome);
statement.setString(6, this.username);
statement.setString(7, this.gamemode);
statement.setString(8, this.uuid.toString());
statement.setLong(9, this.time.getTime() / 1000);
statement.setFloat(4, this.yaw);
statement.setFloat(5, this.pitch);
statement.setString(6, this.world);
statement.setString(7, this.biome);
statement.setString(8, this.username);
statement.setString(9, this.gamemode);
statement.setString(10, this.uuid.toString());
statement.setLong(11, this.time.getTime() / 1000);
statement.addBatch();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@
public class RegionEntry extends DataEntry {

public static final String INSERT_QUERY =
"INSERT INTO `whimc_player_region_events` (`region`, `trigger`, `isEnter`, `x`, `y`, `z`, `world`, `username`, `uuid`, `time`) " +
"VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
"INSERT INTO `whimc_player_region_events` (`region`, `region_members`, `trigger`, `isEnter`, `x`, `y`, `z`, `yaw`, `pitch`, `world`, `username`, `uuid`, `time`) " +
"VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

/**
* The region's name.
*/
private final String regionName;
/**
* The members of the region at the time of the event.
*/
private final String regionMembers;
/**
* The type of action that caused the event.
*/
Expand All @@ -28,9 +32,10 @@ public class RegionEntry extends DataEntry {
*/
private final boolean isEnter;
/**
* The x, y, and z coordinates representing the position.
* The x, y, and z coordinates representing the position as well as the angle they're looking.
*/
private final int x, y, z;
private final float yaw, pitch;
/**
* The current world and player's username.
*/
Expand All @@ -47,12 +52,16 @@ public class RegionEntry extends DataEntry {
public RegionEntry(RegionEvent event) {
Location loc = event.getLocation();
this.regionName = event.getRegion().getId();
this.regionMembers = String.join(",", event.getRegion().getMembers().getPlayers());
this.trigger = event.getTrigger();
this.isEnter = event instanceof RegionLeaveEvent;
this.x = loc.getBlockX();
this.y = loc.getBlockY();
this.z = loc.getBlockZ();
this.world = loc.getWorld().getName();
this.yaw = loc.getYaw();
this.pitch = loc.getPitch();
/* default to unknown if null pointer exception: */
this.world = (loc.getWorld() != null) ? loc.getWorld().getName() : "unknown";
this.username = event.getPlayer().getName();
this.uuid = event.getPlayer().getUniqueId();
this.time = new Timestamp(System.currentTimeMillis());
Expand All @@ -61,15 +70,18 @@ public RegionEntry(RegionEvent event) {
@Override
public void addToStatement(PreparedStatement statement) throws SQLException {
statement.setString(1, this.regionName);
statement.setString(2, this.trigger.toString());
statement.setBoolean(3, this.isEnter);
statement.setInt(4, this.x);
statement.setInt(5, this.y);
statement.setInt(6, this.z);
statement.setString(7, this.world);
statement.setString(8, this.username);
statement.setString(9, this.uuid.toString());
statement.setLong(10, this.time.getTime() / 1000);
statement.setString(2, this.regionMembers);
statement.setString(3, this.trigger.toString());
statement.setBoolean(4, this.isEnter);
statement.setInt(5, this.x);
statement.setInt(6, this.y);
statement.setInt(7, this.z);
statement.setFloat(8, this.yaw);
statement.setFloat(9, this.pitch);
statement.setString(10, this.world);
statement.setString(11, this.username);
statement.setString(12, this.uuid.toString());
statement.setLong(13, this.time.getTime() / 1000);
statement.addBatch();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Schema_2 extends SchemaVersion {
"ALTER TABLE whimc_player_positions ADD COLUMN gamemode VARCHAR(16);";

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

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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_3 extends SchemaVersion {

// Add to whimc_player_positions
private static final String ADD_POS_YAW =
"ALTER TABLE whimc_player_positions ADD COLUMN yaw FLOAT AFTER z;";
private static final String ADD_POS_PITCH =
"ALTER TABLE whimc_player_positions ADD COLUMN pitch FLOAT AFTER yaw;";

// Add to whimc_player_region_events
private static final String ADD_REGION_YAW =
"ALTER TABLE whimc_player_region_events ADD COLUMN yaw FLOAT AFTER z;";
private static final String ADD_REGION_PITCH =
"ALTER TABLE whimc_player_region_events ADD COLUMN pitch FLOAT AFTER yaw;";
private static final String ADD_REGION_MEMBERS =
"ALTER TABLE whimc_player_region_events ADD COLUMN region-members VARCHAR(64) AFTER region;";
Comment on lines +10 to +22
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe these the queries can be combined into 1 per table. But also probably not worth making that change - this is very clear as is.


public Schema_3() {
super(3, null); // No newer schema after this one (yet)
}

@Override
protected void migrateRoutine(Connection connection) throws SQLException {
// Update player_positions table
try (PreparedStatement addYaw = connection.prepareStatement(ADD_POS_YAW)) {
addYaw.execute();
}
try (PreparedStatement addPitch = connection.prepareStatement(ADD_POS_PITCH)) {
addPitch.execute();
}

// Update player_region_events table
try (PreparedStatement addRegionYaw = connection.prepareStatement(ADD_REGION_YAW)) {
addRegionYaw.execute();
}
try (PreparedStatement addRegionPitch = connection.prepareStatement(ADD_REGION_PITCH)) {
addRegionPitch.execute();
}
try (PreparedStatement addRegionPitch = connection.prepareStatement(ADD_REGION_MEMBERS)) {
addRegionPitch.execute();
}
}
}