Advertisement
Guest User

Untitled

a guest
Mar 20th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.93 KB | None | 0 0
  1. package org.arios.game.container.access;
  2.  
  3. import org.arios.game.node.entity.player.Player;
  4.  
  5. /**
  6.  * Contains the mask value methods. The 'access mask' is actually just a bit
  7.  * register that contains permissions for a specific interface (@Peterbjornx)
  8.  * @date 5/02/2013
  9.  * @author Stacx
  10.  * @author Emperor
  11.  */
  12. public final class BitregisterAssembler {
  13.  
  14.     /**
  15.      * The register size.
  16.      */
  17.     public static final int SIZE = 32 - 1;
  18.    
  19.     /**
  20.      * Examine option.
  21.      */
  22.     public static final int EXAMINE_OPT = 9;
  23.    
  24.     /**
  25.      * Allow dragging.
  26.      */
  27.     public static final int DRAGABLE = 17;
  28.  
  29.     /**
  30.      * Allow switching item slots.
  31.      */
  32.     public static final int SLOT_SWITCH = 20;
  33.    
  34.     /**
  35.      * The flags.
  36.      */
  37.     private boolean[] permissions = new boolean[SIZE];
  38.  
  39.     /**
  40.      * Constructs a new {@code BitregisterAssembler} {@code Object}.
  41.      * @param permissions The permissions.
  42.      */
  43.     public BitregisterAssembler(int... permissions) {
  44.         for (int i : permissions) {
  45.             this.permissions[i] = true;
  46.         }
  47.     }
  48.    
  49.     /**
  50.      * Constructs a new {@code BitregisterAssembler} {@code Object}.
  51.      * @param permissions The permissions.
  52.      */
  53.     public BitregisterAssembler(String[] options) {
  54.         enableOptions(options);
  55.     }
  56.    
  57.     /**
  58.      * Enables the given options ({@code null} options will remain disabled).
  59.      * @param options The options.
  60.      */
  61.     public void enableOptions(String...options) {
  62.         if (options.length > 9) {
  63.             throw new IllegalStateException("Too many options specified - maximum 9 allowed!");
  64.         }
  65.         for (int i = 0; i < options.length; i++) {
  66.             if (options[i] != null && !options[i].equals("null")) {
  67.                 permissions[i] = true;
  68.             }
  69.         }
  70.     }
  71.    
  72.     /**
  73.      * Enables the examine option.
  74.      */
  75.     public void enableExamineOption() {
  76.         permissions[EXAMINE_OPT] = true;
  77.     }
  78.    
  79.     /**
  80.      * Enables items being dragged.
  81.      */
  82.     public void enableDragging() {
  83.         permissions[DRAGABLE] = true;
  84.     }
  85.    
  86.     /**
  87.      * Enables item switching slots (& dragging).
  88.      */
  89.     public void enableSlotSwitch() {
  90.         enableDragging();
  91.         permissions[SLOT_SWITCH] = true;
  92.     }
  93.    
  94.     /**
  95.      * <b>Send</b> and assemble a bit register for our
  96.      * @param player , the player instance
  97.      * @param interfaceIndex , the interface index
  98.      * @param childIndex , the child index for our interface
  99.      * @param offset , the offset for the loop in client
  100.      * @param length , the length of our loop
  101.      */
  102.     public static void send(Player player, int interfaceIndex, int childIndex, int offset, int length, BitregisterAssembler assembler) {
  103.         if (offset >= length) {
  104.             throw new RuntimeException("Offset cannot excess length. length = " + length);
  105.         }
  106.         player.getPacketDispatch().sendAccessMask(assembler.calculateRegister(), childIndex, interfaceIndex, offset, length);
  107.     }
  108.    
  109.     /**
  110.      * Calculates the register.
  111.      * @return The value.
  112.      */
  113.     public int calculateRegister() {
  114.         int value = 0;
  115.         for (int i = 0; i < SIZE; i++) {
  116.             if (permissions[i]) {
  117.                 value |= 2 << i;
  118.             }
  119.         }
  120.         return value;
  121.     }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement