Skip to content

Commit 3f0d04a

Browse files
author
iso2013
committed
Move packet improvements
* Add utility methods for locations * Optimize getting/setting the pitch and yaw and remove the unnecessary direction vector. * Properly replace the new packet if the type changed
1 parent 4e9c875 commit 3f0d04a

File tree

3 files changed

+99
-38
lines changed

3 files changed

+99
-38
lines changed

API/src/main/java/net/blitzcube/peapi/api/packet/IEntityMovePacket.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.blitzcube.peapi.api.packet;
22

33
import com.comphenix.protocol.PacketType;
4+
import org.bukkit.Location;
45
import org.bukkit.util.Vector;
56

67
/**
@@ -53,11 +54,14 @@ public interface IEntityMovePacket extends IEntityPacket {
5354
void setOnGround(boolean onGround);
5455

5556
/**
56-
* Get the pitch and yaw associated with the new direction, in the order pitch, yaw.
57-
*
58-
* @return a double array containing the pitch and yaw
57+
* @return the pitch for the new direction
58+
*/
59+
double getPitch();
60+
61+
/**
62+
* @return the yaw for the new direction
5963
*/
60-
double[] getPitchYaw();
64+
double getYaw();
6165

6266
/**
6367
* Set the pitch and yaw for the new direction.
@@ -67,6 +71,22 @@ public interface IEntityMovePacket extends IEntityPacket {
6771
*/
6872
void setPitchYaw(double pitch, double yaw);
6973

74+
/**
75+
* @param currentLocation the location that the entity is currently at
76+
* @return the new location the entity will be moved to, including direction and position
77+
*/
78+
Location getLocation(Location currentLocation);
79+
80+
81+
/**
82+
* A utility method to set the new location based on the current location and determine automatically if it should
83+
* be a teleport packet. Both the position and direction will be read from the given location.
84+
*
85+
* @param newLocation the new location that the entity should be moved to
86+
* @param currentLocation the location that the entity is currently at
87+
*/
88+
void setLocation(Location newLocation, Location currentLocation);
89+
7090
enum MoveType {
7191
REL_MOVE(PacketType.Play.Server.REL_ENTITY_MOVE),
7292
LOOK_AND_REL_MOVE(PacketType.Play.Server.REL_ENTITY_MOVE_LOOK),

Plugin/src/main/java/net/blitzcube/peapi/event/engine/listeners/GenericListener.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ public void onPacketSending(PacketEvent packetEvent) {
8585
if (e.isCancelled()) {
8686
packetEvent.setCancelled(true);
8787
}
88+
if (w.getRawPacket() != c) {
89+
packetEvent.setPacket(w.getRawPacket());
90+
}
8891
}
8992

9093
@Override

Plugin/src/main/java/net/blitzcube/peapi/packet/EntityMovePacket.java

Lines changed: 72 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import net.blitzcube.peapi.api.entity.IEntityIdentifier;
66
import net.blitzcube.peapi.api.packet.IEntityMovePacket;
77
import net.blitzcube.peapi.entity.EntityIdentifier;
8+
import org.bukkit.Location;
89
import org.bukkit.entity.Player;
910
import org.bukkit.util.Vector;
1011

@@ -15,32 +16,29 @@ public class EntityMovePacket extends EntityPacket implements IEntityMovePacket
1516
private boolean onGround;
1617
private MoveType type;
1718
private Vector position;
18-
private Vector direction;
1919
private double pitch, yaw;
2020

2121
EntityMovePacket(IEntityIdentifier identifier, MoveType type) {
2222
super(identifier, new PacketContainer(type.getPacketType()), true);
2323
this.type = type;
2424
}
2525

26-
private EntityMovePacket(IEntityIdentifier identifier, PacketContainer rawPacket, Vector newAngle, Vector
26+
private EntityMovePacket(IEntityIdentifier identifier, PacketContainer rawPacket, Byte newPitch, Byte newYaw, Vector
2727
newLocation, boolean onGround, boolean teleport) {
2828
super(identifier, rawPacket, true);
2929
if (teleport) {
3030
type = MoveType.TELEPORT;
31-
} else if (newAngle == null && newLocation != null) {
31+
} else if (newPitch == null && newLocation != null) {
3232
type = MoveType.REL_MOVE;
33-
} else if (newAngle != null && newLocation == null) {
33+
} else if (newPitch != null && newLocation == null) {
3434
type = MoveType.LOOK;
35-
} else if (newAngle != null) {
35+
} else if (newPitch != null) {
3636
type = MoveType.LOOK_AND_REL_MOVE;
3737
}
38-
this.direction = newAngle != null ? newAngle : new Vector();
38+
this.pitch = newPitch != null ? newPitch : 0;
39+
this.yaw = newYaw != null ? newYaw : 0;
3940
this.position = newLocation != null ? newLocation : new Vector();
4041
this.onGround = onGround;
41-
double[] angles = vectorToAngles(this.direction);
42-
pitch = angles[0];
43-
yaw = angles[1];
4442
}
4543

4644
public static EntityPacket unwrap(int entityID, PacketContainer c, Player p) {
@@ -50,7 +48,7 @@ public static EntityPacket unwrap(int entityID, PacketContainer c, Player p) {
5048
return new EntityMovePacket(
5149
new EntityIdentifier(entityID, p),
5250
c,
53-
vectorFromAngles(c.getBytes().read(1), c.getBytes().read(0)),
51+
c.getBytes().read(1), c.getBytes().read(0),
5452
new Vector(c.getDoubles().read(0), c.getDoubles().read(1), c.getDoubles().read(2)),
5553
c.getBooleans().read(0),
5654
true
@@ -59,7 +57,7 @@ public static EntityPacket unwrap(int entityID, PacketContainer c, Player p) {
5957
return new EntityMovePacket(
6058
new EntityIdentifier(entityID, p),
6159
c,
62-
null,
60+
null, null,
6361
new Vector(
6462
((double) c.getIntegers().read(1)) / 4096.0,
6563
((double) c.getIntegers().read(2)) / 4096.0,
@@ -72,7 +70,7 @@ public static EntityPacket unwrap(int entityID, PacketContainer c, Player p) {
7270
return new EntityMovePacket(
7371
new EntityIdentifier(entityID, p),
7472
c,
75-
vectorFromAngles(c.getBytes().read(1), c.getBytes().read(0)),
73+
c.getBytes().read(1), c.getBytes().read(0),
7674
new Vector(
7775
((double) c.getIntegers().read(1)) / 4096.0,
7876
((double) c.getIntegers().read(2)) / 4096.0,
@@ -85,7 +83,7 @@ public static EntityPacket unwrap(int entityID, PacketContainer c, Player p) {
8583
return new EntityMovePacket(
8684
new EntityIdentifier(entityID, p),
8785
c,
88-
vectorFromAngles(c.getBytes().read(1), c.getBytes().read(0)),
86+
c.getBytes().read(1), c.getBytes().read(0),
8987
null,
9088
c.getBooleans().read(0),
9189
false
@@ -120,25 +118,13 @@ private static double[] vectorToAngles(Vector v) {
120118

121119
@Override
122120
public Vector getNewDirection() {
123-
return direction;
121+
return vectorFromAngles(pitch, yaw);
124122
}
125123

126124
@Override
127125
public void setNewDirection(Vector direction) {
128-
this.direction = direction;
129126
double[] angles = vectorToAngles(direction);
130-
switch (type) {
131-
case REL_MOVE:
132-
setType(MoveType.LOOK_AND_REL_MOVE);
133-
case LOOK_AND_REL_MOVE:
134-
case LOOK:
135-
case TELEPORT:
136-
super.rawPacket.getBytes().write(1, (byte) angles[0]);
137-
super.rawPacket.getBytes().write(0, (byte) angles[1]);
138-
break;
139-
}
140-
this.pitch = angles[0];
141-
this.yaw = angles[1];
127+
setPitchYaw(angles[0], angles[1]);
142128
}
143129

144130
@Override
@@ -149,6 +135,7 @@ public Vector getNewPosition() {
149135
@Override
150136
public void setNewPosition(Vector position, boolean teleport) {
151137
this.position = position;
138+
if (position == null) position = new Vector();
152139
switch (type) {
153140
case REL_MOVE:
154141
case LOOK_AND_REL_MOVE:
@@ -180,7 +167,7 @@ private void setType(MoveType newType) {
180167
super.rawPacket = new PacketContainer(type.getPacketType());
181168
super.rawPacket.getModifier().writeDefaults();
182169
setNewPosition(position, newType == MoveType.TELEPORT);
183-
setNewDirection(direction);
170+
setPitchYaw(pitch, yaw);
184171
setOnGround(onGround);
185172
}
186173

@@ -201,24 +188,75 @@ public void setOnGround(boolean onGround) {
201188
}
202189

203190
@Override
204-
public double[] getPitchYaw() {
205-
return new double[]{pitch, yaw};
191+
public double getPitch() {
192+
return pitch;
206193
}
207194

195+
@Override
196+
public double getYaw() { return yaw; }
197+
208198
@Override
209199
public void setPitchYaw(double pitch, double yaw) {
210-
setNewDirection(vectorFromAngles(pitch, yaw));
200+
switch (type) {
201+
case REL_MOVE:
202+
setType(MoveType.LOOK_AND_REL_MOVE);
203+
case LOOK_AND_REL_MOVE:
204+
case LOOK:
205+
case TELEPORT:
206+
super.rawPacket.getBytes().write(1, (byte) pitch);
207+
super.rawPacket.getBytes().write(0, (byte) yaw);
208+
break;
209+
}
210+
this.pitch = pitch;
211+
this.yaw = yaw;
212+
}
213+
214+
@Override
215+
public Location getLocation(Location currentLocation) {
216+
if (type != MoveType.TELEPORT && position != null) {
217+
return new Location(
218+
currentLocation.getWorld(),
219+
currentLocation.getX() + position.getX(),
220+
currentLocation.getY() + position.getY(),
221+
currentLocation.getZ() + position.getZ(),
222+
(float) yaw, (float) pitch
223+
);
224+
} else if (position == null) {
225+
return new Location(
226+
currentLocation.getWorld(),
227+
currentLocation.getX(),
228+
currentLocation.getY(),
229+
currentLocation.getZ(),
230+
(float) yaw, (float) pitch
231+
);
232+
} else {
233+
return new Location(
234+
currentLocation.getWorld(),
235+
position.getX(),
236+
position.getY(),
237+
position.getZ(),
238+
(float) yaw, (float) pitch
239+
);
240+
}
241+
}
242+
243+
@Override
244+
public void setLocation(Location newLocation, Location currentLocation) {
245+
if (newLocation.distanceSquared(currentLocation) > 64) {
246+
setNewPosition(newLocation.toVector(), true);
247+
} else {
248+
setNewPosition(newLocation.toVector().subtract(currentLocation.toVector()), false);
249+
}
250+
setNewDirection(newLocation.getDirection());
211251
}
212252

213253
@Override
214254
public PacketContainer getRawPacket() {
215255
switch (type) {
216256
case LOOK:
217-
assert direction != null;
218257
break;
219258
case TELEPORT:
220259
case LOOK_AND_REL_MOVE:
221-
assert direction != null;
222260
case REL_MOVE:
223261
assert position != null;
224262
}
@@ -229,7 +267,7 @@ public PacketContainer getRawPacket() {
229267
public EntityPacket clone() {
230268
EntityMovePacket p = new EntityMovePacket(getIdentifier(), type);
231269
p.setNewPosition(position, type == MoveType.TELEPORT);
232-
p.setNewDirection(direction);
270+
p.setPitchYaw(pitch, yaw);
233271
p.setOnGround(onGround);
234272
return p;
235273
}

0 commit comments

Comments
 (0)