Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.rs.game.entity.player.inter;
- import java.util.ArrayList;
- import java.util.List;
- import com.rs.game.entity.player.Player;
- import com.rs.utilities.FileUtils;
- /**
- * A class that acts a handler for the RuneScape interfaces that are handled currently.
- *
- * @author Abdallah Wahidi | Aug 22, 2017 : 10:55:17 PM
- */
- public class InterfaceHandler {
- /**
- * A list of stored interfaces.
- */
- private static final List<Interface> INTERFACES = new ArrayList<>();
- static {
- Class<?>[] classes = FileUtils.getClasses("com.rs.game.entity.player.inter.impl");
- for (Class<?> rsInterfaceClass : classes) {
- Object rsInterfaceObject = null;
- try {
- try {
- rsInterfaceObject = rsInterfaceClass.newInstance();
- } catch (InstantiationException | IllegalAccessException e) {
- e.printStackTrace();
- }
- if (!(rsInterfaceObject instanceof Interface)) {
- throw new IllegalStateException("RSInterface mismatch: " + rsInterfaceObject.toString());
- }
- } finally {
- INTERFACES.add((Interface) rsInterfaceObject);
- }
- }
- }
- /**
- * Handles a RuneScape Interface by registering each button click.
- *
- * @param interfaceId The id of the interface.
- *
- * @param buttonId The id of the button.
- *
- * @param slotId The id of the slot.
- *
- * @param itemId The id of the item.
- *
- * @param player The player that uses these interfaces.
- */
- public static void handle(int interfaceId, int buttonId, int slotId, int itemId, int opcode, Player player) {
- Interface inter = getInterface(interfaceId);
- if (inter == null) {
- System.err.println("Unhandled RS Interface - Interface: " + interfaceId + ", Button: " + buttonId + ", Slot: " + slotId + ", Item ID: " + itemId + ", Opcode: " + opcode);
- return;
- }
- try {
- inter.click(interfaceId, buttonId, slotId, itemId, opcode, player);
- } catch (Exception e) {
- System.err.println("Failed handling interface click: " + interfaceId + ", Button: " + buttonId + ", Slot: " + slotId + ", Item ID: " + itemId + ", Opcode: " + opcode);
- e.printStackTrace();
- }
- }
- /**
- * Gets and returns an interface corresponding to the given ID.
- *
- * @param interfaceId The id of the interface.
- *
- * @return the interface
- */
- private static Interface getInterface(int interfaceId) {
- for (Interface inter : INTERFACES) {
- for (int id : inter.getPossibleIds()) {
- if (id == interfaceId) {
- return inter;
- }
- }
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment