Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 20.33 KB | None | 0 0
  1. package net.zaros.util.task.impl;
  2.  
  3. import java.util.Iterator;
  4.  
  5. import net.zaros.GameEngine;
  6. import net.zaros.model.Entity;
  7. import net.zaros.model.Item;
  8. import net.zaros.model.Location;
  9. import net.zaros.model.UpdateFlags;
  10. import net.zaros.model.World;
  11. import net.zaros.model.UpdateFlags.UpdateFlag;
  12. import net.zaros.model.container.Container;
  13. import net.zaros.model.container.Equipment;
  14. import net.zaros.model.container.Equipment.EquipmentType;
  15. import net.zaros.model.player.Appearance;
  16. import net.zaros.model.player.ChatMessage;
  17. import net.zaros.model.player.Player;
  18. import net.zaros.net.packet.Packet;
  19. import net.zaros.net.packet.PacketBuilder;
  20. import net.zaros.net.packet.out.SendMapRegionPacket;
  21. import net.zaros.util.task.Task;
  22.  
  23. /**
  24.  * A task which creates and sends the player update block.
  25.  *
  26.  * @author Graham Edgecombe
  27.  *
  28.  */
  29. public class PlayerUpdateTask implements Task {
  30.  
  31.     /**
  32.      * The player.
  33.      */
  34.     private Player player;
  35.  
  36.     /**
  37.      * Creates an update task.
  38.      *
  39.      * @param player
  40.      *            The player.
  41.      */
  42.     public PlayerUpdateTask(Player player) {
  43.         this.player = player;
  44.     }
  45.  
  46.     @Override
  47.     public void execute(GameEngine context) {
  48.  
  49.         // long startTime = System.currentTimeMillis();
  50.  
  51.         /*
  52.          * If the map region changed send the new one. We do this immediately as
  53.          * the client can begin loading it before the actual packet is received.
  54.          */
  55.         if (player.isMapRegionChanging()) {
  56.             player.write(new SendMapRegionPacket());
  57.         }
  58.  
  59.         /*
  60.          * The update block packet holds update blocks and is send after the
  61.          * main packet.
  62.          */
  63.         PacketBuilder updateBlock = new PacketBuilder();
  64.  
  65.         /*
  66.          * The main packet is written in bits instead of bytes and holds
  67.          * information about the local list, players to add and remove, movement
  68.          * and which updates are required.
  69.          */
  70.         PacketBuilder packet = new PacketBuilder(81, Packet.Type.VARIABLE_SHORT);
  71.         packet.startBitAccess();
  72.  
  73.         /*
  74.          * Updates this player.
  75.          */
  76.         updateThisPlayerMovement(packet);
  77.         updatePlayer(updateBlock, player, false, true);
  78.  
  79.         /*
  80.          * Write the current size of the player list.
  81.          */
  82.         packet.putBits(8, player.getLocalPlayers().size());
  83.  
  84.         /*
  85.          * Iterate through the local player list.
  86.          */
  87.         for (Iterator<Player> it$ = player.getLocalPlayers().iterator(); it$.hasNext();) {
  88.             /*
  89.              * Get the next player.
  90.              */
  91.             Player otherPlayer = it$.next();
  92.  
  93.             /*
  94.              * If the player should still be in our list.
  95.              */
  96.             if (World.getWorld().getPlayers().contains(otherPlayer) && !otherPlayer.isTeleporting() && otherPlayer.getLocation().isWithinDistance(player.getLocation())) {
  97.                 /*
  98.                  * Update the movement.
  99.                  */
  100.                 updatePlayerMovement(packet, otherPlayer);
  101.  
  102.                 /*
  103.                  * Check if an update is required, and if so, send the update.
  104.                  */
  105.                 if (otherPlayer.getUpdateFlags().isUpdateRequired()) {
  106.                     updatePlayer(updateBlock, otherPlayer, false, false);
  107.                 }
  108.             } else {
  109.                 /*
  110.                  * Otherwise, remove the player from the list.
  111.                  */
  112.                 it$.remove();
  113.  
  114.                 /*
  115.                  * Tell the client to remove the player from the list.
  116.                  */
  117.                 packet.putBits(1, 1);
  118.                 packet.putBits(2, 3);
  119.             }
  120.         }
  121.  
  122.         /*
  123.          * Loop through every player.
  124.          */
  125.         for (Player otherPlayer : World.getWorld().getRegionManager().getLocalPlayers(player)) {
  126.             /*
  127.              * Check if there is room left in the local list.
  128.              */
  129.             if (player.getLocalPlayers().size() >= 255) {
  130.                 /*
  131.                  * There is no more room left in the local list. We cannot add
  132.                  * more players, so we just ignore the extra ones. They will be
  133.                  * added as other players get removed.
  134.                  */
  135.                 break;
  136.             }
  137.  
  138.             /*
  139.              * If they should not be added ignore them.
  140.              */
  141.             if (otherPlayer == player || player.getLocalPlayers().contains(otherPlayer)) {
  142.                 continue;
  143.             }
  144.  
  145.             /*
  146.              * Add the player to the local list if it is within distance.
  147.              */
  148.             player.getLocalPlayers().add(otherPlayer);
  149.  
  150.             /*
  151.              * Add the player in the packet.
  152.              */
  153.             addNewPlayer(packet, otherPlayer);
  154.  
  155.             /*
  156.              * Update the player, forcing the appearance flag.
  157.              */
  158.             updatePlayer(updateBlock, otherPlayer, true, false);
  159.         }
  160.  
  161.         /*
  162.          * Check if the update block is not empty.
  163.          */
  164.         if (!updateBlock.isEmpty()) {
  165.             /*
  166.              * Write a magic id indicating an update block follows.
  167.              */
  168.             packet.putBits(11, 2047);
  169.             packet.finishBitAccess();
  170.  
  171.             /*
  172.              * Add the update block at the end of this packet.
  173.              */
  174.             packet.put(updateBlock.toPacket().getPayload());
  175.         } else {
  176.             /*
  177.              * Terminate the packet normally.
  178.              */
  179.             packet.finishBitAccess();
  180.         }
  181.  
  182.         /*
  183.          * Write the packet.
  184.          */
  185.         player.write(packet.toPacket());
  186.  
  187.         // long endTime = System.currentTimeMillis() - startTime;
  188.         // System.out.println(endTime + " : " +
  189.         // World.getWorld().getPlayers().size());
  190.  
  191.     }
  192.  
  193.     /**
  194.      * Updates a non-this player's movement.
  195.      *
  196.      * @param packet
  197.      *            The packet.
  198.      * @param otherPlayer
  199.      *            The player.
  200.      */
  201.     public void updatePlayerMovement(PacketBuilder packet, Player otherPlayer) {
  202.         /*
  203.          * Check which type of movement took place.
  204.          */
  205.         if (otherPlayer.getSprites().getPrimarySprite() == -1) {
  206.             /*
  207.              * If no movement did, check if an update is required.
  208.              */
  209.             if (otherPlayer.getUpdateFlags().isUpdateRequired()) {
  210.                 /*
  211.                  * Signify that an update happened.
  212.                  */
  213.                 packet.putBits(1, 1);
  214.  
  215.                 /*
  216.                  * Signify that there was no movement.
  217.                  */
  218.                 packet.putBits(2, 0);
  219.             } else {
  220.                 /*
  221.                  * Signify that nothing changed.
  222.                  */
  223.                 packet.putBits(1, 0);
  224.             }
  225.         } else if (otherPlayer.getSprites().getSecondarySprite() == -1) {
  226.             /*
  227.              * The player moved but didn't run. Signify that an update is
  228.              * required.
  229.              */
  230.             packet.putBits(1, 1);
  231.  
  232.             /*
  233.              * Signify we moved one tile.
  234.              */
  235.             packet.putBits(2, 1);
  236.  
  237.             /*
  238.              * Write the primary sprite (i.e. walk direction).
  239.              */
  240.             packet.putBits(3, otherPlayer.getSprites().getPrimarySprite());
  241.  
  242.             /*
  243.              * Write a flag indicating if a block update happened.
  244.              */
  245.             packet.putBits(1, otherPlayer.getUpdateFlags().isUpdateRequired() ? 1 : 0);
  246.         } else {
  247.             /*
  248.              * The player ran. Signify that an update happened.
  249.              */
  250.             packet.putBits(1, 1);
  251.  
  252.             /*
  253.              * Signify that we moved two tiles.
  254.              */
  255.             packet.putBits(2, 2);
  256.  
  257.             /*
  258.              * Write the primary sprite (i.e. walk direction).
  259.              */
  260.             packet.putBits(3, otherPlayer.getSprites().getPrimarySprite());
  261.  
  262.             /*
  263.              * Write the secondary sprite (i.e. run direction).
  264.              */
  265.             packet.putBits(3, otherPlayer.getSprites().getSecondarySprite());
  266.  
  267.             /*
  268.              * Write a flag indicating if a block update happened.
  269.              */
  270.             packet.putBits(1, otherPlayer.getUpdateFlags().isUpdateRequired() ? 1 : 0);
  271.         }
  272.     }
  273.  
  274.     /**
  275.      * Adds a new player.
  276.      *
  277.      * @param packet
  278.      *            The packet.
  279.      * @param otherPlayer
  280.      *            The player.
  281.      */
  282.     public void addNewPlayer(PacketBuilder packet, Player otherPlayer) {
  283.         /*
  284.          * Write the player index.
  285.          */
  286.         packet.putBits(11, otherPlayer.getIndex());
  287.  
  288.         /*
  289.          * Write two flags here: the first indicates an update is required (this
  290.          * is always true as we add the appearance after adding a player) and
  291.          * the second to indicate we should discard client-side walk queues.
  292.          */
  293.         packet.putBits(1, 1);
  294.         packet.putBits(1, 1);
  295.  
  296.         /*
  297.          * Calculate the x and y offsets.
  298.          */
  299.         int yPos = otherPlayer.getLocation().getY() - player.getLocation().getY();
  300.         int xPos = otherPlayer.getLocation().getX() - player.getLocation().getX();
  301.  
  302.         /*
  303.          * Write the x and y offsets.
  304.          */
  305.         packet.putBits(5, yPos);
  306.         packet.putBits(5, xPos);
  307.     }
  308.  
  309.     private static void appendHit2Update(final Player p, final PacketBuilder updateBlock) {
  310.         updateBlock.put((byte) p.getUpdateFlags().getDamage2());
  311.         updateBlock.putByteS((byte) p.getUpdateFlags().getHitType2());
  312.         updateBlock.put((byte) p.getSkills().getLevel(3));
  313.         updateBlock.putByteC(p.getSkills().getLevelForExperience(3));
  314.     }
  315.  
  316.     private static void appendHitUpdate(final Player p, final PacketBuilder updateBlock) {
  317.         updateBlock.put((byte) p.getUpdateFlags().getDamage1());
  318.         updateBlock.putByteA(p.getUpdateFlags().getHitType1());
  319.         updateBlock.putByteC(p.getSkills().getLevel(3));
  320.         updateBlock.put((byte) p.getSkills().getLevelForExperience(3));
  321.     }
  322.  
  323.     /**
  324.      * Updates a player.
  325.      *
  326.      * @param packet
  327.      *            The packet.
  328.      * @param otherPlayer
  329.      *            The other player.
  330.      * @param forceAppearance
  331.      *            The force appearance flag.
  332.      * @param noChat
  333.      *            Indicates chat should not be relayed to this player.
  334.      */
  335.     public void updatePlayer(PacketBuilder packet, Player otherPlayer, boolean forceAppearance, boolean noChat) {
  336.         /*
  337.          * If no update is required and we don't have to force an appearance
  338.          * update, don't write anything.
  339.          */
  340.         if (!otherPlayer.getUpdateFlags().isUpdateRequired() && !forceAppearance) {
  341.             return;
  342.         }
  343.  
  344.         /*
  345.          * We can used the cached update block!
  346.          */
  347.         synchronized (otherPlayer) {
  348.             if (otherPlayer.hasCachedUpdateBlock() && otherPlayer != player && !forceAppearance && !noChat) {
  349.                 packet.put(otherPlayer.getCachedUpdateBlock().getPayload().flip());
  350.                 return;
  351.             }
  352.  
  353.             /*
  354.              * We have to construct and cache our own block.
  355.              */
  356.             PacketBuilder block = new PacketBuilder();
  357.  
  358.             /*
  359.              * Calculate the bitmask.
  360.              */
  361.             int mask = 0;
  362.             final UpdateFlags flags = otherPlayer.getUpdateFlags();
  363.  
  364.             // TODO mask 0x400
  365.             if (flags.get(UpdateFlag.GRAPHICS)) {
  366.                 mask |= 0x100;
  367.             }
  368.             if (flags.get(UpdateFlag.ANIMATION)) {
  369.                 mask |= 0x8;
  370.             }
  371.             if (flags.get(UpdateFlag.FORCED_CHAT)) {
  372.                 mask |= 0x4;
  373.             }
  374.             if (flags.get(UpdateFlag.CHAT) && !noChat) {
  375.                 mask |= 0x80;
  376.             }
  377.             if (flags.get(UpdateFlag.FACE_ENTITY)) {
  378.                 mask |= 0x1;
  379.             }
  380.             if (flags.get(UpdateFlag.APPEARANCE) || forceAppearance) {
  381.                 mask |= 0x10;
  382.             }
  383.             if (flags.get(UpdateFlag.FACE_COORDINATE)) {
  384.                 mask |= 0x2;
  385.             }
  386.             if (flags.get(UpdateFlag.HIT)) {
  387.                 mask |= 0x20;
  388.             }
  389.             if (flags.get(UpdateFlag.HIT_2)) {
  390.                 mask |= 0x200;
  391.             }
  392.  
  393.             /*
  394.              * Check if the bitmask would overflow a byte.
  395.              */
  396.             if (mask >= 0x100) {
  397.                 /*
  398.                  * Write it as a short and indicate we have done so.
  399.                  */
  400.                 mask |= 0x40;
  401.                 block.put((byte) (mask & 0xFF));
  402.                 block.put((byte) (mask >> 8));
  403.             } else {
  404.                 /*
  405.                  * Write it as a byte.
  406.                  */
  407.                 block.put((byte) (mask));
  408.             }
  409.  
  410.             /*
  411.              * Append the appropriate updates.
  412.              */
  413.             if (flags.get(UpdateFlag.GRAPHICS)) {
  414.                 appendGraphicsUpdate(block, otherPlayer);
  415.             }
  416.             if (flags.get(UpdateFlag.ANIMATION)) {
  417.                 appendAnimationUpdate(block, otherPlayer);
  418.             }
  419.             if (flags.get(UpdateFlag.FORCED_CHAT)) {
  420.                 packet.putRS2String(otherPlayer.getUpdateFlags().getForcedMessage());
  421.             }
  422.             if (flags.get(UpdateFlag.CHAT) && !noChat) {
  423.                 appendChatUpdate(block, otherPlayer);
  424.             }
  425.             if (flags.get(UpdateFlag.FACE_ENTITY)) {
  426.                 Entity entity = otherPlayer.getInteractingEntity();
  427.                 block.putLEShort(entity == null ? -1 : entity.getClientIndex());
  428.             }
  429.             if (flags.get(UpdateFlag.APPEARANCE) || forceAppearance) {
  430.                 appendPlayerAppearanceUpdate(block, otherPlayer);
  431.             }
  432.             if (flags.get(UpdateFlag.FACE_COORDINATE)) {
  433.                 Location loc = otherPlayer.getFaceLocation();
  434.                 if (loc == null) {
  435.                     block.putLEShortA(0);
  436.                     block.putLEShort(0);
  437.                 } else {
  438.                     block.putLEShortA(loc.getX() * 2 + 1);
  439.                     block.putLEShort(loc.getY() * 2 + 1);
  440.                 }
  441.             }
  442.             if (flags.get(UpdateFlag.HIT)) {
  443.                 appendHitUpdate(otherPlayer, block);
  444.             }
  445.             if (flags.get(UpdateFlag.HIT_2)) {
  446.                 appendHit2Update(otherPlayer, block);
  447.             }
  448.  
  449.             /*
  450.              * Convert the block builder to a packet.
  451.              */
  452.             Packet blockPacket = block.toPacket();
  453.  
  454.             /*
  455.              * Now it is over, cache the block if we can.
  456.              */
  457.             if (otherPlayer != player && !forceAppearance && !noChat) {
  458.                 otherPlayer.setCachedUpdateBlock(blockPacket);
  459.             }
  460.  
  461.             /*
  462.              * And finally append the block at the end.
  463.              */
  464.             packet.put(blockPacket.getPayload());
  465.         }
  466.     }
  467.  
  468.     /**
  469.      * Appends an animation update.
  470.      *
  471.      * @param block
  472.      *            The update block.
  473.      * @param otherPlayer
  474.      *            The player.
  475.      */
  476.     private void appendAnimationUpdate(PacketBuilder block, Player otherPlayer) {
  477.         block.putLEShort(otherPlayer.getCurrentAnimation().getId());
  478.         block.putByteC(otherPlayer.getCurrentAnimation().getDelay());
  479.     }
  480.  
  481.     /**
  482.      * Appends a graphics update.
  483.      *
  484.      * @param block
  485.      *            The update block.
  486.      * @param otherPlayer
  487.      *            The player.
  488.      */
  489.     private void appendGraphicsUpdate(PacketBuilder block, Player otherPlayer) {
  490.         block.putLEShort(otherPlayer.getCurrentGraphic().getId());
  491.         block.putInt(otherPlayer.getCurrentGraphic().getDelay() | otherPlayer.getCurrentGraphic().getHeight() << 16);
  492.     }
  493.  
  494.     /**
  495.      * Appends a chat text update.
  496.      *
  497.      * @param packet
  498.      *            The packet.
  499.      * @param otherPlayer
  500.      *            The player.
  501.      */
  502.     private void appendChatUpdate(PacketBuilder packet, Player otherPlayer) {
  503.         ChatMessage cm = otherPlayer.getCurrentChatMessage();
  504.  
  505.         byte[] bytes = cm.getText();
  506.  
  507.         packet.putLEShort(((cm.getColour() & 0xFF) << 8) | (cm.getEffects() & 0xFF));
  508.         packet.put((byte) otherPlayer.getRights().toInteger());
  509.         packet.putByteC(bytes.length);
  510.         for (int ptr = bytes.length - 1; ptr >= 0; ptr--) {
  511.             packet.put(bytes[ptr]);
  512.         }
  513.     }
  514.  
  515.     /**
  516.      * Appends an appearance update.
  517.      *
  518.      * @param packet
  519.      *            The packet.
  520.      * @param otherPlayer
  521.      *            The player.
  522.      */
  523.     private void appendPlayerAppearanceUpdate(PacketBuilder packet, Player otherPlayer) {
  524.         Appearance app = otherPlayer.getAppearance();
  525.         Container eq = otherPlayer.getEquipment();
  526.         PacketBuilder playerProps = new PacketBuilder();
  527.         playerProps.put((byte) app.getAppearance(Appearance.GENDER)); // gender
  528.         playerProps.put((byte) otherPlayer.getPrayerIcon()); // prayer icon
  529.         playerProps.put((byte) -1); // skull icon
  530.         //playerProps.put((byte) -1); // hint icon
  531.  
  532.         for (int i = 0; i < 4; i++) {
  533.             if (eq.isSlotUsed(i)) {
  534.                 playerProps.putShort((short) 0x200 + eq.get(i).getId());
  535.             } else {
  536.                 playerProps.put((byte) 0);
  537.             }
  538.         }
  539.         if (eq.isSlotUsed(Equipment.SLOT_CHEST)) {
  540.             playerProps.putShort((short) 0x200 + eq.get(Equipment.SLOT_CHEST).getId());
  541.         } else {
  542.             playerProps.putShort((short) 0x100 + app.getAppearance(Appearance.TORSO)); // chest
  543.         }
  544.         if (eq.isSlotUsed(Equipment.SLOT_SHIELD)) {
  545.             playerProps.putShort((short) 0x200 + eq.get(Equipment.SLOT_SHIELD).getId());
  546.         } else {
  547.             playerProps.put((byte) 0);
  548.         }
  549.         Item chest = eq.get(Equipment.SLOT_CHEST);
  550.         if (chest != null) {
  551.             if (!Equipment.is(EquipmentType.PLATEBODY, chest)) {
  552.                 playerProps.putShort((short) 0x100 + app.getAppearance(Appearance.ARMS));
  553.             } else {
  554.                 playerProps.putShort((short) 0x200 + chest.getId());
  555.             }
  556.         } else {
  557.             playerProps.putShort((short) 0x100 + app.getAppearance(Appearance.ARMS));
  558.         }
  559.         if (eq.isSlotUsed(Equipment.SLOT_BOTTOMS)) {
  560.             playerProps.putShort((short) 0x200 + eq.get(Equipment.SLOT_BOTTOMS).getId());
  561.         } else {
  562.             playerProps.putShort((short) 0x100 + app.getAppearance(Appearance.LEGS));
  563.         }
  564.         Item helm = eq.get(Equipment.SLOT_HELM);
  565.         if (helm != null) {
  566.             if (!Equipment.is(EquipmentType.FULL_HELM, helm) && !Equipment.is(EquipmentType.FULL_MASK, helm)) {
  567.                 playerProps.putShort((short) 0x100 + app.getAppearance(Appearance.HEAD));
  568.             } else {
  569.                 playerProps.put((byte) 0);
  570.             }
  571.         } else {
  572.             playerProps.putShort((short) 0x100 + app.getAppearance(Appearance.HEAD));
  573.         }
  574.         if (eq.isSlotUsed(Equipment.SLOT_GLOVES)) {
  575.             playerProps.putShort((short) 0x200 + eq.get(Equipment.SLOT_GLOVES).getId());
  576.         } else {
  577.             playerProps.putShort((short) 0x100 + app.getAppearance(Appearance.HANDS));
  578.         }
  579.         if (eq.isSlotUsed(Equipment.SLOT_BOOTS)) {
  580.             playerProps.putShort((short) 0x200 + eq.get(Equipment.SLOT_BOOTS).getId());
  581.         } else {
  582.             playerProps.putShort((short) 0x100 + app.getAppearance(Appearance.FEET));
  583.         }
  584.         boolean fullHelm = false;
  585.         if (helm != null) {
  586.             fullHelm = !Equipment.is(EquipmentType.FULL_HELM, helm);
  587.         }
  588.         if (fullHelm || app.getAppearance(Appearance.GENDER) == 1) {
  589.             playerProps.put((byte) 0);
  590.         } else {
  591.             playerProps.putShort((short) 0x100 + app.getAppearance(Appearance.JAW));
  592.         }
  593.         playerProps.put((byte) app.getAppearance(Appearance.HAIR_COLOR)); // hairc
  594.         playerProps.put((byte) app.getAppearance(Appearance.TORSO_COLOR)); // torsoc
  595.         playerProps.put((byte) app.getAppearance(Appearance.LEGS_COLOR)); // legc
  596.         playerProps.put((byte) app.getAppearance(Appearance.FEET_COLOR)); // feetc
  597.         playerProps.put((byte) app.getAppearance(Appearance.SKIN_COLOR)); // skinc
  598.  
  599.         playerProps.putShort(otherPlayer.getAnimations().getStandEmote()); // stand
  600.         playerProps.putShort(otherPlayer.getAnimations().getStandTurnEmote()); // stand turn
  601.         playerProps.putShort(otherPlayer.getAnimations().getWalkEmote()); // walk
  602.         playerProps.putShort(otherPlayer.getAnimations().getTurn180Emote()); // turn 180
  603.         playerProps.putShort(otherPlayer.getAnimations().getTurn90CWEmote()); // turn 90 cw
  604.         playerProps.putShort(otherPlayer.getAnimations().getTurn90CCWEmote()); // turn 90 ccw
  605.         playerProps.putShort(otherPlayer.getAnimations().getRunEmote()); // run
  606.  
  607.         playerProps.putLong(otherPlayer.getNameAsLong()); // player name
  608.         playerProps.put((byte) otherPlayer.getSkills().getCombatLevel());
  609.         playerProps.putShort(0);
  610.  
  611.         Packet propsPacket = playerProps.toPacket();
  612.  
  613.         packet.putByteC(propsPacket.getLength());
  614.         packet.put(propsPacket.getPayload());
  615.  
  616.     }
  617.  
  618.     /**
  619.      * Updates this player's movement.
  620.      *
  621.      * @param packet
  622.      *            The packet.
  623.      */
  624.     private void updateThisPlayerMovement(PacketBuilder packet) {
  625.         /*
  626.          * Check if the player is teleporting.
  627.          */
  628.         if (player.isTeleporting() || player.isMapRegionChanging()) {
  629.             /*
  630.              * They are, so an update is required.
  631.              */
  632.             packet.putBits(1, 1);
  633.  
  634.             /*
  635.              * This value indicates the player teleported.
  636.              */
  637.             packet.putBits(2, 3);
  638.  
  639.             /*
  640.              * This is the new player height.
  641.              */
  642.             packet.putBits(2, player.getLocation().getZ());
  643.  
  644.             /*
  645.              * This indicates that the client should discard the walking queue.
  646.              */
  647.             packet.putBits(1, player.isTeleporting() ? 1 : 0);
  648.  
  649.             /*
  650.              * This flag indicates if an update block is appended.
  651.              */
  652.             packet.putBits(1, player.getUpdateFlags().isUpdateRequired() ? 1 : 0);
  653.  
  654.             /*
  655.              * These are the positions.
  656.              */
  657.             packet.putBits(7, player.getLocation().getLocalY(player.getLastKnownRegion()));
  658.             packet.putBits(7, player.getLocation().getLocalX(player.getLastKnownRegion()));
  659.         } else {
  660.             /*
  661.              * Otherwise, check if the player moved.
  662.              */
  663.             if (player.getSprites().getPrimarySprite() == -1) {
  664.                 /*
  665.                  * The player didn't move. Check if an update is required.
  666.                  */
  667.                 if (player.getUpdateFlags().isUpdateRequired()) {
  668.                     /*
  669.                      * Signifies an update is required.
  670.                      */
  671.                     packet.putBits(1, 1);
  672.  
  673.                     /*
  674.                      * But signifies that we didn't move.
  675.                      */
  676.                     packet.putBits(2, 0);
  677.                 } else {
  678.                     /*
  679.                      * Signifies that nothing changed.
  680.                      */
  681.                     packet.putBits(1, 0);
  682.                 }
  683.             } else {
  684.                 /*
  685.                  * Check if the player was running.
  686.                  */
  687.                 if (player.getSprites().getSecondarySprite() == -1) {
  688.                     /*
  689.                      * The player walked, an update is required.
  690.                      */
  691.                     packet.putBits(1, 1);
  692.  
  693.                     /*
  694.                      * This indicates the player only walked.
  695.                      */
  696.                     packet.putBits(2, 1);
  697.  
  698.                     /*
  699.                      * This is the player's walking direction.
  700.                      */
  701.                     packet.putBits(3, player.getSprites().getPrimarySprite());
  702.  
  703.                     /*
  704.                      * This flag indicates an update block is appended.
  705.                      */
  706.                     packet.putBits(1, player.getUpdateFlags().isUpdateRequired() ? 1 : 0);
  707.                 } else {
  708.                     /*
  709.                      * The player ran, so an update is required.
  710.                      */
  711.                     packet.putBits(1, 1);
  712.  
  713.                     /*
  714.                      * This indicates the player ran.
  715.                      */
  716.                     packet.putBits(2, 2);
  717.  
  718.                     /*
  719.                      * This is the walking direction.
  720.                      */
  721.                     packet.putBits(3, player.getSprites().getPrimarySprite());
  722.  
  723.                     /*
  724.                      * And this is the running direction.
  725.                      */
  726.                     packet.putBits(3, player.getSprites().getSecondarySprite());
  727.  
  728.                     /*
  729.                      * And this flag indicates an update block is appended.
  730.                      */
  731.                     packet.putBits(1, player.getUpdateFlags().isUpdateRequired() ? 1 : 0);
  732.                 }
  733.             }
  734.         }
  735.     }
  736. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement