Search in sources :

Example 1 with WrappedException

use of net.anweisen.utilities.common.collection.WrappedException in project Utility by anweisen.

the class MessageReplyMessageAction method addActionRows.

@Nonnull
@Override
@SuppressWarnings("unchecked")
public ReplyMessageAction addActionRows(@Nonnull ActionRow... rows) {
    try {
        Class<?> actionClass = action.getClass();
        Field componentsField = actionClass.getDeclaredField("components");
        componentsField.setAccessible(true);
        final Collection<ActionRow> components;
        Object componentsObject = componentsField.get(action);
        if (componentsObject instanceof Collection)
            components = (Collection<ActionRow>) componentsObject;
        else
            components = new ArrayList<>(rows.length);
        Collections.addAll(components, rows);
        action.setActionRows(components);
        return this;
    } catch (Throwable ex) {
        throw new WrappedException("Reflections for injecting components failed, JDA v" + JDAInfo.VERSION + " | ActionClass " + action.getClass().getName(), ex);
    }
}
Also used : Field(java.lang.reflect.Field) WrappedException(net.anweisen.utilities.common.collection.WrappedException) ActionRow(net.dv8tion.jda.api.interactions.components.ActionRow) ArrayList(java.util.ArrayList) Collection(java.util.Collection) Nonnull(javax.annotation.Nonnull)

Example 2 with WrappedException

use of net.anweisen.utilities.common.collection.WrappedException in project Utility by anweisen.

the class BukkitModule method onLoad.

@Override
public final void onLoad() {
    isLoaded = true;
    if (!requirementsMet || !(requirementsMet = new RequirementsChecker(this).checkBoolean(getPluginDocument().getDocument("require"))))
        return;
    if (setFirstInstance || firstInstance == null) {
        setFirstInstance(this);
    }
    ILogger.setConstantFactory(this.getLogger());
    trySaveDefaultConfig();
    if (wasShutdown)
        isReloaded = true;
    if (firstInstall = !getDataFolder().exists()) {
        getLogger().info("Detected first install!");
    }
    if (devMode = getConfigDocument().getBoolean("dev-mode") || getConfigDocument().getBoolean("dev-mode.enabled")) {
        getLogger().setLevel(Level.ALL);
        getLogger().debug("Devmode is enabled: Showing debug messages. This can be disabled in the plugin.yml ('dev-mode')");
    } else {
        getLogger().setLevel(Level.INFO);
    }
    injectInstance();
    try {
        handleLoad();
    } catch (Exception ex) {
        throw new WrappedException(ex);
    }
}
Also used : WrappedException(net.anweisen.utilities.common.collection.WrappedException) WrappedException(net.anweisen.utilities.common.collection.WrappedException)

Example 3 with WrappedException

use of net.anweisen.utilities.common.collection.WrappedException in project Utility by anweisen.

the class BukkitModule method onEnable.

@Override
public final void onEnable() {
    if (!requirementsMet)
        return;
    commands.forEach((name, executor) -> registerCommand0(executor, name));
    listeners.forEach(this::registerListener);
    try {
        handleEnable();
    } catch (Exception ex) {
        throw new WrappedException(ex);
    }
}
Also used : WrappedException(net.anweisen.utilities.common.collection.WrappedException) WrappedException(net.anweisen.utilities.common.collection.WrappedException)

Example 4 with WrappedException

use of net.anweisen.utilities.common.collection.WrappedException in project Utility by anweisen.

the class StringUtils method format.

@Nonnull
public static String format(@Nonnull String sequence, @Nonnull Object... args) {
    char start = '{', end = '}';
    boolean inArgument = false;
    StringBuilder argument = new StringBuilder();
    StringBuilder builder = new StringBuilder();
    for (char c : sequence.toCharArray()) {
        if (c == end && inArgument) {
            inArgument = false;
            try {
                int arg = Integer.parseInt(argument.toString());
                Object current = args[arg];
                Object replacement = current instanceof Supplier ? ((Supplier<?>) current).get() : current instanceof Callable ? ((Callable<?>) current).call() : current;
                builder.append(replacement);
            } catch (NumberFormatException | IndexOutOfBoundsException ex) {
                logger.warn("Invalid argument index '{}'", argument);
                builder.append(start).append(argument).append(end);
            } catch (Exception ex) {
                throw new WrappedException(ex);
            }
            argument = new StringBuilder();
            continue;
        }
        if (c == start && !inArgument) {
            inArgument = true;
            continue;
        }
        if (inArgument) {
            argument.append(c);
            continue;
        }
        builder.append(c);
    }
    if (argument.length() > 0)
        builder.append(start).append(argument);
    return builder.toString();
}
Also used : WrappedException(net.anweisen.utilities.common.collection.WrappedException) Supplier(java.util.function.Supplier) Callable(java.util.concurrent.Callable) WrappedException(net.anweisen.utilities.common.collection.WrappedException) Nonnull(javax.annotation.Nonnull)

Example 5 with WrappedException

use of net.anweisen.utilities.common.collection.WrappedException in project Utility by anweisen.

the class Document method readJsonArrayFile.

@Nonnull
@CheckReturnValue
static List<Document> readJsonArrayFile(@Nonnull Path file) {
    try {
        JsonArray array = GsonDocument.GSON.fromJson(FileUtils.newBufferedReader(file), JsonArray.class);
        if (array == null)
            return new ArrayList<>();
        List<Document> documents = new ArrayList<>(array.size());
        array.forEach(element -> documents.add(new GsonDocument(element.getAsJsonObject())));
        return documents;
    } catch (IOException ex) {
        throw new WrappedException(ex);
    }
}
Also used : JsonArray(com.google.gson.JsonArray) WrappedException(net.anweisen.utilities.common.collection.WrappedException) ArrayList(java.util.ArrayList) GsonDocument(net.anweisen.utilities.common.config.document.GsonDocument) PropertiesDocument(net.anweisen.utilities.common.config.document.PropertiesDocument) EmptyDocument(net.anweisen.utilities.common.config.document.EmptyDocument) GsonDocument(net.anweisen.utilities.common.config.document.GsonDocument) CheckReturnValue(javax.annotation.CheckReturnValue) Nonnull(javax.annotation.Nonnull)

Aggregations

WrappedException (net.anweisen.utilities.common.collection.WrappedException)12 Nonnull (javax.annotation.Nonnull)8 GameProfile (com.mojang.authlib.GameProfile)2 Field (java.lang.reflect.Field)2 ArrayList (java.util.ArrayList)2 JsonArray (com.google.gson.JsonArray)1 Method (java.lang.reflect.Method)1 Collection (java.util.Collection)1 Callable (java.util.concurrent.Callable)1 Supplier (java.util.function.Supplier)1 CheckReturnValue (javax.annotation.CheckReturnValue)1 StringBuilderPrintWriter (net.anweisen.utilities.common.collection.StringBuilderPrintWriter)1 Tuple (net.anweisen.utilities.common.collection.pair.Tuple)1 EmptyDocument (net.anweisen.utilities.common.config.document.EmptyDocument)1 GsonDocument (net.anweisen.utilities.common.config.document.GsonDocument)1 PropertiesDocument (net.anweisen.utilities.common.config.document.PropertiesDocument)1 IllegalArgumentParserValueException (net.anweisen.utilities.jda.manager.arguments.IllegalArgumentParserValueException)1 ParserOptions (net.anweisen.utilities.jda.manager.arguments.ParserOptions)1 RequiredArgument (net.anweisen.utilities.jda.manager.hooks.registered.RequiredArgument)1 CommandData (net.dv8tion.jda.api.interactions.commands.build.CommandData)1