Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 31.83 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.Container;
  4. import java.awt.Font;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.GridBagConstraints;
  8. import java.awt.GridBagLayout;
  9. import java.awt.Insets;
  10. import java.awt.Point;
  11. import java.awt.RenderingHints;
  12. import java.awt.event.ActionEvent;
  13. import java.awt.event.ActionListener;
  14. import java.io.File;
  15. import java.io.FileInputStream;
  16. import java.io.FileNotFoundException;
  17. import java.io.FileWriter;
  18. import java.io.IOException;
  19. import java.text.NumberFormat;
  20. import java.util.ArrayList;
  21. import java.util.Locale;
  22. import java.util.Properties;
  23.  
  24. import javax.swing.DefaultComboBoxModel;
  25. import javax.swing.GroupLayout;
  26. import javax.swing.JButton;
  27. import javax.swing.JComboBox;
  28. import javax.swing.JFrame;
  29. import javax.swing.JLabel;
  30. import javax.swing.JPanel;
  31. import javax.swing.JTextField;
  32. import javax.swing.LayoutStyle;
  33. import javax.swing.SwingConstants;
  34. import javax.swing.border.EmptyBorder;
  35.  
  36. import org.rsbot.event.listeners.PaintListener;
  37. import org.rsbot.script.Script;
  38. import org.rsbot.script.ScriptManifest;
  39. import org.rsbot.script.methods.Skills;
  40. import org.rsbot.script.util.Filter;
  41. import org.rsbot.script.wrappers.RSCharacter;
  42. import org.rsbot.script.wrappers.RSNPC;
  43. import org.rsbot.script.wrappers.RSTile;
  44. import org.rsbot.util.GlobalConfiguration;
  45.  
  46. @ScriptManifest(authors = "Dunnkers", name = "DunkYakKilla", keywords = "Combat", version = 1.3, description = "Kills yaks on Neitiznot.")
  47. public class DunkYakKilla extends Script implements PaintListener {
  48.  
  49.     /* MISC */
  50.     public double VERSION = 1;
  51.     public String NAME = "";
  52.     public long startTime = System.currentTimeMillis();
  53.     public String status = "";
  54.     public int kills = 0;
  55.     public skillInfo infoSkill;
  56.     public int curAFKT;
  57.     public long curAFKST;
  58.     public boolean curAFK = false, run = false;
  59.     private Properties settingsFile;
  60.  
  61.     /* IDS */
  62.     public int YAKDEADANIMATIONID = 5784;
  63.     public String YAKNAME = "Yak";
  64.     public int SKILLINTERFACE = 320;
  65.     public int[] ATTACK = { 125, 123, 121, 2428 }, STRENGTH = { 119, 117, 115,
  66.             113 }, DEFENCE = { 137, 135, 133, 2432 }, SUPERATTACK = { 149, 147,
  67.             145, 2436 }, SUPERSTRENGTH = { 161, 159, 157, 2440 },
  68.             SUPERDEFENCE = { 167, 165, 163, 2442 };
  69.  
  70.     /* SETTINGS */
  71.     public int pouchID = 12029, pouchCost = 10, foodID = 7946, eatPercent = 50,
  72.             foodHeal = 16;
  73.     public boolean useAttack = false, useStrength = false, useDefence = false,
  74.             superAttack = false, superStrength = false, superDefence = false;
  75.  
  76.     public boolean onStart() {
  77.         VERSION = getClass().getAnnotation(ScriptManifest.class).version();
  78.         NAME = getClass().getAnnotation(ScriptManifest.class).name();
  79.         settingsFile = new Properties();
  80.         final DunkYakKillaGUI GUI = new DunkYakKillaGUI();
  81.         GUI.setVisible(true);
  82.         while (GUI.isVisible()) {
  83.             sleep(10);
  84.         }
  85.         if (superAttack)
  86.             ATTACK = SUPERATTACK;
  87.         if (superStrength)
  88.             STRENGTH = SUPERSTRENGTH;
  89.         if (superDefence)
  90.             DEFENCE = SUPERDEFENCE;
  91.         infoSkill = new skillInfo();
  92.         mouse.setSpeed(5);
  93.         return run;
  94.     }
  95.  
  96.     private enum state {
  97.         EAT, POTIONS, FAMILIAR, ATTACK, FIND, NONE
  98.     }
  99.  
  100.     public state getState() {
  101.         if (getHPPercent() < eatPercent) {
  102.             if (inventory.contains(foodID)) {
  103.                 return state.EAT;
  104.             } else if (getHPPercent() > 0) {
  105.                 log("We ran out of food and we're low on hp, stopping script.");
  106.                 stopScript();
  107.             }
  108.         }
  109.         if (useAttack && inventory.containsOneOf(ATTACK)
  110.                 && !isSkillBoosted(Skills.ATTACK) || useStrength
  111.                 && inventory.containsOneOf(STRENGTH)
  112.                 && !isSkillBoosted(Skills.STRENGTH) || useDefence
  113.                 && inventory.containsOneOf(DEFENCE)
  114.                 && !isSkillBoosted(Skills.DEFENSE)) {
  115.             return state.POTIONS;
  116.         }
  117.         if (inventory.contains(pouchID) && !summoning.isFamiliarSummoned()
  118.                 && summoning.getSummoningPoints() >= pouchCost) {
  119.             return state.FAMILIAR;
  120.         }
  121.         if (needAttack()) {
  122.             return state.ATTACK;
  123.         }
  124.         if (getMyPlayer().getInteracting() != null) {
  125.             if (getMyPlayer().getInteracting().getHPPercent() < 10) {
  126.                 return state.FIND;
  127.             }
  128.         }
  129.         return state.NONE;
  130.     }
  131.  
  132.     public int loop() {
  133.         if (getMyPlayer().getInteracting() != null) {
  134.             if (getMyPlayer().getInteracting().getAnimation() == YAKDEADANIMATIONID) {
  135.                 kills++;
  136.             }
  137.         }
  138.         switch (getState()) {
  139.         case EAT:
  140.             status = "Eating food...";
  141.             int eatN = getFoodToHeal();
  142.             log("[eat] eating " + eatN + " food with id: (" + foodID + ")");
  143.             for (int i = 0; i < eatN; i++) {
  144.                 inventory.getItem(foodID).doAction("Eat");
  145.                 sleep(random(1500, 2000));
  146.             }
  147.             break;
  148.         case POTIONS:
  149.             status = "Drinking potions...";
  150.             int[] potions = {};
  151.             if (useAttack && !isSkillBoosted(Skills.ATTACK))
  152.                 potions = ATTACK;
  153.             if (useStrength && !isSkillBoosted(Skills.STRENGTH))
  154.                 potions = STRENGTH;
  155.             if (useDefence && !isSkillBoosted(Skills.DEFENSE))
  156.                 potions = DEFENCE;
  157.             int id = 0;
  158.             for (int potion : potions) {
  159.                 if (inventory.contains(potion)) {
  160.                     id = potion;
  161.                     break;
  162.                 }
  163.             }
  164.             if (id != 0) {
  165.                 log("[potions] drinking potion with id (" + id + ")");
  166.                 try {
  167.                     inventory.getItem(id).doAction("Drink");
  168.                 } catch (Exception e) {
  169.                     e.printStackTrace();
  170.                 }
  171.                 sleep(random(1000, 1250));
  172.             }
  173.         case FAMILIAR:
  174.             status = "Summoning familiar...";
  175.             log("[familiar] summoning familiar with pouch id: " + pouchID);
  176.             try {
  177.                 inventory.getItem(pouchID).doAction("Summon");
  178.             } catch (Exception e) {
  179.                 e.printStackTrace();
  180.             }
  181.             sleep(random(1000, 2000));
  182.             break;
  183.         case ATTACK:
  184.             status = "Attacking...";
  185.             RSNPC toAttack = getNPCToAttack();
  186.             if (toAttack != null) {
  187.                 if (toAttack.isOnScreen()) {
  188.                     toAttack.doAction("Attack");
  189.                 } else {
  190.                     if (calc.distanceTo(toAttack) > 5) {
  191.                         walking.walkTileMM(toAttack.getLocation());
  192.                     }
  193.                     camera.turnToCharacter(toAttack);
  194.                 }
  195.             } else {
  196.                 if (random(0, 3) == 2) {
  197.                     RSTile[] area = { new RSTile(2327, 3792),
  198.                             new RSTile(2323, 3795), new RSTile(2320, 3794),
  199.                             new RSTile(2323, 3792), new RSTile(2323, 3795) };
  200.                     walking.walkTileMM(area[random(0, area.length)]);
  201.                     sleep(random(1000, 2000));
  202.                 }
  203.             }
  204.             sleep(random(100, 1000));
  205.             break;
  206.         case FIND:
  207.             status = "Finding new opponent...";
  208.             RSNPC nextOpponent = getNPCToAttack();
  209.             if (nextOpponent != null) {
  210.                 if (!nextOpponent.isOnScreen()) {
  211.                     camera.turnToCharacter(nextOpponent);
  212.                 } else {
  213.                     Point p = nextOpponent.getScreenLocation();
  214.                     if (calc.pointOnScreen(p)) {
  215.                         mouse
  216.                                 .move(p.x - random(-10, 10), p.y
  217.                                         - random(-10, 10));
  218.                     }
  219.                 }
  220.                 sleep(random(1000, 2000));
  221.             }
  222.             break;
  223.         case NONE:
  224.             status = "Waiting...";
  225.             antiban();
  226.             break;
  227.         }
  228.         return 1;
  229.     }
  230.  
  231.     public void antiban() {
  232.         int t = random(0, 100);
  233.         switch (t) {
  234.         case 1:
  235.             if (random(0, 10) == 5) {
  236.                 log("[antiban] move mouse");
  237.                 mouse.moveSlightly();
  238.             }
  239.             break;
  240.         case 2:
  241.             if (random(0, 25) == 5) {
  242.                 log("[antiban] check combat skill");
  243.                 int[] childs = { 1, 2, 4, 22, 46, 87 };
  244.                 game.openTab(1);
  245.                 interfaces.getComponent(SKILLINTERFACE,
  246.                         childs[random(0, childs.length)]).doHover();
  247.                 sleep(random(1000, 2000));
  248.             }
  249.             break;
  250.         case 3:
  251.             if (random(0, 30) == 5) {
  252.                 log("[antiban] open random tab and move mouse within");
  253.                 game.openTab(random(0, 15));
  254.                 mouse.move(random(550, 735), random(210, 465));
  255.                 sleep(random(1000, 2000));
  256.             }
  257.             break;
  258.         case 4:
  259.             if (random(0, 30) == 5) {
  260.                 log("[antiban] open random tab");
  261.                 game.openTab(random(0, 15));
  262.                 sleep(random(500, random(1000, 2000)));
  263.             }
  264.             break;
  265.         case 5:
  266.             if (random(0, 40) == 5) {
  267.                 log("[antiban] check objective");
  268.                 game.openTab(8);
  269.                 interfaces.getComponent(891, 10).doHover();
  270.                 sleep(random(1000, 2000));
  271.             }
  272.             break;
  273.         case 6:
  274.             if (random(0, 70) == 5) {
  275.                 log("[antiban] away from keyboard");
  276.                 mouse.moveOffScreen();
  277.                 curAFKST = System.currentTimeMillis();
  278.                 curAFKT = random(0, random(5000, random(10000, random(15000,
  279.                         20000))));
  280.                 log("[antiban] afk for " + curAFKT);
  281.                 curAFK = true;
  282.                 sleep(curAFKT);
  283.                 curAFK = false;
  284.             }
  285.             break;
  286.         default:
  287.             break;
  288.         }
  289.     }
  290.  
  291.     public void onFinish() {
  292.     }
  293.  
  294.     public void onRepaint(final Graphics g) {
  295.         ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  296.                 RenderingHints.VALUE_ANTIALIAS_ON);
  297.         g.setFont(new Font("Tahoma", Font.PLAIN, 13));
  298.         g.setColor(Color.WHITE);
  299.         String[] list = {
  300.                 NAME + " v" + VERSION,
  301.                 "Runtime: "
  302.                         + timeFormat((int) (System.currentTimeMillis() - startTime)),
  303.                 "Status: " + status, "Kills: " + numberFormat(kills) };
  304.         drawColumn(g, list, 7, 337, -3, false, true);
  305.         ArrayList<String> infoSkillsRaw = new ArrayList<String>();
  306.         for (int i = 0; i < infoSkill.skillAmount; i++) {
  307.             if (infoSkill.getExpGained(i) > 0) {
  308.                 infoSkillsRaw.add("");
  309.                 infoSkillsRaw.add(setFirstUppercase(infoSkill.getSkillName(i))
  310.                         + " exp gained: "
  311.                         + numberFormat(infoSkill.getExpGained(i)));
  312.                 infoSkillsRaw.add("  Exp per hour: "
  313.                         + numberFormat((int) getPerHour(infoSkill
  314.                                 .getExpGained(i), startTime)));
  315.             }
  316.         }
  317.         String[] infoSkills = toArray(infoSkillsRaw);
  318.         g.setFont(new Font("Tahoma", Font.PLAIN, 12));
  319.         drawColumn(g, infoSkills, 515, 337, -3, true, true);
  320.         g.setColor(setAlpha(Color.GREEN, 150));
  321.         RSNPC[] a = npcs.getAll(new Filter<RSNPC>() {
  322.             public boolean accept(RSNPC v) {
  323.                 return isValidToAttack(v);
  324.             }
  325.         });
  326.         for (RSNPC b : a) {
  327.             Point l = b.getScreenLocation();
  328.             g.drawOval(l.x - 5, l.y - 5, 10, 10);
  329.         }
  330.         g.setColor(Color.YELLOW);
  331.         if (curAFK) {
  332.             drawNote(g, true, new String[] {
  333.                     "Away from keyboard for " + secondFormat(curAFKT)
  334.                             + " seconds...",
  335.                     "Time left: "
  336.                             + secondFormat((int) (curAFKT - (System
  337.                                     .currentTimeMillis() - curAFKST))) });
  338.         }
  339.     }
  340.  
  341.     /* VOIDS */
  342.     public void drawColumn(Graphics g, String[] column, int x, int y, int offset, boolean reverseHorizontal, boolean reverseVertical) {
  343.         int h = g.getFontMetrics().getHeight(), xp = x;
  344.         h += offset;
  345.         if (reverseVertical) {
  346.             y -= (column.length-1)*h;
  347.         }else {
  348.             y += h;
  349.         }
  350.         for (int i = 0; i < column.length; i++) {
  351.             x = xp;
  352.             if (reverseHorizontal) {
  353.                 x += -g.getFontMetrics().stringWidth(column[i]);
  354.             }
  355.             g.drawString(column[i], x, y+(h*i));
  356.         }
  357.     }
  358.  
  359.     public void drawNote(Graphics g, boolean dark, String[] strA) {
  360.         if (dark) {
  361.             g.setColor(setAlpha(Color.BLACK, 125));
  362.             g.fillRect(0, 0, game.getWidth(), game.getHeight());
  363.         }
  364.         int MW = getMostWidth(g, strA), w = MW + (((MW / 2) / 2) / 2), oY = g
  365.                 .getFontMetrics().getHeight(), x = (game.getWidth() / 2)
  366.                 - (MW / 2), y = (game.getHeight() / 2)
  367.                 - ((oY * strA.length) / 2), h = oY * strA.length + 5;
  368.         g.setColor(setAlpha(Color.WHITE, 200));
  369.         g.fill3DRect(x, y, w, h, true);
  370.         g.setColor(Color.BLACK);
  371.         for (int i = 0; i < strA.length; i++) {
  372.             g.drawString(strA[i],
  373.                     x
  374.                             + ((w / 2) - (g.getFontMetrics().stringWidth(
  375.                                     strA[i]) / 2)), y + oY * (i + 1));
  376.         }
  377.     }
  378.  
  379.     /* BOOLEANS */
  380.     public boolean needAttack() {
  381.         RSCharacter opponent = getMyPlayer().getInteracting();
  382.         return opponent == null
  383.                 || opponent.getAnimation() == YAKDEADANIMATIONID;
  384.     }
  385.  
  386.     public boolean isValidToAttack(RSNPC v) {
  387.         return v.getName().equals(YAKNAME) && v.getInteracting() == null
  388.                 && !v.isInCombat();
  389.     }
  390.  
  391.     public boolean isSkillBoosted(int skill) {
  392.         return skills.getRealLevel(skill) != skills.getCurrentLevel(skill);
  393.     }
  394.  
  395.     /* INTS */
  396.     public int getFoodToHeal() {
  397.         return (getMaxHP() - getHP()) / (foodHeal * 10);
  398.     }
  399.  
  400.     public int getHPPercent() {
  401.         if (game.isLoggedIn()) {
  402.             try {
  403.                 float currentHP = Integer.parseInt(interfaces.getComponent(748,
  404.                         8).getText());
  405.                 float maxHP = Integer.parseInt(interfaces
  406.                         .getComponent(320, 190).getText()) * 10;
  407.                 float HPPercent = (currentHP / maxHP) * 100;
  408.                 if (HPPercent > 100) {
  409.                     game.openTab(1);
  410.                 }
  411.                 return (int) ((currentHP / maxHP) * 100);
  412.             } catch (NumberFormatException e) {
  413.                 return 100;
  414.             }
  415.         } else {
  416.             return 100;
  417.         }
  418.     }
  419.  
  420.     public int getHP() {
  421.         try {
  422.             return Integer.parseInt(interfaces.getComponent(748, 8).getText());
  423.         } catch (NumberFormatException e) {
  424.             return -1;
  425.         }
  426.     }
  427.  
  428.     public int getMaxHP() {
  429.         try {
  430.             return Integer
  431.                     .parseInt(interfaces.getComponent(320, 190).getText()) * 10;
  432.         } catch (NumberFormatException e) {
  433.             return -1;
  434.         }
  435.     }
  436.  
  437.     public int getMostWidth(Graphics g, String[] array) {
  438.         int o = 0;
  439.         for (String str : array) {
  440.             int d = g.getFontMetrics().stringWidth(str);
  441.             if (d > o) {
  442.                 o = d;
  443.             }
  444.         }
  445.         return o;
  446.     }
  447.  
  448.     /* STRINGS */
  449.     public String timeFormat(final int currentTime) {
  450.         NumberFormat nf = NumberFormat.getInstance();
  451.         nf.setMinimumIntegerDigits(2);
  452.         final long millisecondsL = currentTime / 10;
  453.         final long secondsL = currentTime / 1000;
  454.         final long minutesL = secondsL / 60;
  455.         final long hoursL = minutesL / 60;
  456.         long milliseconds = (int) (millisecondsL % 60);
  457.         final int seconds = (int) (secondsL % 60);
  458.         final int minutes = (int) (minutesL % 60);
  459.         final int hours = (int) (hoursL % 60);
  460.         final String h = nf.format(hours), m = nf.format(minutes), s = nf
  461.                 .format(seconds);
  462.         nf.setMinimumIntegerDigits(1);
  463.         milliseconds = milliseconds / 10;
  464.         final String ms = nf.format(milliseconds);
  465.         return (h + ":" + m + ":" + s + "." + ms);
  466.     }
  467.  
  468.     String secondFormat(final int currentTime) {
  469.         NumberFormat nf = NumberFormat.getInstance();
  470.         nf.setMinimumIntegerDigits(1);
  471.         String ms = nf.format(((currentTime / 10) % 60) / 10);
  472.         String s = nf.format((currentTime / 1000) % 60);
  473.         return (s + "." + ms);
  474.     }
  475.  
  476.     public String numberFormat(final int number) {
  477.         NumberFormat nf = NumberFormat.getInstance(Locale.UK);
  478.         return (nf.format(number));
  479.     }
  480.  
  481.     public String setFirstUppercase(String str) {
  482.         return Character.toUpperCase(str.charAt(0))
  483.                 + str.substring(1).toLowerCase();
  484.     }
  485.  
  486.     public String[] toArray(ArrayList<String> a) {
  487.         String[] result = new String[a.size()];
  488.         for (int i = 0; i < a.size(); i++) {
  489.             result[i] = a.get(i);
  490.         }
  491.         return result;
  492.     }
  493.  
  494.     /* OTHER */
  495.     public RSNPC getNPCToAttack() {
  496.         return npcs.getNearest(new Filter<RSNPC>() {
  497.             public boolean accept(RSNPC v) {
  498.                 return isValidToAttack(v);
  499.             }
  500.         });
  501.     }
  502.  
  503.     public Color setAlpha(Color o, int alpha) {
  504.         return new Color(o.getRed(), o.getGreen(), o.getBlue(), alpha);
  505.     }
  506.  
  507.     public float getPerMili(final int amount, long startTime) {
  508.         return ((float) amount)
  509.                 / ((float) (System.currentTimeMillis() - startTime));
  510.     }
  511.  
  512.     public float getPerSec(final int amount, long startTime) {
  513.         return getPerMili(amount, startTime) * 1000;
  514.     }
  515.  
  516.     public float getPerMin(final int amount, long startTime) {
  517.         return getPerSec(amount, startTime) * 60;
  518.     }
  519.  
  520.     public float getPerHour(final int amount, long startTime) {
  521.         return getPerMin(amount, startTime) * 60;
  522.     }
  523.  
  524.     public class skillInfo {
  525.  
  526.         public int skillAmount = Skills.SKILL_NAMES.length - 1;
  527.         public int[] expStarts = new int[skillAmount];
  528.         public int[] lvlStarts = new int[skillAmount];
  529.         public boolean initialized = false;
  530.  
  531.         public int getExpGained(int skill) {
  532.             if (!initialized) {
  533.                 if (game.isLoggedIn() && skillsInitialized()) {
  534.                     initialize();
  535.                 }
  536.                 return 0;
  537.             }
  538.             return skills.getCurrentExp(skill) - expStarts[skill];
  539.         }
  540.  
  541.         public String getSkillName(int skill) {
  542.             return Skills.SKILL_NAMES[skill];
  543.         }
  544.  
  545.         public void initialize() {
  546.             for (int i = 0; i < skillAmount; i++) {
  547.                 expStarts[i] = skills.getCurrentExp(i);
  548.                 lvlStarts[i] = skills.getRealLevel(i);
  549.             }
  550.             initialized = true;
  551.         }
  552.  
  553.         public boolean skillsInitialized() {
  554.             return skills.getCurrentExp(Skills.CONSTITUTION) > 5;
  555.         }
  556.     }
  557.  
  558.     public class DunkYakKillaGUI extends JFrame {
  559.  
  560.         private static final long serialVersionUID = 1L;
  561.         private JPanel dialogPane;
  562.         private JPanel contentPanel;
  563.         private JLabel title;
  564.         private JLabel label1;
  565.         private JLabel label2;
  566.         private JLabel label3;
  567.         private JLabel label4;
  568.         private JLabel label5;
  569.         private JLabel label6;
  570.         private JLabel label7;
  571.         private JComboBox attack;
  572.         private JComboBox strength;
  573.         private JComboBox defence;
  574.         private JTextField food;
  575.         private JTextField pouch;
  576.         private JPanel buttonBar;
  577.         private JButton okButton;
  578.         private JButton cancelButton;
  579.  
  580.         public DunkYakKillaGUI() {
  581.             initComponents();
  582.         }
  583.  
  584.         private void okButtonActionPerformed() {
  585.             saveSettings();
  586.             saveSettingsFile(settingsFile);
  587.             run = true;
  588.             dispose();
  589.         }
  590.  
  591.         private void cancelButtonActionPerformed() {
  592.             dispose();
  593.         }
  594.  
  595.         private void loadSettings() {
  596.             if (loadSettingsFile(settingsFile)) {
  597.                 if (settingsFile.getProperty("attack") != null) {
  598.                     attack.setSelectedItem(settingsFile.getProperty("attack"));
  599.                 }
  600.                 if (settingsFile.getProperty("strength") != null) {
  601.                     strength.setSelectedItem(settingsFile
  602.                             .getProperty("strength"));
  603.                 }
  604.                 if (settingsFile.getProperty("defence") != null) {
  605.                     defence
  606.                             .setSelectedItem(settingsFile
  607.                                     .getProperty("defence"));
  608.                 }
  609.                 if (settingsFile.getProperty("pouchID") != null) {
  610.                     pouch.setText(settingsFile.getProperty("pouchID"));
  611.                 }
  612.                 if (settingsFile.getProperty("foodID") != null) {
  613.                     food.setText(settingsFile.getProperty("foodID"));
  614.                 }
  615.             }
  616.         }
  617.  
  618.         private void saveSettings() {
  619.             String att = attack.getSelectedItem().toString();
  620.             settingsFile.setProperty("attack", att);
  621.             if (!att.equals("Dont use")) {
  622.                 useAttack = true;
  623.                 if (att.equals("Super")) {
  624.                     superAttack = true;
  625.                 }
  626.             }
  627.             String str = strength.getSelectedItem().toString();
  628.             settingsFile.setProperty("strength", str);
  629.             if (!str.equals("Dont use")) {
  630.                 useStrength = true;
  631.                 if (str.equals("Super")) {
  632.                     superStrength = true;
  633.                 }
  634.             }
  635.             String def = defence.getSelectedItem().toString();
  636.             settingsFile.setProperty("defence", def);
  637.             if (!def.equals("Dont use")) {
  638.                 useDefence = true;
  639.                 if (def.equals("Super")) {
  640.                     superDefence = true;
  641.                 }
  642.             }
  643.             try {
  644.                 foodID = Integer.parseInt(food.getText());
  645.                 settingsFile.setProperty("foodID", food.getText());
  646.             } catch (NumberFormatException ignored) {
  647.                 foodID = 0;
  648.                 settingsFile.remove("foodID");
  649.             }
  650.             try {
  651.                 pouchID = Integer.parseInt(pouch.getText());
  652.                 settingsFile.setProperty("pouchID", pouch.getText());
  653.             } catch (NumberFormatException ignored) {
  654.                 pouchID = 0;
  655.                 settingsFile.remove("pouchID");
  656.             }
  657.         }
  658.  
  659.         public boolean loadSettingsFile(Properties f) {
  660.             try {
  661.                 f.load(new FileInputStream(new File(GlobalConfiguration.Paths
  662.                         .getSettingsDirectory(), NAME + "Settings.ini")));
  663.                 return true;
  664.             } catch (FileNotFoundException e) {
  665.                 return false;
  666.             } catch (IOException e) {
  667.                 return false;
  668.             }
  669.         }
  670.  
  671.         public boolean saveSettingsFile(Properties f) {
  672.             try {
  673.                 f.store(new FileWriter(new File(GlobalConfiguration.Paths
  674.                         .getSettingsDirectory(), NAME + "Settings.ini")),
  675.                         "The settings of " + NAME);
  676.                 return true;
  677.             } catch (IOException e) {
  678.                 return false;
  679.             }
  680.         }
  681.  
  682.         private void initComponents() {
  683.             dialogPane = new JPanel();
  684.             contentPanel = new JPanel();
  685.             title = new JLabel();
  686.             label1 = new JLabel();
  687.             label2 = new JLabel();
  688.             label3 = new JLabel();
  689.             label4 = new JLabel();
  690.             label5 = new JLabel();
  691.             label6 = new JLabel();
  692.             label7 = new JLabel();
  693.             attack = new JComboBox();
  694.             strength = new JComboBox();
  695.             defence = new JComboBox();
  696.             food = new JTextField();
  697.             pouch = new JTextField();
  698.             buttonBar = new JPanel();
  699.             okButton = new JButton();
  700.             cancelButton = new JButton();
  701.  
  702.             // ======== this ========
  703.             setResizable(false);
  704.             setTitle("DunkYakKilla");
  705.             setBackground(Color.white);
  706.             Container contentPane = getContentPane();
  707.             contentPane.setLayout(new BorderLayout());
  708.  
  709.             // ======== dialogPane ========
  710.             {
  711.                 dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
  712.                 dialogPane.setBackground(Color.white);
  713.                 dialogPane.setLayout(new BorderLayout());
  714.  
  715.                 // ======== contentPanel ========
  716.                 {
  717.                     contentPanel.setInheritsPopupMenu(true);
  718.                     contentPanel.setBackground(Color.white);
  719.  
  720.                     // ---- title ----
  721.                     title.setText("DunkYakKilla");
  722.                     title.setFont(new Font("Tahoma", Font.BOLD, 16));
  723.                     title.setHorizontalAlignment(SwingConstants.CENTER);
  724.  
  725.                     // ---- label1 ----
  726.                     label1.setText("Attack potion: ");
  727.  
  728.                     // ---- label2 ----
  729.                     label2.setText("Strength potion:");
  730.  
  731.                     // ---- label3 ----
  732.                     label3.setText("Defence potion:");
  733.  
  734.                     // ---- label4 ----
  735.                     label4.setText("Familiar pouch ID:");
  736.  
  737.                     // ---- label5 ----
  738.                     label5.setText("Food ID:");
  739.  
  740.                     // ---- label6 ----
  741.                     label6.setText("Leave a textfield blank to disable");
  742.  
  743.                     // ---- label7 ----
  744.                     label7.setText("All settings are auto saved");
  745.                     label7.setFont(new Font("Tahoma", Font.BOLD, 11));
  746.                     label7.setHorizontalAlignment(SwingConstants.CENTER);
  747.  
  748.                     String[] model = new String[] { "Dont use", "Normal",
  749.                             "Super" };
  750.  
  751.                     // ---- attack ----
  752.                     attack.setModel(new DefaultComboBoxModel(model));
  753.  
  754.                     // ---- strength ----
  755.                     strength.setModel(new DefaultComboBoxModel(model));
  756.  
  757.                     // ---- defence ----
  758.                     defence.setModel(new DefaultComboBoxModel(model));
  759.  
  760.                     GroupLayout contentPanelLayout = new GroupLayout(
  761.                             contentPanel);
  762.                     contentPanel.setLayout(contentPanelLayout);
  763.                     contentPanelLayout
  764.                             .setHorizontalGroup(contentPanelLayout
  765.                                     .createParallelGroup()
  766.                                     .addGroup(
  767.                                             contentPanelLayout
  768.                                                     .createSequentialGroup()
  769.                                                     .addContainerGap()
  770.                                                     .addGroup(
  771.                                                             contentPanelLayout
  772.                                                                     .createParallelGroup()
  773.                                                                     .addGroup(
  774.                                                                             GroupLayout.Alignment.TRAILING,
  775.                                                                             contentPanelLayout
  776.                                                                                     .createSequentialGroup()
  777.                                                                                     .addGroup(
  778.                                                                                             contentPanelLayout
  779.                                                                                                     .createParallelGroup(
  780.                                                                                                             GroupLayout.Alignment.TRAILING)
  781.                                                                                                     .addGroup(
  782.                                                                                                             GroupLayout.Alignment.LEADING,
  783.                                                                                                             contentPanelLayout
  784.                                                                                                                     .createSequentialGroup()
  785.                                                                                                                     .addComponent(
  786.                                                                                                                             label3,
  787.                                                                                                                             GroupLayout.PREFERRED_SIZE,
  788.                                                                                                                             81,
  789.                                                                                                                             GroupLayout.PREFERRED_SIZE)
  790.                                                                                                                     .addGap(
  791.                                                                                                                             18,
  792.                                                                                                                             18,
  793.                                                                                                                             18)
  794.                                                                                                                     .addComponent(
  795.                                                                                                                             defence,
  796.                                                                                                                             GroupLayout.PREFERRED_SIZE,
  797.                                                                                                                             GroupLayout.DEFAULT_SIZE,
  798.                                                                                                                             GroupLayout.PREFERRED_SIZE))
  799.                                                                                                     .addGroup(
  800.                                                                                                             GroupLayout.Alignment.LEADING,
  801.                                                                                                             contentPanelLayout
  802.                                                                                                                     .createSequentialGroup()
  803.                                                                                                                     .addComponent(
  804.                                                                                                                             label2,
  805.                                                                                                                             GroupLayout.PREFERRED_SIZE,
  806.                                                                                                                             81,
  807.                                                                                                                             GroupLayout.PREFERRED_SIZE)
  808.                                                                                                                     .addGap(
  809.                                                                                                                             18,
  810.                                                                                                                             18,
  811.                                                                                                                             18)
  812.                                                                                                                     .addComponent(
  813.                                                                                                                             strength,
  814.                                                                                                                             GroupLayout.PREFERRED_SIZE,
  815.                                                                                                                             GroupLayout.DEFAULT_SIZE,
  816.                                                                                                                             GroupLayout.PREFERRED_SIZE))
  817.                                                                                                     .addGroup(
  818.                                                                                                             GroupLayout.Alignment.LEADING,
  819.                                                                                                             contentPanelLayout
  820.                                                                                                                     .createSequentialGroup()
  821.                                                                                                                     .addComponent(
  822.                                                                                                                             label1,
  823.                                                                                                                             GroupLayout.PREFERRED_SIZE,
  824.                                                                                                                             81,
  825.                                                                                                                             GroupLayout.PREFERRED_SIZE)
  826.                                                                                                                     .addGap(
  827.                                                                                                                             18,
  828.                                                                                                                             18,
  829.                                                                                                                             18)
  830.                                                                                                                     .addComponent(
  831.                                                                                                                             attack,
  832.                                                                                                                             GroupLayout.PREFERRED_SIZE,
  833.                                                                                                                             GroupLayout.DEFAULT_SIZE,
  834.                                                                                                                             GroupLayout.PREFERRED_SIZE)))
  835.                                                                                     .addContainerGap(
  836.                                                                                             13,
  837.                                                                                             Short.MAX_VALUE))
  838.                                                                     .addGroup(
  839.                                                                             contentPanelLayout
  840.                                                                                     .createSequentialGroup()
  841.                                                                                     .addGroup(
  842.                                                                                             contentPanelLayout
  843.                                                                                                     .createParallelGroup(
  844.                                                                                                             GroupLayout.Alignment.TRAILING)
  845.                                                                                                     .addComponent(
  846.                                                                                                             label6,
  847.                                                                                                             GroupLayout.Alignment.LEADING)
  848.                                                                                                     .addGroup(
  849.                                                                                                             contentPanelLayout
  850.                                                                                                                     .createSequentialGroup()
  851.                                                                                                                     .addGroup(
  852.                                                                                                                             contentPanelLayout
  853.                                                                                                                                     .createParallelGroup()
  854.                                                                                                                                     .addComponent(
  855.                                                                                                                                             label5,
  856.                                                                                                                                             GroupLayout.PREFERRED_SIZE,
  857.                                                                                                                                             55,
  858.                                                                                                                                             GroupLayout.PREFERRED_SIZE)
  859.                                                                                                                                     .addComponent(
  860.                                                                                                                                             label4))
  861.                                                                                                                     .addPreferredGap(
  862.                                                                                                                             LayoutStyle.ComponentPlacement.RELATED,
  863.                                                                                                                             13,
  864.                                                                                                                             Short.MAX_VALUE)
  865.                                                                                                                     .addGroup(
  866.                                                                                                                             contentPanelLayout
  867.                                                                                                                                     .createParallelGroup(
  868.                                                                                                                                             GroupLayout.Alignment.LEADING,
  869.                                                                                                                                             false)
  870.                                                                                                                                     .addComponent(
  871.                                                                                                                                             pouch)
  872.                                                                                                                                     .addComponent(
  873.                                                                                                                                             food,
  874.                                                                                                                                             GroupLayout.DEFAULT_SIZE,
  875.                                                                                                                                             71,
  876.                                                                                                                                             Short.MAX_VALUE))))
  877.                                                                                     .addContainerGap())
  878.                                                                     .addGroup(
  879.                                                                             GroupLayout.Alignment.TRAILING,
  880.                                                                             contentPanelLayout
  881.                                                                                     .createSequentialGroup()
  882.                                                                                     .addComponent(
  883.                                                                                             title,
  884.                                                                                             GroupLayout.DEFAULT_SIZE,
  885.                                                                                             170,
  886.                                                                                             Short.MAX_VALUE)
  887.                                                                                     .addContainerGap())
  888.                                                                     .addGroup(
  889.                                                                             contentPanelLayout
  890.                                                                                     .createSequentialGroup()
  891.                                                                                     .addComponent(
  892.                                                                                             label7,
  893.                                                                                             GroupLayout.DEFAULT_SIZE,
  894.                                                                                             160,
  895.                                                                                             Short.MAX_VALUE)
  896.                                                                                     .addGap(
  897.                                                                                             20,
  898.                                                                                             20,
  899.                                                                                             20)))));
  900.                     contentPanelLayout
  901.                             .setVerticalGroup(contentPanelLayout
  902.                                     .createParallelGroup()
  903.                                     .addGroup(
  904.                                             contentPanelLayout
  905.                                                     .createSequentialGroup()
  906.                                                     .addComponent(title)
  907.                                                     .addPreferredGap(
  908.                                                             LayoutStyle.ComponentPlacement.RELATED)
  909.                                                     .addGroup(
  910.                                                             contentPanelLayout
  911.                                                                     .createParallelGroup(
  912.                                                                             GroupLayout.Alignment.BASELINE)
  913.                                                                     .addComponent(
  914.                                                                             attack,
  915.                                                                             GroupLayout.PREFERRED_SIZE,
  916.                                                                             GroupLayout.DEFAULT_SIZE,
  917.                                                                             GroupLayout.PREFERRED_SIZE)
  918.                                                                     .addComponent(
  919.                                                                             label1))
  920.                                                     .addPreferredGap(
  921.                                                             LayoutStyle.ComponentPlacement.RELATED)
  922.                                                     .addGroup(
  923.                                                             contentPanelLayout
  924.                                                                     .createParallelGroup(
  925.                                                                             GroupLayout.Alignment.BASELINE)
  926.                                                                     .addComponent(
  927.                                                                             label2)
  928.                                                                     .addComponent(
  929.                                                                             strength,
  930.                                                                             GroupLayout.PREFERRED_SIZE,
  931.                                                                             GroupLayout.DEFAULT_SIZE,
  932.                                                                             GroupLayout.PREFERRED_SIZE))
  933.                                                     .addPreferredGap(
  934.                                                             LayoutStyle.ComponentPlacement.RELATED)
  935.                                                     .addGroup(
  936.                                                             contentPanelLayout
  937.                                                                     .createParallelGroup(
  938.                                                                             GroupLayout.Alignment.BASELINE)
  939.                                                                     .addComponent(
  940.                                                                             label3)
  941.                                                                     .addComponent(
  942.                                                                             defence,
  943.                                                                             GroupLayout.PREFERRED_SIZE,
  944.                                                                             GroupLayout.DEFAULT_SIZE,
  945.                                                                             GroupLayout.PREFERRED_SIZE))
  946.                                                     .addPreferredGap(
  947.                                                             LayoutStyle.ComponentPlacement.RELATED)
  948.                                                     .addComponent(label6)
  949.                                                     .addPreferredGap(
  950.                                                             LayoutStyle.ComponentPlacement.RELATED)
  951.                                                     .addGroup(
  952.                                                             contentPanelLayout
  953.                                                                     .createParallelGroup(
  954.                                                                             GroupLayout.Alignment.BASELINE)
  955.                                                                     .addComponent(
  956.                                                                             label5)
  957.                                                                     .addComponent(
  958.                                                                             food,
  959.                                                                             GroupLayout.PREFERRED_SIZE,
  960.                                                                             GroupLayout.DEFAULT_SIZE,
  961.                                                                             GroupLayout.PREFERRED_SIZE))
  962.                                                     .addPreferredGap(
  963.                                                             LayoutStyle.ComponentPlacement.RELATED)
  964.                                                     .addGroup(
  965.                                                             contentPanelLayout
  966.                                                                     .createParallelGroup(
  967.                                                                             GroupLayout.Alignment.BASELINE)
  968.                                                                     .addComponent(
  969.                                                                             label4)
  970.                                                                     .addComponent(
  971.                                                                             pouch,
  972.                                                                             GroupLayout.PREFERRED_SIZE,
  973.                                                                             GroupLayout.DEFAULT_SIZE,
  974.                                                                             GroupLayout.PREFERRED_SIZE))
  975.                                                     .addPreferredGap(
  976.                                                             LayoutStyle.ComponentPlacement.RELATED,
  977.                                                             GroupLayout.DEFAULT_SIZE,
  978.                                                             Short.MAX_VALUE)
  979.                                                     .addComponent(label7)
  980.                                                     .addContainerGap()));
  981.                 }
  982.                 dialogPane.add(contentPanel, BorderLayout.NORTH);
  983.  
  984.                 // ======== buttonBar ========
  985.                 {
  986.                     buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
  987.                     buttonBar.setBackground(Color.white);
  988.                     buttonBar.setLayout(new GridBagLayout());
  989.                     ((GridBagLayout) buttonBar.getLayout()).columnWidths = new int[] {
  990.                             0, 85, 80 };
  991.                     ((GridBagLayout) buttonBar.getLayout()).columnWeights = new double[] {
  992.                             1.0, 0.0, 0.0 };
  993.  
  994.                     // ---- okButton ----
  995.                     okButton.setText("OK");
  996.                     okButton.addActionListener(new ActionListener() {
  997.                         public void actionPerformed(ActionEvent e) {
  998.                             okButtonActionPerformed();
  999.                         }
  1000.                     });
  1001.                     buttonBar.add(okButton, new GridBagConstraints(1, 0, 1, 1,
  1002.                             0.0, 0.0, GridBagConstraints.CENTER,
  1003.                             GridBagConstraints.BOTH, new Insets(0, 0, 0, 5), 0,
  1004.                             0));
  1005.  
  1006.                     // ---- cancelButton ----
  1007.                     cancelButton.setText("Cancel");
  1008.                     cancelButton.addActionListener(new ActionListener() {
  1009.                         public void actionPerformed(ActionEvent e) {
  1010.                             cancelButtonActionPerformed();
  1011.                         }
  1012.                     });
  1013.                     buttonBar.add(cancelButton, new GridBagConstraints(2, 0, 1,
  1014.                             1, 0.0, 0.0, GridBagConstraints.CENTER,
  1015.                             GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0,
  1016.                             0));
  1017.                 }
  1018.                 dialogPane.add(buttonBar, BorderLayout.SOUTH);
  1019.             }
  1020.             contentPane.add(dialogPane, BorderLayout.CENTER);
  1021.             pack();
  1022.             setLocationRelativeTo(getOwner());
  1023.             loadSettings();
  1024.         }
  1025.     }
  1026. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement