Skip to content

Commit d3d73cb

Browse files
authored
Record in-game founding time of nations (#26)
2 parents 08c2d2b + 5560113 commit d3d73cb

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

src/main/java/pro/cloudnode/smp/smpcore/Nation.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ public final class Nation {
5858
*/
5959
public final @NotNull Date founded;
6060

61+
/**
62+
* In-game absolute ticks when this nation was founded (based on {@link org.bukkit.World#getFullTime()})
63+
*/
64+
public final long foundedTicks;
65+
6166
/**
6267
* National bank account of the nation
6368
*/
@@ -71,16 +76,18 @@ public final class Nation {
7176
* @param leaderUUID See {@link #leaderUUID}
7277
* @param viceLeaderUUID See {@link #viceLeaderUUID}
7378
* @param founded See {@link #founded}
79+
* @param foundedTicks See {@link #foundedTicks}
7480
* @param bank See {@link #bank}
7581
*/
76-
public Nation(final @NotNull String id, final @NotNull String name, final @NotNull String shortName, final @NotNull String colour, final @NotNull UUID leaderUUID, final @NotNull UUID viceLeaderUUID, final @NotNull Date founded, final @NotNull String bank) {
82+
public Nation(final @NotNull String id, final @NotNull String name, final @NotNull String shortName, final @NotNull String colour, final @NotNull UUID leaderUUID, final @NotNull UUID viceLeaderUUID, final @NotNull Date founded, final long foundedTicks, final @NotNull String bank) {
7783
this.id = id;
7884
this.name = name;
7985
this.shortName = shortName;
8086
this.color = colour;
8187
this.leaderUUID = leaderUUID;
8288
this.viceLeaderUUID = viceLeaderUUID;
8389
this.founded = founded;
90+
this.foundedTicks = foundedTicks;
8491
this.bank = bank;
8592
}
8693

@@ -126,14 +133,15 @@ public Nation(final @NotNull ResultSet rs) throws @NotNull SQLException {
126133
UUID.fromString(rs.getString("leader")),
127134
UUID.fromString(rs.getString("vice")),
128135
rs.getTimestamp("founded"),
136+
rs.getLong("founded_ticks"),
129137
rs.getString("bank")
130138
);
131139
}
132140

133141
public void save() {
134142
try (
135143
final @NotNull Connection conn = SMPCore.getInstance().db()
136-
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("INSERT OR REPLACE INTO `nations` (`id`, `name`, `short_name`, `color`, `leader`, `vice`, `founded`, `bank`) VALUES (?, ?, ?, ?, ?, ?, ?, ?)")
144+
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("INSERT OR REPLACE INTO `nations` (`id`, `name`, `short_name`, `color`, `leader`, `vice`, `founded`, `founded_ticks`, `bank`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")
137145
) {
138146
stmt.setString(1, id);
139147
stmt.setString(2, name);
@@ -142,7 +150,8 @@ public void save() {
142150
stmt.setString(5, leaderUUID.toString());
143151
stmt.setString(6, viceLeaderUUID.toString());
144152
stmt.setTimestamp(7, new java.sql.Timestamp(founded.getTime()));
145-
stmt.setString(8, bank);
153+
stmt.setLong(8, foundedTicks);
154+
stmt.setString(9, bank);
146155
stmt.executeUpdate();
147156
}
148157
catch (final @NotNull SQLException e) {

src/main/java/pro/cloudnode/smp/smpcore/Rest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ private void e404 (final @NotNull io.javalin.http.Context ctx) {
5555
obj.addProperty("viceLeader", nation.viceLeaderUUID.toString());
5656
obj.addProperty("members", nation.members().size());
5757
obj.addProperty("founded", nation.founded.getTime());
58+
obj.addProperty("foundedGameTicks", nation.foundedTicks);
59+
obj.addProperty("foundedGameDate", SMPCore.gameTime(nation.foundedTicks).getTime());
5860
obj.addProperty("bank", nation.bank);
5961
return obj;
6062
}

src/main/java/pro/cloudnode/smp/smpcore/SMPCore.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ public static boolean ifDisallowedCharacters(final @NotNull String source, final
204204
}
205205

206206
public static @NotNull Date gameTime() {
207-
return new Date(overworld().getFullTime() * 3600 + 21600000);
207+
return gameTime(overworld().getFullTime());
208+
}
209+
210+
public static @NotNull Date gameTime(final long ticks) {
211+
return new Date(ticks * 3600 + 21600000);
208212
}
209213
}

src/main/resources/init.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ CREATE TABLE IF NOT EXISTS `nations` (
2020
`color` CHAR(6) NOT NULL COLLATE NOCASE,
2121
`leader` CHAR(36) NOT NULL COLLATE NOCASE,
2222
`vice` CHAR(36) NOT NULL COLLATE NOCASE,
23-
`founded` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
23+
`founded` DATETIME NOT NULL,
24+
`founded_ticks` INTEGER NOT NULL,
2425
`bank` CHAR(16) NOT NULL COLLATE BINARY
2526
);

0 commit comments

Comments
 (0)