Abdallah_Wahidi

Untitled

Aug 22nd, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.47 KB | None | 0 0
  1. package com.rs.game.entity.player.inter;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import com.rs.game.entity.player.Player;
  7. import com.rs.utilities.FileUtils;
  8.  
  9. /**
  10.  * A class that acts a handler for the RuneScape interfaces that are handled currently.
  11.  *
  12.  * @author Abdallah Wahidi | Aug 22, 2017 : 10:55:17 PM
  13.  */
  14. public class InterfaceHandler {
  15.  
  16.     /**
  17.      * A list of stored interfaces.
  18.      */
  19.     private static final List<Interface> INTERFACES = new ArrayList<>();
  20.  
  21.     static {
  22.         Class<?>[] classes = FileUtils.getClasses("com.rs.game.entity.player.inter.impl");
  23.         for (Class<?> rsInterfaceClass : classes) {
  24.             Object rsInterfaceObject = null;
  25.             try {
  26.                 try {
  27.                     rsInterfaceObject = rsInterfaceClass.newInstance();
  28.                 } catch (InstantiationException | IllegalAccessException e) {
  29.                     e.printStackTrace();
  30.                 }
  31.                 if (!(rsInterfaceObject instanceof Interface)) {
  32.                     throw new IllegalStateException("RSInterface mismatch: " + rsInterfaceObject.toString());
  33.                 }
  34.             } finally {
  35.                 INTERFACES.add((Interface) rsInterfaceObject);
  36.             }
  37.         }
  38.     }
  39.  
  40.     /**
  41.      * Handles a RuneScape Interface by registering each button click.
  42.      *
  43.      * @param interfaceId The id of the interface.
  44.      *
  45.      * @param buttonId The id of the button.
  46.      *
  47.      * @param slotId The id of the slot.
  48.      *
  49.      * @param itemId The id of the item.
  50.      *
  51.      * @param player The player that uses these interfaces.
  52.      */
  53.     public static void handle(int interfaceId, int buttonId, int slotId, int itemId, int opcode, Player player) {
  54.         Interface inter = getInterface(interfaceId);
  55.         if (inter == null) {
  56.             System.err.println("Unhandled RS Interface - Interface: " + interfaceId + ", Button: " + buttonId + ", Slot: " + slotId + ", Item ID: " + itemId + ", Opcode: " + opcode);
  57.             return;
  58.         }
  59.         try {
  60.             inter.click(interfaceId, buttonId, slotId, itemId, opcode, player);
  61.         } catch (Exception e) {
  62.             System.err.println("Failed handling interface click: " + interfaceId + ", Button: " + buttonId + ", Slot: " + slotId + ", Item ID: " + itemId + ", Opcode: " + opcode);
  63.             e.printStackTrace();
  64.         }
  65.     }
  66.  
  67.     /**
  68.      * Gets and returns an interface corresponding to the given ID.
  69.      *
  70.      * @param interfaceId The id of the interface.
  71.      *
  72.      * @return the interface
  73.      */
  74.     private static Interface getInterface(int interfaceId) {
  75.         for (Interface inter : INTERFACES) {
  76.             for (int id : inter.getPossibleIds()) {
  77.                 if (id == interfaceId) {
  78.                     return inter;
  79.                 }
  80.             }
  81.         }
  82.         return null;
  83.     }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment