aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/cowtipper/cowlection/command/exception/ThrowingConsumer.java
blob: 9eae1c3fe281fb283b95564a9b642fa4dae2407b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package de.cowtipper.cowlection.command.exception;

import de.cowtipper.cowlection.Cowlection;
import de.cowtipper.cowlection.util.MooChatComponent;
import net.minecraft.command.CommandException;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.IChatComponent;

import java.util.function.Consumer;

@FunctionalInterface
public interface ThrowingConsumer<T> extends Consumer<T> {
    @Override
    default void accept(T t) {
        try {
            acceptThrows(t);
        } catch (CommandException e) {
            ChatComponentTranslation errorMsg = new ChatComponentTranslation(e.getMessage(), e.getErrorObjects());
            errorMsg.getChatStyle().setColor(EnumChatFormatting.RED);
            handleException(e, errorMsg);
        } catch (Exception e) {
            String stackTraceInfo = null;
            for (StackTraceElement traceElement : e.getStackTrace()) {
                if (traceElement.getClassName().startsWith("de.cowtipper")) {
                    stackTraceInfo = traceElement.getClassName()
                            + EnumChatFormatting.WHITE + "#" + EnumChatFormatting.GRAY + traceElement.getMethodName()
                            + EnumChatFormatting.WHITE + ":" + EnumChatFormatting.GRAY + traceElement.getLineNumber();
                    break;
                }
            }
            handleException(e, new MooChatComponent(EnumChatFormatting.DARK_RED + "Something went wrong: " + EnumChatFormatting.RED + e.toString()
                    + (stackTraceInfo == null ? "" : EnumChatFormatting.GRAY + " (" + EnumChatFormatting.WHITE + "in " + EnumChatFormatting.GRAY + stackTraceInfo + EnumChatFormatting.GRAY + ")")));
        }
    }

    default void handleException(Exception exception, IChatComponent errorMsg) {
        Cowlection.getInstance().getChatHelper().sendMessage(errorMsg);
        exception.printStackTrace();
    }

    void acceptThrows(T t) throws CommandException;
}