001/*
002 * PlotSquared, a land and world management plugin for Minecraft.
003 * Copyright (C) IntellectualSites <https://intellectualsites.com>
004 * Copyright (C) IntellectualSites team and contributors
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
018 */
019package com.plotsquared.core;
020
021import org.checkerframework.checker.nullness.qual.NonNull;
022
023public final class PlotVersion {
024
025    public final int year, month, day, hash;
026    public final String versionString;
027    public final int[] version;
028    public final String suffix;
029
030    public PlotVersion(
031            final int year,
032            final int month,
033            final int day,
034            final int hash,
035            final String rawVersion
036    ) {
037        String versionString = rawVersion;
038        this.year = year;
039        this.month = month;
040        this.day = day;
041        this.hash = hash;
042        int dash = versionString.indexOf('-');
043        if (dash != -1) {
044            suffix = versionString.substring(dash);
045            versionString = versionString.substring(0, dash);
046        } else {
047            suffix = "";
048        }
049        this.versionString = versionString.substring(versionString.indexOf('=') + 1);
050        version = new int[3];
051        String[] verArray = versionString.substring(versionString.indexOf('=') + 1).split("\\.");
052        version[0] = verArray.length > 0 ? Integer.parseInt(verArray[0]) : 0;
053        version[1] = verArray.length > 1 ? Integer.parseInt(verArray[1]) : 0;
054        version[2] = verArray.length > 2 ? Integer.parseInt(verArray[2]) : 0;
055    }
056
057    public PlotVersion(
058            final String rawVersion,
059            final String commit,
060            final String date
061    ) {
062        String versionString = rawVersion;
063        int dash = versionString.indexOf('-');
064        if (dash != -1) {
065            suffix = versionString.substring(dash);
066            versionString = versionString.substring(0, dash);
067        } else {
068            suffix = "";
069        }
070        this.versionString = versionString.substring(versionString.indexOf('=') + 1);
071        version = new int[3];
072        String[] verArray = this.versionString.split("\\.");
073        version[0] = verArray.length > 0 ? Integer.parseInt(verArray[0]) : 0;
074        version[1] = verArray.length > 1 ? Integer.parseInt(verArray[1]) : 0;
075        version[2] = verArray.length > 2 ? Integer.parseInt(verArray[2]) : 0;
076
077        this.hash = Integer.parseInt(commit.substring(commit.indexOf('=') + 1), 16);
078        String[] split1 = date.substring(date.indexOf('=') + 1).split("\\.");
079        this.year = Integer.parseInt(split1[0]);
080        this.month = Integer.parseInt(split1[1]);
081        this.day = Integer.parseInt(split1[2]);
082    }
083
084    public static @NonNull PlotVersion tryParse(
085            final @NonNull String versionString,
086            final @NonNull String commit,
087            final @NonNull String date
088    ) {
089        try {
090            return new PlotVersion(versionString, commit, date);
091        } catch (Exception e) {
092            e.printStackTrace();
093            return new PlotVersion(0, 0, 0, 0, "0");
094        }
095    }
096
097    public @NonNull String versionString() {
098        if (hash == 0 && versionString == null) {
099            return "NoVer-SNAPSHOT";
100        } else {
101            return versionString + suffix;
102        }
103    }
104
105    @Override
106    public String toString() {
107        if (hash == 0 && versionString == null) {
108            return "PlotSquared-NoVer-SNAPSHOT";
109        } else {
110            return "PlotSquared-" + versionString + suffix;
111        }
112    }
113
114    /**
115     * Compare a given version string with the one cached here.
116     *
117     * @param versionString the version to compare
118     * @return {@code true} if the given version is a "later" version
119     */
120    public boolean isLaterVersion(final @NonNull String versionString) {
121        int dash = versionString.indexOf('-');
122        String[] verArray =
123                versionString.substring(0, dash == -1 ? versionString.length() : dash).split("\\.");
124        int one = Integer.parseInt(verArray[0]);
125        int two = Integer.parseInt(verArray[1]);
126        int three = Integer.parseInt(verArray[2]);
127        if (one > version[0]) {
128            return true;
129        } else if (one == version[0] && two > version[1]) {
130            return true;
131        } else {
132            return one == version[0] && two == version[1] && three > version[2];
133        }
134    }
135
136    /**
137     * Compare a given version with the one cached here.
138     *
139     * @param verArray the version to compare
140     * @return {@code true} if the given version is a "later" version
141     */
142    public boolean isLaterVersion(int[] verArray) {
143        if (verArray[0] > version[0]) {
144            return true;
145        } else if (verArray[0] == version[0] && verArray[1] > version[1]) {
146            return true;
147        } else {
148            return verArray[0] == version[0] && verArray[1] == version[1]
149                    && verArray[2] > version[2];
150        }
151    }
152
153}