Skip to content

Commit

Permalink
Get rid of WhitelistImportEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
voidpointer0x00 committed Apr 15, 2023
1 parent 1d05755 commit c4d4f89
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@
import voidpointer.spigot.voidwhitelist.command.arg.Args;
import voidpointer.spigot.voidwhitelist.command.arg.ImportOptions;
import voidpointer.spigot.voidwhitelist.event.EventManager;
import voidpointer.spigot.voidwhitelist.event.WhitelistImportEvent;
import voidpointer.spigot.voidwhitelist.storage.StorageFactory;
import voidpointer.spigot.voidwhitelist.storage.WhitelistService;
import voidpointer.spigot.voidwhitelist.storage.db.OrmliteWhitelistService;
import voidpointer.spigot.voidwhitelist.storage.json.JsonWhitelistService;

import java.util.List;
import java.util.Set;
import java.util.UUID;

import static java.lang.System.currentTimeMillis;
import static java.util.Collections.emptyList;
Expand Down Expand Up @@ -72,18 +69,17 @@ private void importJson(final Args args) {
.set("storage", WHITELIST_FILE_NAME)
.send(args.getSender());
OrmliteWhitelistService database = (OrmliteWhitelistService) whitelistService;
Set<UUID> imported;
final int importedInTotal;
if (args.hasOption(REPLACE))
imported = database.addAllReplacing(json.getWhitelist()).join();
importedInTotal = database.addAllReplacing(json.getWhitelist()).join();
else
imported = database.addAllIfNotExists(json.getWhitelist()).join();
importedInTotal = database.addAllIfNotExists(json.getWhitelist()).join();
final long end = currentTimeMillis();
locale.localize(IMPORT_RESULT)
.set("imported", imported.size())
.set("imported", importedInTotal)
.set("loaded", json.getWhitelist().size())
.set("ms-spent", end - start)
.send(args.getSender());
eventManager.callAsyncEvent(new WhitelistImportEvent(imported));
}

@Override public List<String> tabComplete(final Args args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.j256.ormlite.dao.CloseableWrappedIterable;
import com.j256.ormlite.misc.TransactionManager;
import org.apache.commons.lang.mutable.MutableInt;
import org.bukkit.plugin.Plugin;
import org.checkerframework.checker.nullness.qual.NonNull;
import voidpointer.spigot.framework.localemodule.LocaleLog;
Expand Down Expand Up @@ -218,44 +219,44 @@ private Optional<Whitelistable> onFindException(final Throwable thrown) {
return Optional.empty();
}

public CompletableFuture<Set<UUID>> addAllIfNotExists(final Collection<Whitelistable> all) {
public CompletableFuture<Integer> addAllIfNotExists(final Collection<Whitelistable> all) {
if (all.isEmpty())
return completedFuture(emptySet());
return supplyAsync(() -> addAll((model, addedSet) -> {
return completedFuture(0);
return supplyAsync(() -> addAll((model, addedInTotal) -> {
if (!ormliteDatabase.getWhitelistDao().idExists(model.getUniqueId())) {
ormliteDatabase.getWhitelistDao().create(model);
addedSet.add(model.getUniqueId());
addedInTotal.increment();
}
}, all));
}

public CompletableFuture<Set<UUID>> addAllReplacing(final Collection<Whitelistable> all) {
public CompletableFuture<Integer> addAllReplacing(final Collection<Whitelistable> all) {
if (all.isEmpty())
return completedFuture(emptySet());
return supplyAsync(() -> addAll((model, addedSet) -> {
return completedFuture(0);
return supplyAsync(() -> addAll((model, addedInTotal) -> {
ormliteDatabase.getWhitelistDao().createOrUpdate(model);
addedSet.add(model.getUniqueId());
addedInTotal.increment();
}, all));
}

private Set<UUID> addAll(
final CheckedBiConsumer<WhitelistableModel, Set<UUID>> addFunction,
private int addAll(
final CheckedBiConsumer<WhitelistableModel, MutableInt> addFunction,
final Collection<Whitelistable> all) {
final Set<UUID> added = new HashSet<>();
final MutableInt addedInTotal = new MutableInt();
try {
requireConnection();
return ormliteDatabase.getWhitelistDao().callBatchTasks(() -> {
for (final Whitelistable whitelistable : all) {
if (whitelistable instanceof WhitelistableModel)
addFunction.consume((WhitelistableModel) whitelistable, added);
addFunction.consume((WhitelistableModel) whitelistable, addedInTotal);
else
addFunction.consume(WhitelistableModel.copyOf(whitelistable), added);
addFunction.consume(WhitelistableModel.copyOf(whitelistable), addedInTotal);
}
return unmodifiableSet(added);
return addedInTotal.intValue();
});
} catch (final Exception exception) {
log.warn("Unable to add all whitelistable entities: {0}", exception.getMessage());
return unmodifiableSet(added);
return addedInTotal.intValue();
}
}

Expand Down

0 comments on commit c4d4f89

Please sign in to comment.