Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.27 KB | None | 0 0
  1. package com.rs.game.player.dialogues;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Collections;
  5. import java.util.Iterator;
  6. import java.util.List;
  7.  
  8. import com.rs.cache.loaders.ItemDefinitions;
  9. import com.rs.cache.loaders.NPCDefinitions;
  10. import com.rs.game.player.Player;
  11.  
  12. /*
  13.  * Tweaks, few improvements and bug fixes @NoContinueDialogues
  14.  * Credits: Tristam - Project Rain
  15.  */
  16.  
  17. public abstract class Dialogue {
  18.  
  19.     protected Player player;
  20.     protected byte stage = -1;
  21.     protected final int END = -1;
  22.     protected String stageName;
  23.  
  24.     public Dialogue() {
  25.  
  26.     }
  27.  
  28.     public Object[] parameters;
  29.  
  30.     public void setPlayer(Player player) {
  31.         this.player = player;
  32.     }
  33.  
  34.     public abstract void start();
  35.  
  36.     public abstract void run(int interfaceId, int componentId);
  37.  
  38.     public abstract void finish();
  39.  
  40.     protected final void end() {
  41.         player.getDialogueManager().finishDialogue();
  42.     }
  43.  
  44.     protected static final short SEND_1_TEXT_INFO = 210;
  45.     protected static final short SEND_2_TEXT_INFO = 211;
  46.     protected static final short SEND_3_TEXT_INFO = 212;
  47.     protected static final short SEND_4_TEXT_INFO = 213;
  48.     protected static final String SEND_DEFAULT_OPTIONS_TITLE = "Select an Option", TITLE = "Select An Option";
  49.     protected static final short SEND_2_OPTIONS = 236;
  50.     protected static final short SEND_3_OPTIONS = 230;
  51.     protected static final short SEND_4_OPTIONS = 237;
  52.     protected static final short SEND_5_OPTIONS = 238;
  53.     protected static final short SEND_2_LARGE_OPTIONS = 229;
  54.     protected static final short SEND_3_LARGE_OPTIONS = 231;
  55.     protected static final short SEND_1_TEXT_CHAT = 241;
  56.     protected static final short SEND_2_TEXT_CHAT = 242;
  57.     protected static final short SEND_3_TEXT_CHAT = 243;
  58.     protected static final short SEND_4_TEXT_CHAT = 244;
  59.     protected static final short SEND_NO_CONTINUE_1_TEXT_CHAT = 245;
  60.     protected static final short SEND_NO_CONTINUE_2_TEXT_CHAT = 246;
  61.     protected static final short SEND_NO_CONTINUE_3_TEXT_CHAT = 247;
  62.     protected static final short SEND_NO_CONTINUE_4_TEXT_CHAT = 248;
  63.     protected static final short SEND_NO_EMOTE = -1;
  64.     protected final static short SEND_ITEM_DIALOGUE = 1189;
  65.     protected static final byte IS_NOTHING = -1;
  66.     protected static final byte IS_PLAYER = 0;
  67.     protected static final byte IS_NPC = 1;
  68.     protected static final byte IS_ITEM = 2;
  69.     protected static final byte IS_ITEM_WITH_SIZE = 3;
  70.     protected static final byte DOUBLE_ITEM_SELECTION = 4;
  71.  
  72.     protected final int Listening = 9804, Plain = 9808, Snobby = 9832, Unsure = 9836, ListenAndLaugh = 9840,
  73.             Swaying = 9844, Normal = 9847, Laughing = 9851, Sad = 9760, Crying = 9765, Why = 9776, Angry = 9788,
  74.             Furious = 9792, Thinking = 9827, Happy = 9850;
  75.  
  76.     private static int[] getIComponentsIds(short interId) {
  77.         int[] childOptions;
  78.         switch (interId) {
  79.         case SEND_1_TEXT_INFO:
  80.             childOptions = new int[1];
  81.             childOptions[0] = 1;
  82.             break;
  83.         case SEND_ITEM_DIALOGUE:
  84.             childOptions = new int[2];
  85.             childOptions[0] = 5;
  86.             childOptions[1] = 4;
  87.             break;
  88.         case SEND_2_TEXT_INFO:
  89.             childOptions = new int[2];
  90.             childOptions[0] = 1;
  91.             childOptions[1] = 2;
  92.             break;
  93.         case SEND_3_TEXT_INFO:
  94.             childOptions = new int[3];
  95.             childOptions[0] = 1;
  96.             childOptions[1] = 2;
  97.             childOptions[2] = 3;
  98.             break;
  99.         case SEND_4_TEXT_INFO:
  100.             childOptions = new int[4];
  101.             childOptions[0] = 1;
  102.             childOptions[1] = 2;
  103.             childOptions[2] = 3;
  104.             childOptions[3] = 4;
  105.             break;
  106.         case SEND_2_LARGE_OPTIONS:
  107.             childOptions = new int[3];
  108.             childOptions[0] = 1;
  109.             childOptions[1] = 2;
  110.             childOptions[2] = 3;
  111.             break;
  112.         case SEND_3_LARGE_OPTIONS:
  113.             childOptions = new int[4];
  114.             childOptions[0] = 1;
  115.             childOptions[1] = 2;
  116.             childOptions[2] = 3;
  117.             childOptions[3] = 4;
  118.             break;
  119.         case SEND_2_OPTIONS:
  120.             childOptions = new int[3];
  121.             childOptions[0] = 0;
  122.             childOptions[1] = 1;
  123.             childOptions[2] = 2;
  124.             break;
  125.         case SEND_3_OPTIONS:
  126.             childOptions = new int[4];
  127.             childOptions[0] = 1;
  128.             childOptions[1] = 2;
  129.             childOptions[2] = 3;
  130.             childOptions[3] = 4;
  131.             break;
  132.         case SEND_4_OPTIONS:
  133.             childOptions = new int[5];
  134.             childOptions[0] = 0;
  135.             childOptions[1] = 1;
  136.             childOptions[2] = 2;
  137.             childOptions[3] = 3;
  138.             childOptions[4] = 4;
  139.             break;
  140.         case SEND_5_OPTIONS:
  141.             childOptions = new int[6];
  142.             childOptions[0] = 0;
  143.             childOptions[1] = 1;
  144.             childOptions[2] = 2;
  145.             childOptions[3] = 3;
  146.             childOptions[4] = 4;
  147.             childOptions[5] = 5;
  148.             break;
  149.         case SEND_1_TEXT_CHAT:
  150.         case SEND_NO_CONTINUE_1_TEXT_CHAT:
  151.             childOptions = new int[2];
  152.             childOptions[0] = 3;
  153.             childOptions[1] = 4;
  154.             break;
  155.         case SEND_2_TEXT_CHAT:
  156.         case SEND_NO_CONTINUE_2_TEXT_CHAT:
  157.             childOptions = new int[3];
  158.             childOptions[0] = 3;
  159.             childOptions[1] = 4;
  160.             childOptions[2] = 5;
  161.             break;
  162.         case SEND_3_TEXT_CHAT:
  163.         case SEND_NO_CONTINUE_3_TEXT_CHAT:
  164.             childOptions = new int[4];
  165.             childOptions[0] = 3;
  166.             childOptions[1] = 4;
  167.             childOptions[2] = 5;
  168.             childOptions[3] = 6;
  169.             break;
  170.         case SEND_4_TEXT_CHAT:
  171.         case SEND_NO_CONTINUE_4_TEXT_CHAT:
  172.             childOptions = new int[5];
  173.             childOptions[0] = 3;
  174.             childOptions[1] = 4;
  175.             childOptions[2] = 5;
  176.             childOptions[3] = 6;
  177.             childOptions[4] = 7;
  178.             break;
  179.         default:
  180.             return null;
  181.         }
  182.         return childOptions;
  183.     }
  184.  
  185.     public boolean sendNPCDialogue(int npcId, int animationId, String... text) {
  186.         return sendEntityDialogue(IS_NPC, npcId, animationId, text);
  187.     }
  188.  
  189.     public boolean sendPlayerDialogue(int animationId, String... text) {
  190.         return sendEntityDialogue(IS_PLAYER, -1, animationId, text);
  191.     }
  192.  
  193.     public boolean sendEntityDialogue(int type, int entityId, int animationId, String... text) {
  194.         String title = "";
  195.         if (type == IS_PLAYER) {
  196.             title = player.getDisplayName();
  197.         } else if (type == IS_NPC) {
  198.             title = NPCDefinitions.getNPCDefinitions(entityId).name;
  199.         } else if (type == IS_ITEM)
  200.             title = ItemDefinitions.getItemDefinitions(entityId).getName();
  201.         return sendEntityDialogue(type, title, entityId, animationId, text);
  202.     }
  203.  
  204.     public int getP() {
  205.         return 1;
  206.     }
  207.  
  208.     public static final int OPTION_1 = 11, OPTION_2 = 13, OPTION_3 = 14, OPTION_4 = 15, OPTION_5 = 16;
  209.  
  210.     public boolean sendOptionsDialogue(String title, String... options) {
  211.         int i = 0;
  212.         player.getInterfaceManager().sendChatBoxInterface(1188);
  213.         Object params[] = new Object[options.length + 1];
  214.         params[i++] = Integer.valueOf(options.length);
  215.         List<String> optionsList = Arrays.asList(options);
  216.         Collections.reverse(optionsList);
  217.         for (Iterator<String> iterator = optionsList.iterator(); iterator.hasNext();) {
  218.             String string = (String) iterator.next();
  219.             params[i++] = string;
  220.         }
  221.  
  222.         player.getPackets().sendIComponentText(1188, 20, title);
  223.         player.getPackets().sendRunScript(5589, params);
  224.         return true;
  225.     }
  226.  
  227.     public boolean sendOptions(String title, String... options) {
  228.         int i = 0;
  229.         player.getInterfaceManager().sendChatBoxInterface(1188);
  230.         Object params[] = new Object[options.length + 1];
  231.         params[i++] = Integer.valueOf(options.length);
  232.         List<String> optionsList = Arrays.asList(options);
  233.         Collections.reverse(optionsList);
  234.         for (Iterator<String> iterator = optionsList.iterator(); iterator.hasNext();) {
  235.             String string = (String) iterator.next();
  236.             params[i++] = string;
  237.         }
  238.  
  239.         player.getPackets().sendIComponentText(1188, 20, title);
  240.         player.getPackets().sendRunScript(5589, params);
  241.         return true;
  242.     }
  243.  
  244.     public static boolean sendNPCDialogueNoContinue(Player player, int npcId, int animationId, String... text) {
  245.         return sendEntityDialogueNoContinue(player, IS_NPC, npcId, animationId, text);
  246.     }
  247.  
  248.     public static boolean sendPlayerDialogueNoContinue(Player player, int animationId, String... text) {
  249.         return sendEntityDialogueNoContinue(player, IS_PLAYER, -1, animationId, text);
  250.     }
  251.  
  252.     public static boolean sendEntityDialogueNoContinue(Player player, int type, int entityId, int animationId,
  253.             String... text) {
  254.         String title = "";
  255.         if (type == IS_PLAYER) {
  256.             title = player.getDisplayName();
  257.         } else if (type == IS_NPC) {
  258.             title = NPCDefinitions.getNPCDefinitions(entityId).name;
  259.         } else if (type == IS_ITEM)
  260.             title = ItemDefinitions.getItemDefinitions(entityId).getName();
  261.         return sendEntityDialogueNoContinue(player, type, title, entityId, animationId, text);
  262.     }
  263.  
  264.     public static boolean sendEntityDialogueNoContinue(Player player, int type, String title, int entityId,
  265.             int animationId, String... texts) {
  266.         StringBuilder builder = new StringBuilder();
  267.         for (int line = 0; line < texts.length; line++)
  268.             builder.append(" " + texts[line]);
  269.         String text = builder.toString();
  270.         if (type == IS_ITEM) {
  271.             player.getInterfaceManager().sendChatBoxInterface(1190);
  272.             player.getPackets().sendItemOnIComponent(1190, 1, entityId, animationId);
  273.             player.getPackets().sendIComponentText(1190, 4, text);
  274.         } else {
  275.             player.getInterfaceManager().sendChatBoxInterface(1192);
  276.             player.getPackets().sendIComponentText(1192, 16, title);
  277.             player.getPackets().sendIComponentText(1192, 12, text);
  278.             player.getPackets().sendEntityOnIComponent(type == IS_PLAYER, entityId, 1192, 11);
  279.             if (animationId != -1)
  280.                 player.getPackets().sendIComponentAnimation(animationId, 1192, 11);
  281.         }
  282.         return true;
  283.     }
  284.  
  285.     public static void closeNoContinueDialogue(Player player) {
  286.         player.getInterfaceManager().closeReplacedRealChatBoxInterface();
  287.     }
  288.  
  289.     public boolean sendItemDialogue(int itemId, int amount, String... text) {
  290.         return sendEntityDialogue(IS_ITEM, itemId, amount, text);
  291.     }
  292.  
  293.     public static boolean sendItemDialogueNoContinue(Player player, int itemId, int amount, String... text) {
  294.         return sendEntityDialogueNoContinue(player, IS_ITEM, itemId, amount, text);
  295.     }
  296.  
  297.     public static boolean sendDoubleItemSelection(Player player, int[] item1, int[] item2, String option1,
  298.             String option2) {
  299.         return sendDoubleItemSelect(player, DOUBLE_ITEM_SELECTION, item1, item2, option1, option2);
  300.     }
  301.  
  302.     public static boolean sendDoubleItemSelect(Player player, int type, int[] item1, int[] item2, String option1,
  303.             String option2) {
  304.         if (type == DOUBLE_ITEM_SELECTION) {
  305.             player.getInterfaceManager().sendChatBoxInterface(1185);
  306.             player.getPackets().sendItemOnIComponent(1185, 4, item1[0], item1[1]);
  307.             player.getPackets().sendItemOnIComponent(1185, 5, item2[0], item2[1]);
  308.             player.getPackets().sendIComponentText(1185, 20, option1);
  309.             player.getPackets().sendIComponentText(1185, 25, option2);
  310.         }
  311.         return true;
  312.     }
  313.  
  314.     public boolean sendEntityDialogue(int type, String title, int entityId, int animationId, String... texts) {
  315.         StringBuilder builder = new StringBuilder();
  316.         for (int line = 0; line < texts.length; line++)
  317.             builder.append(" " + texts[line]);
  318.         String text = builder.toString();
  319.         if (type == IS_NPC) {
  320.             player.getInterfaceManager().sendChatBoxInterface(1184);
  321.             player.getPackets().sendIComponentText(1184, 17, title);
  322.             player.getPackets().sendIComponentText(1184, 13, text);
  323.             player.getPackets().sendNPCOnIComponent(1184, 11, entityId);
  324.             if (animationId != -1)
  325.                 player.getPackets().sendIComponentAnimation(animationId, 1184, 11);
  326.         } else if (type == IS_PLAYER) {
  327.             player.getInterfaceManager().sendChatBoxInterface(1191);
  328.             player.getPackets().sendIComponentText(1191, 8, title);
  329.             player.getPackets().sendIComponentText(1191, 17, text);
  330.             player.getPackets().sendPlayerOnIComponent(1191, 15);
  331.             if (animationId != -1)
  332.                 player.getPackets().sendIComponentAnimation(animationId, 1191, 15);
  333.         } else if (type == IS_ITEM) {
  334.             player.getInterfaceManager().sendChatBoxInterface(1189);
  335.             player.getPackets().sendItemOnIComponent(1189, 1, entityId, animationId);
  336.             player.getPackets().sendIComponentText(1189, 4, text);
  337.         }
  338.         return true;
  339.     }
  340.  
  341.     public boolean sendDialogue(String... texts) {
  342.         StringBuilder builder = new StringBuilder();
  343.         for (int line = 0; line < texts.length; line++)
  344.             builder.append((line == 0 ? "<p=" + getP() + ">" : "<br>") + texts[line]);
  345.         String text = builder.toString();
  346.         player.getInterfaceManager().sendChatBoxInterface(1186);
  347.         player.getPackets().sendIComponentText(1186, 1, text);
  348.         return true;
  349.     }
  350.  
  351.     public boolean sendEntityDialogue(short interId, String[] talkDefinitons, byte type, int entityId,
  352.             int animationId) {
  353.         if (type == IS_PLAYER || type == IS_NPC) { // auto convert to new
  354.                                                     // dialogue all old
  355.                                                     // dialogues
  356.             String[] texts = new String[talkDefinitons.length - 1];
  357.             for (int i = 0; i < texts.length; i++)
  358.                 texts[i] = talkDefinitons[i + 1];
  359.             sendEntityDialogue(type, talkDefinitons[0], entityId, animationId, texts);
  360.             return true;
  361.         }
  362.         int[] componentOptions = getIComponentsIds(interId);
  363.         if (componentOptions == null)
  364.             return false;
  365.         player.getInterfaceManager().sendChatBoxInterface(interId);
  366.         if (talkDefinitons.length != componentOptions.length)
  367.             return false;
  368.         for (int childOptionId = 0; childOptionId < componentOptions.length; childOptionId++)
  369.             player.getPackets().sendIComponentText(interId, componentOptions[childOptionId],
  370.                     talkDefinitons[childOptionId]);
  371.         if (type == IS_PLAYER || type == IS_NPC) {
  372.             player.getPackets().sendEntityOnIComponent(type == IS_PLAYER, entityId, interId, 2);
  373.             if (animationId != -1)
  374.                 player.getPackets().sendIComponentAnimation(animationId, interId, 2);
  375.         } else if (type == IS_ITEM)
  376.             player.getPackets().sendItemOnIComponent(interId, 1, entityId, animationId);
  377.         return true;
  378.     }
  379.  
  380. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement