aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/thatgravyboat/skyblockhud/handlers/TimeHandler.java
blob: f1aa22295d859c147d6185e9b4cec442673dd60a (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
package com.thatgravyboat.skyblockhud.handlers;

import com.thatgravyboat.skyblockhud.api.events.SidebarLineUpdateEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.regex.Pattern;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import org.apache.logging.log4j.LogManager;

public class TimeHandler {

    public static long time;

    @SubscribeEvent
    public void onSidebarLineUpdate(SidebarLineUpdateEvent event) {
        if (Pattern.matches("([0-9]*):([0-9]*)(pm|am)", event.formattedLine.toLowerCase().trim())) {
            boolean isPm = event.formattedLine.toLowerCase().trim().endsWith("pm");
            SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm a", Locale.CANADA);
            String currentTimeString = event.formattedLine.replace(" ", "").replace(isPm ? "pm" : "am", isPm ? " pm" : " am");
            try {
                time = (parseFormat.parse(currentTimeString).getTime() - parseFormat.parse("00:00 am").getTime()) / 1000L;
            } catch (ParseException ignored) {
                LogManager.getLogger().warn("timeformat error: " + currentTimeString);
            }
        }
    }
}