Skip to content

Commit

Permalink
Add fix command. Fix #16.
Browse files Browse the repository at this point in the history
  • Loading branch information
IzzelAliz committed Aug 6, 2019
1 parent 411779e commit 69fe70f
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/main/java/io/izzel/ambershop/cmd/AmberCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public class AmberCommands {
public AmberCommands(AmberLocale locale, AmberConfManager cm,
CreateShopExecutor cse, RemoveShopExecutor rse,
SetUnlimitedExecutor sue, SetOwnerExecutor soe,
SetPriceExecutor spe, QueryExecutor qe) {
SetPriceExecutor spe, QueryExecutor qe,
FixExecutor fe) {
val executor = CommandSpec.builder();
if (cm.get().shopSettings.enable) {
val create = CommandSpec.builder()
Expand Down Expand Up @@ -52,6 +53,11 @@ public AmberCommands(AmberLocale locale, AmberConfManager cm,
.executor(spe)
.build();
executor.child(price, "setprice", "price", "p");
val fix = CommandSpec.builder()
.permission("ambershop.admin.fix")
.executor(fe)
.build();
executor.child(fix, "fix");
{
val queryRemove = CommandSpec.builder()
.permission("ambershop.user.query.remove")
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/io/izzel/ambershop/cmd/FixExecutor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.izzel.ambershop.cmd;

import com.google.inject.Inject;
import io.izzel.amber.commons.i18n.AmberLocale;
import io.izzel.ambershop.data.ShopDataSource;
import io.izzel.ambershop.util.AmberTasks;
import lombok.val;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.spec.CommandExecutor;

public class FixExecutor implements CommandExecutor {

@Inject private AmberLocale locale;
@Inject private AmberTasks tasks;
@Inject private ShopDataSource ds;

@Override
public CommandResult execute(CommandSource src, CommandContext args) {
tasks.async().submit(() -> {
val result = ds.fixAll().get();
locale.to(src, result.getPath(), result.getArgs());
return null;
});
return CommandResult.success();
}

}
2 changes: 2 additions & 0 deletions src/main/java/io/izzel/ambershop/data/ShopDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@ default Future<List<ShopRecord>> getByPlayer(Player player) {

Future<Void> unloadChunk(UUID world, int x, int z);

Future<OperationResult> fixAll();

}
31 changes: 31 additions & 0 deletions src/main/java/io/izzel/ambershop/data/ShopDataSourceImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.izzel.ambershop.data;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.Futures;
import com.google.inject.Inject;
import com.google.inject.Singleton;
Expand All @@ -21,6 +22,7 @@
import java.sql.Statement;
import java.util.*;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

@Singleton
class ShopDataSourceImpl implements ShopDataSource {
Expand Down Expand Up @@ -229,4 +231,33 @@ public Future<Void> unloadChunk(UUID world, int x, int z) {
});
}

@Override
public Future<OperationResult> fixAll() {
return tasks.async().submit(() -> {
@Cleanup val conn = storage.connection();
@Cleanup val stmt = conn.prepareStatement("select * from ambershop_shops;");
@Cleanup val rs = stmt.executeQuery();
val corrupt = Lists.<Integer>newArrayList();
while (rs.next()) {
try {
val id = rs.getInt("id");
try {
ShopRecord.readResultSet(rs);
} catch (Exception e) {
corrupt.add(id);
}
} catch (Exception ignored) {
}
}
if (!corrupt.isEmpty()) {
@Cleanup val statement = conn.createStatement();
val joiner = new StringJoiner(",", "delete from ambershop_shops where id in (", ")");
corrupt.stream().map(String::valueOf).forEach(joiner::add);
statement.executeUpdate(joiner.toString());
}
return OperationResult.of("commands.fix", corrupt.size(),
corrupt.stream().map(Objects::toString).collect(Collectors.joining(" ")));
});
}

}
1 change: 1 addition & 0 deletions src/main/resources/assets/ambershop/locale/en_us.conf
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ commands {
remove = "&eRemoved {0} shops."
set = "&eUpdated {0} shops."
}
fix = "&aDeleted {0} corrpted shop records, ID: {1}"
}
updater = [
"AmberShop has an update."
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/ambershop/locale/zh_cn.conf
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ commands {
remove = "&e移除了 {0} 个商店"
set = "&e更新了 {0} 个商店"
}
fix = "&a删除了 {0} 个损坏的商店记录,ID: {1}"
}
updater = [
"AmberShop 有更新了"
Expand Down

0 comments on commit 69fe70f

Please sign in to comment.