Skip to content

Commit

Permalink
Regex-Parsing for version-getting
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakllp committed Jul 5, 2021
1 parent e7164c9 commit 0a494da
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 31 deletions.
52 changes: 28 additions & 24 deletions src/main/java/com/useful/ucars/CartOrientationUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,20 @@ public static void setPitch(Entity cart, float pitch){
Method getHandle = cmr.getMethod("getHandle");
Object nmsCart = getHandle.invoke(cmr.cast(cart));
Field p = null;
if(ucars.MCVersion < 17) {
Class<?> ema = Reflect.getNMSClass("world.entity.vehicle.","EntityMinecartAbstract");
p = ema.getField("pitch");
p.setAccessible(true);
p.set(ema.cast(nmsCart), -pitch);
p.setAccessible(false);
} else {
Class<?> e = Reflect.getNMSClass("world.entity.","Entity");
p = e.getDeclaredField("az"); //Pitch field is now az and in the Entity-Class
p.setAccessible(true);
p.set(e.cast(nmsCart), -pitch);
p.setAccessible(false);
if(ucars.MCVersion.get(0) == 1) {
if(ucars.MCVersion.get(1) < 17) {
Class<?> ema = Reflect.getNMSClass("world.entity.vehicle.","EntityMinecartAbstract");
p = ema.getField("pitch");
p.setAccessible(true);
p.set(ema.cast(nmsCart), -pitch);
p.setAccessible(false);
} else {
Class<?> e = Reflect.getNMSClass("world.entity.","Entity");
p = e.getDeclaredField("az"); //Pitch field is now az and in the Entity-Class
p.setAccessible(true);
p.set(e.cast(nmsCart), -pitch);
p.setAccessible(false);
}
}
} catch (Exception e) {
e.printStackTrace();
Expand All @@ -71,18 +73,20 @@ public static void setYaw(Entity cart, float yaw){
Method getHandle = cmr.getMethod("getHandle");
Object nmsCart = getHandle.invoke(cmr.cast(cart));
Field p = null;
if(ucars.MCVersion < 17) {
Class<?> ema = Reflect.getNMSClass("world.entity.vehicle.","EntityMinecartAbstract");
p = ema.getField("yaw");
p.setAccessible(true);
p.set(ema.cast(nmsCart), yaw);
p.setAccessible(false);
} else {
Class<?> e = Reflect.getNMSClass("world.entity.","Entity");
p = e.getDeclaredField("ay"); //Yaw field is now ay and in the Entity-Class
p.setAccessible(true);
p.set(e.cast(nmsCart), yaw);
p.setAccessible(false);
if(ucars.MCVersion.get(0) == 1) {
if(ucars.MCVersion.get(1) < 17) {
Class<?> ema = Reflect.getNMSClass("world.entity.vehicle.","EntityMinecartAbstract");
p = ema.getField("yaw");
p.setAccessible(true);
p.set(ema.cast(nmsCart), yaw);
p.setAccessible(false);
} else {
Class<?> e = Reflect.getNMSClass("world.entity.","Entity");
p = e.getDeclaredField("ay"); //Yaw field is now ay and in the Entity-Class
p.setAccessible(true);
p.set(e.cast(nmsCart), yaw);
p.setAccessible(false);
}
}
} catch (Exception e) {
e.printStackTrace();
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/useful/ucars/Reflect.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ public static void sendPacket(Player p, Object packet) {

public static Class<?> getNMSClass(String newPackage, String ClassName) {
String className = "net.minecraft.";
if(ucars.MCVersion < 17) {
className += "server."+ version + ClassName;
} else {
className += newPackage+ClassName;
if(ucars.MCVersion.get(0) == 1) {
if(ucars.MCVersion.get(1) < 17) {
className += "server."+ version + ClassName;
} else {
className += newPackage+ClassName;
}
}

Class<?> c = null;
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/com/useful/ucars/ucars.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
Expand Down Expand Up @@ -63,7 +65,7 @@ public class ucars extends JavaPlugin {
public static boolean playersIgnoreTrafficLights = false;
public static boolean turningCircles = true;
public static boolean fireUpdateEvent = false;
public static Float MCVersion = null;
public static ArrayList<Integer> MCVersion = new ArrayList<Integer>();

public static String colorise(String prefix) {
return ChatColor.translateAlternateColorCodes('&', prefix);
Expand Down Expand Up @@ -171,8 +173,15 @@ public void run() {
@Override
public void onEnable() {
plugin = this;

MCVersion = Float.valueOf(Bukkit.getVersion().replace(")","").split("MC: 1.")[1]);

Pattern pattern = Pattern.compile(".v(.*?)_R"); //Get MC-Version
Matcher matcher = pattern.matcher(Bukkit.getServer().getClass().getPackage().getName());
if(matcher.find()) {
String[] MCVersionStr = matcher.group(1).split("_");
for(String s:MCVersionStr) {
MCVersion.add(Integer.parseInt(s));
}
}

File langFile = new File(getDataFolder().getAbsolutePath()
+ File.separator + "lang.yml");
Expand Down

0 comments on commit 0a494da

Please sign in to comment.