From 5c0a67da64675d59402de1920b75cbb6095aa4b5 Mon Sep 17 00:00:00 2001
From: Jakl <57708725+Jakllp@users.noreply.github.com>
Date: Mon, 4 Nov 2024 09:36:24 +0100
Subject: [PATCH] Update to upto 1.21.3
---
pom.xml | 6 +-
.../com/useful/ucars/CartOrientationUtil.java | 26 ++++--
.../java/com/useful/ucars/MotionManager.java | 9 +-
.../java/com/useful/ucars/MoveIntent.java | 19 ++++
.../java/com/useful/ucars/uCarsListener.java | 6 ++
src/main/java/com/useful/ucars/ucars.java | 89 +++++++++++++------
6 files changed, 120 insertions(+), 35 deletions(-)
create mode 100644 src/main/java/com/useful/ucars/MoveIntent.java
diff --git a/pom.xml b/pom.xml
index e11e84b..bd0850b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -96,11 +96,11 @@
com.comphenix.protocol
ProtocolLib
- 4.5.0
+ 5.3.0
org.spigotmc
- spigot
+ spigot-api
1.13.2-R0.1-SNAPSHOT
provided
@@ -127,5 +127,5 @@
UTF-8
- 1.20-R1.1.5
+ 1.21-R1.1.5
diff --git a/src/main/java/com/useful/ucars/CartOrientationUtil.java b/src/main/java/com/useful/ucars/CartOrientationUtil.java
index 1347e7b..854285b 100644
--- a/src/main/java/com/useful/ucars/CartOrientationUtil.java
+++ b/src/main/java/com/useful/ucars/CartOrientationUtil.java
@@ -43,8 +43,16 @@ public static void setPitch(Entity cart, float pitch){
Class > ema = Reflect.getNMSClass("world.entity.","Entity");
if(ucars.MCVersion.get(0) == 1) {
- if(ucars.MCVersion.get(1) >= 20) {
- p = ema.getDeclaredField("aH");
+ if(ucars.MCVersion.get(1) >= 21) {
+ if(ucars.MCVersion.get(2) >= 2)
+ p = ema.getDeclaredField("aB");
+ else
+ p = ema.getDeclaredField("aE");
+ } else if(ucars.MCVersion.get(1) >= 20) {
+ if(ucars.MCVersion.get(2) >= 5)
+ p = ema.getDeclaredField("aG");
+ else
+ p = ema.getDeclaredField("aH");
} else if(ucars.MCVersion.get(1) >= 18) {
p = ema.getDeclaredField("aB");
} else if(ucars.MCVersion.get(1) == 17) {
@@ -76,10 +84,18 @@ public static void setYaw(Entity cart, float yaw){
Class> ema = Reflect.getNMSClass("world.entity.","Entity");
Object nmsCart = getHandle.invoke(cmr.cast(cart));
Field p = null;
-
+
if(ucars.MCVersion.get(0) == 1) {
- if(ucars.MCVersion.get(1) >= 20) {
- p = ema.getDeclaredField("aG");
+ if(ucars.MCVersion.get(1) >= 21) {
+ if(ucars.MCVersion.get(2) >= 2)
+ p = ema.getDeclaredField("aA");
+ else
+ p = ema.getDeclaredField("aD");
+ } else if(ucars.MCVersion.get(1) >= 20) {
+ if(ucars.MCVersion.get(2) >= 5)
+ p = ema.getDeclaredField("aF");
+ else
+ p = ema.getDeclaredField("aG");
} else if(ucars.MCVersion.get(1) >= 18) {
p = ema.getDeclaredField("aA");
} else if(ucars.MCVersion.get(1) == 17) {
diff --git a/src/main/java/com/useful/ucars/MotionManager.java b/src/main/java/com/useful/ucars/MotionManager.java
index 246b9a6..9bcf29a 100644
--- a/src/main/java/com/useful/ucars/MotionManager.java
+++ b/src/main/java/com/useful/ucars/MotionManager.java
@@ -14,6 +14,8 @@
import com.useful.ucarsCommon.StatValue;
public class MotionManager {
+
+ public static MoveIntent MOVE_INTENT = null;
public static Vector rotateXZVector3dDegrees(Vector original, double degrees){
double[] out = rotateVector2dRadians(original.getX(), original.getZ(), Math.toRadians(degrees));
@@ -33,6 +35,12 @@ public static double[] rotateVector2dRadians(double x, double y, double radians)
return result;
}
+ public static void move(Player player) {
+ if (MOVE_INTENT == null)
+ return;
+ move(player, MOVE_INTENT.forward, MOVE_INTENT.sideways, MOVE_INTENT.jumping);
+ }
+
public static void move(Player player, float f, float s, boolean jumping) { // 'f' and 's' are values taken in by the vehicle control packet
Vector vec = new Vector();
Entity ent = player.getVehicle();
@@ -265,5 +273,4 @@ public void run() {
return;
}
}
-
}
diff --git a/src/main/java/com/useful/ucars/MoveIntent.java b/src/main/java/com/useful/ucars/MoveIntent.java
new file mode 100644
index 0000000..c2ef10a
--- /dev/null
+++ b/src/main/java/com/useful/ucars/MoveIntent.java
@@ -0,0 +1,19 @@
+package com.useful.ucars;
+
+public class MoveIntent {
+ public float forward;
+ public float sideways;
+ public boolean jumping;
+
+ public MoveIntent(float forward, float sideways, boolean jumping) {
+ this.forward = forward;
+ this.sideways = sideways;
+ this.jumping = jumping;
+ }
+
+ public void update(float forward, float sideways, boolean jumping) {
+ this.forward = forward;
+ this.sideways = sideways;
+ this.jumping = jumping;
+ }
+}
diff --git a/src/main/java/com/useful/ucars/uCarsListener.java b/src/main/java/com/useful/ucars/uCarsListener.java
index 24c3617..8d0ff42 100644
--- a/src/main/java/com/useful/ucars/uCarsListener.java
+++ b/src/main/java/com/useful/ucars/uCarsListener.java
@@ -504,11 +504,17 @@ public void tickCalcsAndLegacy(VehicleUpdateEvent event) {
return;
}
+ // TODO add motionmanager.move with current intent if Intent exists
+
+ if (MotionManager.MOVE_INTENT != null)
+ MotionManager.move(player);
+
Vehicle car = (Vehicle) vehicle;
if (!isACar(car)) {
return;
}
+
Vector vel = car.getVelocity();
diff --git a/src/main/java/com/useful/ucars/ucars.java b/src/main/java/com/useful/ucars/ucars.java
index bb19f5b..289eb8b 100644
--- a/src/main/java/com/useful/ucars/ucars.java
+++ b/src/main/java/com/useful/ucars/ucars.java
@@ -8,6 +8,7 @@
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
+import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -17,6 +18,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import com.comphenix.protocol.events.InternalStructure;
+import com.comphenix.protocol.wrappers.Vector3F;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.block.Block;
@@ -39,6 +42,7 @@
import com.useful.ucars.util.UMeta;
import net.milkbowl.vault.economy.Economy;
+import org.bukkit.util.Vector;
public class ucars extends JavaPlugin {
// The main file
@@ -144,26 +148,59 @@ private Boolean setupProtocol() {
* PacketAdapter(plugin, ConnectionSide.CLIENT_SIDE,
* ListenerPriority.NORMAL, 0x1b) {
*/
-
- ((ProtocolManager) this.protocolManager).addPacketListener(
- new PacketAdapter(this, PacketType.Play.Client.STEER_VEHICLE) {
- @Override
- public void onPacketReceiving(final PacketEvent event) {
- PacketContainer packet = event.getPacket();
- final float sideways = packet.getFloat().read(0);
- final float forwards = packet.getFloat().read(1);
- final boolean jumping = packet.getBooleans().read(0);
- Bukkit.getScheduler().runTask(ucars.plugin, new Runnable(){
-
- @Override
- public void run() {
- MotionManager.move(event.getPlayer(), forwards,
- sideways, jumping);
- return;
- }});
-
- }
- });
+
+ if(MCVersion.get(0) == 1) {
+ if (MCVersion.get(1) >= 22 || MCVersion.get(1) == 21 && MCVersion.get(2) >= 2) {
+ MotionManager.MOVE_INTENT = new MoveIntent(0F, 0F, false);
+
+ ((ProtocolManager) this.protocolManager).addPacketListener(
+ new PacketAdapter(this, PacketType.Play.Client.STEER_VEHICLE) {
+ @Override
+ public void onPacketReceiving(final PacketEvent event) {
+ PacketContainer packet = event.getPacket();
+
+ InternalStructure struct = packet.getStructures().getValues().getFirst();
+ // forward, backward, left, right, jump, shift, sprint
+ List bools = struct.getBooleans().getValues();
+ float forwards = bools.get(0) ? 1F : bools.get(1) ? -1F : 0F;
+ float sideways = bools.get(2) ? 1F : bools.get(3) ? -1F : 0F;
+ boolean jumping = bools.get(4);
+
+ Bukkit.getScheduler().runTask(ucars.plugin, new Runnable() {
+
+ @Override
+ public void run() {
+ MotionManager.MOVE_INTENT.update(forwards, sideways, jumping);
+ return;
+ }
+ });
+
+ }
+ });
+ }
+ else
+ ((ProtocolManager) this.protocolManager).addPacketListener(
+ new PacketAdapter(this, PacketType.Play.Client.STEER_VEHICLE) {
+ @Override
+ public void onPacketReceiving(final PacketEvent event) {
+ PacketContainer packet = event.getPacket();
+
+ final float forwards = packet.getFloat().read(1);
+ final float sideways = packet.getFloat().read(0);
+ final boolean jumping = packet.getBooleans().read(0);
+ Bukkit.getScheduler().runTask(ucars.plugin, new Runnable() {
+
+ @Override
+ public void run() {
+ MotionManager.move(event.getPlayer(), forwards,
+ sideways, jumping);
+ return;
+ }
+ });
+
+ }
+ });
+ }
} catch (Exception e) {
return false;
}
@@ -174,10 +211,10 @@ public void run() {
public void onEnable() {
plugin = this;
- Pattern pattern = Pattern.compile(".v(.*?)_R"); //Get MC-Version
- Matcher matcher = pattern.matcher(Bukkit.getServer().getClass().getPackage().getName());
+ Pattern pattern = Pattern.compile("\\(MC: (\\d\\.\\d+(?:\\.\\d+)?)"); //Get MC-Version
+ Matcher matcher = pattern.matcher(Bukkit.getVersion());
if(matcher.find()) {
- String[] MCVersionStr = matcher.group(1).split("_");
+ String[] MCVersionStr = matcher.group(1).split("\\.");
for(String s:MCVersionStr) {
MCVersion.add(Integer.parseInt(s));
}
@@ -781,10 +818,10 @@ public Boolean isPluginHooked(Plugin plugin) {
return getAPI().isPluginHooked(plugin);
}
- public Plugin getPlugin(String name){
+ public Plugin getPlugin(String name) {
try {
- for(Plugin p:this.hookedPlugins){
- if(p.getName().equalsIgnoreCase(name)){
+ for (Plugin p : this.hookedPlugins) {
+ if (p.getName().equalsIgnoreCase(name)) {
return p;
}
}