Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.52 KB | None | 0 0
  1. @pastieorg | status | Running Blind This is Pastie.
  2. ShareBrowseToolsHelpAbout
  3. Document@2x Chain_link@2x Arrow_down@2x Spiral_paper@2x A A A
  4. Java 1.12.2012 Report abuse
  5.  
  6. 1
  7. 2
  8. 3
  9. 4
  10. 5
  11. 6
  12. 7
  13. 8
  14. 9
  15. 10
  16. 11
  17. 12
  18. 13
  19. 14
  20. 15
  21. 16
  22. 17
  23. 18
  24. 19
  25. 20
  26. 21
  27. 22
  28. 23
  29. 24
  30. 25
  31. 26
  32. 27
  33. 28
  34. 29
  35. 30
  36. 31
  37. 32
  38. 33
  39. 34
  40. 35
  41. 36
  42. 37
  43. 38
  44. 39
  45. 40
  46. 41
  47. 42
  48. 43
  49. 44
  50. 45
  51. 46
  52. 47
  53. 48
  54. 49
  55. 50
  56. 51
  57. 52
  58. 53
  59. 54
  60. 55
  61. 56
  62. 57
  63. 58
  64. 59
  65. 60
  66. 61
  67. 62
  68. 63
  69. 64
  70. 65
  71. 66
  72. 67
  73. 68
  74. 69
  75. 70
  76. 71
  77. 72
  78. 73
  79. 74
  80. 75
  81. 76
  82. 77
  83. 78
  84. 79
  85. 80
  86. 81
  87. 82
  88. 83
  89. 84
  90. 85
  91. 86
  92. 87
  93. 88
  94. 89
  95. 90
  96. 91
  97. 92
  98. 93
  99. 94
  100. 95
  101. 96
  102. 97
  103. 98
  104. 99
  105. 100
  106. 101
  107. 102
  108. 103
  109. 104
  110. 105
  111. 106
  112. 107
  113. 108
  114. 109
  115. 110
  116. 111
  117. 112
  118. 113
  119. 114
  120. 115
  121. 116
  122. 117
  123. 118
  124. 119
  125. 120
  126. 121
  127. 122
  128. 123
  129. 124
  130. 125
  131. 126
  132. 127
  133. 128
  134. 129
  135. 130
  136. 131
  137. 132
  138. 133
  139. 134
  140. 135
  141. 136
  142. 137
  143. 138
  144. 139
  145. 140
  146. 141
  147. 142
  148. 143
  149. 144
  150. 145
  151. 146
  152. 147
  153. 148
  154. 149
  155. 150
  156. 151
  157. 152
  158. 153
  159. 154
  160. 155
  161. 156
  162. 157
  163. 158
  164. 159
  165. 160
  166. 161
  167. 162
  168. 163
  169. 164
  170. 165
  171. 166
  172. 167
  173. 168
  174. 169
  175. 170
  176. 171
  177. 172
  178. 173
  179. 174
  180. 175
  181. 176
  182. 177
  183. 178
  184. 179
  185. 180
  186. 181
  187. 182
  188. 183
  189. 184
  190. 185
  191. 186
  192. 187
  193. 188
  194. 189
  195. 190
  196. 191
  197. 192
  198. 193
  199. 194
  200. 195
  201. 196
  202. 197
  203. 198
  204. 199
  205. 200
  206. 201
  207. 202
  208. 203
  209. 204
  210. 205
  211. 206
  212. 207
  213. 208
  214. 209
  215. 210
  216. 211
  217. 212
  218. 213
  219. 214
  220. 215
  221. 216
  222. 217
  223. 218
  224. 219
  225. 220
  226. 221
  227. 222
  228. 223
  229. 224
  230. 225
  231. 226
  232. 227
  233. 228
  234. 229
  235. 230
  236. 231
  237. 232
  238. 233
  239. 234
  240. 235
  241. 236
  242. 237
  243. 238
  244. 239
  245. 240
  246. 241
  247. 242
  248. 243
  249. 244
  250. 245
  251. 246
  252. 247
  253. 248
  254. 249
  255. 250
  256. 251
  257. 252
  258. 253
  259. 254
  260. 255
  261. 256
  262. 257
  263. 258
  264. 259
  265. 260
  266. 261
  267. 262
  268. 263
  269. 264
  270. 265
  271. 266
  272. 267
  273. 268
  274. 269
  275. 270
  276. 271
  277. 272
  278. 273
  279. 274
  280. 275
  281. 276
  282. 277
  283. 278
  284. 279
  285. 280
  286. 281
  287. 282
  288. 283
  289. 284
  290. 285
  291. 286
  292. 287
  293. 288
  294. 289
  295. 290
  296. 291
  297. 292
  298. 293
  299. 294
  300. 295
  301. 296
  302. 297
  303. 298
  304. 299
  305. 300
  306. 301
  307. 302
  308. 303
  309. 304
  310. 305
  311. 306
  312. 307
  313. 308
  314. 309
  315. 310
  316. 311
  317. 312
  318. 313
  319. 314
  320. 315
  321. 316
  322. 317
  323. 318
  324. 319
  325. 320
  326. 321
  327. 322
  328. 323
  329. 324
  330. 325
  331. 326
  332. 327
  333. 328
  334. 329
  335. 330
  336. 331
  337. 332
  338. 333
  339. 334
  340. 335
  341. 336
  342. 337
  343. 338
  344. 339
  345. 340
  346. 341
  347. 342
  348. 343
  349. 344
  350. 345
  351. 346
  352. 347
  353. 348
  354. 349
  355. 350
  356. 351
  357. 352
  358. 353
  359. 354
  360. 355
  361. 356
  362. 357
  363. 358
  364. 359
  365. 360
  366. 361
  367. 362
  368. 363
  369. 364
  370. 365
  371. 366
  372. 367
  373. 368
  374. 369
  375. 370
  376. 371
  377. 372
  378. 373
  379. 374
  380. 375
  381. 376
  382. 377
  383. 378
  384. 379
  385. 380
  386. 381
  387. 382
  388. 383
  389. 384
  390. 385
  391. 386
  392. 387
  393. 388
  394. 389
  395. 390
  396. 391
  397. 392
  398. 393
  399. 394
  400. 395
  401. 396
  402. 397
  403. 398
  404. 399
  405. 400
  406. 401
  407. 402
  408. 403
  409. 404
  410. 405
  411. 406
  412. 407
  413. 408
  414. 409
  415. 410
  416. 411
  417. 412
  418. 413
  419. 414
  420. 415
  421. 416
  422. 417
  423. 418
  424. 419
  425. 420
  426. 421
  427. 422
  428. 423
  429. 424
  430. 425
  431. 426
  432. 427
  433. 428
  434. 429
  435. 430
  436. 431
  437. 432
  438. 433
  439. 434
  440. 435
  441. 436
  442. 437
  443. 438
  444. 439
  445. 440
  446. 441
  447. 442
  448. 443
  449. 444
  450. 445
  451. 446
  452. 447
  453. 448
  454. 449
  455. 450
  456. 451
  457. 452
  458. 453
  459. 454
  460. 455
  461. 456
  462. 457
  463. 458
  464. 459
  465. 460
  466. 461
  467. 462
  468. 463
  469. 464
  470. 465
  471. 466
  472. 467
  473. 468
  474. 469
  475. 470
  476. 471
  477. 472
  478. 473
  479. 474
  480. 475
  481. 476
  482. 477
  483. 478
  484. 479
  485. 480
  486. 481
  487. 482
  488. 483
  489. 484
  490. 485
  491. 486
  492. 487
  493. 488
  494. 489
  495. 490
  496. 491
  497. 492
  498. 493
  499. 494
  500. 495
  501. 496
  502. 497
  503. 498
  504. 499
  505. 500
  506. 501
  507. 502
  508. 503
  509. 504
  510. 505
  511. 506
  512. 507
  513. 508
  514. 509
  515. 510
  516. 511
  517. 512
  518. 513
  519. 514
  520. 515
  521. 516
  522. 517
  523. 518
  524. 519
  525. 520
  526. 521
  527. 522
  528. 523
  529. 524
  530. 525
  531. 526
  532. 527
  533. 528
  534. 529
  535. 530
  536. 531
  537. 532
  538. 533
  539. 534
  540. 535
  541. 536
  542. 537
  543. 538
  544. 539
  545. 540
  546. 541
  547. 542
  548. 543
  549. 544
  550. 545
  551. 546
  552. 547
  553. 548
  554. 549
  555. 550
  556. 551
  557. 552
  558. 553
  559. 554
  560. 555
  561. 556
  562. 557
  563. 558
  564. 559
  565. 560
  566. 561
  567. 562
  568. 563
  569. 564
  570. 565
  571. 566
  572. 567
  573. 568
  574. 569
  575. 570
  576. 571
  577. 572
  578. 573
  579. 574
  580. 575
  581. 576
  582. 577
  583. 578
  584. 579
  585. 580
  586. 581
  587. 582
  588. 583
  589. 584
  590. 585
  591. 586
  592. 587
  593. 588
  594. 589
  595. 590
  596. 591
  597. 592
  598. 593
  599. 594
  600. 595
  601. 596
  602. 597
  603. 598
  604. 599
  605. 600
  606. 601
  607. 602
  608. 603
  609. 604
  610. 605
  611. 606
  612. 607
  613. 608
  614. 609
  615. 610
  616. 611
  617. 612
  618. 613
  619. 614
  620. 615
  621. 616
  622. 617
  623. 618
  624. 619
  625. 620
  626. 621
  627. 622
  628. 623
  629. 624
  630. 625
  631. 626
  632. 627
  633. 628
  634. 629
  635. 630
  636. 631
  637. 632
  638. 633
  639. 634
  640. 635
  641. 636
  642. 637
  643. 638
  644. 639
  645. 640
  646. 641
  647. 642
  648. 643
  649. 644
  650. 645
  651. 646
  652. 647
  653. 648
  654. 649
  655. 650
  656. 651
  657. 652
  658. 653
  659. 654
  660. 655
  661. 656
  662. 657
  663. 658
  664. 659
  665. 660
  666. 661
  667. 662
  668. 663
  669. 664
  670. 665
  671. 666
  672. 667
  673. 668
  674. 669
  675. 670
  676. 671
  677. 672
  678. 673
  679. 674
  680. 675
  681. 676
  682. 677
  683. 678
  684. 679
  685. 680
  686. 681
  687. 682
  688. 683
  689. 684
  690. 685
  691. 686
  692. 687
  693. 688
  694. 689
  695. 690
  696. 691
  697. 692
  698. 693
  699. 694
  700. 695
  701. 696
  702. 697
  703. 698
  704. 699
  705. 700
  706. 701
  707. 702
  708. 703
  709. 704
  710. 705
  711. 706
  712. 707
  713. 708
  714. 709
  715. 710
  716. 711
  717. 712
  718. 713
  719. 714
  720. 715
  721. 716
  722. 717
  723. 718
  724. 719
  725. 720
  726. 721
  727. 722
  728. 723
  729. 724
  730. 725
  731. 726
  732. 727
  733. 728
  734. 729
  735. 730
  736. 731
  737. 732
  738. 733
  739. 734
  740. 735
  741. 736
  742. 737
  743. 738
  744. 739
  745. 740
  746. 741
  747. 742
  748. 743
  749. 744
  750. 745
  751. 746
  752. 747
  753. 748
  754. 749
  755. 750
  756. 751
  757. 752
  758. 753
  759. 754
  760. 755
  761. 756
  762. 757
  763. 758
  764. 759
  765. 760
  766. 761
  767. 762
  768. 763
  769. 764
  770. 765
  771. 766
  772. 767
  773. 768
  774. 769
  775. 770
  776. 771
  777. 772
  778. 773
  779. 774
  780. 775
  781. 776
  782. 777
  783. 778
  784. 779
  785. 780
  786. 781
  787. 782
  788. 783
  789. 784
  790. 785
  791. 786
  792. 787
  793. 788
  794. 789
  795. 790
  796. 791
  797. 792
  798. 793
  799. 794
  800. 795
  801. 796
  802. 797
  803. 798
  804. 799
  805. 800
  806. 801
  807. 802
  808. 803
  809. 804
  810. 805
  811. 806
  812. 807
  813. 808
  814. 809
  815. 810
  816. 811
  817. 812
  818. 813
  819. 814
  820. 815
  821. 816
  822. 817
  823. 818
  824. 819
  825. 820
  826. 821
  827. 822
  828. 823
  829. 824
  830. 825
  831. 826
  832. 827
  833. 828
  834. 829
  835. 830
  836. 831
  837. 832
  838. 833
  839. 834
  840. 835
  841. 836
  842. 837
  843. 838
  844. // Add your package name here...
  845.  
  846. import java.io.File;
  847. import java.io.FileNotFoundException;
  848. import java.io.FileOutputStream;
  849. import java.io.InputStream;
  850. import java.io.InputStreamReader;
  851. import java.io.OutputStream;
  852. import java.io.PrintWriter;
  853. import java.io.StringWriter;
  854. import java.lang.reflect.Constructor;
  855. import java.lang.reflect.Field;
  856. import java.net.HttpURLConnection;
  857. import java.net.URL;
  858. import java.util.ArrayList;
  859. import java.util.Set;
  860. import java.util.concurrent.atomic.AtomicBoolean;
  861. import java.util.logging.Logger;
  862.  
  863. import org.bukkit.ChatColor;
  864. import org.bukkit.Server;
  865. import org.bukkit.command.Command;
  866. import org.bukkit.command.CommandExecutor;
  867. import org.bukkit.command.CommandSender;
  868. import org.bukkit.command.PluginCommand;
  869. import org.bukkit.command.SimpleCommandMap;
  870. import org.bukkit.configuration.Configuration;
  871. import org.bukkit.entity.Player;
  872. import org.bukkit.event.EventHandler;
  873. import org.bukkit.event.EventPriority;
  874. import org.bukkit.event.Listener;
  875. import org.bukkit.event.player.PlayerJoinEvent;
  876. import org.bukkit.permissions.Permissible;
  877. import org.bukkit.permissions.Permission;
  878. import org.bukkit.permissions.PermissionAttachment;
  879. import org.bukkit.permissions.PermissionAttachmentInfo;
  880. import org.bukkit.plugin.Plugin;
  881. import org.bukkit.plugin.SimplePluginManager;
  882. import org.bukkit.scheduler.BukkitScheduler;
  883. import org.json.simple.JSONObject;
  884. import org.json.simple.parser.JSONParser;
  885. import org.json.simple.parser.ParseException;
  886.  
  887. /**
  888. *
  889. * @author V10lator
  890. * @version 1.4
  891. * website: http://forums.bukkit.org/threads/autoupdate-update-your-plugins.84421/
  892. *
  893. */
  894. public class AutoUpdate implements Runnable, Listener, CommandExecutor, CommandSender
  895. {
  896. /*
  897. * Configuration:
  898. *
  899. * delay = The delay this class checks for new updates. This time is in ticks (1 tick = 1/20 second).
  900. * bukitdevSlug = The bukkitDev Slug. Leave empty for autodetection (uses plugin.getName().toLowerCase()).
  901. * COLOR_INFO = The default text color.
  902. * COLOR_OK = The text color for positive messages.
  903. * COLOR_ERROR = The text color for error messages.
  904. */
  905. private long delay = 216000L;
  906. private String bukkitdevSlug = "";
  907. private final ChatColor COLOR_INFO = ChatColor.BLUE;
  908. private final ChatColor COLOR_OK = ChatColor.GREEN;
  909. private final ChatColor COLOR_ERROR = ChatColor.RED;
  910. private boolean debug = true;
  911. /*
  912. * End of configuration.
  913. *
  914. * !!! Don't change anything below if you don't know what you are doing !!!
  915. *
  916. * WARNING: If you change anything below you loose support.
  917. * Also you have to replace every "http://forums.bukkit.org/threads/autoupdate-update-your-plugins.84421/" with a link to your
  918. * plugin and change the version to something unique (like adding -<yourName>).
  919. */
  920.  
  921. private final String version = "1.4";
  922.  
  923. private final Plugin plugin;
  924. private int pid = -1;
  925. private final String av;
  926. private Configuration config;
  927.  
  928. boolean enabled = false;
  929. private final AtomicBoolean lock = new AtomicBoolean(false);
  930. private boolean needUpdate = false;
  931. private boolean updatePending = false;
  932. private String updateURL;
  933. private String updateVersion;
  934. private String pluginURL;
  935. private String type;
  936.  
  937. private ArrayList<CommandExecutor> otherUpdaters;
  938.  
  939. /**
  940. * This will use your main configuration (config.yml).
  941. * Use this in onEnable().
  942. * @param plugin The instance of your plugins main class.
  943. * @throws Exception
  944. */
  945. public AutoUpdate(Plugin plugin) throws Exception
  946. {
  947. this(plugin, plugin.getConfig());
  948. }
  949.  
  950. /**
  951. * This will use a custom configuration.
  952. * Use this in onEnable().
  953. * @param plugin The instance of your plugins main class.
  954. * @param config The configuration to use.
  955. * @throws Exception
  956. */
  957. public AutoUpdate(Plugin plugin, Configuration config) throws Exception
  958. {
  959. if(plugin == null)
  960. throw new Exception("Plugin can not be null");
  961. if(!plugin.isEnabled())
  962. throw new Exception("Plugin not enabled");
  963. this.plugin = plugin;
  964. av = plugin.getDescription().getVersion();
  965. if(bukkitdevSlug == null || bukkitdevSlug.equals(""))
  966. bukkitdevSlug = plugin.getName();
  967. bukkitdevSlug = bukkitdevSlug.toLowerCase();
  968. if(delay < 72000L)
  969. {
  970. plugin.getLogger().info("[AutoUpdate] delay < 72000 ticks not supported. Setting delay to 72000.");
  971. delay = 72000L;
  972. }
  973. setConfig(config);
  974. registerCommand();
  975. plugin.getServer().getPluginManager().registerEvents(this, plugin);
  976. }
  977.  
  978. /**
  979. * Use this to restart the main task.
  980. * This is useful after scheduler.cancelTasks(plugin); for example.
  981. */
  982. public boolean restartMainTask()
  983. {
  984. try
  985. {
  986. ResetTask rt = new ResetTask(enabled);
  987. rt.setPid(plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, rt, 0L, 1L));
  988. return enabled;
  989. }
  990. catch(Throwable t)
  991. {
  992. printStackTraceSync(t, false);
  993. return false;
  994. }
  995. }
  996.  
  997. private boolean checkState(boolean newState, boolean restart)
  998. {
  999. if(enabled != newState)
  1000. {
  1001. enabled = newState;
  1002. plugin.getLogger().info("[AutoUpdate] v"+version+(enabled ? " enabled" : " disabled")+"!");
  1003. if(restart)
  1004. return restartMainTask();
  1005. }
  1006. return enabled;
  1007. }
  1008.  
  1009. private class ResetTask implements Runnable
  1010. {
  1011. private int pid;
  1012. private final boolean restart;
  1013.  
  1014. private ResetTask(boolean restart)
  1015. {
  1016. this.restart = restart;
  1017. }
  1018.  
  1019. private void setPid(int pid)
  1020. {
  1021. this.pid = pid;
  1022. }
  1023.  
  1024. public void run()
  1025. {
  1026. try
  1027. {
  1028. if(!lock.compareAndSet(false, true))
  1029. return;
  1030. BukkitScheduler bs = plugin.getServer().getScheduler();
  1031. if(bs.isQueued(AutoUpdate.this.pid) || bs.isCurrentlyRunning(AutoUpdate.this.pid))
  1032. bs.cancelTask(AutoUpdate.this.pid);
  1033. if(restart)
  1034. AutoUpdate.this.pid = bs.scheduleAsyncRepeatingTask(plugin, AutoUpdate.this, 5L, delay);
  1035. else
  1036. AutoUpdate.this.pid = -1;
  1037. lock.set(false);
  1038. bs.cancelTask(pid);
  1039. }
  1040. catch(Throwable t)
  1041. {
  1042. printStackTraceSync(t, false);
  1043. }
  1044. }
  1045. }
  1046.  
  1047. /**
  1048. * This will overwrite the pre-saved configuration.
  1049. * use this after reloadConfig(), for example.
  1050. * This will use your main configuration (config.yml).
  1051. * This will call {@link #restartMainTask()} internally.
  1052. * @throws FileNotFoundException
  1053. */
  1054. public void resetConfig() throws FileNotFoundException
  1055. {
  1056. setConfig(plugin.getConfig());
  1057. }
  1058.  
  1059. /**
  1060. * This will overwrite the pre-saved configuration.
  1061. * use this after config.load(file), for example.
  1062. * This will use a custom configuration.
  1063. * This will call {@link #restartMainTask()} internally.
  1064. * @param config The new configuration to use.
  1065. * @throws FileNotFoundException
  1066. */
  1067. public void setConfig(Configuration config) throws FileNotFoundException
  1068. {
  1069. if(config == null)
  1070. throw new FileNotFoundException("Config can not be null");
  1071. try
  1072. {
  1073. if(!lock.compareAndSet(false, true))
  1074. {
  1075. ConfigSetter cf = new ConfigSetter(config);
  1076. cf.setPid(plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, cf, 0L, 1L));
  1077. }
  1078. else
  1079. {
  1080. setConfig2(config);
  1081. lock.set(false);
  1082. }
  1083. }
  1084. catch(Throwable t)
  1085. {
  1086. printStackTraceSync(t, false);
  1087. }
  1088. }
  1089.  
  1090. private class ConfigSetter implements Runnable
  1091. {
  1092. private final Configuration config;
  1093. private int pid;
  1094.  
  1095. private ConfigSetter(Configuration config)
  1096. {
  1097. this.config = config;
  1098. }
  1099.  
  1100. private void setPid(int pid)
  1101. {
  1102. this.pid = pid;
  1103. }
  1104.  
  1105. public void run()
  1106. {
  1107. if(!lock.compareAndSet(false, true))
  1108. return;
  1109. setConfig2(config);
  1110. lock.set(false);
  1111. plugin.getServer().getScheduler().cancelTask(pid);
  1112. }
  1113. }
  1114.  
  1115. private void setConfig2(Configuration config)
  1116. {
  1117. if(!config.isSet("AutoUpdate"))
  1118. config.set("AutoUpdate", true);
  1119. checkState(config.getBoolean("AutoUpdate"), true);
  1120. }
  1121.  
  1122. /**
  1123. * This is internal stuff.
  1124. * Don't call this directly!
  1125. */
  1126. public void run()
  1127. {
  1128. if(!plugin.isEnabled())
  1129. {
  1130. plugin.getServer().getScheduler().cancelTask(pid);
  1131. return;
  1132. }
  1133. try
  1134. {
  1135. while(!lock.compareAndSet(false, true))
  1136. {
  1137. try
  1138. {
  1139. Thread.sleep(1L);
  1140. }
  1141. catch(InterruptedException e)
  1142. {
  1143. }
  1144. continue;
  1145. }
  1146. try
  1147. {
  1148. InputStreamReader ir;
  1149. URL url = new URL("http://api.bukget.org/api2/bukkit/plugin/"+bukkitdevSlug+"/latest");
  1150. HttpURLConnection con = (HttpURLConnection)url.openConnection();
  1151. con.connect();
  1152. int res = con.getResponseCode();
  1153. if(res != 200)
  1154. {
  1155. if(debug)
  1156. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new SyncMessageDelayer(null, new String[] {"[AutoUpdate] WARNING: Bukget returned "+res}));
  1157. lock.set(false);
  1158. return;
  1159. }
  1160. ir = new InputStreamReader(con.getInputStream());
  1161.  
  1162. String nv;
  1163. try
  1164. {
  1165. JSONParser jp = new JSONParser();
  1166. Object o = jp.parse(ir);
  1167.  
  1168. if(!(o instanceof JSONObject))
  1169. {
  1170. ir.close();
  1171. lock.set(false);
  1172. return;
  1173. }
  1174.  
  1175. JSONObject jo = (JSONObject)o;
  1176. jo = (JSONObject)jo.get("versions");
  1177. nv = (String)jo.get("version");
  1178. if(av.equals(nv) || (updateVersion != null && updateVersion.equals(nv)))
  1179. {
  1180. ir.close();
  1181. pluginURL = null;
  1182. lock.set(false);
  1183. return;
  1184. }
  1185. updateURL = (String)jo.get("download");
  1186. pluginURL = (String)jo.get("link");
  1187. updateVersion = nv;
  1188. type = (String)jo.get("type");
  1189. needUpdate = true;
  1190. ir.close();
  1191. }
  1192. catch(ParseException e)
  1193. {
  1194. lock.set(false);
  1195. printStackTraceSync(e, true);
  1196. ir.close();
  1197. return;
  1198. }
  1199. final String[] out = new String[] {
  1200. "["+plugin.getName()+"] New "+type+" available!",
  1201. "If you want to update from "+av+" to "+updateVersion+" use /update "+plugin.getName(),
  1202. "See "+pluginURL+" for more information."
  1203. };
  1204. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new SyncMessageDelayer(null, out));
  1205. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable()
  1206. {
  1207. public void run()
  1208. {
  1209. String[] rout = new String[3];
  1210. for(int i = 0; i < 3; i++)
  1211. rout[i] = COLOR_INFO+out[i];
  1212. for(Player p: plugin.getServer().getOnlinePlayers())
  1213. if(hasPermission(p, "autoupdate.announce"))
  1214. p.sendMessage(rout);
  1215. }
  1216. });
  1217. }
  1218. catch(Exception e)
  1219. {
  1220. printStackTraceSync(e, true);
  1221. }
  1222. lock.set(false);
  1223. }
  1224. catch(Throwable t)
  1225. {
  1226. printStackTraceSync(t, false);
  1227. }
  1228. }
  1229.  
  1230. /**
  1231. * This is internal stuff.
  1232. * Don't call this directly!
  1233. */
  1234. @EventHandler(priority = EventPriority.MONITOR)
  1235. public void adminJoin(PlayerJoinEvent event)
  1236. {
  1237. try
  1238. {
  1239. if(!enabled || !lock.compareAndSet(false, true))
  1240. return;
  1241. Player p = event.getPlayer();
  1242. String[] out;
  1243. if(needUpdate)
  1244. {
  1245. if(hasPermission(p, "autoupdate.announce"))
  1246. {
  1247. out = new String[] {
  1248. COLOR_INFO+"["+plugin.getName()+"] New "+type+" available!",
  1249. COLOR_INFO+"If you want to update from "+av+" to "+updateVersion+" use /update "+plugin.getName(),
  1250. COLOR_INFO+"See "+pluginURL+" for more information."
  1251. };
  1252. }
  1253. else
  1254. out = null;
  1255. }
  1256. else if(updatePending)
  1257. {
  1258. if(hasPermission(p, "autoupdate.announce"))
  1259. {
  1260. out = new String[] {
  1261. COLOR_INFO+"Please restart the server to finish the update of "+plugin.getName(),
  1262. COLOR_INFO+"See "+pluginURL+" for more information."
  1263. };
  1264. }
  1265. else
  1266. out = null;
  1267. }
  1268. else
  1269. out = null;
  1270. lock.set(false);
  1271. if(out != null)
  1272. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new SyncMessageDelayer(p.getName(), out));
  1273. }
  1274. catch(Throwable t)
  1275. {
  1276. printStackTraceSync(t, false);
  1277. }
  1278. }
  1279.  
  1280. private class SyncMessageDelayer implements Runnable
  1281. {
  1282. private final String p;
  1283. private final String[] msgs;
  1284.  
  1285. private SyncMessageDelayer(String p, String[] msgs)
  1286. {
  1287. this.p = p;
  1288. this.msgs = msgs;
  1289. }
  1290.  
  1291. public void run()
  1292. {
  1293. try
  1294. {
  1295. if(p != null)
  1296. {
  1297. Player p = plugin.getServer().getPlayerExact(this.p);
  1298. if(p != null)
  1299. for(String msg: msgs)
  1300. if(msg != null)
  1301. p.sendMessage(msg);
  1302. }
  1303. else
  1304. {
  1305. Logger log = plugin.getLogger();
  1306. for(String msg: msgs)
  1307. if(msg != null)
  1308. log.info(msg);
  1309. }
  1310. }
  1311. catch(Throwable t)
  1312. {
  1313. printStackTraceSync(t, false);
  1314. }
  1315. }
  1316. }
  1317.  
  1318. private void registerCommand()
  1319. {
  1320. try
  1321. {
  1322. SimplePluginManager pm = (SimplePluginManager)plugin.getServer().getPluginManager();
  1323. Field f = SimplePluginManager.class.getDeclaredField("commandMap");
  1324. f.setAccessible(true);
  1325. SimpleCommandMap cm = (SimpleCommandMap)f.get(pm);
  1326. f.setAccessible(false);
  1327. if(cm.getCommand("update") == null) // First!
  1328. {
  1329. Constructor<PluginCommand> c = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
  1330. c.setAccessible(true);
  1331. PluginCommand cmd = c.newInstance("update", plugin);
  1332. c.setAccessible(false);
  1333. cmd.setExecutor(this);
  1334. cm.register("update", cmd);
  1335. otherUpdaters = new ArrayList<CommandExecutor>();
  1336. }
  1337. else
  1338. plugin.getServer().dispatchCommand(this, "update [REGISTER]");
  1339. }
  1340. catch(Throwable t)
  1341. {
  1342. printStackTraceSync(t, false);
  1343. }
  1344. }
  1345.  
  1346. /**
  1347. * This is internal stuff.
  1348. * Don't call this directly!
  1349. */
  1350. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
  1351. {
  1352. try
  1353. {
  1354. if(args.length > 0)
  1355. {
  1356. if(args[0].equals("[REGISTER]"))
  1357. {
  1358. otherUpdaters.add((CommandExecutor)sender);
  1359. return true;
  1360. }
  1361. if(!plugin.getName().equalsIgnoreCase(args[0]))
  1362. {
  1363. informOtherUpdaters(sender, args);
  1364. return true;
  1365. }
  1366. }
  1367. else
  1368. informOtherUpdaters(sender, args);
  1369. update(sender);
  1370. }
  1371. catch(Throwable t)
  1372. {
  1373. printStackTraceSync(t, false);
  1374. }
  1375. return true;
  1376. }
  1377.  
  1378. private void informOtherUpdaters(CommandSender sender, String[] args)
  1379. {
  1380. try
  1381. {
  1382. if(otherUpdaters != null)
  1383. for(CommandExecutor ou: otherUpdaters)
  1384. ou.onCommand(sender, null, null, args);
  1385. }
  1386. catch(Throwable t)
  1387. {
  1388. printStackTraceSync(t, false);
  1389. }
  1390. }
  1391.  
  1392. private void update(CommandSender sender)
  1393. {
  1394. if(!hasPermission(sender, "autoupdate.update."+plugin.getName()))
  1395. {
  1396. sender.sendMessage(COLOR_ERROR+plugin.getName()+": You are not allowed to update me!");
  1397. return;
  1398. }
  1399. final BukkitScheduler bs = plugin.getServer().getScheduler();
  1400. final String pn = sender instanceof Player ? ((Player)sender).getName() : null;
  1401. bs.scheduleAsyncDelayedTask(plugin, new Runnable()
  1402. {
  1403. public void run()
  1404. {
  1405. try
  1406. {
  1407. while(!lock.compareAndSet(false, true))
  1408. {
  1409. try
  1410. {
  1411. Thread.sleep(1L);
  1412. }
  1413. catch(InterruptedException e)
  1414. {
  1415. }
  1416. }
  1417. String out;
  1418. try
  1419. {
  1420. File to = new File(plugin.getServer().getUpdateFolderFile(), updateURL.substring(updateURL.lastIndexOf('/')+1, updateURL.length()));
  1421. File tmp = new File(to.getPath()+".au");
  1422. if(!tmp.exists())
  1423. {
  1424. plugin.getServer().getUpdateFolderFile().mkdirs();
  1425. tmp.createNewFile();
  1426. }
  1427. URL url = new URL(updateURL);
  1428. InputStream is = url.openStream();
  1429. OutputStream os = new FileOutputStream(tmp);
  1430. byte[] buffer = new byte[4096];
  1431. int fetched;
  1432. while((fetched = is.read(buffer)) != -1)
  1433. os.write(buffer, 0, fetched);
  1434. is.close();
  1435. os.flush();
  1436. os.close();
  1437. if(to.exists())
  1438. to.delete();
  1439. tmp.renameTo(to);
  1440. out = COLOR_OK+plugin.getName()+" ready! Restart server to finish the update.";
  1441. needUpdate = false;
  1442. updatePending = true;
  1443. updateURL = type = null;
  1444. }
  1445. catch(Exception e)
  1446. {
  1447. out = COLOR_ERROR+plugin.getName()+" failed to update!";
  1448. printStackTraceSync(e, true);
  1449. }
  1450. bs.scheduleSyncDelayedTask(plugin, new SyncMessageDelayer(pn, new String[] {out}));
  1451. lock.set(false);
  1452. }
  1453. catch(Throwable t)
  1454. {
  1455. printStackTraceSync(t, false);
  1456. }
  1457. }
  1458. });
  1459. }
  1460.  
  1461. private void printStackTraceSync(Throwable t, boolean expected)
  1462. {
  1463. BukkitScheduler bs = plugin.getServer().getScheduler();
  1464. try
  1465. {
  1466. String prefix = "[AutoUpdate] ";
  1467. StringWriter sw = new StringWriter();
  1468. PrintWriter pw = new PrintWriter(sw);
  1469. t.printStackTrace(pw);
  1470. String[] sts = sw.toString().replace("\r", "").split("\n");
  1471. String[] out;
  1472. if(expected)
  1473. out = new String[sts.length+25];
  1474. else
  1475. out = new String[sts.length+27];
  1476. out[0] = prefix;
  1477. out[1] = prefix+"Internal error!";
  1478. out[2] = prefix+"If this bug hasn't been reported please open a ticket at http://forums.bukkit.org/threads/autoupdate-update-your-plugins.84421/";
  1479. out[3] = prefix+"Include the following into your bug report:";
  1480. out[4] = prefix+" ======= SNIP HERE =======";
  1481. int i = 5;
  1482. for(; i-5 < sts.length; i++)
  1483. out[i] = prefix+sts[i-5];
  1484. out[++i] = prefix+" ======= DUMP =======";
  1485. out[++i] = prefix+"version : "+version;
  1486. out[++i] = prefix+"delay : "+delay;
  1487. out[++i] = prefix+"bukkitdevSlug : "+bukkitdevSlug;
  1488. out[++i] = prefix+"COLOR_INFO : "+COLOR_INFO.name();
  1489. out[++i] = prefix+"COLO_OK : "+COLOR_OK.name();
  1490. out[++i] = prefix+"COLOR_ERROR : "+COLOR_ERROR.name();
  1491. out[++i] = prefix+"pid : "+pid;
  1492. out[++i] = prefix+"av : "+av;
  1493. out[++i] = prefix+"config : "+config;
  1494. out[++i] = prefix+"lock : "+lock.get();
  1495. out[++i] = prefix+"needUpdate : "+needUpdate;
  1496. out[++i] = prefix+"updatePending : "+updatePending;
  1497. out[++i] = prefix+"UpdateUrl : "+updateURL;
  1498. out[++i] = prefix+"updateVersion : "+updateVersion;
  1499. out[++i] = prefix+"pluginURL : "+pluginURL;
  1500. out[++i] = prefix+"type : "+type;
  1501. out[++i] = prefix+" ======= SNIP HERE =======";
  1502. out[++i] = prefix;
  1503. if(!expected)
  1504. {
  1505. out[++i] = prefix+"DISABLING UPDATER!";
  1506. out[++i] = prefix;
  1507. }
  1508. bs.scheduleSyncDelayedTask(plugin, new SyncMessageDelayer(null, out));
  1509. }
  1510. catch(Throwable e) //This prevents endless loops.
  1511. {
  1512. e.printStackTrace();
  1513. }
  1514. if(!expected)
  1515. {
  1516. bs.cancelTask(pid);
  1517. bs.scheduleAsyncDelayedTask(plugin, new Runnable()
  1518. {
  1519. public void run()
  1520. {
  1521. while(!lock.compareAndSet(false, true))
  1522. {
  1523. try
  1524. {
  1525. Thread.sleep(1L);
  1526. }
  1527. catch(InterruptedException e)
  1528. {
  1529. }
  1530. }
  1531. pid = -1;
  1532. config = null;
  1533. needUpdate = updatePending = enabled = false;
  1534. updateURL = updateVersion = pluginURL = type = null;
  1535. lock.set(false);
  1536. }
  1537. });
  1538. }
  1539. }
  1540.  
  1541. private boolean hasPermission(Permissible player, String node)
  1542. {
  1543. if(player.isPermissionSet(node))
  1544. return player.hasPermission(node);
  1545. while(node.contains("."))
  1546. {
  1547. node = node.substring(0, node.lastIndexOf("."));
  1548. if(player.isPermissionSet(node))
  1549. return player.hasPermission(node);
  1550. if(player.isPermissionSet(node+".*"))
  1551. return player.hasPermission(node+".*");
  1552. }
  1553. if(player.isPermissionSet("*"))
  1554. return player.hasPermission("*");
  1555. return player.isOp();
  1556. }
  1557.  
  1558. /**
  1559. * Use this to enable/disable debugging mode at runtime.
  1560. * @param mode True if you want to enable it, false otherwise.
  1561. */
  1562. public void setDebug(boolean mode)
  1563. {
  1564. debug = mode;
  1565. }
  1566.  
  1567. /**
  1568. * Use this to get the debugging mode.
  1569. * @return True if enabled, false otherwise.
  1570. */
  1571. public boolean getDebug()
  1572. {
  1573. return debug;
  1574. }
  1575.  
  1576. /**
  1577. * This is a dummy. Don't use
  1578. * @return null
  1579. */
  1580. public PermissionAttachment addAttachment(Plugin arg0) {
  1581. return null;
  1582. }
  1583. /**
  1584. * This is a dummy. Don't use
  1585. * @return null
  1586. */
  1587. public PermissionAttachment addAttachment(Plugin arg0, int arg1) {
  1588. return null;
  1589. }
  1590. /**
  1591. * This is a dummy. Don't use
  1592. * @return null
  1593. */
  1594. public PermissionAttachment addAttachment(Plugin arg0, String arg1, boolean arg2) {
  1595. return null;
  1596. }
  1597. /**
  1598. * This is a dummy. Don't use
  1599. * @return null
  1600. */
  1601. public PermissionAttachment addAttachment(Plugin arg0, String arg1, boolean arg2, int arg3) {
  1602. return null;
  1603. }
  1604. /**
  1605. * This is a dummy. Don't use
  1606. * @return null
  1607. */
  1608. public Set<PermissionAttachmentInfo> getEffectivePermissions() {
  1609. return null;
  1610. }
  1611. /**
  1612. * This is a dummy. Don't use
  1613. * @return null
  1614. */
  1615. public boolean hasPermission(String arg0) {
  1616. return false;
  1617. }
  1618. /**
  1619. * This is a dummy. Don't use
  1620. * @return null
  1621. */
  1622. public boolean hasPermission(Permission arg0) {
  1623. return false;
  1624. }
  1625. /**
  1626. * This is a dummy. Don't use
  1627. * @return null
  1628. */
  1629. public boolean isPermissionSet(String arg0) {
  1630. return false;
  1631. }
  1632. /**
  1633. * This is a dummy. Don't use
  1634. * @return null
  1635. */
  1636. public boolean isPermissionSet(Permission arg0) {
  1637. return false;
  1638. }
  1639. /**
  1640. * This is a dummy. Don't use
  1641. */
  1642. public void recalculatePermissions() {}
  1643. /**
  1644. * This is a dummy. Don't use
  1645. */
  1646. public void removeAttachment(PermissionAttachment arg0) {}
  1647. /**
  1648. * This is a dummy. Don't use
  1649. * @return false
  1650. */
  1651. public boolean isOp() {
  1652. return false;
  1653. }
  1654. /**
  1655. * This is a dummy. Don't use
  1656. */
  1657. public void setOp(boolean arg0) {}
  1658. /**
  1659. * This is a dummy. Don't use
  1660. * @return null
  1661. */
  1662. public String getName() {
  1663. return null;
  1664. }
  1665. /**
  1666. * This is a dummy. Don't use
  1667. * @return null
  1668. */
  1669. public Server getServer() {
  1670. return null;
  1671. }
  1672. /**
  1673. * This is a dummy. Don't use
  1674. */
  1675. public void sendMessage(String arg0) {}
  1676. /**
  1677. * This is a dummy. Don't use
  1678. * @return null
  1679. */
  1680. public void sendMessage(String[] arg0) {}
  1681. }
  1682. 20861 characters / 838 lines
  1683. Advertising from Carbon:
  1684.  
  1685.  
  1686. Be nice. :-) Use Pastie for good, not evil.
  1687. Legal Created by
  1688. Josh Goebel Monitored by
  1689. New Relic
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement