I think the picture explains but the problem is, on the account logging in I don't see the person already in, but on account that is logged in I see the dude logging in.
Confusion
Upto date code:
Code:
public static void update(Player p) {
StaticPacketBuilder mainPacket = new StaticPacketBuilder().setId(81)
.setSize(Size.VariableShort).initBitAccess();
StaticPacketBuilder updateBlock = new StaticPacketBuilder().setBare(true);
updateThisPlayerMovement(p, mainPacket);
if(p.getUpdateFlags().isUpdateRequired()) {
appendUpdateBlock(p, updateBlock, false);
}
mainPacket.addBits(8, p.getPlayerListSize());
int size = p.getPlayerListSize();
p.setPlayerListSize(0);//Reset size and rebuild it straight away
boolean[] newPlayer = new boolean[Constants.PLAYER_CAP];
for (int i = 0; i < size; i ++) {
if(p.getPlayerList()[i] == null || //If player is null
p.getPlayerList()[i].isDisconnected() || //If player disconnected
!p.getPlayerList()[i].getLocation().withinDistance(p.getLocation()) ||
//If player isn't in distance
p.getPlayerList()[i].getUpdateFlags().didTeleport()) { //If player teleported
if(p.getPlayerList()[i] != null) { //If player isn't null anymore
p.getPlayersInList()[p.getPlayerList()[i].getIndex()] = 0;//Set the player index to 0?
}
mainPacket.addBits(1, 1);//1 bit of data
mainPacket.addBits(2, 3);//3 bits of data
} else {
updatePlayerMovement(p.getPlayerList()[i], mainPacket);
p.getPlayerList()[p.getPlayerListSize()] = p.getPlayerList()[i];
p.setPlayerListSize(p.getPlayerListSize()+1);
}
}
for(Player p2 : World.getInstance().getPlayerList()) {
if(p2 == null || p2.getIndex() == p.getIndex()) {
continue;
}
if(p.getPlayersInList()[p2.getIndex()] == 1 || !p2.getLocation().withinDistance(p.getLocation())) {
continue;
}
newPlayer[p.getPlayerListSize()] = true;
addNewPlayer(p, p2, mainPacket, updateBlock);
}
if(updateBlock.getLength() > 0) {
mainPacket.addBits(11, 2047);
}
mainPacket.finishBitAccess();
if(updateBlock.getLength() > 0) {
mainPacket.addBytes(updateBlock.toPacket().getData());
}
p.getSession().write(mainPacket.toPacket());
}
private static void addNewPlayer(Player p, Player p2, StaticPacketBuilder mainPacket, StaticPacketBuilder updateBlock) {
p.getPlayersInList()[p2.getIndex()] = 1;
p.getPlayerList()[p.getPlayerListSize()] = p2;
p.setPlayerListSize(p.getPlayerListSize()+1);
mainPacket.addBits(11, p2.getIndex());
int yPos = p2.getLocation().getY() - p.getLocation().getY();
int xPos = p2.getLocation().getX() - p.getLocation().getX();
if(xPos < 0) {
xPos += 32;
}
if(yPos < 0) {
yPos += 32;
}
mainPacket.addBits(1, 1);
appendUpdateBlock(p2, updateBlock, false);
mainPacket.addBits(1, 1);
mainPacket.addBits(5, yPos).addBits(5, xPos);
}
private static void updatePlayerMovement(Player p, StaticPacketBuilder mainPacket) {
if (p.getSprites().getPrimarySprite() == -1) { //No movement.
if(p.getUpdateFlags().isUpdateRequired() ||
p.getUpdateFlags().isChatTextUpdateRequired()) {
mainPacket.addBits(1, 1);
mainPacket.addBits(2, 0);
} else {
mainPacket.addBits(1, 0);
}
} else if(p.getSprites().getSecondarySprite() == -1) { //Walking
mainPacket.addBits(1, 1);
mainPacket.addBits(2, 1);
mainPacket.addBits(3, p.getSprites().getPrimarySprite());
mainPacket.addBits(1, p.getUpdateFlags().isUpdateRequired() || p.getUpdateFlags().isChatTextUpdateRequired() ? 1 : 0);
} else { //Running
mainPacket.addBits(1, 1);
mainPacket.addBits(2, 2);
mainPacket.addBits(3, p.getSprites().getPrimarySprite());
mainPacket.addBits(3, p.getSprites().getSecondarySprite());
mainPacket.addBits(1, p.getUpdateFlags().isUpdateRequired() || p.getUpdateFlags().isChatTextUpdateRequired() ? 1 : 0);
}
}
and :
Code:
private static void updateThisPlayerMovement(Player p, StaticPacketBuilder mainPacket) {
if(p.getUpdateFlags().didMapRegionChange()) {
p.getActionSender().sendMapRegion();
}
if (p.getUpdateFlags().didTeleport()) {
mainPacket.addBits(1, 1);
mainPacket.addBits(2, 3); //Update type 3
mainPacket.addBits(2, p.getLocation().getZ()); //Height
mainPacket.addBits(1, 1); //Discard walking queue.
mainPacket.addBits(1, p.getUpdateFlags().isUpdateRequired() ? 1 : 0); //Updates required
mainPacket.addBits(7, p.getLocation().getLocalY()); //Local Y
mainPacket.addBits(7, p.getLocation().getLocalX()); //Local X
} else {
if(p.getSprites().getPrimarySprite() == -1) {
mainPacket.addBits(1, p.getUpdateFlags().isUpdateRequired() ? 1 : 0);
if(p.getUpdateFlags().isUpdateRequired()) {
mainPacket.addBits(2, 0);
}
} else {
if(p.getSprites().getSecondarySprite() != -1) {
mainPacket.addBits(1, 1);
mainPacket.addBits(2, 2);
mainPacket.addBits(3, p.getSprites().getPrimarySprite());
mainPacket.addBits(3, p.getSprites().getSecondarySprite());
mainPacket.addBits(1, p.getUpdateFlags().isUpdateRequired() ? 1 : 0);
} else {
mainPacket.addBits(1, 1);
mainPacket.addBits(2, 1);
mainPacket.addBits(3, p.getSprites().getPrimarySprite());
mainPacket.addBits(1, p.getUpdateFlags().isUpdateRequired() ? 1 : 0);
}
}
}
}