Skip to content

Commit 9747518

Browse files
committed
Fix issue with SQLite town creation not setting the right IDs
1 parent 297043b commit 9747518

File tree

6 files changed

+26
-16
lines changed

6 files changed

+26
-16
lines changed

common/src/main/java/net/william278/husktowns/HuskTowns.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,14 @@ default Optional<Preferences> getUserPreferences(@NotNull UUID uuid) {
159159
@NotNull
160160
List<Town> getTowns();
161161

162-
default Optional<Member> getUserTown(@NotNull User user) {
162+
default Optional<Member> getUserTown(@NotNull User user) throws IllegalStateException {
163163
for (int i = 0; i < getTowns().size(); i++) {
164164
final Town town = getTowns().get(i);
165165
if (town.getMembers().containsKey(user.getUuid())) {
166166
final int weight = town.getMembers().get(user.getUuid());
167-
return getRoles().fromWeight(weight)
168-
.map(role -> new Member(user, town, role));
167+
return Optional.of(getRoles().fromWeight(weight)
168+
.map(role -> new Member(user, town, role))
169+
.orElseThrow(() -> new IllegalStateException("No role found for weight " + weight)));
169170
}
170171
}
171172
return Optional.empty();

common/src/main/java/net/william278/husktowns/claim/TownClaim.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,9 @@ public boolean isAdminClaim(@NotNull HuskTowns plugin) {
2323
public boolean contains(@NotNull Position position) {
2424
return claim.contains(position);
2525
}
26+
27+
@Override
28+
public String toString() {
29+
return "Town: " + town.getName() + ", Claim: " + claim;
30+
}
2631
}

common/src/main/java/net/william278/husktowns/database/SqLiteDatabase.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,15 +213,20 @@ public List<Town> getAllTowns() {
213213
}
214214

215215
@Override
216-
public @NotNull Town createTown(@NotNull String name, @NotNull User creator) {
216+
@NotNull
217+
public Town createTown(@NotNull String name, @NotNull User creator) {
217218
final Town town = Town.create(name, creator, plugin);
218219
town.addMember(creator.getUuid(), plugin.getRoles().getMayorRole());
219220
try (PreparedStatement statement = getConnection().prepareStatement(format("""
220221
INSERT INTO `%town_data%` (`name`, `data`)
221-
VALUES (?, ?)"""))) {
222+
VALUES (?, ?)"""), Statement.RETURN_GENERATED_KEYS)) {
222223
statement.setString(1, town.getName());
223224
statement.setBytes(2, plugin.getGson().toJson(town).getBytes(StandardCharsets.UTF_8));
224-
town.setId(statement.executeUpdate());
225+
statement.executeUpdate();
226+
final ResultSet resultSet = statement.getGeneratedKeys();
227+
if (resultSet.next()) {
228+
town.setId(resultSet.getInt(1));
229+
}
225230
} catch (SQLException | JsonSyntaxException e) {
226231
plugin.log(Level.SEVERE, "Failed to create town in table", e);
227232
}

common/src/main/java/net/william278/husktowns/manager/ClaimsManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void createClaimData(@NotNull OnlineUser user, @NotNull TownClaim claim,
8888
claimWorld.addClaim(claim);
8989
plugin.getManager().editTown(user, claim.town(), (town -> {
9090
town.setClaimCount(town.getClaimCount() + 1);
91-
town.getLog().log(Action.of(user, Action.Type.CREATE_CLAIM, claim.toString()));
91+
town.getLog().log(Action.of(user, Action.Type.CREATE_CLAIM, claim.claim().toString()));
9292
}));
9393
}
9494
plugin.getDatabase().updateClaimWorld(claimWorld);

common/src/main/java/net/william278/husktowns/manager/Manager.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,14 @@ protected void ifClaimOwner(@NotNull Member member, @NotNull OnlineUser user, @N
203203
* @param town The town to update
204204
*/
205205
public void updateTownData(@NotNull OnlineUser actor, @NotNull Town town) {
206+
// Update in the cache
207+
plugin.getTowns().remove(town);
208+
plugin.getTowns().add(town);
209+
210+
// Update in the database
206211
plugin.getDatabase().updateTown(town);
207-
if (plugin.getTowns().contains(town)) {
208-
plugin.getTowns().replaceAll(t -> t.getId() == town.getId() ? town : t);
209-
} else {
210-
plugin.getTowns().add(town);
211-
}
212+
213+
// Propagate to other servers
212214
plugin.getMessageBroker().ifPresent(broker -> Message.builder()
213215
.type(Message.Type.TOWN_UPDATE)
214216
.payload(Payload.integer(town.getId()))

common/src/main/java/net/william278/husktowns/network/Broker.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,7 @@ protected void handle(@Nullable OnlineUser receiver, @NotNull Message message) {
5959
case TOWN_UPDATE -> plugin.runAsync(() -> message.getPayload().getInteger()
6060
.flatMap(id -> plugin.getDatabase().getTown(id))
6161
.ifPresentOrElse(town -> {
62-
if (plugin.getTowns().contains(town)) {
63-
plugin.getTowns().replaceAll(t -> t.getId() == town.getId() ? town : t);
64-
return;
65-
}
62+
plugin.getTowns().remove(town);
6663
plugin.getTowns().add(town);
6764
}, () -> plugin.log(Level.WARNING, "Failed to update town: Town not found")));
6865
case TOWN_INVITE_REQUEST -> {

0 commit comments

Comments
 (0)