Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed 1.20.6 and 1.21 support #10

Merged
merged 3 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ dependencies {


group = "me.tigerhix.lib"
version = "1.4.4"
version = "1.4.4-SNAPSHOT1"
description = "scoreboard"
java.sourceCompatibility = JavaVersion.VERSION_1_8

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/me/tigerhix/lib/scoreboard/ScoreboardLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static Scoreboard createScoreboard(Player holder) {
if(Bukkit.getPluginManager().isPluginEnabled("ProtocolSupport")) {
try {
int version = ProtocolSupportAPI.getProtocolVersion(holder).getId();
if(version >= 401 && ServerVersion.Version.isCurrentEqualOrHigher(ServerVersion.Version.v1_13_R1)) {
if(version >= 401 && ServerVersion.Version.isCurrentEqualOrHigher(ServerVersion.Version.v1_13)) {
//only give player & server higher 1.12 the better scoreboard
return new SimpleScoreboard(holder);
}
Expand All @@ -42,14 +42,14 @@ public static Scoreboard createScoreboard(Player holder) {
try {
ViaAPI api = Via.getAPI(); // Get the API
int version = api.getPlayerVersion(holder); // Get the protocol version
if(version >= 401 && ServerVersion.Version.isCurrentEqualOrHigher(ServerVersion.Version.v1_13_R1)) {
if(version >= 401 && ServerVersion.Version.isCurrentEqualOrHigher(ServerVersion.Version.v1_13)) {
//only give player & server higher 1.12 the better scoreboard
return new SimpleScoreboard(holder);
}
} catch(Exception ignored) {
//Not using ViaVersion 4 or unable to get ViaVersion return LegacyBoard!
}
} else if(ServerVersion.Version.isCurrentEqualOrHigher(ServerVersion.Version.v1_13_R1)) {
} else if(ServerVersion.Version.isCurrentEqualOrHigher(ServerVersion.Version.v1_13)) {
return new SimpleScoreboard(holder);
}
return new LegacySimpleScoreboard(holder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public List<Entry> build() {

private String adapt(String entry) {
// Cut off the exceeded part if needed
if (ServerVersion.Version.isCurrentEqualOrHigher(ServerVersion.Version.v1_14_R1)) {
if (ServerVersion.Version.isCurrentEqualOrHigher(ServerVersion.Version.v1_14)) {
if (entry.length() > 144) entry = entry.substring(0, 143);
} else {
if (entry.length() > 48) entry = entry.substring(0, 47);
Expand Down
134 changes: 61 additions & 73 deletions src/main/java/me/tigerhix/lib/scoreboard/util/ServerVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,128 +2,116 @@

import org.bukkit.Bukkit;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ServerVersion {

public Version getVersion() {
return Version.getCurrent();
}

public enum Version {
v0_0_R0,
v1_8_R1,
v1_8_R2,
v1_8_R3,
v1_9_R1,
v1_9_R2,
v1_10_R1,
v1_10_R2,
v1_11_R1,
v1_12_R1,
v1_13_R1,
v1_13_R2,
v1_14_R1,
v1_14_R2,
v1_15_R1,
v1_15_R2,
v1_16_R1,
v1_16_R2,
v1_16_R3,
v1_17_R1,
v1_17_R2,
v1_18_R1,
v1_18_R2,
v1_19_R1,
v1_19_R2,
v1_19_R3,
v1_20_R1,
v1_20_R2,
v1_20_R3,
v1_20_R4,
v1_20_R5,
v1_20_R6,
v1_21_R1;

private final Integer value;
private final String shortVersion;
private final String packageVersion;
private static Version current;
v0_0_0(0, 0),
v1_8_8(8, 4),
v1_9(9, 4),
v1_10(10, 2),
v1_11(11, 0),
v1_12(12, 0),
v1_13(13, 1),
v1_14(14, 0),
v1_15(15, 0),
v1_16(16, 0),
v1_17(17, 0),
v1_18(18, 0),
v1_19(19, 0),
v1_20(20, 0),
v1_21(21, 0);

Version() {
value = Integer.valueOf(name().replaceAll("[^\\d.]", ""));
shortVersion = name().substring(0, name().length() - 3);
packageVersion = Bukkit.getServer().getClass().getPackage().getName().replace('.', ',').split(",")[3];
}

public Integer getValue() {
return value;
private static Version current;
private final int minor;
private final int minPatch;

Version(int minor, int minPatch) {
this.minor = minor;
this.minPatch = minPatch;
}

public String getShortVersion() {
return shortVersion;
public int getMinor() {
return minor;
}

public String getPackageVersion() {
return packageVersion;
public int getMinPatch() {
return minPatch;
}

public static Version getCurrent() {
if(current != null)
if(current != null) {
return current;
}

String[] v = Bukkit.getServer().getClass().getPackage().getName().split("\\.");
String vv = v[v.length - 1];
for(Version one : values()) {
if(one.name().equalsIgnoreCase(vv)) {
current = one;
break;
Matcher serverVersion = Pattern.compile("^(?<major>\\d+)\\.(?<minor>\\d+)(?:\\.(?<patch>\\d+))?").matcher(Bukkit.getBukkitVersion());
if(serverVersion.find()) {
int serverMinor = Integer.parseInt(serverVersion.group("minor"));
String patch = serverVersion.group("patch");
int serverPatch = Integer.parseInt((patch == null || patch.isEmpty()) ? "0" : patch);

for(Version value : values()) {
if(value.getMinor() == serverMinor && serverPatch >= value.getMinPatch()) {
current = value;
break;
}
}
} else {
throw new IllegalStateException("Cannot parse server version: \"" + Bukkit.getBukkitVersion() + '"');
}

if (current == null) { // If we forgot to add new version to enum
current = Version.v0_0_R0;
if(current == null) { // Fallback
current = Version.v0_0_0;
}

return current;
}

public boolean isLower(Version version) {
return getValue() < version.getValue();
return minor < version.getMinor();
}

public boolean isHigher(Version version) {
return getValue() > version.getValue();
return minor > version.getMinor();
}

public boolean isEqual(Version version) {
return getValue().equals(version.getValue());
return minor == version.getMinor();
}

public boolean isEqualOrLower(Version version) {
return getValue() <= version.getValue();
return minor <= version.getMinor();
}

public boolean isEqualOrHigher(Version version) {
return getValue() >= version.getValue();
return minor >= version.getMinor();
}

public static boolean isCurrentEqualOrHigher(Version v) {
return getCurrent().getValue() >= v.getValue();
public static boolean isCurrentEqualOrHigher(Version fixedVersion) {
return getCurrent().getMinor() >= fixedVersion.getMinor();
}

public static boolean isCurrentHigher(Version v) {
return getCurrent().getValue() > v.getValue();
public static boolean isCurrentHigher(Version fixedVersion) {
return getCurrent().getMinor() > fixedVersion.getMinor();
}

public static boolean isCurrentLower(Version v) {
return getCurrent().getValue() < v.getValue();
public static boolean isCurrentLower(Version fixedVersion) {
return getCurrent().getMinor() < fixedVersion.getMinor();
}

public static boolean isCurrentEqualOrLower(Version v) {
return getCurrent().getValue() <= v.getValue();
public static boolean isCurrentEqualOrLower(Version fixedVersion) {
return getCurrent().getMinor() <= fixedVersion.getMinor();
}

public static boolean isCurrentEqual(Version v) {
return getCurrent().getValue().equals(v.getValue());
public static boolean isCurrentEqual(Version fixedVersion) {
return getCurrent().getMinor() == fixedVersion.getMinor();
}
}
}
Loading