-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlayerMovementManager.java
164 lines (131 loc) · 6.72 KB
/
PlayerMovementManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.util.text.StringTextComponent;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class PlayerMovementManager {
private static final Logger LOGGER = LogManager.getLogger();
private static final boolean DEBUG_MODE = false; // set to true for debug messages
public static double airAcceleration = SourceMovementConfig.AIR_ACCELERATION.get();
public static double airStrafeSpeed = SourceMovementConfig.AIR_STRAFE_SPEED.get();
public static double groundSpeed = SourceMovementConfig.GROUND_SPEED.get();
public static double bunnyHopMultiplier = SourceMovementConfig.BUNNY_HOP_MULTIPLIER.get();
public static double maxAirSpeed = SourceMovementConfig.MAX_AIR_SPEED.get();
public static double friction = SourceMovementConfig.FRICTION.get();
private static boolean wasOnGround = false;
private static long lastTickTime = 0;
private static Vector3d lastLookVec = null;
@SubscribeEvent
public static void playerTick(TickEvent.PlayerTickEvent event) {
if (event.phase != TickEvent.Phase.END) {
return;
}
PlayerEntity player = event.player;
if (!CommandHandler.isPlayerPluginEnabled(player.getUniqueID())) {
return;
}
adjustRuntimeValues(player);
Vector3d motion = player.getMotion();
long currentTime = System.currentTimeMillis();
float tickLength = (currentTime - lastTickTime) / 1000.0f;
lastTickTime = currentTime;
boolean isOnGround = player.isOnGround();
Vector3d lookVec = player.getLookVec();
double moveForward = player.moveForward;
double moveStrafing = player.moveStrafing;
if (!isOnGround) {
Vector3d wishdir = Vector3d.ZERO;
if (moveForward != 0 || moveStrafing != 0) {
Vector3d forwardVec = new Vector3d(lookVec.x, 0, lookVec.z).normalize();
Vector3d strafeVec = new Vector3d(-lookVec.z, 0, lookVec.x).normalize();
wishdir = wishdir.add(forwardVec.scale(moveForward));
wishdir = wishdir.add(strafeVec.scale(moveStrafing));
}
double wishspeed = wishdir.length();
double currentSpeed = motion.dotProduct(wishdir);
double addSpeed = Math.min(wishspeed - currentSpeed, airStrafeSpeed);
AirAccelerate(motion, wishdir, addSpeed, airAcceleration, tickLength);
if (motion.length() > maxAirSpeed) {
motion = motion.scale(maxAirSpeed / motion.length());
}
player.setMotion(motion);
} else {
if (moveForward > 0) {
Vector3d forwardVec = new Vector3d(lookVec.x, 0, lookVec.z).normalize();
player.setMotion(player.getMotion().add(forwardVec.scale(moveForward * groundSpeed)));
}
double speed = motion.length();
if (speed > 0.0) {
double control = Math.max(speed, 10.0);
double drop = control * friction * tickLength;
motion = motion.scale(Math.max(speed - drop, 0) / speed);
player.setMotion(motion);
}
if (wasOnGround && player.isJumping) {
Vector3d direction = player.getMotion().normalize();
motion = direction.scale(motion.length() * bunnyHopMultiplier);
player.setMotion(motion);
}
}
wasOnGround = isOnGround;
}
private static void adjustRuntimeValues(PlayerEntity player) {
boolean valuesAdjusted = false;
if (airAcceleration > SourceMovementConfig.MAX_AIR_ACCELERATION.get()) {
airAcceleration = SourceMovementConfig.MAX_AIR_ACCELERATION.get();
valuesAdjusted = true;
}
if (airStrafeSpeed > SourceMovementConfig.MAX_AIR_STRAFE_SPEED.get()) {
airStrafeSpeed = SourceMovementConfig.MAX_AIR_STRAFE_SPEED.get();
valuesAdjusted = true;
}
if (groundSpeed > SourceMovementConfig.MAX_GROUND_SPEED.get()) {
groundSpeed = SourceMovementConfig.MAX_GROUND_SPEED.get();
valuesAdjusted = true;
}
if (bunnyHopMultiplier > SourceMovementConfig.MAX_BUNNY_HOP_MULTIPLIER.get()) {
bunnyHopMultiplier = SourceMovementConfig.MAX_BUNNY_HOP_MULTIPLIER.get();
valuesAdjusted = true;
}
if (maxAirSpeed > SourceMovementConfig.MAX_MAX_AIR_SPEED.get()) {
maxAirSpeed = SourceMovementConfig.MAX_MAX_AIR_SPEED.get();
valuesAdjusted = true;
}
if (friction > SourceMovementConfig.MAX_FRICTION.get()) {
friction = SourceMovementConfig.MAX_FRICTION.get();
valuesAdjusted = true;
}
if (valuesAdjusted) {
if (DEBUG_MODE) {
LOGGER.info("Runtime values adjusted for player: " + player.getName().getString());
}
player.sendMessage(new StringTextComponent("Your movement settings have been adjusted to stay within the allowed limits."), player.getUniqueID());
}
}
private static void AirAccelerate(Vector3d velocity, Vector3d wishdir, double wishspeed, double accel, float tickLength) {
double currentSpeed = velocity.dotProduct(wishdir);
double addSpeed = wishspeed - currentSpeed;
if (addSpeed > 0) {
double accelSpeed = accel * wishspeed * tickLength;
accelSpeed = Math.min(accelSpeed, addSpeed);
// Check if the projected velocity exceeds the speed limit
Vector3d projectedVelocity = velocity.add(wishdir.scale(accelSpeed));
if (projectedVelocity.length() > maxAirSpeed) {
double cosAngle = velocity.dotProduct(wishdir) / (velocity.length() * wishdir.length());
double angleRadians = Math.acos(cosAngle);
double angleDegrees = Math.toDegrees(angleRadians);
if (angleDegrees < 90) {
// Clamp the acceleration to maintain the speed limit
double clampedAccelSpeed = Math.max(0, maxAirSpeed - velocity.length());
accelSpeed = Math.min(accelSpeed, clampedAccelSpeed);
} else {
// Allow acceleration beyond the speed limit for angles greater than 90 degrees
accelSpeed = Math.min(accelSpeed, addSpeed);
}
}
velocity = velocity.add(wishdir.scale(accelSpeed));
}
}
}