summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/util/PrintUtil.java
blob: de63037a126c6cb0c09e921756ac06ad7e0491e6 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.keuin.kbackupfabric.util;

import com.mojang.brigadier.context.CommandContext;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public final class PrintUtil {

    private static final Logger LOGGER = LogManager.getLogger();

    private static final boolean printDebugMessages = true;
    private static final boolean printErrorMessages = true;
    private static final boolean printInfoMessages = true;

    private static final Object syncDebug = new Object();
    private static final Object syncError = new Object();
    private static final Object syncInfo = new Object();
    private static final Object syncMessage = new Object();

    private static final Style infoStyle = new Style().setColor(Formatting.WHITE);
    private static final Style debugStyle = new Style().setUnderline(true);
    private static final Style warnStyle = new Style().setColor(Formatting.YELLOW);
    private static final Style errorStyle = new Style().setColor(Formatting.DARK_RED);


    public static CommandContext<ServerCommandSource> msgInfo(CommandContext<ServerCommandSource> context, String messageText) {
        return msgInfo(context, messageText, false);
    }

    public static CommandContext<ServerCommandSource> msgWarn(CommandContext<ServerCommandSource> context, String messageText) {
        return msgWarn(context, messageText, false);
    }

    public static CommandContext<ServerCommandSource> msgErr(CommandContext<ServerCommandSource> context, String messageText) {
        return msgErr(context, messageText, false);
    }

    public static CommandContext<ServerCommandSource> msgInfo(CommandContext<ServerCommandSource> context, String messageText, boolean broadcastToOps) {
        return message(context, messageText, broadcastToOps, infoStyle);
    }

    public static CommandContext<ServerCommandSource> msgWarn(CommandContext<ServerCommandSource> context, String messageText, boolean broadcastToOps) {
        return message(context, messageText, broadcastToOps, warnStyle);
    }

    public static CommandContext<ServerCommandSource> msgErr(CommandContext<ServerCommandSource> context, String messageText, boolean broadcastToOps) {
        return message(context, messageText, broadcastToOps, errorStyle);
    }

    private static CommandContext<ServerCommandSource> message(CommandContext<ServerCommandSource> context, String messageText, boolean broadcastToOps, Style style) {
        synchronized (syncMessage) {
            Text text = new LiteralText(messageText);
            text.setStyle(style);
            context.getSource().sendFeedback(text, broadcastToOps);
        }
        return context;
    }

    public static void debug(String message) {
        synchronized (syncDebug) {
            if (printDebugMessages) {
                //System.out.println(String.format("[DBG] [KB] %s", message));
                LOGGER.debug("[KB][DEBUG] " + message);
            }
        }
    }

    public static void error(String message) {
        synchronized (syncError) {
            if (printErrorMessages) {
                //System.out.println(String.format("[ERR] [KB] %s", message));
                LOGGER.error("[KB][ERROR]" + message);
            }
        }
    }

    public static void info(String message) {
        synchronized (syncInfo) {
            if (printInfoMessages) {
                //System.out.println(String.format("[INF] [KB] %s", message));
                LOGGER.info("[KB][INFO] " + message);
            }
        }
    }
}