Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Asintotoo authored Jul 20, 2024
1 parent 1030d50 commit 3275423
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.asintoto</groupId>
<artifactId>Basic</artifactId>
<version>1.2.12</version>
<version>1.2.13</version>
<packaging>jar</packaging>

<name>Basic</name>
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/asintoto/basic/Basic.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.asintoto.basic.listeners.InventoryClickListener;
import com.asintoto.basic.listeners.InventoryCloseListener;
import com.asintoto.basic.menu.MenuManager;
import com.asintoto.basic.reflection.MinecraftVersion;
import com.asintoto.basic.reflection.ReflectionUtils;
import com.asintoto.basic.regions.RegionManager;
import com.asintoto.basic.utils.Debug;
Expand Down Expand Up @@ -206,6 +207,15 @@ public static void reload() {
}
}

/**
* Get the Server Version (ex. 1.20.1)
*
* @return
*/
public static String getServerVersion() {
return MinecraftVersion.getFullVersion();
}

public static <T extends BasicCommand> void registerCommand(String cmd, T commandClass) {
plugin.getCommand(cmd).setExecutor(commandClass);
plugin.getCommand(cmd).setTabCompleter(commandClass);
Expand Down
126 changes: 126 additions & 0 deletions src/main/java/com/asintoto/basic/reflection/MinecraftVersion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package com.asintoto.basic.reflection;

import org.bukkit.Bukkit;



public class MinecraftVersion {

private static String serverVersion;
private static V current;
private static int subversion;

public static int getSubversion() {
return subversion;
}

public static V getCurrent() {
return current;
}

public enum V {
v1_22(22),
v1_21(21),
v1_20(20),
v1_19(19),
v1_18(18),
v1_17(17),
v1_16(16),
v1_15(15),
v1_14(14),
v1_13(13),
v1_12(12),
v1_11(11),
v1_10(10),
v1_9(9),
v1_8(8),
v1_7(7),
v1_6(6),
v1_5(5),
v1_4(4),
v1_3_AND_BELOW(3);


private final int minorVersionNumber;


V(int version) {
this.minorVersionNumber = version;
}


protected static V parse(int number) {
for (final V v : values())
if (v.minorVersionNumber == number)
return v;

throw new RuntimeException();
}


@Override
public String toString() {
return "1." + this.minorVersionNumber;
}
}


public static boolean equals(V version) {
return compareWith(version) == 0;
}


public static boolean olderThan(V version) {
return compareWith(version) < 0;
}


public static boolean newerThan(V version) {
return compareWith(version) > 0;
}


public static boolean atLeast(V version) {
return equals(version) || newerThan(version);
}


private static int compareWith(V version) {
try {
return getCurrent().minorVersionNumber - version.minorVersionNumber;

} catch (final Throwable t) {
t.printStackTrace();

return 0;
}
}


public static String getFullVersion() {
return current.toString() + (subversion > 0 ? "." + subversion : "");
}


@Deprecated
public static String getServerVersion() {
return serverVersion.equals("craftbukkit") ? "" : serverVersion;
}

static {

final String packageName = Bukkit.getServer() == null ? "" : Bukkit.getServer().getClass().getPackage().getName();
final String curr = packageName.substring(packageName.lastIndexOf('.') + 1);
serverVersion = !"craftbukkit".equals(curr) && !"".equals(packageName) ? curr : "";

final String bukkitVersion = Bukkit.getServer().getBukkitVersion(); // 1.20.6-R0.1-SNAPSHOT
final String versionString = bukkitVersion.split("\\-")[0]; // 1.20.6
final String[] versions = versionString.split("\\.");

final int version = Integer.parseInt(versions[1]); // 20

current = version < 3 ? V.v1_3_AND_BELOW : V.parse(version);
subversion = versions.length == 3 ? Integer.parseInt(versions[2]) : 0;

}
}

0 comments on commit 3275423

Please sign in to comment.