Advertisement
Guest User

DTimer.java

a guest
Jul 4th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.58 KB | None | 0 0
  1. package scripts.DTools;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.net.MalformedURLException;
  6. import java.net.URI;
  7. import java.net.URISyntaxException;
  8. import java.net.URL;
  9.  
  10. import javax.swing.*;
  11. import javax.swing.GroupLayout;
  12. import javax.swing.LayoutStyle;
  13. import javax.swing.border.*;
  14. import org.tribot.api.General;
  15. import org.tribot.api2007.Login;
  16. import org.tribot.api2007.Player;
  17.  
  18.  
  19. public class DTimer
  20. {
  21.  
  22. /**
  23. * @author TheD
  24. * {@http://www.d-scripting.com}
  25. * @category DTools
  26. */
  27.  
  28.  
  29. private static long startTime = 0;
  30.  
  31. private static int timeRan = 0;
  32. private static double multiplier = 0;
  33. private static int hours = 0;
  34. private static int minutes = 0;
  35. private static int seconds = 0;
  36.  
  37. private static boolean end_after_time = false;
  38. private static int end_hour = 0;
  39. private static int end_minute = 0;
  40. private static int end_second = General.random(1, 59);
  41.  
  42. private static DEndTimer g = new DEndTimer();
  43. private static boolean GUIWait = true;
  44.  
  45. /**
  46. * call in your main loop, if true endscript.
  47. * @return true if we should end, false if we should not end yet
  48. */
  49.  
  50. public static boolean checkEndTimer()
  51. {
  52. setTime(); //updating time statistics
  53. if(end_after_time)
  54. {
  55. if((hours == end_hour && minutes >= end_minute && seconds >= end_second) || (hours >= end_hour && minutes > end_minute) || (hours > end_hour))
  56. {
  57. if(Player.getRSPlayer() != null && Player.getRSPlayer().isInCombat()) return false; //still in combat, not going to spam log-out.
  58. System.out.println("Starting log-out...");
  59. long starttime = System.currentTimeMillis();
  60. long endtime = starttime + 7500; //if we take longer than 7.5 seconds clicking logout, we exit the loop.
  61. while(Login.getLoginState() == Login.STATE.INGAME)
  62. {
  63.  
  64. Login.logout();
  65. General.sleep(500,5000);
  66. if(Login.getLoginState() != Login.STATE.INGAME || endtime >= System.currentTimeMillis()) break;
  67. }
  68. System.out.println("Logged out...");
  69. return true;
  70. }
  71. }
  72. return false;
  73. }
  74.  
  75. /**
  76. * Is called from everything that involves time, so we always have the correct time.
  77. */
  78.  
  79. private static void setTime()
  80. {
  81. timeRan = (int) (System.currentTimeMillis() - startTime);
  82. multiplier = timeRan / 3600000D;
  83. hours = timeRan / 3600000;
  84. timeRan = timeRan - (hours*3600000);
  85. minutes = timeRan / 60000;
  86. timeRan = timeRan - (minutes*60000);
  87. seconds = timeRan / 1000;
  88. timeRan = timeRan - (seconds*1000);
  89. }
  90.  
  91.  
  92. /**
  93. * Do you want to let the user decide wether to use EndTimer or not?
  94. * use callGUI in those cases.
  95. */
  96. public static void callGUI()
  97. {
  98. g.setVisible(true);
  99. while(GUIWait)
  100. {
  101. General.sleep(500);
  102. if(!GUIWait) break;
  103. }
  104. }
  105.  
  106. /**
  107. * @param endHour after how many hours should the script be terminated ?
  108. * @NOTE Both Minute & Second will be totally randomized
  109. * @NOTE If you use this, you do not also have to call enableEnTimer(b)
  110. */
  111. public static void setEnd(int endHour)
  112. {
  113. end_after_time = true;
  114. end_hour = endHour;
  115. end_minute = General.random(1, 59);
  116. }
  117.  
  118. /**
  119. * @param b should we end after time?
  120. * @NOTE No need to call this if you used setEnd(endHour)
  121. * Only useful if you want to disable endtimer when we already called it.
  122. */
  123. public static void setEndtimer(boolean b)
  124. {
  125. end_after_time = b;
  126. }
  127.  
  128. /**
  129. * Only call this method once, and only when you want to start the the ScriptTimer
  130. *
  131. */
  132. public static void startScriptTimer()
  133. {
  134. startTime = System.currentTimeMillis();
  135. }
  136.  
  137. /**
  138. *
  139. * @returns the hours run if you called startScriptTimer
  140. */
  141. public static int getHour()
  142. {
  143. setTime(); //updating time statistics
  144. if(startTime != 0)
  145. return hours;
  146. return 0;
  147. }
  148.  
  149. /**
  150. *
  151. * @returns the minutes run if you called startScriptTimer
  152. */
  153. public static int getMinute()
  154. {
  155. setTime(); //updating time statistics
  156. if(startTime != 0)
  157. return minutes;
  158. return 0;
  159. }
  160.  
  161. /**
  162. *
  163. * @returns the seconds run if you called startScriptTimer
  164. */
  165. public static int getSecond()
  166. {
  167. setTime(); //updating time statistics
  168. if(startTime != 0)
  169. return seconds;
  170. return 0;
  171. }
  172.  
  173.  
  174. /**
  175. *
  176. * @returns the end hour if we use end_after_time, if not returns 0
  177. */
  178. public static int getEndHour()
  179. {
  180. if(end_after_time)
  181. return end_hour;
  182. return 0;
  183. }
  184.  
  185. /**
  186. *
  187. * @returns the end minute if we use end_after_time, if not returns 0
  188. */
  189. public static int getEndMinute()
  190. {
  191. if(end_after_time)
  192. return end_minute;
  193. return 0;
  194. }
  195.  
  196. /**
  197. *
  198. * @returns the end seconds if we use end_after_time, if not returns 0
  199. */
  200. public static int getEndSeconds()
  201. {
  202. if(end_after_time)
  203. return end_second;
  204. return 0;
  205. }
  206.  
  207.  
  208. /**
  209. * @param i - the variable of which you want to know how much we did p/h
  210. * @return how much of i we did per hour
  211. */
  212. public static int getPH(int i)
  213. {
  214. setTime(); //updating time statistics
  215. return (int) (i / multiplier);
  216. }
  217.  
  218. /**
  219. *
  220. * @return true if we enabled end after time.
  221. */
  222. public static boolean getEndAfterTime()
  223. {
  224. return end_after_time;
  225. }
  226.  
  227. /**
  228. * @author TheD
  229. */
  230. public static class DEndTimer extends JFrame {
  231. public DEndTimer() {
  232. initComponents();
  233. }
  234.  
  235. private void endtimerstartActionPerformed(ActionEvent e)
  236. {
  237. if(useEndTimer.isSelected())
  238. {
  239. setEnd((int) hourSpinner.getValue());
  240. }
  241. else
  242. {
  243. end_after_time = false;
  244. }
  245. GUIWait = false;
  246. g.dispose();
  247. }
  248.  
  249. private void whatisendtimerActionPerformed(ActionEvent e)
  250. {
  251. openSite("http://www.d-scripting.com/whatisendtimer");
  252. }
  253.  
  254. private void visittribotActionPerformed(ActionEvent e) {
  255. openSite("http://www.tribot.org");
  256. }
  257.  
  258. private void initComponents()
  259. {
  260. menuBar1 = new JMenuBar();
  261. menu1 = new JMenu();
  262. whatisendtimer = new JMenuItem();
  263. visittribot = new JMenuItem();
  264. dialogPane = new JPanel();
  265. contentPanel = new JPanel();
  266. useEndTimer = new JCheckBox();
  267. hourSpinner = new JSpinner();
  268. label1 = new JLabel();
  269. label2 = new JLabel();
  270. buttonBar = new JPanel();
  271. endtimerstart = new JButton();
  272.  
  273. //======== this ========
  274. setAlwaysOnTop(true);
  275. setResizable(false);
  276. setTitle("EndTimer by TheD");
  277. Container contentPane = getContentPane();
  278. contentPane.setLayout(new BorderLayout());
  279.  
  280. //======== menuBar1 ========
  281. {
  282.  
  283. //======== menu1 ========
  284. {
  285. menu1.setText("General");
  286.  
  287. //---- whatisendtimer ----
  288. whatisendtimer.setText("What is EndTimer?");
  289. whatisendtimer.addActionListener(e -> whatisendtimerActionPerformed(e));
  290. menu1.add(whatisendtimer);
  291.  
  292. //---- visittribot ----
  293. visittribot.setText("Visit TRiBot.org");
  294. visittribot.addActionListener(e -> visittribotActionPerformed(e));
  295. menu1.add(visittribot);
  296. }
  297. menuBar1.add(menu1);
  298. }
  299. setJMenuBar(menuBar1);
  300.  
  301. //======== dialogPane ========
  302. {
  303. dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
  304.  
  305. dialogPane.setBorder(new javax.swing.border.CompoundBorder(
  306. new javax.swing.border.TitledBorder(new javax.swing.border.EmptyBorder(0, 0, 0, 0),
  307. "DTimer", javax.swing.border.TitledBorder.CENTER,
  308. javax.swing.border.TitledBorder.BOTTOM, new java.awt.Font("Dialog", java.awt.Font.BOLD, 12),
  309. java.awt.Color.red), dialogPane.getBorder())); dialogPane.addPropertyChangeListener(new java.beans.PropertyChangeListener(){public void propertyChange(java.beans.PropertyChangeEvent e){if("border".equals(e.getPropertyName()))throw new RuntimeException();}});
  310.  
  311. dialogPane.setLayout(new BorderLayout());
  312.  
  313. //======== contentPanel ========
  314. {
  315.  
  316. //---- useEndTimer ----
  317. useEndTimer.setText("Use EndTimer");
  318. useEndTimer.setFont(new Font("Tahoma", Font.PLAIN, 14));
  319.  
  320. //---- hourSpinner ----
  321. hourSpinner.setFont(new Font("Tahoma", Font.PLAIN, 14));
  322. hourSpinner.setModel(new SpinnerNumberModel(0, 0, null, 1));
  323.  
  324. //---- label1 ----
  325. label1.setText("End after");
  326. label1.setFont(new Font("Tahoma", Font.PLAIN, 14));
  327.  
  328. //---- label2 ----
  329. label2.setText("hours");
  330. label2.setFont(new Font("Tahoma", Font.PLAIN, 14));
  331.  
  332. GroupLayout contentPanelLayout = new GroupLayout(contentPanel);
  333. contentPanel.setLayout(contentPanelLayout);
  334. contentPanelLayout.setHorizontalGroup(
  335. contentPanelLayout.createParallelGroup()
  336. .addGroup(contentPanelLayout.createSequentialGroup()
  337. .addComponent(useEndTimer)
  338. .addGap(0, 251, Short.MAX_VALUE))
  339. .addGroup(contentPanelLayout.createSequentialGroup()
  340. .addGap(21, 21, 21)
  341. .addComponent(label1)
  342. .addGap(40, 40, 40)
  343. .addComponent(hourSpinner, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE)
  344. .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
  345. .addComponent(label2)
  346. .addContainerGap(110, Short.MAX_VALUE))
  347. );
  348. contentPanelLayout.setVerticalGroup(
  349. contentPanelLayout.createParallelGroup()
  350. .addGroup(contentPanelLayout.createSequentialGroup()
  351. .addComponent(useEndTimer)
  352. .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
  353. .addGroup(contentPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
  354. .addComponent(label1)
  355. .addComponent(label2)
  356. .addComponent(hourSpinner, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
  357. .addGap(0, 12, Short.MAX_VALUE))
  358. );
  359. }
  360. dialogPane.add(contentPanel, BorderLayout.CENTER);
  361.  
  362. //======== buttonBar ========
  363. {
  364. buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
  365. buttonBar.setLayout(new GridBagLayout());
  366. ((GridBagLayout)buttonBar.getLayout()).columnWidths = new int[] {0, 80};
  367. ((GridBagLayout)buttonBar.getLayout()).columnWeights = new double[] {1.0, 0.0};
  368.  
  369. //---- endtimerstart ----
  370. endtimerstart.setText("Start");
  371. endtimerstart.setFont(new Font("Tahoma", Font.PLAIN, 16));
  372. endtimerstart.addActionListener(e -> endtimerstartActionPerformed(e));
  373. buttonBar.add(endtimerstart, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
  374. GridBagConstraints.CENTER, GridBagConstraints.BOTH,
  375. new Insets(0, 0, 0, 0), 0, 0));
  376. }
  377. dialogPane.add(buttonBar, BorderLayout.SOUTH);
  378. }
  379. contentPane.add(dialogPane, BorderLayout.CENTER);
  380. pack();
  381. setLocationRelativeTo(getOwner());
  382. }
  383.  
  384. private JMenuBar menuBar1;
  385. private JMenu menu1;
  386. private JMenuItem whatisendtimer;
  387. private JMenuItem visittribot;
  388. private JPanel dialogPane;
  389. private JPanel contentPanel;
  390. private JCheckBox useEndTimer;
  391. private JSpinner hourSpinner;
  392. private JLabel label1;
  393. private JLabel label2;
  394. private JPanel buttonBar;
  395. private JButton endtimerstart;
  396. //end
  397. }
  398.  
  399.  
  400. public static void openSite(String url)
  401. {
  402. URL a = null;
  403. try {
  404. a = new URL(url);
  405. } catch (MalformedURLException e1) {
  406. System.out.println("ERROR OPENING URL");
  407. e1.printStackTrace();
  408. }
  409. openWebpage(a);
  410. }
  411.  
  412. public static void openWebpage(URI uri) {
  413. Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
  414. if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
  415. try {
  416. desktop.browse(uri);
  417. } catch (Exception e) {
  418. e.printStackTrace();
  419. }
  420. }
  421. }
  422.  
  423. public static void openWebpage(URL url) {
  424. try {
  425. openWebpage(url.toURI());
  426. } catch (URISyntaxException e) {
  427. e.printStackTrace();
  428. }
  429. }
  430.  
  431. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement