Skip to content

Commit f7bb502

Browse files
authored
Merge pull request #19 from whimc/QRF-data
Qrf data update - adding more entries to database
2 parents 40e668c + 0988dbd commit f7bb502

File tree

6 files changed

+102
-26
lines changed

6 files changed

+102
-26
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>edu.whimc</groupId>
66
<artifactId>WHIMC-PositionTracker</artifactId>
7-
<version>3.1.0</version>
7+
<version>3.2.0</version>
88
<name>WHIMC Position Tracker</name>
99
<description>Track player positions to a database</description>
1010

src/main/java/edu/whimc/positiontracker/PositionTracker.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,22 @@ public void onEnable() {
3434
getConfig().options().copyDefaults(false);
3535
this.debug = getConfig().getBoolean("debug", false);
3636

37-
getCommand("positiontracker").setExecutor(new TrackerCommand(this));
37+
/* Defensive null check */
38+
if (getCommand("positiontracker") != null) {
39+
getCommand("positiontracker").setExecutor(new TrackerCommand(this));
40+
} else {
41+
getLogger().warning("Command 'positiontracker' is not defined in plugin.yml!");
42+
}
43+
3844

3945
// Load WorldGuard-specific things if we have WorldGuard
4046
if (Bukkit.getPluginManager().isPluginEnabled("WorldGuard")) {
4147
this.wgPlayerCache = new WgPlayerCache(this);
4248
Bukkit.getPluginManager().registerEvents(new RegionListeners(this, this.wgPlayerCache), this);
4349
Bukkit.getPluginManager().registerEvents(new RegionEnterLeaveListener(this), this);
50+
this.getLogger().info("WorldGuard integration enabled.");
51+
} else {
52+
this.getLogger().info("WorldGuard not found. Skipping region tracking.");
4453
}
4554

4655
this.dataStore = new DataStore(this, success -> {

src/main/java/edu/whimc/positiontracker/sql/entries/PositionEntry.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
public class PositionEntry extends DataEntry {
1414

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

1919
/**
20-
* The x, y, and z coordinates representing the position.
20+
* The x, y, and z coordinates representing the position. Yaw and Pitch for player facing.
2121
*/
2222
private final int x, y, z;
23+
private final float yaw, pitch;
2324
/**
2425
* The current world, biome, and player's username.
2526
*/
@@ -43,7 +44,10 @@ public PositionEntry(Player player) {
4344
this.x = loc.getBlockX();
4445
this.y = loc.getBlockY();
4546
this.z = loc.getBlockZ();
46-
this.world = loc.getWorld().getName();
47+
this.yaw = loc.getYaw();
48+
this.pitch = loc.getPitch();
49+
/* default to unknown if null pointer exception: */
50+
this.world = (loc.getWorld() != null) ? loc.getWorld().getName() : "unknown";
4751
String biome;
4852
try {
4953
biome = loc.getBlock().getBiome().name();
@@ -67,12 +71,14 @@ public void addInsertionToBatch(PreparedStatement statement) throws SQLException
6771
statement.setInt(1, this.x);
6872
statement.setInt(2, this.y);
6973
statement.setInt(3, this.z);
70-
statement.setString(4, this.world);
71-
statement.setString(5, this.biome);
72-
statement.setString(6, this.username);
73-
statement.setString(7, this.gamemode);
74-
statement.setString(8, this.uuid.toString());
75-
statement.setLong(9, this.time.getTime() / 1000);
74+
statement.setFloat(4, this.yaw);
75+
statement.setFloat(5, this.pitch);
76+
statement.setString(6, this.world);
77+
statement.setString(7, this.biome);
78+
statement.setString(8, this.username);
79+
statement.setString(9, this.gamemode);
80+
statement.setString(10, this.uuid.toString());
81+
statement.setLong(11, this.time.getTime() / 1000);
7682
statement.addBatch();
7783
}
7884

src/main/java/edu/whimc/positiontracker/sql/entries/RegionEntry.java

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@
1212
public class RegionEntry extends DataEntry {
1313

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

1818
/**
1919
* The region's name.
2020
*/
2121
private final String regionName;
22+
/**
23+
* The members of the region at the time of the event.
24+
*/
25+
private final String regionMembers;
2226
/**
2327
* The type of action that caused the event.
2428
*/
@@ -28,9 +32,10 @@ public class RegionEntry extends DataEntry {
2832
*/
2933
private final boolean isEnter;
3034
/**
31-
* The x, y, and z coordinates representing the position.
35+
* The x, y, and z coordinates representing the position as well as the angle they're looking.
3236
*/
3337
private final int x, y, z;
38+
private final float yaw, pitch;
3439
/**
3540
* The current world and player's username.
3641
*/
@@ -47,12 +52,16 @@ public class RegionEntry extends DataEntry {
4752
public RegionEntry(RegionEvent event) {
4853
Location loc = event.getLocation();
4954
this.regionName = event.getRegion().getId();
55+
this.regionMembers = String.join(",", event.getRegion().getMembers().getPlayers());
5056
this.trigger = event.getTrigger();
5157
this.isEnter = event instanceof RegionLeaveEvent;
5258
this.x = loc.getBlockX();
5359
this.y = loc.getBlockY();
5460
this.z = loc.getBlockZ();
55-
this.world = loc.getWorld().getName();
61+
this.yaw = loc.getYaw();
62+
this.pitch = loc.getPitch();
63+
/* default to unknown if null pointer exception: */
64+
this.world = (loc.getWorld() != null) ? loc.getWorld().getName() : "unknown";
5665
this.username = event.getPlayer().getName();
5766
this.uuid = event.getPlayer().getUniqueId();
5867
this.time = new Timestamp(System.currentTimeMillis());
@@ -61,15 +70,18 @@ public RegionEntry(RegionEvent event) {
6170
@Override
6271
public void addToStatement(PreparedStatement statement) throws SQLException {
6372
statement.setString(1, this.regionName);
64-
statement.setString(2, this.trigger.toString());
65-
statement.setBoolean(3, this.isEnter);
66-
statement.setInt(4, this.x);
67-
statement.setInt(5, this.y);
68-
statement.setInt(6, this.z);
69-
statement.setString(7, this.world);
70-
statement.setString(8, this.username);
71-
statement.setString(9, this.uuid.toString());
72-
statement.setLong(10, this.time.getTime() / 1000);
73+
statement.setString(2, this.regionMembers);
74+
statement.setString(3, this.trigger.toString());
75+
statement.setBoolean(4, this.isEnter);
76+
statement.setInt(5, this.x);
77+
statement.setInt(6, this.y);
78+
statement.setInt(7, this.z);
79+
statement.setFloat(8, this.yaw);
80+
statement.setFloat(9, this.pitch);
81+
statement.setString(10, this.world);
82+
statement.setString(11, this.username);
83+
statement.setString(12, this.uuid.toString());
84+
statement.setLong(13, this.time.getTime() / 1000);
7385
statement.addBatch();
7486
}
7587
}

src/main/java/edu/whimc/positiontracker/sql/migration/schemas/Schema_2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class Schema_2 extends SchemaVersion {
1111
"ALTER TABLE whimc_player_positions ADD COLUMN gamemode VARCHAR(16);";
1212

1313
public Schema_2() {
14-
super(2, null);
14+
super(2, new Schema_3());
1515
}
1616

1717
@Override
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package edu.whimc.positiontracker.sql.migration.schemas;
2+
3+
import edu.whimc.positiontracker.sql.migration.SchemaVersion;
4+
import java.sql.Connection;
5+
import java.sql.PreparedStatement;
6+
import java.sql.SQLException;
7+
8+
public class Schema_3 extends SchemaVersion {
9+
10+
// Add to whimc_player_positions
11+
private static final String ADD_POS_YAW =
12+
"ALTER TABLE whimc_player_positions ADD COLUMN yaw FLOAT AFTER z;";
13+
private static final String ADD_POS_PITCH =
14+
"ALTER TABLE whimc_player_positions ADD COLUMN pitch FLOAT AFTER yaw;";
15+
16+
// Add to whimc_player_region_events
17+
private static final String ADD_REGION_YAW =
18+
"ALTER TABLE whimc_player_region_events ADD COLUMN yaw FLOAT AFTER z;";
19+
private static final String ADD_REGION_PITCH =
20+
"ALTER TABLE whimc_player_region_events ADD COLUMN pitch FLOAT AFTER yaw;";
21+
private static final String ADD_REGION_MEMBERS =
22+
"ALTER TABLE whimc_player_region_events ADD COLUMN region-members VARCHAR(64) AFTER region;";
23+
24+
public Schema_3() {
25+
super(3, null); // No newer schema after this one (yet)
26+
}
27+
28+
@Override
29+
protected void migrateRoutine(Connection connection) throws SQLException {
30+
// Update player_positions table
31+
try (PreparedStatement addYaw = connection.prepareStatement(ADD_POS_YAW)) {
32+
addYaw.execute();
33+
}
34+
try (PreparedStatement addPitch = connection.prepareStatement(ADD_POS_PITCH)) {
35+
addPitch.execute();
36+
}
37+
38+
// Update player_region_events table
39+
try (PreparedStatement addRegionYaw = connection.prepareStatement(ADD_REGION_YAW)) {
40+
addRegionYaw.execute();
41+
}
42+
try (PreparedStatement addRegionPitch = connection.prepareStatement(ADD_REGION_PITCH)) {
43+
addRegionPitch.execute();
44+
}
45+
try (PreparedStatement addRegionPitch = connection.prepareStatement(ADD_REGION_MEMBERS)) {
46+
addRegionPitch.execute();
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)