Merge remote-tracking branch 'origin/main'

This commit is contained in:
RaphiMC 2023-12-04 00:01:24 +01:00
commit 337c355866
No known key found for this signature in database
GPG Key ID: 0F6BB0657A03AC94
3 changed files with 56 additions and 9 deletions

View File

@ -1,6 +1,6 @@
<component name="CopyrightManager"> <component name="CopyrightManager">
<copyright> <copyright>
<option name="notice" value="This file is part of ViaProtocolHack - https://github.com/RaphiMC/ViaProtocolHack&#10;Copyright (C) &amp;#36;today.year RK_01/RaphiMC and contributors&#10;&#10;This program is free software: you can redistribute it and/or modify&#10;it under the terms of the GNU General Public License as published by&#10;the Free Software Foundation, either version 3 of the License, or&#10;(at your option) any later version.&#10;&#10;This program is distributed in the hope that it will be useful,&#10;but WITHOUT ANY WARRANTY; without even the implied warranty of&#10;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the&#10;GNU General Public License for more details.&#10;&#10;You should have received a copy of the GNU General Public License&#10;along with this program. If not, see &lt;http://www.gnu.org/licenses/&gt;." /> <option name="notice" value="This file is part of ViaLoader - https://github.com/RaphiMC/ViaLoader&#10;Copyright (C) &amp;#36;today.year RK_01/RaphiMC and contributors&#10;&#10;This program is free software: you can redistribute it and/or modify&#10;it under the terms of the GNU General Public License as published by&#10;the Free Software Foundation, either version 3 of the License, or&#10;(at your option) any later version.&#10;&#10;This program is distributed in the hope that it will be useful,&#10;but WITHOUT ANY WARRANTY; without even the implied warranty of&#10;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the&#10;GNU General Public License for more details.&#10;&#10;You should have received a copy of the GNU General Public License&#10;along with this program. If not, see &lt;http://www.gnu.org/licenses/&gt;." />
<option name="myName" value="GPL-3.0" /> <option name="myName" value="GPL-3.0" />
</copyright> </copyright>
</component> </component>

View File

@ -220,6 +220,12 @@ public enum VersionEnum {
return fromProtocolVersion(ProtocolVersion.getProtocol(protocolId)); return fromProtocolVersion(ProtocolVersion.getProtocol(protocolId));
} }
public static VersionEnum fromProtocolName(final String protocolName) {
final ProtocolVersion protocolVersion = ProtocolVersion.getClosest(protocolName);
if (protocolVersion == null) return UNKNOWN;
return fromProtocolVersion(protocolVersion);
}
public static VersionEnum fromUserConnection(final UserConnection userConnection) { public static VersionEnum fromUserConnection(final UserConnection userConnection) {
return fromUserConnection(userConnection, true); return fromUserConnection(userConnection, true);
} }

View File

@ -77,15 +77,56 @@ public class VersionRange {
@Override @Override
public String toString() { public String toString() {
if (this.min == null && this.max == null) return "*"; if (this.min == null && this.max == null) return "*";
else if (this.min == null) return "<= " + this.max.getName(); else {
else if (this.max == null) return ">= " + this.min.getName(); StringBuilder rangeString = new StringBuilder();
else if (Objects.equals(this.min, this.max)) return this.min.getName(); if (!this.ranges.isEmpty()) {
for (VersionRange range : this.ranges) rangeString.append(", ").append(range.toString());
final StringBuilder thisName = new StringBuilder(this.min.getName() + " - " + this.max.getName()); }
if (!this.ranges.isEmpty()) { if (this.min == null) return "<= " + this.max.getName() + rangeString;
for (VersionRange range : this.ranges) thisName.append(", ").append(range.toString()); else if (this.max == null) return ">= " + this.min.getName() + rangeString;
else if (Objects.equals(this.min, this.max)) return this.min.getName();
else return this.min.getName() + " - " + this.max.getName() + rangeString;
} }
return thisName.toString(); }
@Override
public boolean equals(Object object) {
if (this == object) return true;
if (object == null || getClass() != object.getClass()) return false;
VersionRange that = (VersionRange) object;
return min == that.min && max == that.max && Objects.equals(ranges, that.ranges);
}
@Override
public int hashCode() {
return Objects.hash(min, max, ranges);
}
public static VersionRange fromString(String str) {
if ("*".equals(str)) return all();
else if (str.contains(",")) {
String[] rangeParts = str.split(", ");
VersionRange versionRange = null;
for (String part : rangeParts) {
if (versionRange == null) versionRange = parseSinglePart(part);
else versionRange.add(parseSinglePart(part));
}
return versionRange;
} else {
return parseSinglePart(str);
}
}
private static VersionRange parseSinglePart(String part) {
if (part.startsWith("<= ")) return andOlder(VersionEnum.fromProtocolName(part.substring(3)));
else if (part.startsWith(">= ")) return andNewer(VersionEnum.fromProtocolName(part.substring(3)));
else if (part.contains(" - ")) {
String[] rangeParts = part.split(" - ");
VersionEnum min = VersionEnum.fromProtocolName(rangeParts[0]);
VersionEnum max = VersionEnum.fromProtocolName(rangeParts[1]);
return of(min, max);
} else return single(VersionEnum.fromProtocolName(part));
} }
} }