Skip to content

Commit

Permalink
Move packet improvements
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
iso2013 committed Aug 7, 2018
1 parent 4e9c875 commit 3f0d04a
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.blitzcube.peapi.api.packet;

import com.comphenix.protocol.PacketType;
import org.bukkit.Location;
import org.bukkit.util.Vector;

/**
Expand Down Expand Up @@ -53,11 +54,14 @@ public interface IEntityMovePacket extends IEntityPacket {
void setOnGround(boolean onGround);

/**
* Get the pitch and yaw associated with the new direction, in the order pitch, yaw.
*
* @return a double array containing the pitch and yaw
* @return the pitch for the new direction
*/
double getPitch();

/**
* @return the yaw for the new direction
*/
double[] getPitchYaw();
double getYaw();

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

/**
* @param currentLocation the location that the entity is currently at
* @return the new location the entity will be moved to, including direction and position
*/
Location getLocation(Location currentLocation);


/**
* A utility method to set the new location based on the current location and determine automatically if it should
* be a teleport packet. Both the position and direction will be read from the given location.
*
* @param newLocation the new location that the entity should be moved to
* @param currentLocation the location that the entity is currently at
*/
void setLocation(Location newLocation, Location currentLocation);

enum MoveType {
REL_MOVE(PacketType.Play.Server.REL_ENTITY_MOVE),
LOOK_AND_REL_MOVE(PacketType.Play.Server.REL_ENTITY_MOVE_LOOK),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ public void onPacketSending(PacketEvent packetEvent) {
if (e.isCancelled()) {
packetEvent.setCancelled(true);
}
if (w.getRawPacket() != c) {
packetEvent.setPacket(w.getRawPacket());
}
}

@Override
Expand Down
106 changes: 72 additions & 34 deletions Plugin/src/main/java/net/blitzcube/peapi/packet/EntityMovePacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.blitzcube.peapi.api.entity.IEntityIdentifier;
import net.blitzcube.peapi.api.packet.IEntityMovePacket;
import net.blitzcube.peapi.entity.EntityIdentifier;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;

Expand All @@ -15,32 +16,29 @@ public class EntityMovePacket extends EntityPacket implements IEntityMovePacket
private boolean onGround;
private MoveType type;
private Vector position;
private Vector direction;
private double pitch, yaw;

EntityMovePacket(IEntityIdentifier identifier, MoveType type) {
super(identifier, new PacketContainer(type.getPacketType()), true);
this.type = type;
}

private EntityMovePacket(IEntityIdentifier identifier, PacketContainer rawPacket, Vector newAngle, Vector
private EntityMovePacket(IEntityIdentifier identifier, PacketContainer rawPacket, Byte newPitch, Byte newYaw, Vector
newLocation, boolean onGround, boolean teleport) {
super(identifier, rawPacket, true);
if (teleport) {
type = MoveType.TELEPORT;
} else if (newAngle == null && newLocation != null) {
} else if (newPitch == null && newLocation != null) {
type = MoveType.REL_MOVE;
} else if (newAngle != null && newLocation == null) {
} else if (newPitch != null && newLocation == null) {
type = MoveType.LOOK;
} else if (newAngle != null) {
} else if (newPitch != null) {
type = MoveType.LOOK_AND_REL_MOVE;
}
this.direction = newAngle != null ? newAngle : new Vector();
this.pitch = newPitch != null ? newPitch : 0;
this.yaw = newYaw != null ? newYaw : 0;
this.position = newLocation != null ? newLocation : new Vector();
this.onGround = onGround;
double[] angles = vectorToAngles(this.direction);
pitch = angles[0];
yaw = angles[1];
}

public static EntityPacket unwrap(int entityID, PacketContainer c, Player p) {
Expand All @@ -50,7 +48,7 @@ public static EntityPacket unwrap(int entityID, PacketContainer c, Player p) {
return new EntityMovePacket(
new EntityIdentifier(entityID, p),
c,
vectorFromAngles(c.getBytes().read(1), c.getBytes().read(0)),
c.getBytes().read(1), c.getBytes().read(0),
new Vector(c.getDoubles().read(0), c.getDoubles().read(1), c.getDoubles().read(2)),
c.getBooleans().read(0),
true
Expand All @@ -59,7 +57,7 @@ public static EntityPacket unwrap(int entityID, PacketContainer c, Player p) {
return new EntityMovePacket(
new EntityIdentifier(entityID, p),
c,
null,
null, null,
new Vector(
((double) c.getIntegers().read(1)) / 4096.0,
((double) c.getIntegers().read(2)) / 4096.0,
Expand All @@ -72,7 +70,7 @@ public static EntityPacket unwrap(int entityID, PacketContainer c, Player p) {
return new EntityMovePacket(
new EntityIdentifier(entityID, p),
c,
vectorFromAngles(c.getBytes().read(1), c.getBytes().read(0)),
c.getBytes().read(1), c.getBytes().read(0),
new Vector(
((double) c.getIntegers().read(1)) / 4096.0,
((double) c.getIntegers().read(2)) / 4096.0,
Expand All @@ -85,7 +83,7 @@ public static EntityPacket unwrap(int entityID, PacketContainer c, Player p) {
return new EntityMovePacket(
new EntityIdentifier(entityID, p),
c,
vectorFromAngles(c.getBytes().read(1), c.getBytes().read(0)),
c.getBytes().read(1), c.getBytes().read(0),
null,
c.getBooleans().read(0),
false
Expand Down Expand Up @@ -120,25 +118,13 @@ private static double[] vectorToAngles(Vector v) {

@Override
public Vector getNewDirection() {
return direction;
return vectorFromAngles(pitch, yaw);
}

@Override
public void setNewDirection(Vector direction) {
this.direction = direction;
double[] angles = vectorToAngles(direction);
switch (type) {
case REL_MOVE:
setType(MoveType.LOOK_AND_REL_MOVE);
case LOOK_AND_REL_MOVE:
case LOOK:
case TELEPORT:
super.rawPacket.getBytes().write(1, (byte) angles[0]);
super.rawPacket.getBytes().write(0, (byte) angles[1]);
break;
}
this.pitch = angles[0];
this.yaw = angles[1];
setPitchYaw(angles[0], angles[1]);
}

@Override
Expand All @@ -149,6 +135,7 @@ public Vector getNewPosition() {
@Override
public void setNewPosition(Vector position, boolean teleport) {
this.position = position;
if (position == null) position = new Vector();
switch (type) {
case REL_MOVE:
case LOOK_AND_REL_MOVE:
Expand Down Expand Up @@ -180,7 +167,7 @@ private void setType(MoveType newType) {
super.rawPacket = new PacketContainer(type.getPacketType());
super.rawPacket.getModifier().writeDefaults();
setNewPosition(position, newType == MoveType.TELEPORT);
setNewDirection(direction);
setPitchYaw(pitch, yaw);
setOnGround(onGround);
}

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

@Override
public double[] getPitchYaw() {
return new double[]{pitch, yaw};
public double getPitch() {
return pitch;
}

@Override
public double getYaw() { return yaw; }

@Override
public void setPitchYaw(double pitch, double yaw) {
setNewDirection(vectorFromAngles(pitch, yaw));
switch (type) {
case REL_MOVE:
setType(MoveType.LOOK_AND_REL_MOVE);
case LOOK_AND_REL_MOVE:
case LOOK:
case TELEPORT:
super.rawPacket.getBytes().write(1, (byte) pitch);
super.rawPacket.getBytes().write(0, (byte) yaw);
break;
}
this.pitch = pitch;
this.yaw = yaw;
}

@Override
public Location getLocation(Location currentLocation) {
if (type != MoveType.TELEPORT && position != null) {
return new Location(
currentLocation.getWorld(),
currentLocation.getX() + position.getX(),
currentLocation.getY() + position.getY(),
currentLocation.getZ() + position.getZ(),
(float) yaw, (float) pitch
);
} else if (position == null) {
return new Location(
currentLocation.getWorld(),
currentLocation.getX(),
currentLocation.getY(),
currentLocation.getZ(),
(float) yaw, (float) pitch
);
} else {
return new Location(
currentLocation.getWorld(),
position.getX(),
position.getY(),
position.getZ(),
(float) yaw, (float) pitch
);
}
}

@Override
public void setLocation(Location newLocation, Location currentLocation) {
if (newLocation.distanceSquared(currentLocation) > 64) {
setNewPosition(newLocation.toVector(), true);
} else {
setNewPosition(newLocation.toVector().subtract(currentLocation.toVector()), false);
}
setNewDirection(newLocation.getDirection());
}

@Override
public PacketContainer getRawPacket() {
switch (type) {
case LOOK:
assert direction != null;
break;
case TELEPORT:
case LOOK_AND_REL_MOVE:
assert direction != null;
case REL_MOVE:
assert position != null;
}
Expand All @@ -229,7 +267,7 @@ public PacketContainer getRawPacket() {
public EntityPacket clone() {
EntityMovePacket p = new EntityMovePacket(getIdentifier(), type);
p.setNewPosition(position, type == MoveType.TELEPORT);
p.setNewDirection(direction);
p.setPitchYaw(pitch, yaw);
p.setOnGround(onGround);
return p;
}
Expand Down

0 comments on commit 3f0d04a

Please sign in to comment.