aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/cowtipper/cowlection/search/GuiDateField.java
blob: 9e3a3ffe1b512bf8faf7c8ecb692bb17e035a53e (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
package de.cowtipper.cowlection.search;

import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.GuiTextField;

import java.time.LocalDate;
import java.time.format.DateTimeParseException;

class GuiDateField extends GuiTextField {
    GuiDateField(int componentId, FontRenderer fontRendererObj, int x, int y, int width, int height) {
        super(componentId, fontRendererObj, x, y, width, height);
    }

    LocalDate getDate() {
        try {
            return LocalDate.parse(this.getText());
        } catch (DateTimeParseException e) {
            return LocalDate.now();
        }
    }

    boolean validateDate() {
        try {
            LocalDate localDate = LocalDate.parse(this.getText());
            if (localDate.isAfter(LocalDate.now()) || localDate.isBefore(LocalDate.ofYearDay(2009, 1))) {
                // searching for things written in the future isn't possible (yet). It is also not possible to perform a search before the existence of mc.
                setTextColor(0xFFFF3333);
                return false;
            }
        } catch (DateTimeParseException e) {
            setTextColor(0xFFFF3333);
            return false;
        }
        setTextColor(0xFFFFFF);
        return true;
    }
}