fix(db): resolve unique constraint error by overwriting existing shops at location

This commit is contained in:
2026-02-04 21:26:27 -05:00
parent 3e1698614e
commit 087bf7eb40
3 changed files with 20 additions and 2 deletions

View File

@@ -178,6 +178,21 @@ public class ShopRepository {
} }
} }
/**
* delete shop by location (world, x, y, z)
*/
public void deleteShopByLocation(Location loc) throws SQLException {
String sql = "delete from shops where world_uuid = ? and sign_x = ? and sign_y = ? and sign_z = ?";
try (PreparedStatement stmt = dbManager.getConnection().prepareStatement(sql)) {
stmt.setString(1, loc.getWorld().getUID().toString());
stmt.setInt(2, loc.getBlockX());
stmt.setInt(3, loc.getBlockY());
stmt.setInt(4, loc.getBlockZ());
stmt.executeUpdate();
}
}
/** /**
* get all shops owned by player * get all shops owned by player
*/ */

View File

@@ -30,7 +30,7 @@ public class SetupDialog {
.inputs(List.of( .inputs(List.of(
// product (selling) // product (selling)
DialogInput.text("product_item", DialogInput.text("product_item",
Component.text("what are you selling? (e.g. diamond)", Component.text("what are you selling? (e.g. oak log)",
NamedTextColor.YELLOW)) NamedTextColor.YELLOW))
.build(), .build(),
DialogInput.text("product_qty", DialogInput.text("product_qty",
@@ -41,7 +41,7 @@ public class SetupDialog {
// price (buying) // price (buying)
DialogInput.text("price_item", DialogInput.text("price_item",
Component.text("what do you want? (e.g. gold ingot)", Component.text("what do you want? (e.g. diamond)",
NamedTextColor.GREEN)) NamedTextColor.GREEN))
.build(), .build(),
DialogInput.text("price_qty", DialogInput.text("price_qty",

View File

@@ -198,6 +198,9 @@ public class ShopActivationListener implements Listener {
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> { plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
try { try {
// cleanup potential existing shop to avoid unique constraint error
plugin.getShopRepository().deleteShopByLocation(signLocation);
int shopId = plugin.getShopRepository().createShop(shop); int shopId = plugin.getShopRepository().createShop(shop);
plugin.getServer().getScheduler().runTask(plugin, () -> { plugin.getServer().getScheduler().runTask(plugin, () -> {
// re-verify sign on main thread // re-verify sign on main thread